Skip to content

Commit

Permalink
Merge pull request #249 from Eriten/JENKINS-32375
Browse files Browse the repository at this point in the history
JENKINS-32375: GHPRB - Add DSL Support for Downstream Commit Status and Whitelist Target Branches
  • Loading branch information
DavidTanner committed Jan 14, 2016
2 parents acec5fe + f27dbc9 commit 07748d6
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 4 deletions.
22 changes: 21 additions & 1 deletion README.md
Expand Up @@ -110,10 +110,13 @@ If you want to manually build the job, in the job setting check ``This build is
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.

It is also possible to set Downstream job commit statuses when `displayBuildErrorsOnDownstreamBuilds()` is set in the
upstream job's triggers and downstreamCommitStatus block is included in the downstream job's wrappers.

Here is an example showing all DSL syntax elements:

```groovy
job('example') {
job('upstreamJob') {
scm {
git {
remote {
Expand All @@ -123,6 +126,7 @@ job('example') {
branch('${sha1}')
}
}
triggers {
githubPullRequest {
admin('user_1')
Expand All @@ -137,6 +141,8 @@ job('example') {
useGitHubHooks()
permitAll()
autoCloseFailedPullRequests()
displayBuildErrorsOnDownstreamBuilds()
whiteListTargetBranches(['master','test', 'test2'])
allowMembersOfWhitelistedOrgsAsAdmin()
extensions {
commitStatus {
Expand All @@ -162,6 +168,20 @@ job('example') {
}
}
}
job('downstreamJob') {
wrappers {
downstreamCommitStatus {
context('CONTEXT NAME')
triggeredStatus("The job has triggered")
startedStatus("The job has started")
statusUrl()
completedStatus('SUCCESS', "The job has passed")
completedStatus('FAILURE', "The job has failed")
completedStatus('ERROR', "The job has resulted in an error")
}
}
}
```

### Updates
Expand Down
Expand Up @@ -5,11 +5,13 @@
import hudson.Extension;
import javaposse.jobdsl.dsl.helpers.publisher.PublisherContext;
import javaposse.jobdsl.dsl.helpers.triggers.TriggerContext;
import javaposse.jobdsl.dsl.helpers.wrapper.WrapperContext;
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 org.jenkinsci.plugins.ghprb.upstream.GhprbUpstreamStatus;

import java.util.ArrayList;

Expand All @@ -29,9 +31,9 @@ public Object githubPullRequest(Runnable closure) throws ANTLRException {
context.useGitHubHooks,
context.permitAll,
context.autoCloseFailedPullRequests,
context.displayBuildErrorsOnDownstreamBuilds,
null,
null,
new ArrayList<GhprbBranch>(),
context.whiteListTargetBranches,
context.allowMembersOfWhitelistedOrgsAsAdmin,
null,
null,
Expand All @@ -55,4 +57,18 @@ public Object mergeGithubPullRequest(Runnable closure) {
context.deleteOnMerge
);
}

@DslExtensionMethod(context = WrapperContext.class)
public Object downstreamCommitStatus(Runnable closure) {
GhprbUpstreamStatusContext context = new GhprbUpstreamStatusContext();
executeInContext(closure, context);

return new GhprbUpstreamStatus(
context.context,
context.statusUrl,
context.triggeredStatus,
context.startedStatus,
context.completedStatus
);
}
}
@@ -1,8 +1,8 @@
package org.jenkinsci.plugins.ghprb.jobdsl;

import javaposse.jobdsl.dsl.Context;
import javaposse.jobdsl.dsl.helpers.triggers.GitHubPullRequestBuilderExtensionContext;
import javaposse.jobdsl.plugin.ContextExtensionPoint;
import org.jenkinsci.plugins.ghprb.GhprbBranch;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -11,13 +11,15 @@ class GhprbTriggerContext implements Context {
List<String> admins = new ArrayList<String>();
List<String> userWhitelist = new ArrayList<String>();
List<String> orgWhitelist = new ArrayList<String>();
List<GhprbBranch> whiteListTargetBranches = new ArrayList<GhprbBranch>();
String cron = "H/5 * * * *";
String triggerPhrase;
boolean onlyTriggerPhrase;
boolean useGitHubHooks;
boolean permitAll;
boolean autoCloseFailedPullRequests;
boolean allowMembersOfWhitelistedOrgsAsAdmin;
boolean displayBuildErrorsOnDownstreamBuilds;
GhprbExtensionContext extensionContext = new GhprbExtensionContext();

/**
Expand Down Expand Up @@ -68,6 +70,23 @@ public void orgWhitelist(Iterable<String> organizations) {
}
}


/**
* Add branch names whose they are considered whitelisted for this specific job
*/
public void whiteListTargetBranch(String branch) {
whiteListTargetBranches.add(new GhprbBranch(branch));
}

/**
* Add branch names whose they are considered whitelisted for this specific job
*/
public void whiteListTargetBranches(Iterable<String> branches) {
for (String branch : branches) {
whiteListTargetBranches.add(new GhprbBranch(branch));
}
}

/**
* This schedules polling to GitHub for new changes in pull requests.
*/
Expand Down Expand Up @@ -152,6 +171,20 @@ public void allowMembersOfWhitelistedOrgsAsAdmin() {
allowMembersOfWhitelistedOrgsAsAdmin(true);
}

/**
* Allow this upstream job to get commit statuses from downstream builds
*/
public void displayBuildErrorsOnDownstreamBuilds(boolean displayBuildErrorsOnDownstreamBuilds) {
this.displayBuildErrorsOnDownstreamBuilds = displayBuildErrorsOnDownstreamBuilds;
}

/**
* Allow this upstream job to get commit statuses from downstream builds
*/
public void displayBuildErrorsOnDownstreamBuilds() {
displayBuildErrorsOnDownstreamBuilds(true);
}

/**
* Adds additional trigger options.
*/
Expand Down
@@ -0,0 +1,55 @@
package org.jenkinsci.plugins.ghprb.jobdsl;

import javaposse.jobdsl.dsl.Context;
import org.jenkinsci.plugins.ghprb.extensions.comments.GhprbBuildResultMessage;
import org.kohsuke.github.GHCommitState;

import java.util.ArrayList;
import java.util.List;

class GhprbUpstreamStatusContext implements Context {
String context;
String triggeredStatus;
String startedStatus;
String statusUrl;
List<GhprbBuildResultMessage> completedStatus = new ArrayList<GhprbBuildResultMessage>();

/**
* A string label to differentiate this status from the status of other systems.
*/
void context(String context) {
this.context = context;
}

/**
* Use a custom status for when a build is triggered.
*/
void triggeredStatus(String triggeredStatus) {
this.triggeredStatus = triggeredStatus;
}

/**
* Use a custom status for when a build is started.
*/
void startedStatus(String startedStatus) {
this.startedStatus = startedStatus;
}

/**
* Use a custom URL instead of the job default.
*/
void statusUrl(String statusUrl) {
this.statusUrl = statusUrl;
}

/**
* Use a custom status for when a build is completed. Can be called multiple times to set messages for different
* build results. Valid build results are {@code 'SUCCESS'}, {@code 'FAILURE'}, and {@code 'ERROR'}.
*/
void completedStatus(String buildResult, String message) {
completedStatus.add(new GhprbBuildResultMessage(
GHCommitState.valueOf(buildResult),
message
));
}
}

0 comments on commit 07748d6

Please sign in to comment.