|
1 | 1 | package com.e_gineering.maven.gitflowhelper; |
2 | 2 |
|
3 | 3 | import org.apache.commons.lang.StringUtils; |
| 4 | +import org.apache.maven.plugin.logging.Log; |
4 | 5 | import org.apache.maven.project.MavenProject; |
5 | 6 | import org.apache.maven.scm.ScmException; |
6 | | -import org.apache.maven.scm.ScmFile; |
7 | 7 | import org.apache.maven.scm.ScmFileSet; |
8 | 8 | import org.apache.maven.scm.log.ScmLogDispatcher; |
9 | 9 | import org.apache.maven.scm.manager.ScmManager; |
|
13 | 13 | import org.apache.maven.scm.repository.ScmRepository; |
14 | 14 |
|
15 | 15 | public abstract class ScmUtils { |
16 | | - public static String getGitBranch(ScmManager scmManager, MavenProject project) throws ScmException { |
17 | | - String scmConnectionUrl = project.getScm().getConnection(); |
18 | | - String scmDeveloperConnectionUrl = project.getScm().getDeveloperConnection(); |
19 | | - String connectionUrl = StringUtils.isNotBlank(scmDeveloperConnectionUrl) ? scmDeveloperConnectionUrl : scmConnectionUrl; |
20 | | - if (StringUtils.isBlank(connectionUrl)) { |
21 | | - return "${env.GIT_BRANCH}"; |
| 16 | + /** |
| 17 | + * Given the ScmManager for the current execution cycle, and the MavenProject structure, determine if we can |
| 18 | + * find a maven-provided manner of resolving the current git branch. |
| 19 | + * |
| 20 | + * @param scmManager The current maven ScmManager |
| 21 | + * @param project The Current maven Project |
| 22 | + * @param log A Log to write to |
| 23 | + * @return The current git branch name, or <code>${env.GIT_BRACH}</code> if the current git branch could not be resolved. |
| 24 | + * @throws ScmException |
| 25 | + */ |
| 26 | + public static String resolveBranchOrExpression(final ScmManager scmManager, final MavenProject project, final Log log) { |
| 27 | + String connectionUrl = null; |
| 28 | + |
| 29 | + // Some projects don't specify SCM Blocks, and instead rely upon the CI server to provide an '${env.GIT_BRANCH}' |
| 30 | + if (project.getScm() != null) { |
| 31 | + // Start with the developer connection, then fall back to the non-developer connection. |
| 32 | + connectionUrl = project.getScm().getDeveloperConnection(); |
| 33 | + if (StringUtils.isBlank(connectionUrl)) { |
| 34 | + connectionUrl = project.getScm().getConnection(); |
| 35 | + } |
22 | 36 | } |
23 | 37 |
|
24 | | - ScmRepository repository = scmManager.makeScmRepository(connectionUrl); |
25 | | - ScmProvider provider = scmManager.getProviderByRepository(repository); |
26 | | - if (!GitScmProviderRepository.PROTOCOL_GIT.equals(provider.getScmType())) { |
27 | | - return null; |
28 | | - } else { |
29 | | - ScmFileSet fileSet = new ScmFileSet(project.getBasedir()); |
30 | | - return GitBranchCommand.getCurrentBranch(new ScmLogDispatcher(), (GitScmProviderRepository)repository.getProviderRepository(), fileSet); |
| 38 | + if (StringUtils.isNotBlank(connectionUrl)) { |
| 39 | + try { |
| 40 | + ScmRepository repository = scmManager.makeScmRepository(connectionUrl); |
| 41 | + ScmProvider provider = scmManager.getProviderByRepository(repository); |
| 42 | + |
| 43 | + if (GitScmProviderRepository.PROTOCOL_GIT.equals(provider.getScmType())) { |
| 44 | + ScmFileSet fileSet = new ScmFileSet(project.getBasedir()); |
| 45 | + return GitBranchCommand.getCurrentBranch(new ScmLogDispatcher(), (GitScmProviderRepository) repository.getProviderRepository(), fileSet); |
| 46 | + } else { |
| 47 | + log.warn("Project SCM defines a non-git SCM provider. Falling back to variable resolution."); |
| 48 | + } |
| 49 | + } catch (ScmException se) { |
| 50 | + log.warn("Unable to resolve Git Branch from Project SCM definition.", se); |
| 51 | + } |
31 | 52 | } |
| 53 | + |
| 54 | + log.debug("Git branch unresolvable from Project SCM definition, defaulting to ${env.GIT_BRANCH}"); |
| 55 | + return "${env.GIT_BRANCH}"; |
32 | 56 | } |
33 | 57 | } |
0 commit comments