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.Result;
import hudson.model.AbstractProject; import hudson.model.AbstractProject;
import hudson.model.Cause; import hudson.model.Cause;
import hudson.model.CauseAction;
import hudson.model.Job; import hudson.model.Job;
import hudson.model.ParameterDefinition; import hudson.model.ParameterDefinition;
import hudson.model.ParametersAction; import hudson.model.ParametersAction;
@ -231,104 +232,91 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
if (triggerOnPush && this.isBranchAllowed(this.getSourceBranch(req))) { if (triggerOnPush && this.isBranchAllowed(this.getSourceBranch(req))) {
getDescriptor().queue.execute(new Runnable() { LOGGER.log(Level.INFO, "{0} triggered for push.", job.getFullName());
public void run() { Action[] actions = createActions(req, job);
LOGGER.log(Level.INFO, "{0} triggered for push.", job.getFullName());
String name = " #" + job.getNextBuildNumber(); int projectbuildDelay = 0;
GitLabPushCause cause = createGitLabPushCause(req);
Action[] actions = createActions(req);
boolean scheduled; if (job instanceof ParameterizedJobMixIn.ParameterizedJob) {
ParameterizedJobMixIn.ParameterizedJob abstractProject = (ParameterizedJobMixIn.ParameterizedJob)job;
if (job instanceof AbstractProject<?,?>) { if (abstractProject.getQuietPeriod() > projectbuildDelay) {
AbstractProject job_ap = (AbstractProject<?, ?>) job; projectbuildDelay = abstractProject.getQuietPeriod();
scheduled = job_ap.scheduleBuild(job_ap.getQuietPeriod(), cause, actions);
}
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());
}
} }
}
private GitLabPushCause createGitLabPushCause(GitLabPushRequest req) { if(addCiMessage) {
GitLabPushCause cause; req.createCommitStatus(getDescriptor().getGitlab().instance(), "pending", Jenkins.getInstance().getRootUrl() + job.getUrl());
try { }
cause = new GitLabPushCause(req, getLogFile());
} catch (IOException ex) {
cause = new GitLabPushCause(req);
}
return cause;
}
private Action[] createActions(GitLabPushRequest req) { scheduledJob.scheduleBuild2(projectbuildDelay, actions);
ArrayList<Action> actions = new ArrayList<Action>();
String branch = getSourceBranch(req);
LOGGER.log(Level.INFO, "GitLab Push Request from branch {0}.", branch);
Map<String, ParameterValue> values = getDefaultParameters();
values.put("gitlabSourceBranch", new StringParameterValue("gitlabSourceBranch", branch));
values.put("gitlabTargetBranch", new StringParameterValue("gitlabTargetBranch", branch));
values.put("gitlabBranch", new StringParameterValue("gitlabBranch", branch));
values.put("gitlabActionType", new StringParameterValue("gitlabActionType", "PUSH"));
values.put("gitlabUserName", new StringParameterValue("gitlabUserName", req.getCommits().get(0).getAuthor().getName()));
values.put("gitlabUserEmail", new StringParameterValue("gitlabUserEmail", req.getCommits().get(0).getAuthor().getEmail()));
values.put("gitlabMergeRequestTitle", new StringParameterValue("gitlabMergeRequestTitle", ""));
values.put("gitlabMergeRequestId", new StringParameterValue("gitlabMergeRequestId", ""));
values.put("gitlabMergeRequestAssignee", new StringParameterValue("gitlabMergeRequestAssignee", ""));
LOGGER.log(Level.INFO, "Trying to get name and URL for job: {0}", job.getFullName());
String sourceRepoName = getDesc().getSourceRepoNameDefault(job);
String sourceRepoURL = getDesc().getSourceRepoURLDefault(job).toString();
if (!getDescriptor().getGitlabHostUrl().isEmpty()) {
// Get source repository if communication to Gitlab is possible
try {
sourceRepoName = req.getSourceProject(getDesc().getGitlab()).getPathWithNamespace();
sourceRepoURL = req.getSourceProject(getDesc().getGitlab()).getSshUrl();
} catch (IOException ex) {
LOGGER.log(Level.WARNING, "Could not fetch source project''s data from Gitlab. '('{0}':' {1}')'", new String[]{ex.toString(), ex.getMessage()});
}
}
values.put("gitlabSourceRepoName", new StringParameterValue("gitlabSourceRepoName", sourceRepoName));
values.put("gitlabSourceRepoURL", new StringParameterValue("gitlabSourceRepoURL", sourceRepoURL));
List<ParameterValue> listValues = new ArrayList<ParameterValue>(values.values());
ParametersAction parametersAction = new ParametersAction(listValues);
actions.add(parametersAction);
RevisionParameterAction revision;
revision = createPushRequestRevisionParameter(job, req);
if (revision==null) {
return null;
}
actions.add(revision);
Action[] actionsArray = actions.toArray(new Action[0]);
return actionsArray;
}
});
} }
} }
private GitLabPushCause createGitLabPushCause(GitLabPushRequest req) {
GitLabPushCause cause;
try {
cause = new GitLabPushCause(req, getLogFile());
} catch (IOException ex) {
cause = new GitLabPushCause(req);
}
return cause;
}
private Action[] createActions(GitLabPushRequest req, Job job) {
ArrayList<Action> actions = new ArrayList<Action>();
actions.add(new CauseAction(createGitLabPushCause(req)));
String branch = getSourceBranch(req);
LOGGER.log(Level.INFO, "GitLab Push Request from branch {0}.", branch);
Map<String, ParameterValue> values = getDefaultParameters();
values.put("gitlabSourceBranch", new StringParameterValue("gitlabSourceBranch", branch));
values.put("gitlabTargetBranch", new StringParameterValue("gitlabTargetBranch", branch));
values.put("gitlabBranch", new StringParameterValue("gitlabBranch", branch));
values.put("gitlabActionType", new StringParameterValue("gitlabActionType", "PUSH"));
values.put("gitlabUserName", new StringParameterValue("gitlabUserName", req.getCommits().get(0).getAuthor().getName()));
values.put("gitlabUserEmail", new StringParameterValue("gitlabUserEmail", req.getCommits().get(0).getAuthor().getEmail()));
values.put("gitlabMergeRequestTitle", new StringParameterValue("gitlabMergeRequestTitle", ""));
values.put("gitlabMergeRequestId", new StringParameterValue("gitlabMergeRequestId", ""));
values.put("gitlabMergeRequestAssignee", new StringParameterValue("gitlabMergeRequestAssignee", ""));
LOGGER.log(Level.INFO, "Trying to get name and URL for job: {0}", job.getFullName());
String sourceRepoName = getDesc().getSourceRepoNameDefault(job);
String sourceRepoURL = getDesc().getSourceRepoURLDefault(job).toString();
if (!getDescriptor().getGitlabHostUrl().isEmpty()) {
// Get source repository if communication to Gitlab is possible
try {
sourceRepoName = req.getSourceProject(getDesc().getGitlab()).getPathWithNamespace();
sourceRepoURL = req.getSourceProject(getDesc().getGitlab()).getSshUrl();
} catch (IOException ex) {
LOGGER.log(Level.WARNING, "Could not fetch source project''s data from Gitlab. '('{0}':' {1}')'", new String[]{ex.toString(), ex.getMessage()});
}
}
values.put("gitlabSourceRepoName", new StringParameterValue("gitlabSourceRepoName", sourceRepoName));
values.put("gitlabSourceRepoURL", new StringParameterValue("gitlabSourceRepoURL", sourceRepoURL));
List<ParameterValue> listValues = new ArrayList<ParameterValue>(values.values());
ParametersAction parametersAction = new ParametersAction(listValues);
actions.add(parametersAction);
RevisionParameterAction revision;
revision = createPushRequestRevisionParameter(job, req);
if (revision==null) {
return null;
}
actions.add(revision);
Action[] actionsArray = actions.toArray(new Action[0]);
return actionsArray;
}
public RevisionParameterAction createPushRequestRevisionParameter(Job<?, ?> job, GitLabPushRequest req) { public RevisionParameterAction createPushRequestRevisionParameter(Job<?, ?> job, GitLabPushRequest req) {
RevisionParameterAction revision = null; RevisionParameterAction revision = null;
@ -358,100 +346,85 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
// executes when the Trigger receives a merge request // executes when the Trigger receives a merge request
public void onPost(final GitLabMergeRequest req) { public void onPost(final GitLabMergeRequest req) {
final ParameterizedJobMixIn scheduledJob = new ParameterizedJobMixIn() {
@Override
protected Job asJob() {
return job;
}
};
if (triggerOnMergeRequest && this.isBranchAllowed(req.getObjectAttribute().getTargetBranch())) { if (triggerOnMergeRequest && this.isBranchAllowed(req.getObjectAttribute().getTargetBranch())) {
getDescriptor().queue.execute(new Runnable() { LOGGER.log(Level.INFO, "{0} triggered for merge request.", job.getFullName());
public void run() {
LOGGER.log(Level.INFO, "{0} triggered for merge request.", job.getFullName());
String name = " #" + job.getNextBuildNumber();
GitLabMergeCause cause = createGitLabMergeCause(req); GitLabMergeCause cause = createGitLabMergeCause(req);
Action[] actions = createActions(req); Action action = createAction(req, job);
ParameterizedJobMixIn scheduledJob = new ParameterizedJobMixIn() {
@Override
protected Job asJob() {
return job;
}
};
boolean scheduled; int projectbuildDelay = 0;
if (job instanceof AbstractProject<?,?>) {
AbstractProject job_ap = (AbstractProject<?, ?>) job; if (job instanceof ParameterizedJobMixIn.ParameterizedJob) {
scheduled = job_ap.scheduleBuild(job_ap.getQuietPeriod(), cause, actions); ParameterizedJobMixIn.ParameterizedJob abstractProject = (ParameterizedJobMixIn.ParameterizedJob)job;
} if (abstractProject.getQuietPeriod() > projectbuildDelay) {
else { projectbuildDelay = abstractProject.getQuietPeriod();
scheduled = scheduledJob.scheduleBuild(cause);
}
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());
}
if(addCiMessage) {
req.createCommitStatus(getDescriptor().getGitlab().instance(), "pending", Jenkins.getInstance().getRootUrl() + job.getUrl());
}
} }
}
private GitLabMergeCause createGitLabMergeCause(GitLabMergeRequest req) { if(addCiMessage) {
GitLabMergeCause cause; req.createCommitStatus(getDescriptor().getGitlab().instance(), "pending", Jenkins.getInstance().getRootUrl() + job.getUrl());
try { }
cause = new GitLabMergeCause(req, getLogFile());
} catch (IOException ex) {
cause = new GitLabMergeCause(req);
}
return cause;
}
private Action[] createActions(GitLabMergeRequest req) { scheduledJob.scheduleBuild2(projectbuildDelay, action, new CauseAction(cause));
List<Action> actions = new ArrayList<Action>(); } else {
LOGGER.log(Level.INFO, "trigger on merge request not set");
}
}
Map<String, ParameterValue> values = getDefaultParameters(); private GitLabMergeCause createGitLabMergeCause(GitLabMergeRequest req) {
values.put("gitlabSourceBranch", new StringParameterValue("gitlabSourceBranch", getSourceBranch(req))); GitLabMergeCause cause;
values.put("gitlabTargetBranch", new StringParameterValue("gitlabTargetBranch", req.getObjectAttribute().getTargetBranch())); try {
values.put("gitlabActionType", new StringParameterValue("gitlabActionType", "MERGE")); cause = new GitLabMergeCause(req, getLogFile());
if (req.getObjectAttribute().getAuthor() != null) { } catch (IOException ex) {
values.put("gitlabUserName", new StringParameterValue("gitlabUserName", req.getObjectAttribute().getAuthor().getName())); cause = new GitLabMergeCause(req);
values.put("gitlabUserEmail", new StringParameterValue("gitlabUserEmail", req.getObjectAttribute().getAuthor().getEmail()));
}
values.put("gitlabMergeRequestTitle", new StringParameterValue("gitlabMergeRequestTitle", req.getObjectAttribute().getTitle()));
values.put("gitlabMergeRequestId", new StringParameterValue("gitlabMergeRequestId", req.getObjectAttribute().getIid().toString()));
if (req.getObjectAttribute().getAssignee() != null) {
values.put("gitlabMergeRequestAssignee", new StringParameterValue("gitlabMergeRequestAssignee", req.getObjectAttribute().getAssignee().getName()));
}
LOGGER.log(Level.INFO, "Trying to get name and URL for job: {0}", job.getFullName());
String sourceRepoName = getDesc().getSourceRepoNameDefault(job);
String sourceRepoURL = getDesc().getSourceRepoURLDefault(job).toString();
if (!getDescriptor().getGitlabHostUrl().isEmpty()) {
// Get source repository if communication to Gitlab is possible
try {
sourceRepoName = req.getSourceProject(getDesc().getGitlab()).getPathWithNamespace();
sourceRepoURL = req.getSourceProject(getDesc().getGitlab()).getSshUrl();
} catch (IOException ex) {
LOGGER.log(Level.WARNING, "Could not fetch source project''s data from Gitlab. '('{0}':' {1}')'", new String[]{ex.toString(), ex.getMessage()});
}
}
values.put("gitlabSourceRepoName", new StringParameterValue("gitlabSourceRepoName", sourceRepoName));
values.put("gitlabSourceRepoURL", new StringParameterValue("gitlabSourceRepoURL", sourceRepoURL));
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 cause;
}
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()));
values.put("gitlabActionType", new StringParameterValue("gitlabActionType", "MERGE"));
if (req.getObjectAttribute().getAuthor() != null) {
values.put("gitlabUserName", new StringParameterValue("gitlabUserName", req.getObjectAttribute().getAuthor().getName()));
values.put("gitlabUserEmail", new StringParameterValue("gitlabUserEmail", req.getObjectAttribute().getAuthor().getEmail()));
}
values.put("gitlabMergeRequestTitle", new StringParameterValue("gitlabMergeRequestTitle", req.getObjectAttribute().getTitle()));
values.put("gitlabMergeRequestId", new StringParameterValue("gitlabMergeRequestId", req.getObjectAttribute().getIid().toString()));
if (req.getObjectAttribute().getAssignee() != null) {
values.put("gitlabMergeRequestAssignee", new StringParameterValue("gitlabMergeRequestAssignee", req.getObjectAttribute().getAssignee().getName()));
}
LOGGER.log(Level.INFO, "Trying to get name and URL for job: {0}", job.getFullName());
String sourceRepoName = getDesc().getSourceRepoNameDefault(job);
String sourceRepoURL = getDesc().getSourceRepoURLDefault(job).toString();
if (!getDescriptor().getGitlabHostUrl().isEmpty()) {
// Get source repository if communication to Gitlab is possible
try {
sourceRepoName = req.getSourceProject(getDesc().getGitlab()).getPathWithNamespace();
sourceRepoURL = req.getSourceProject(getDesc().getGitlab()).getSshUrl();
} catch (IOException ex) {
LOGGER.log(Level.WARNING, "Could not fetch source project''s data from Gitlab. '('{0}':' {1}')'", new String[]{ex.toString(), ex.getMessage()});
}
}
values.put("gitlabSourceRepoName", new StringParameterValue("gitlabSourceRepoName", sourceRepoName));
values.put("gitlabSourceRepoURL", new StringParameterValue("gitlabSourceRepoURL", sourceRepoURL));
List<ParameterValue> listValues = new ArrayList<ParameterValue>(values.values());
return new ParametersAction(listValues);
} }
private Map<String, ParameterValue> getDefaultParameters() { private Map<String, ParameterValue> getDefaultParameters() {