Merge pull request #206 from jenkinsci/revised_workflow_branch_param_fix

Fix Jenkins Workflow support in build trigger code
This commit is contained in:
Owen Mehegan 2016-03-01 13:49:27 -08:00
commit d705ceb42f
1 changed files with 147 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;
@ -231,36 +232,25 @@ 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) {
@ -273,8 +263,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);
@ -325,9 +316,6 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
return actionsArray;
}
});
}
}
public RevisionParameterAction createPushRequestRevisionParameter(Job<?, ?> job, GitLabPushRequest req) {
RevisionParameterAction revision = null;
@ -358,40 +346,37 @@ 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());
GitLabMergeCause cause = createGitLabMergeCause(req);
Action action = createAction(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, action, new CauseAction(cause));
} else {
LOGGER.log(Level.INFO, "trigger on merge request not set");
}
}
private GitLabMergeCause createGitLabMergeCause(GitLabMergeRequest req) {
@ -404,9 +389,7 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
return cause;
}
private Action[] createActions(GitLabMergeRequest req) {
List<Action> actions = new ArrayList<Action>();
private Action createAction(GitLabMergeRequest req, Job job) {
Map<String, ParameterValue> values = getDefaultParameters();
values.put("gitlabSourceBranch", new StringParameterValue("gitlabSourceBranch", getSourceBranch(req)));
values.put("gitlabTargetBranch", new StringParameterValue("gitlabTargetBranch", req.getObjectAttribute().getTargetBranch()));
@ -441,17 +424,7 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
List<ParameterValue> listValues = new ArrayList<ParameterValue>(values.values());
ParametersAction parametersAction = new ParametersAction(listValues);
actions.add(parametersAction);
Action[] actionsArray = actions.toArray(new Action[0]);
return actionsArray;
}
});
}
return new ParametersAction(listValues);
}
private Map<String, ParameterValue> getDefaultParameters() {