PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/jgitflow-maven-plugin/src/main/java/com/atlassian/maven/plugins/jgitflow/manager/DefaultFlowFeatureManager.java

https://bitbucket.org/lukejackson/jgit-flow
Java | 252 lines | 196 code | 51 blank | 5 comment | 11 complexity | f0fe6f1716612654e4200e1d9ac6e7a8 MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.maven.plugins.jgitflow.manager;
  2. import java.util.List;
  3. import com.atlassian.jgitflow.core.JGitFlow;
  4. import com.atlassian.jgitflow.core.JGitFlowReporter;
  5. import com.atlassian.jgitflow.core.exception.JGitFlowException;
  6. import com.atlassian.jgitflow.core.BranchType;
  7. import com.atlassian.maven.plugins.jgitflow.ReleaseContext;
  8. import com.atlassian.maven.plugins.jgitflow.exception.MavenJGitFlowException;
  9. import com.atlassian.maven.plugins.jgitflow.exception.ReactorReloadException;
  10. import com.atlassian.maven.plugins.jgitflow.extension.FeatureFinishPluginExtension;
  11. import com.atlassian.maven.plugins.jgitflow.extension.FeatureStartPluginExtension;
  12. import com.atlassian.maven.plugins.jgitflow.helper.*;
  13. import com.atlassian.maven.plugins.jgitflow.provider.BranchLabelProvider;
  14. import com.atlassian.maven.plugins.jgitflow.provider.ProjectCacheKey;
  15. import com.atlassian.maven.plugins.jgitflow.util.NamingUtil;
  16. import com.google.common.base.Splitter;
  17. import org.apache.commons.lang.StringUtils;
  18. import org.apache.maven.execution.MavenSession;
  19. import org.apache.maven.project.MavenProject;
  20. import org.apache.maven.shared.release.exec.MavenExecutorException;
  21. import org.apache.maven.shared.release.util.ReleaseUtil;
  22. import org.codehaus.plexus.component.annotations.Component;
  23. import org.codehaus.plexus.component.annotations.Requirement;
  24. import org.eclipse.jgit.api.MergeResult;
  25. import org.eclipse.jgit.api.ResetCommand;
  26. import org.eclipse.jgit.api.errors.GitAPIException;
  27. /**
  28. * @since version
  29. */
  30. @Component(role = FlowReleaseManager.class, hint = "feature")
  31. public class DefaultFlowFeatureManager extends AbstractFlowReleaseManager
  32. {
  33. @Requirement
  34. private MavenExecutionHelper mavenExecutionHelper;
  35. @Requirement
  36. private BranchLabelProvider labelProvider;
  37. @Requirement
  38. private PomUpdater pomUpdater;
  39. @Requirement
  40. private FeatureStartPluginExtension startExtension;
  41. @Requirement
  42. private FeatureFinishPluginExtension finishExtension;
  43. @Override
  44. public void start(ReleaseContext ctx, List<MavenProject> reactorProjects, MavenSession session) throws MavenJGitFlowException
  45. {
  46. JGitFlow flow = null;
  47. try
  48. {
  49. String featureName = getStartLabelAndRunPreflight(ctx,reactorProjects,session);
  50. flow = jGitFlowProvider.gitFlow();
  51. startExtension.init();
  52. flow.featureStart(featureName)
  53. .setAllowUntracked(ctx.isAllowUntracked())
  54. .setPush(ctx.isPushFeatures())
  55. .setStartCommit(ctx.getStartCommit())
  56. .setScmMessagePrefix(ctx.getScmCommentPrefix())
  57. .setScmMessageSuffix(ctx.getScmCommentSuffix())
  58. .setExtension(startExtension)
  59. .call();
  60. }
  61. catch (JGitFlowException e)
  62. {
  63. throw new MavenJGitFlowException("Error starting feature: " + e.getMessage(), e);
  64. }
  65. finally
  66. {
  67. if (null != flow)
  68. {
  69. flow.getReporter().flush();
  70. }
  71. }
  72. }
  73. @Override
  74. public void finish(ReleaseContext ctx, List<MavenProject> reactorProjects, MavenSession session) throws MavenJGitFlowException
  75. {
  76. JGitFlow flow = null;
  77. try
  78. {
  79. finishExtension.init();
  80. String featureLabel = getFinishLabelAndRunPreflight(ctx,reactorProjects,session);
  81. flow = jGitFlowProvider.gitFlow();
  82. JGitFlowReporter reporter = flow.getReporter();
  83. getLogger().info("running jgitflow feature finish...");
  84. MergeResult mergeResult = flow.featureFinish(featureLabel)
  85. .setKeepBranch(ctx.isKeepBranch())
  86. .setSquash(ctx.isSquash())
  87. .setRebase(ctx.isFeatureRebase())
  88. .setAllowUntracked(ctx.isAllowUntracked())
  89. .setPush(ctx.isPushFeatures())
  90. .setNoMerge(ctx.isNoFeatureMerge())
  91. .setScmMessagePrefix(ctx.getScmCommentPrefix())
  92. .setScmMessageSuffix(ctx.getScmCommentSuffix())
  93. .setExtension(finishExtension)
  94. .call();
  95. if (!mergeResult.getMergeStatus().isSuccessful())
  96. {
  97. getLogger().error("Error merging into " + flow.getDevelopBranchName() + ":");
  98. getLogger().error(mergeResult.toString());
  99. getLogger().error("see .git/jgitflow.log for more info");
  100. throw new MavenJGitFlowException("Error while merging feature!");
  101. }
  102. }
  103. catch (JGitFlowException e)
  104. {
  105. throw new MavenJGitFlowException("Error finish feature: " + e.getMessage(), e);
  106. }
  107. catch (GitAPIException e)
  108. {
  109. throw new MavenJGitFlowException("Error finish feature: " + e.getMessage(), e);
  110. }
  111. finally
  112. {
  113. if (null != flow)
  114. {
  115. flow.getReporter().flush();
  116. }
  117. }
  118. }
  119. @Override
  120. public void deploy(ReleaseContext ctx, List<MavenProject> reactorProjects, MavenSession session, String buildNumber, String goals) throws MavenJGitFlowException
  121. {
  122. JGitFlow flow = null;
  123. try
  124. {
  125. String featureLabel = getFinishLabelAndRunPreflight(ctx,reactorProjects,session);
  126. flow = jGitFlowProvider.gitFlow();
  127. JGitFlowReporter reporter = flow.getReporter();
  128. SessionAndProjects sessionAndProjects = checkoutAndGetProjects.run(flow.getFeatureBranchPrefix() + featureLabel);
  129. List<MavenProject> featureProjects = sessionAndProjects.getProjects();
  130. MavenSession featureSession = sessionAndProjects.getSession();
  131. String featureVersion = NamingUtil.camelCaseOrSpaceToDashed(featureLabel);
  132. featureVersion = StringUtils.replace(featureVersion, "-", "_");
  133. if (StringUtils.isNotBlank(buildNumber))
  134. {
  135. featureVersion = featureVersion + "-build" + buildNumber;
  136. }
  137. else
  138. {
  139. featureVersion = featureVersion + "-SNAPSHOT";
  140. }
  141. pomUpdater.removeSnapshotFromFeatureVersions(ProjectCacheKey.FEATURE_DEPLOY_LABEL, featureVersion, reactorProjects);
  142. MavenProject rootProject = ReleaseUtil.getRootProject(featureProjects);
  143. featureSession = mavenExecutionHelper.reloadReactor(rootProject, session);
  144. rootProject = ReleaseUtil.getRootProject(featureSession.getSortedProjects());
  145. if (!ctx.isNoBuild())
  146. {
  147. String mvnGoals = "clean install deploy";
  148. if (StringUtils.isNotBlank(goals))
  149. {
  150. mvnGoals = goals;
  151. }
  152. try
  153. {
  154. for (String goal : Splitter.on(" ").trimResults().omitEmptyStrings().split(mvnGoals))
  155. {
  156. mavenExecutionHelper.execute(rootProject, featureSession, goal);
  157. }
  158. }
  159. catch (MavenExecutorException e)
  160. {
  161. throw new MavenJGitFlowException("Error building: " + e.getMessage(), e);
  162. }
  163. }
  164. //revert our local changes
  165. flow.git().reset().setMode(ResetCommand.ResetType.HARD).call();
  166. }
  167. catch (JGitFlowException e)
  168. {
  169. throw new MavenJGitFlowException("Error finish feature: " + e.getMessage(), e);
  170. }
  171. catch (GitAPIException e)
  172. {
  173. throw new MavenJGitFlowException("Error finish feature: " + e.getMessage(), e);
  174. }
  175. catch (ReactorReloadException e)
  176. {
  177. throw new MavenJGitFlowException("Error finish feature: " + e.getMessage(), e);
  178. }
  179. finally
  180. {
  181. if (null != flow)
  182. {
  183. flow.getReporter().flush();
  184. }
  185. }
  186. }
  187. public String getStartLabelAndRunPreflight(ReleaseContext ctx, List<MavenProject> reactorProjects, MavenSession session) throws JGitFlowException, MavenJGitFlowException
  188. {
  189. runPreflight(ctx,reactorProjects,session);
  190. JGitFlow flow = jGitFlowProvider.gitFlow();
  191. //make sure we're on develop
  192. List<MavenProject> branchProjects = checkoutAndGetProjects.run(flow.getDevelopBranchName()).getProjects();
  193. verifyInitialVersionState.run(BranchType.FEATURE, branchProjects);
  194. return labelProvider.getFeatureStartName();
  195. }
  196. public String getFinishLabelAndRunPreflight(ReleaseContext ctx, List<MavenProject> reactorProjects, MavenSession session) throws JGitFlowException, MavenJGitFlowException
  197. {
  198. runPreflight(ctx,reactorProjects,session);
  199. return labelProvider.getFeatureFinishName();
  200. }
  201. }