Fix Jenkins Workflow support so that triggered builds receive the right parameters from GitLab

This commit is contained in:
Owen Mehegan 2016-02-22 16:33:27 -08:00
parent 9ff93e9321
commit f0279355a1
1 changed files with 156 additions and 174 deletions

View File

@ -9,6 +9,7 @@ import hudson.model.ParameterValue;
import hudson.model.Result;
import hudson.model.AbstractProject;
import hudson.model.Cause;
import hudson.model.CauseAction;
import hudson.model.Job;
import hudson.model.ParameterDefinition;
import hudson.model.ParametersAction;
@ -222,36 +223,26 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
if (triggerOnPush && this.isBranchAllowed(this.getSourceBranch(req))) {
getDescriptor().queue.execute(new Runnable() {
public void run() {
LOGGER.log(Level.INFO, "{0} triggered for push.", job.getFullName());
String name = " #" + job.getNextBuildNumber();
GitLabPushCause cause = createGitLabPushCause(req);
Action[] actions = createActions(req);
Action[] actions = createActions(req, job);
boolean scheduled;
int projectbuildDelay = 0;
if (job instanceof AbstractProject<?,?>) {
AbstractProject job_ap = (AbstractProject<?, ?>) job;
scheduled = job_ap.scheduleBuild(job_ap.getQuietPeriod(), cause, actions);
if (job instanceof ParameterizedJobMixIn.ParameterizedJob) {
ParameterizedJobMixIn.ParameterizedJob abstractProject = (ParameterizedJobMixIn.ParameterizedJob)job;
if (abstractProject.getQuietPeriod() > projectbuildDelay) {
projectbuildDelay = abstractProject.getQuietPeriod();
}
else {
scheduled = scheduledJob.scheduleBuild(cause);
}
if (scheduled) {
LOGGER.log(Level.INFO, "GitLab Push Request detected in {0}. Triggering {1}",
new String[] { job.getFullName(), name });
} else {
LOGGER.log(Level.INFO, "GitLab Push Request detected in {0}. Job is already in the queue.",
job.getFullName());
}
if(addCiMessage) {
req.createCommitStatus(getDescriptor().getGitlab().instance(), "pending", Jenkins.getInstance().getRootUrl() + job.getUrl());
}
scheduledJob.scheduleBuild2(projectbuildDelay, actions);
}
}
private GitLabPushCause createGitLabPushCause(GitLabPushRequest req) {
@ -264,8 +255,9 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
return cause;
}
private Action[] createActions(GitLabPushRequest req) {
private Action[] createActions(GitLabPushRequest req, Job job) {
ArrayList<Action> actions = new ArrayList<Action>();
actions.add(new CauseAction(createGitLabPushCause(req)));
String branch = getSourceBranch(req);
@ -316,9 +308,6 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
return actionsArray;
}
});
}
}
public RevisionParameterAction createPushRequestRevisionParameter(Job<?, ?> job, GitLabPushRequest req) {
RevisionParameterAction revision = null;
@ -349,40 +338,38 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
// executes when the Trigger receives a merge request
public void onPost(final GitLabMergeRequest req) {
if (triggerOnMergeRequest && this.isBranchAllowed(req.getObjectAttribute().getTargetBranch())) {
getDescriptor().queue.execute(new Runnable() {
public void run() {
LOGGER.log(Level.INFO, "{0} triggered for merge request.", job.getFullName());
String name = " #" + job.getNextBuildNumber();
GitLabMergeCause cause = createGitLabMergeCause(req);
Action[] actions = createActions(req);
ParameterizedJobMixIn scheduledJob = new ParameterizedJobMixIn() {
final ParameterizedJobMixIn scheduledJob = new ParameterizedJobMixIn() {
@Override
protected Job asJob() {
return job;
}
};
boolean scheduled;
if (job instanceof AbstractProject<?,?>) {
AbstractProject job_ap = (AbstractProject<?, ?>) job;
scheduled = job_ap.scheduleBuild(job_ap.getQuietPeriod(), cause, actions);
}
else {
scheduled = scheduledJob.scheduleBuild(cause);
}
if (triggerOnMergeRequest && this.isBranchAllowed(req.getObjectAttribute().getTargetBranch())) {
if (scheduled) {
LOGGER.log(Level.INFO, "GitLab Merge Request detected in {0}. Triggering {1}", new String[]{job.getFullName(), name});
} else {
LOGGER.log(Level.INFO, "GitLab Merge Request detected in {0}. Job is already in the queue.", job.getFullName());
LOGGER.log(Level.INFO, "{0} triggered for merge request.", job.getFullName());
String name = " #" + job.getNextBuildNumber();
GitLabMergeCause cause = createGitLabMergeCause(req);
Action[] actions = createActions(req, job);
int projectbuildDelay = 0;
if (job instanceof ParameterizedJobMixIn.ParameterizedJob) {
ParameterizedJobMixIn.ParameterizedJob abstractProject = (ParameterizedJobMixIn.ParameterizedJob)job;
if (abstractProject.getQuietPeriod() > projectbuildDelay) {
projectbuildDelay = abstractProject.getQuietPeriod();
}
}
if(addCiMessage) {
req.createCommitStatus(getDescriptor().getGitlab().instance(), "pending", Jenkins.getInstance().getRootUrl() + job.getUrl());
}
scheduledJob.scheduleBuild2(projectbuildDelay, actions);
} else {
LOGGER.log(Level.INFO, "trigger on merge request not set");
}
}
private GitLabMergeCause createGitLabMergeCause(GitLabMergeRequest req) {
@ -395,7 +382,7 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
return cause;
}
private Action[] createActions(GitLabMergeRequest req) {
private Action[] createActions(GitLabMergeRequest req, Job job) {
List<Action> actions = new ArrayList<Action>();
Map<String, ParameterValue> values = getDefaultParameters();
@ -440,11 +427,6 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
return actionsArray;
}
});
}
}
private Map<String, ParameterValue> getDefaultParameters() {
Map<String, ParameterValue> values = new HashMap<String, ParameterValue>();
ParametersDefinitionProperty definitionProperty = job.getProperty(ParametersDefinitionProperty.class);