Skip to content

Commit

Permalink
Merge pull request #246 from daspilker/JENKINS-31214
Browse files Browse the repository at this point in the history
[JENKINS-31214] added Job DSL for GhprbPullRequestMerge and docs
  • Loading branch information
DavidTanner committed Jan 7, 2016
2 parents 070f988 + e75db21 commit ab983d4
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
58 changes: 58 additions & 0 deletions README.md
Expand Up @@ -105,6 +105,64 @@ Make sure you **DON'T** have ``Prune remote branches before build`` advanced opt
#### Parameterized Builds
If you want to manually build the job, in the job setting check ``This build is parameterized`` and add string parameter named ``sha1`` with a default value of ``master``. When starting build give the ``sha1`` parameter commit id you want to build or refname (eg: ``origin/pr/9/head``).

### Job DSL Support

Since the plugin contains an extension for the Job DSL plugin to add DSL syntax for configuring the build trigger and
the pull request merger post-build action.

Here is an example showing all DSL syntax elements:

```groovy
job('example') {
scm {
git {
remote {
github('test-owner/test-project')
refspec('+refs/pull/*:refs/remotes/origin/pr/*')
}
branch('${sha1}')
}
}
triggers {
githubPullRequest {
admin('user_1')
admins(['user_2', 'user_3'])
userWhitelist('you@you.com')
userWhitelist(['me@me.com', 'they@they.com'])
orgWhitelist('my_github_org')
orgWhitelist(['your_github_org', 'another_org'])
cron('H/5 * * * *')
triggerPhrase('OK to test')
onlyTriggerPhrase()
useGitHubHooks()
permitAll()
autoCloseFailedPullRequests()
allowMembersOfWhitelistedOrgsAsAdmin()
extensions {
commitStatus {
context('deploy to staging site')
triggeredStatus('starting deployment to staging site...')
startedStatus('deploying to staging site...')
statusUrl('http://mystatussite.com/prs')
completedStatus('SUCCESS', 'All is well')
completedStatus('FAILURE', 'Something went wrong. Investigate!')
completedStatus('PENDING', 'still in progress...')
completedStatus('ERROR', 'Something went really wrong. Investigate!')
}
}
}
}
publishers {
mergeGithubPullRequest {
mergeComment('merged by Jenkins')
onlyAdminsMerge()
disallowOwnCode()
failOnNonMerge()
deleteOnMerge()
}
}
}
```

### Updates

Expand Down
Expand Up @@ -3,10 +3,12 @@
import antlr.ANTLRException;
import com.google.common.base.Joiner;
import hudson.Extension;
import javaposse.jobdsl.dsl.helpers.publisher.PublisherContext;
import javaposse.jobdsl.dsl.helpers.triggers.TriggerContext;
import javaposse.jobdsl.plugin.ContextExtensionPoint;
import javaposse.jobdsl.plugin.DslExtensionMethod;
import org.jenkinsci.plugins.ghprb.GhprbBranch;
import org.jenkinsci.plugins.ghprb.GhprbPullRequestMerge;
import org.jenkinsci.plugins.ghprb.GhprbTrigger;

import java.util.ArrayList;
Expand Down Expand Up @@ -39,4 +41,18 @@ public Object githubPullRequest(Runnable closure) throws ANTLRException {
context.extensionContext.extensions
);
}

@DslExtensionMethod(context = PublisherContext.class)
public Object mergeGithubPullRequest(Runnable closure) {
GhprbPullRequestMergeContext context = new GhprbPullRequestMergeContext();
executeInContext(closure, context);

return new GhprbPullRequestMerge(
context.mergeComment,
context.onlyAdminsMerge,
context.disallowOwnCode,
context.failOnNonMerge,
context.deleteOnMerge
);
}
}
@@ -0,0 +1,74 @@
package org.jenkinsci.plugins.ghprb.jobdsl;

import javaposse.jobdsl.dsl.Context;

public class GhprbPullRequestMergeContext implements Context {
String mergeComment;
boolean onlyAdminsMerge;
boolean disallowOwnCode;
boolean failOnNonMerge;
boolean deleteOnMerge;

/**
* Sets a comment that should show up when the merge command is sent to GitHub.
*/
public void mergeComment(String mergeComment) {
this.mergeComment = mergeComment;
}

/**
* Allows only admin users to trigger a pull request merge. Defaults to {@code false}.
*/
public void onlyAdminsMerge(boolean onlyAdminsMerge) {
this.onlyAdminsMerge = onlyAdminsMerge;
}

/**
* Allows only admin users to trigger a pull request merge. Defaults to {@code false}.
*/
public void onlyAdminsMerge() {
onlyAdminsMerge(true);
}

/**
* Disallows a user to merge their own code. Defaults to {@code false}.
*/
public void disallowOwnCode(boolean disallowOwnCode) {
this.disallowOwnCode = disallowOwnCode;
}

/**
* Disallows a user to merge their own code. Defaults to {@code false}.
*/
public void disallowOwnCode() {
disallowOwnCode(true);
}

/**
* Fails the build if the pull request can't be merged. Defaults to {@code false}.
*/
public void failOnNonMerge(boolean failOnNonMerge) {
this.failOnNonMerge = failOnNonMerge;
}

/**
* Fails the build if the pull request can't be merged. Defaults to {@code false}.
*/
public void failOnNonMerge() {
failOnNonMerge(true);
}

/**
* Deletes the branch after a successful merge. Defaults to {@code false}.
*/
public void deleteOnMerge(boolean deleteOnMerge) {
this.deleteOnMerge = deleteOnMerge;
}

/**
* Deletes the branch after a successful merge. Defaults to {@code false}.
*/
public void deleteOnMerge() {
deleteOnMerge(true);
}
}

0 comments on commit ab983d4

Please sign in to comment.