diff --git a/pom.xml b/pom.xml index 5dee3ec84..9d128cd63 100644 --- a/pom.xml +++ b/pom.xml @@ -70,6 +70,12 @@ ssh-agent 1.3 + + org.jenkins-ci.plugins + job-dsl + 1.39 + true + junit junit diff --git a/src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbContextExtensionPoint.java b/src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbContextExtensionPoint.java new file mode 100644 index 000000000..f0f4f0ae5 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbContextExtensionPoint.java @@ -0,0 +1,42 @@ +package org.jenkinsci.plugins.ghprb.jobdsl; + +import antlr.ANTLRException; +import com.google.common.base.Joiner; +import hudson.Extension; +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.GhprbTrigger; + +import java.util.ArrayList; + +@Extension(optional = true) +public class GhprbContextExtensionPoint extends ContextExtensionPoint { + @DslExtensionMethod(context = TriggerContext.class) + public Object githubPullRequest(Runnable closure) throws ANTLRException { + GhprbTriggerContext context = new GhprbTriggerContext(); + executeInContext(closure, context); + return new GhprbTrigger( + Joiner.on("\n").join(context.admins), + Joiner.on("\n").join(context.userWhitelist), + Joiner.on("\n").join(context.orgWhitelist), + context.cron, + context.triggerPhrase, + context.onlyTriggerPhrase, + context.useGitHubHooks, + context.permitAll, + context.autoCloseFailedPullRequests, + null, + null, + new ArrayList(), + context.allowMembersOfWhitelistedOrgsAsAdmin, + null, + null, + null, + null, + null, + context.extensionContext.extensions + ); + } +} diff --git a/src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbExtensionContext.java b/src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbExtensionContext.java new file mode 100644 index 000000000..70e02c7fb --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbExtensionContext.java @@ -0,0 +1,29 @@ +package org.jenkinsci.plugins.ghprb.jobdsl; + +import javaposse.jobdsl.dsl.Context; +import javaposse.jobdsl.plugin.ContextExtensionPoint; +import org.jenkinsci.plugins.ghprb.extensions.GhprbExtension; +import org.jenkinsci.plugins.ghprb.extensions.status.GhprbSimpleStatus; + +import java.util.ArrayList; +import java.util.List; + +class GhprbExtensionContext implements Context { + List extensions = new ArrayList(); + + /** + * Updates the commit status during the build. + */ + void commitStatus(Runnable closure) { + GhprbSimpleStatusContext context = new GhprbSimpleStatusContext(); + ContextExtensionPoint.executeInContext(closure, context); + + extensions.add(new GhprbSimpleStatus( + context.context, + context.statusUrl, + context.triggeredStatus, + context.startedStatus, + context.completedStatus + )); + } +} diff --git a/src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbSimpleStatusContext.java b/src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbSimpleStatusContext.java new file mode 100644 index 000000000..0d71cd06f --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbSimpleStatusContext.java @@ -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 GhprbSimpleStatusContext implements Context { + String context; + String triggeredStatus; + String startedStatus; + String statusUrl; + List completedStatus = new ArrayList(); + + /** + * 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 + )); + } +} diff --git a/src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbTriggerContext.java b/src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbTriggerContext.java new file mode 100644 index 000000000..d4fc4ccca --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbTriggerContext.java @@ -0,0 +1,161 @@ +package org.jenkinsci.plugins.ghprb.jobdsl; + +import javaposse.jobdsl.dsl.Context; +import javaposse.jobdsl.dsl.helpers.triggers.GitHubPullRequestBuilderExtensionContext; +import javaposse.jobdsl.plugin.ContextExtensionPoint; + +import java.util.ArrayList; +import java.util.List; + +class GhprbTriggerContext implements Context { + List admins = new ArrayList(); + List userWhitelist = new ArrayList(); + List orgWhitelist = new ArrayList(); + String cron = "H/5 * * * *"; + String triggerPhrase; + boolean onlyTriggerPhrase; + boolean useGitHubHooks; + boolean permitAll; + boolean autoCloseFailedPullRequests; + boolean allowMembersOfWhitelistedOrgsAsAdmin; + GhprbExtensionContext extensionContext = new GhprbExtensionContext(); + + /** + * Adds admins for this job. + */ + public void admin(String admin) { + admins.add(admin); + } + + /** + * Adds admins for this job. + */ + public void admins(Iterable admins) { + for (String admin : admins) { + admin(admin); + } + } + + /** + * Adds whitelisted users for this job. + */ + public void userWhitelist(String user) { + userWhitelist.add(user); + } + + /** + * Adds whitelisted users for this job. + */ + public void userWhitelist(Iterable users) { + for (String user : users) { + userWhitelist(user); + } + } + + /** + * Adds organisation names whose members are considered whitelisted for this specific job. + */ + public void orgWhitelist(String organization) { + orgWhitelist.add(organization); + } + + /** + * Adds organisation names whose members are considered whitelisted for this specific job. + */ + public void orgWhitelist(Iterable organizations) { + for (String organization : organizations) { + orgWhitelist(organization); + } + } + + /** + * This schedules polling to GitHub for new changes in pull requests. + */ + public void cron(String cron) { + this.cron = cron; + } + + /** + * When filled, commenting this phrase in the pull request will trigger a build. + */ + public void triggerPhrase(String triggerPhrase) { + this.triggerPhrase = triggerPhrase; + } + + /** + * When set, only commenting the trigger phrase in the pull request will trigger a build. + */ + public void onlyTriggerPhrase(boolean onlyTriggerPhrase) { + this.onlyTriggerPhrase = onlyTriggerPhrase; + } + + /** + * When set, only commenting the trigger phrase in the pull request will trigger a build. + */ + public void onlyTriggerPhrase() { + onlyTriggerPhrase(true); + } + + /** + * Checking this option will disable regular polling for changes in GitHub and will try to create a GitHub hook. + */ + public void useGitHubHooks(boolean useGitHubHooks) { + this.useGitHubHooks = useGitHubHooks; + } + + /** + * Checking this option will disable regular polling for changes in GitHub and will try to create a GitHub hook. + */ + public void useGitHubHooks() { + useGitHubHooks(true); + } + + /** + * Build every pull request automatically without asking. + */ + public void permitAll(boolean permitAll) { + this.permitAll = permitAll; + } + + /** + * Build every pull request automatically without asking. + */ + public void permitAll() { + permitAll(true); + } + + /** + * Close pull request automatically when the build fails. + */ + public void autoCloseFailedPullRequests(boolean autoCloseFailedPullRequests) { + this.autoCloseFailedPullRequests = autoCloseFailedPullRequests; + } + + /** + * Close pull request automatically when the build fails. + */ + public void autoCloseFailedPullRequests() { + autoCloseFailedPullRequests(true); + } + + /** + * Allows members of whitelisted organisations to behave like admins. + */ + public void allowMembersOfWhitelistedOrgsAsAdmin(boolean allowMembersOfWhitelistedOrgsAsAdmin) { + this.allowMembersOfWhitelistedOrgsAsAdmin = allowMembersOfWhitelistedOrgsAsAdmin; + } + + /** + * Allows members of whitelisted organisations to behave like admins. + */ + public void allowMembersOfWhitelistedOrgsAsAdmin() { + allowMembersOfWhitelistedOrgsAsAdmin(true); + } + + /** + * Adds additional trigger options. + */ + public void extensions(Runnable closure) { + ContextExtensionPoint.executeInContext(closure, extensionContext); + } +}