diff --git a/src/main/java/org/jenkinsci/plugins/ghprb/GhprbPullRequestMerge.java b/src/main/java/org/jenkinsci/plugins/ghprb/GhprbPullRequestMerge.java index 89d9320d7..6382ecf4d 100644 --- a/src/main/java/org/jenkinsci/plugins/ghprb/GhprbPullRequestMerge.java +++ b/src/main/java/org/jenkinsci/plugins/ghprb/GhprbPullRequestMerge.java @@ -14,7 +14,9 @@ import hudson.tasks.Publisher; import hudson.tasks.Recorder; import hudson.util.FormValidation; +import hudson.util.ListBoxModel; import jenkins.tasks.SimpleBuildStep; +import org.apache.commons.lang.StringUtils; import org.kohsuke.github.GHIssue; import org.kohsuke.github.GHPullRequest; import org.kohsuke.github.GHPullRequestCommitDetail; @@ -42,6 +44,8 @@ public class GhprbPullRequestMerge extends Recorder implements SimpleBuildStep { private final String mergeComment; + private final String mergeMethod; + private final Boolean failOnNonMerge; private final Boolean deleteOnMerge; @@ -49,10 +53,11 @@ public class GhprbPullRequestMerge extends Recorder implements SimpleBuildStep { private final Boolean allowMergeWithoutTriggerPhrase; @DataBoundConstructor - public GhprbPullRequestMerge(String mergeComment, boolean onlyAdminsMerge, boolean disallowOwnCode, boolean failOnNonMerge, - boolean deleteOnMerge, boolean allowMergeWithoutTriggerPhrase) { + public GhprbPullRequestMerge(String mergeComment, String mergeMethod, boolean onlyAdminsMerge, boolean disallowOwnCode, + boolean failOnNonMerge, boolean deleteOnMerge, boolean allowMergeWithoutTriggerPhrase) { this.mergeComment = mergeComment; + this.mergeMethod = mergeMethod; this.onlyAdminsMerge = onlyAdminsMerge; this.disallowOwnCode = disallowOwnCode; this.failOnNonMerge = failOnNonMerge; @@ -64,6 +69,10 @@ public String getMergeComment() { return mergeComment; } + public String getMergeMethod() { + return mergeMethod; + } + public boolean getOnlyAdminsMerge() { return onlyAdminsMerge == null ? false : onlyAdminsMerge; } @@ -216,7 +225,7 @@ public void perform( e.printStackTrace(listener.getLogger()); } String mergeComment = Ghprb.replaceMacros(run, listener, getMergeComment()); - pr.merge(mergeComment); + pr.merge(mergeComment, null, GHPullRequest.MergeMethod.valueOf(getMergeMethod())); listener.getLogger().println("Pull request successfully merged"); deleteBranch(run, launcher, listener); } @@ -309,6 +318,18 @@ public FormValidation doCheck(@AncestorInPath Job project, @QueryParameter FilePath buildDirectory = new FilePath(project.getBuildDir()); return FilePath.validateFileMask(buildDirectory, value); } + + public ListBoxModel doFillMergeMethodItems() { + ListBoxModel items = new ListBoxModel(); + for (GHPullRequest.MergeMethod mergeMethod: GHPullRequest.MergeMethod.values()) { + items.add(StringUtils.capitalize(mergeMethod.name().toLowerCase()), mergeMethod.name()); + } + return items; + } + + public String defaultMergeMethod() { + return GHPullRequest.MergeMethod.MERGE.toString(); + } } } diff --git a/src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbContextExtensionPoint.java b/src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbContextExtensionPoint.java index 06275f15f..2fa9f476f 100644 --- a/src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbContextExtensionPoint.java +++ b/src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbContextExtensionPoint.java @@ -56,6 +56,7 @@ public Object mergeGithubPullRequest(Runnable closure) { return new GhprbPullRequestMerge( context.mergeComment, + context.mergeMethod, context.onlyAdminsMerge, context.disallowOwnCode, context.failOnNonMerge, diff --git a/src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbPullRequestMergeContext.java b/src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbPullRequestMergeContext.java index 341daa397..9fe570f04 100644 --- a/src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbPullRequestMergeContext.java +++ b/src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbPullRequestMergeContext.java @@ -5,6 +5,8 @@ public class GhprbPullRequestMergeContext implements Context { String mergeComment; + String mergeMethod; + boolean onlyAdminsMerge; boolean disallowOwnCode; @@ -22,6 +24,13 @@ public void mergeComment(String mergeComment) { this.mergeComment = mergeComment; } + /** + * @param mergeMethod Sets a merge method that should be used when the merge command is sent to GitHub. + */ + public void mergeMethod(String mergeMethod) { + this.mergeMethod = mergeMethod; + } + /** * @param onlyAdminsMerge Allows only admin users to trigger a pull request merge. Defaults to {@code false}. */ diff --git a/src/main/resources/org/jenkinsci/plugins/ghprb/GhprbPullRequestMerge/config.jelly b/src/main/resources/org/jenkinsci/plugins/ghprb/GhprbPullRequestMerge/config.jelly index 4a2acdff2..5b90192f6 100644 --- a/src/main/resources/org/jenkinsci/plugins/ghprb/GhprbPullRequestMerge/config.jelly +++ b/src/main/resources/org/jenkinsci/plugins/ghprb/GhprbPullRequestMerge/config.jelly @@ -3,6 +3,9 @@ + + + diff --git a/src/main/resources/org/jenkinsci/plugins/ghprb/GhprbPullRequestMerge/help-mergeMethod.html b/src/main/resources/org/jenkinsci/plugins/ghprb/GhprbPullRequestMerge/help-mergeMethod.html new file mode 100644 index 000000000..edb9a104d --- /dev/null +++ b/src/main/resources/org/jenkinsci/plugins/ghprb/GhprbPullRequestMerge/help-mergeMethod.html @@ -0,0 +1,3 @@ +
+ Merge method to use (About merge methods). +
diff --git a/src/test/java/org/jenkinsci/plugins/ghprb/GhprbPullRequestMergeTest.java b/src/test/java/org/jenkinsci/plugins/ghprb/GhprbPullRequestMergeTest.java index c4e21a0c6..d330586f5 100644 --- a/src/test/java/org/jenkinsci/plugins/ghprb/GhprbPullRequestMergeTest.java +++ b/src/test/java/org/jenkinsci/plugins/ghprb/GhprbPullRequestMergeTest.java @@ -116,6 +116,8 @@ public class GhprbPullRequestMergeTest { private final String mergeComment = "merge"; + private final GHPullRequest.MergeMethod mergeMethod = GHPullRequest.MergeMethod.MERGE; + private final Integer pullId = 1; private Map triggerValues; @@ -244,6 +246,7 @@ private GhprbPullRequestMerge setupMerger( GhprbPullRequestMerge merger = spy(new GhprbPullRequestMerge( mergeComment, + mergeMethod.name(), onlyAdminsMerge, disallowOwnCode, failOnNonMerge, @@ -272,35 +275,35 @@ public void testApproveMerge() throws Exception { setupConditions(nonAdminLogin, committerName, committerEmail, triggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(1)).merge(mergeComment); + verify(pr, times(1)).merge(mergeComment, null, mergeMethod); setupConditions(adminLogin, nonCommitterName, nonCommitterEmail, triggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(2)).merge(mergeComment); + verify(pr, times(2)).merge(mergeComment, null, mergeMethod); setupConditions(adminLogin, committerName, committerEmail, nonTriggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(2)).merge(mergeComment); + verify(pr, times(2)).merge(mergeComment, null, mergeMethod); setupConditions(nonAdminLogin, nonCommitterName, nonCommitterEmail, triggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(3)).merge(mergeComment); + verify(pr, times(3)).merge(mergeComment, null, mergeMethod); setupConditions(nonAdminLogin, nonCommitterName, nonCommitterEmail, nonTriggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(3)).merge(mergeComment); + verify(pr, times(3)).merge(mergeComment, null, mergeMethod); setupConditions(adminLogin, nonCommitterName, nonCommitterEmail, nonTriggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(3)).merge(mergeComment); + verify(pr, times(3)).merge(mergeComment, null, mergeMethod); setupConditions(nonAdminLogin, nonCommitterName, nonCommitterEmail, nonTriggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(3)).merge(mergeComment); + verify(pr, times(3)).merge(mergeComment, null, mergeMethod); setupConditions(adminLogin, committerName, committerEmail, triggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(4)).merge(mergeComment); + verify(pr, times(4)).merge(mergeComment, null, mergeMethod); } @Test @@ -313,11 +316,11 @@ public void testAdminMerge() throws Exception { setupConditions(adminLogin, committerName, committerEmail, triggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(1)).merge(mergeComment); + verify(pr, times(1)).merge(mergeComment, null, mergeMethod); setupConditions(nonAdminLogin, committerName, committerEmail, triggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(1)).merge(mergeComment); + verify(pr, times(1)).merge(mergeComment, null, mergeMethod); } @Test @@ -330,11 +333,11 @@ public void testTriggerMerge() throws Exception { setupConditions(adminLogin, committerName, committerEmail, triggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(1)).merge(mergeComment); + verify(pr, times(1)).merge(mergeComment, null, mergeMethod); setupConditions(adminLogin, committerName, committerEmail, nonTriggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(1)).merge(mergeComment); + verify(pr, times(1)).merge(mergeComment, null, mergeMethod); } @Test @@ -347,11 +350,11 @@ public void testOwnCodeMerge() throws Exception { setupConditions(adminLogin, nonCommitterName, nonCommitterEmail, triggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(1)).merge(mergeComment); + verify(pr, times(1)).merge(mergeComment, null, mergeMethod); setupConditions(adminLogin, committerName, committerEmail, triggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(1)).merge(mergeComment); + verify(pr, times(1)).merge(mergeComment, null, mergeMethod); } @Test @@ -364,39 +367,39 @@ public void testDenyMerge() throws Exception { setupConditions(nonAdminLogin, nonAdminLogin, nonCommitterName, nonCommitterEmail, triggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(0)).merge(mergeComment); + verify(pr, times(0)).merge(mergeComment, null, mergeMethod); setupConditions(nonAdminLogin, adminLogin, committerName, committerEmail, triggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(0)).merge(mergeComment); + verify(pr, times(0)).merge(mergeComment, null, mergeMethod); setupConditions(nonAdminLogin, adminLogin, nonCommitterName, nonCommitterEmail, nonTriggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(0)).merge(mergeComment); + verify(pr, times(0)).merge(mergeComment, null, mergeMethod); setupConditions(nonAdminLogin, nonAdminLogin, nonCommitterName, nonCommitterEmail, triggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(0)).merge(mergeComment); + verify(pr, times(0)).merge(mergeComment, null, mergeMethod); setupConditions(nonAdminLogin, nonAdminLogin, nonCommitterName, nonCommitterEmail, nonTriggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(0)).merge(mergeComment); + verify(pr, times(0)).merge(mergeComment, null, mergeMethod); setupConditions(nonAdminLogin, adminLogin, committerName, committerEmail, nonTriggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(0)).merge(mergeComment); + verify(pr, times(0)).merge(mergeComment, null, mergeMethod); setupConditions(nonAdminLogin, nonAdminLogin, committerName, committerEmail, nonTriggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(0)).merge(mergeComment); + verify(pr, times(0)).merge(mergeComment, null, mergeMethod); setupConditions(adminLogin, adminLogin, nonCommitterName, nonCommitterEmail, triggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(0)).merge(mergeComment); + verify(pr, times(0)).merge(mergeComment, null, mergeMethod); setupConditions(nonAdminLogin, adminLogin, nonCommitterName, nonCommitterEmail, triggerPhrase); merger.perform(build, mockFilePath, launcher, listener); - verify(pr, times(1)).merge(mergeComment); + verify(pr, times(1)).merge(mergeComment, null, mergeMethod); } @Test