forked from jenkinsci/gitlab-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request jenkinsci#229 from daspilker/JENKINS-31214
[JENKINS-31214] added Job DSL for GhprbTrigger
- Loading branch information
Showing
5 changed files
with
293 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbContextExtensionPoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<GhprbBranch>(), | ||
context.allowMembersOfWhitelistedOrgsAsAdmin, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
context.extensionContext.extensions | ||
); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbExtensionContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<GhprbExtension> extensions = new ArrayList<GhprbExtension>(); | ||
|
||
/** | ||
* 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 | ||
)); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbSimpleStatusContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<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 | ||
)); | ||
} | ||
} |
161 changes: 161 additions & 0 deletions
161
src/main/java/org/jenkinsci/plugins/ghprb/jobdsl/GhprbTriggerContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> admins = new ArrayList<String>(); | ||
List<String> userWhitelist = new ArrayList<String>(); | ||
List<String> orgWhitelist = new ArrayList<String>(); | ||
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<String> 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<String> 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<String> 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); | ||
} | ||
} |