Skip to content

Commit

Permalink
Merge pull request #20 from nemccarthy/master
Browse files Browse the repository at this point in the history
Sync repos
  • Loading branch information
nemccarthy authored Sep 22, 2016
2 parents bf42717 + 1f2b683 commit c160e04
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public void onCompleted(AbstractBuild abstractBuild, @Nonnull TaskListener liste
if (trigger == null) {
return;
}
trigger.getBuilder().getBuilds().onCompleted(abstractBuild);
trigger.getBuilder().getBuilds().onCompleted(abstractBuild, listener);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public class StashBuildTrigger extends Trigger<AbstractProject<?, ?>> {
private final boolean ignoreSsl;
private final boolean checkDestinationCommit;
private final boolean checkMergeable;
private final boolean mergeOnSuccess;
private final boolean checkNotConflicted;
private final boolean onlyBuildOnComment;
private final boolean deletePreviousBuildFinishComments;
Expand All @@ -98,6 +99,7 @@ public StashBuildTrigger(
boolean ignoreSsl,
boolean checkDestinationCommit,
boolean checkMergeable,
boolean mergeOnSuccess,
boolean checkNotConflicted,
boolean onlyBuildOnComment,
String ciBuildPhrases,
Expand All @@ -118,6 +120,7 @@ public StashBuildTrigger(
this.ignoreSsl = ignoreSsl;
this.checkDestinationCommit = checkDestinationCommit;
this.checkMergeable = checkMergeable;
this.mergeOnSuccess = mergeOnSuccess;
this.checkNotConflicted = checkNotConflicted;
this.onlyBuildOnComment = onlyBuildOnComment;
this.deletePreviousBuildFinishComments = deletePreviousBuildFinishComments;
Expand Down Expand Up @@ -192,6 +195,10 @@ public String getTargetBranchesToBuild() {
return targetBranchesToBuild;
}

public boolean getMergeOnSuccess() {
return mergeOnSuccess;
}

public boolean isCancelOutdatedJobsEnabled() {
return cancelOutdatedJobsEnabled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import hudson.model.AbstractBuild;
import hudson.model.Cause;
import hudson.model.Result;
import hudson.model.TaskListener;
import jenkins.model.Jenkins;
import jenkins.model.JenkinsLocationConfiguration;

Expand Down Expand Up @@ -43,7 +44,7 @@ public void onStarted(AbstractBuild build) {
}
}

public void onCompleted(AbstractBuild build) {
public void onCompleted(AbstractBuild build, TaskListener listener) {
StashCause cause = this.getCause(build);
if (cause == null) {
return;
Expand All @@ -60,8 +61,30 @@ public void onCompleted(AbstractBuild build) {
}
repository.deletePullRequestComment(cause.getPullRequestId(), cause.getBuildStartCommentId());

StashPostBuildCommentAction comments = build.getAction(StashPostBuildCommentAction.class);
String additionalComment = "";
StashBuildTrigger trig = StashBuildTrigger.getTrigger(build.getProject());
if(trig.getMergeOnSuccess() == true && build.getResult() == Result.SUCCESS)
{
boolean mergeStat = repository.mergePullRequest(cause.getPullRequestId(), cause.getPullRequestVersion());
if(mergeStat == true)
{
String logmsg = "Merged pull request " + cause.getPullRequestId() + "(" +
cause.getSourceBranch() + ") to branch " + cause.getTargetBranch();
logger.log(Level.INFO, logmsg);
listener.getLogger().println(logmsg);
}
else
{
String logmsg = "Failed to merge pull request " + cause.getPullRequestId() + "(" +
cause.getSourceBranch() + ") to branch " + cause.getTargetBranch() +
" because it's out of date";
logger.log(Level.INFO, logmsg);
listener.getLogger().println(logmsg);
additionalComment = additionalComment + "\n\n" + logmsg;
}
}

StashPostBuildCommentAction comments = build.getAction(StashPostBuildCommentAction.class);
if(comments != null) {
String buildComment = result == Result.SUCCESS ? comments.getBuildSuccessfulComment() : comments.getBuildFailedComment();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class StashCause extends Cause {
private final String sourceCommitHash;
private final String destinationCommitHash;
private final String buildStartCommentId;
private final String pullRequestVersion;
private final String stashHost;
private final Map<String,String> additionalParameters;

Expand All @@ -34,6 +35,7 @@ public StashCause(String stashHost,
String sourceCommitHash,
String destinationCommitHash,
String buildStartCommentId,
String pullRequestVersion,
Map<String,String> additionalParameters) {
this.sourceBranch = sourceBranch;
this.targetBranch = targetBranch;
Expand All @@ -46,6 +48,7 @@ public StashCause(String stashHost,
this.sourceCommitHash = sourceCommitHash;
this.destinationCommitHash = destinationCommitHash;
this.buildStartCommentId = buildStartCommentId;
this.pullRequestVersion = pullRequestVersion;
this.stashHost = stashHost.replaceAll("/$", "");
this.additionalParameters = additionalParameters;
}
Expand All @@ -69,6 +72,9 @@ public String getPullRequestId() {
return pullRequestId;
}

public String getPullRequestVersion() {
return pullRequestVersion;
}

public String getDestinationRepositoryOwner() {
return destinationRepositoryOwner;
Expand All @@ -89,7 +95,7 @@ public String getPullRequestTitle() {
public String getBuildStartCommentId() { return buildStartCommentId; }

public Map<String,String> getAdditionalParameters() { return additionalParameters; }

@Override
public String getShortDescription() {
return "<a href=\"" + stashHost + "/projects/" + this.getDestinationRepositoryOwner() + "/repos/" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public static AbstractMap.SimpleEntry<String,String> getParameter(String content
}
return null;
}

public static Map<String, String> getParametersFromContent(String content){
Map<String, String> result = new TreeMap<String, String>();
String lines[] = content.split("\\r?\\n|\\r");
Expand All @@ -108,10 +108,10 @@ public static Map<String, String> getParametersFromContent(String content){
result.put(parameter.getKey(), parameter.getValue());
}
}

return result;
}

public Map<String, String> getAdditionalParameters(StashPullRequestResponseValue pullRequest){
StashPullRequestResponseValueRepository destination = pullRequest.getToRef();
String owner = destination.getRepository().getProjectName();
Expand All @@ -124,7 +124,7 @@ public Map<String, String> getAdditionalParameters(StashPullRequestResponseValue
// Collections.reverse(comments);

Map<String, String> result = new TreeMap<String, String>();

for (StashPullRequestComment comment : comments) {
String content = comment.getText();
if (content == null || content.isEmpty()) {
Expand All @@ -140,7 +140,7 @@ public Map<String, String> getAdditionalParameters(StashPullRequestResponseValue
}
return null;
}

public void addFutureBuildTasks(Collection<StashPullRequestResponseValue> pullRequests) {
for(StashPullRequestResponseValue pullRequest : pullRequests) {
Map<String, String> additionalParameters = getAdditionalParameters(pullRequest);
Expand All @@ -161,6 +161,7 @@ public void addFutureBuildTasks(Collection<StashPullRequestResponseValue> pullRe
pullRequest.getFromRef().getLatestCommit(),
pullRequest.getToRef().getLatestCommit(),
commentId,
pullRequest.getVersion(),
additionalParameters);
this.builder.getTrigger().startJob(cause);

Expand All @@ -187,7 +188,7 @@ private String getMessageForBuildResult(Result result) {
}
return message;
}

public void postFinishedComment(String pullRequestId, String sourceCommit, String destinationCommit, Result buildResult, String buildUrl, int buildNumber, String additionalComment, String duration) {
String message = getMessageForBuildResult(buildResult);
String comment = format(BUILD_FINISH_SENTENCE, builder.getProject().getDisplayName(), sourceCommit, destinationCommit, message, buildUrl, buildNumber, duration);
Expand All @@ -197,6 +198,11 @@ public void postFinishedComment(String pullRequestId, String sourceCommit, Stri
this.client.postPullRequestComment(pullRequestId, comment);
}

public boolean mergePullRequest(String pullRequestId, String version)
{
return this.client.mergePullRequest(pullRequestId, version);
}

private Boolean isPullRequestMergable(StashPullRequestResponseValue pullRequest) {
if (trigger.isCheckMergeable() || trigger.isCheckNotConflicted()) {
StashPullRequestMergableResponse mergable = client.getPullRequestMergeStatus(pullRequest.getId());
Expand Down
Loading

0 comments on commit c160e04

Please sign in to comment.