Merge pull request #7 from DABSquared/selective_trigger
Selective trigger
This commit is contained in:
commit
cdfc21d2db
|
@ -42,111 +42,125 @@ import com.dabsquared.gitlabjenkins.GitLabPushRequest.Commit;
|
||||||
* @author Daniel Brooks
|
* @author Daniel Brooks
|
||||||
*/
|
*/
|
||||||
public class GitLabPushTrigger extends Trigger<AbstractProject<?, ?>> {
|
public class GitLabPushTrigger extends Trigger<AbstractProject<?, ?>> {
|
||||||
|
private boolean triggerOnPush = true;
|
||||||
|
private boolean triggerOnMergeRequest = true;
|
||||||
|
|
||||||
@DataBoundConstructor
|
@DataBoundConstructor
|
||||||
public GitLabPushTrigger() {
|
public GitLabPushTrigger(boolean triggerOnPush, boolean triggerOnMergeRequest) {
|
||||||
|
this.triggerOnPush = triggerOnPush;
|
||||||
|
this.triggerOnMergeRequest = triggerOnMergeRequest;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getTriggerOnPush() {
|
||||||
|
return triggerOnPush;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getTriggerOnMergeRequest() {
|
||||||
|
return triggerOnMergeRequest;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onPost(final GitLabPushRequest req) {
|
public void onPost(final GitLabPushRequest req) {
|
||||||
getDescriptor().queue.execute(new Runnable() {
|
if (triggerOnPush) {
|
||||||
|
getDescriptor().queue.execute(new Runnable() {
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
LOGGER.log(Level.INFO, "{0} triggered.", job.getName());
|
LOGGER.log(Level.INFO, "{0} triggered.", job.getName());
|
||||||
String name = " #" + job.getNextBuildNumber();
|
String name = " #" + job.getNextBuildNumber();
|
||||||
GitLabPushCause cause = createGitLabPushCause(req);
|
GitLabPushCause cause = createGitLabPushCause(req);
|
||||||
Action[] actions = createActions(req);
|
Action[] actions = createActions(req);
|
||||||
|
|
||||||
if (job.scheduleBuild(job.getQuietPeriod(), cause, actions)) {
|
if (job.scheduleBuild(job.getQuietPeriod(), cause, actions)) {
|
||||||
LOGGER.log(Level.INFO, "GitLab Push Request detected in {0}. Triggering {1}", new String[]{job.getName(), name});
|
LOGGER.log(Level.INFO, "GitLab Push Request detected in {0}. Triggering {1}", new String[]{job.getName(), name});
|
||||||
} else {
|
} else {
|
||||||
LOGGER.log(Level.INFO, "GitLab Push Request detected in {0}. Job is already in the queue.", job.getName());
|
LOGGER.log(Level.INFO, "GitLab Push Request detected in {0}. Job is already in the queue.", job.getName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private GitLabPushCause createGitLabPushCause(GitLabPushRequest req) {
|
private GitLabPushCause createGitLabPushCause(GitLabPushRequest req) {
|
||||||
GitLabPushCause cause;
|
GitLabPushCause cause;
|
||||||
String triggeredByUser = req.getCommits().get(0).getAuthor().getName();
|
String triggeredByUser = req.getCommits().get(0).getAuthor().getName();
|
||||||
try {
|
try {
|
||||||
cause = new GitLabPushCause(triggeredByUser, getLogFile());
|
cause = new GitLabPushCause(triggeredByUser, getLogFile());
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
cause = new GitLabPushCause(triggeredByUser);
|
cause = new GitLabPushCause(triggeredByUser);
|
||||||
|
}
|
||||||
|
return cause;
|
||||||
}
|
}
|
||||||
return cause;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Action[] createActions(GitLabPushRequest req) {
|
private Action[] createActions(GitLabPushRequest req) {
|
||||||
ArrayList<Action> actions = new ArrayList<Action>();
|
ArrayList<Action> actions = new ArrayList<Action>();
|
||||||
|
|
||||||
String branch = req.getRef().replaceAll("refs/heads/", "");
|
String branch = req.getRef().replaceAll("refs/heads/", "");
|
||||||
|
|
||||||
LOGGER.log(Level.INFO, "GitLab Push Request from branch {0}.", branch);
|
LOGGER.log(Level.INFO, "GitLab Push Request from branch {0}.", branch);
|
||||||
|
|
||||||
Map<String, ParameterValue> values = new HashMap<String, ParameterValue>();
|
Map<String, ParameterValue> values = new HashMap<String, ParameterValue>();
|
||||||
values.put("gitlabSourceBranch", new StringParameterValue("gitlabSourceBranch", branch));
|
values.put("gitlabSourceBranch", new StringParameterValue("gitlabSourceBranch", branch));
|
||||||
values.put("gitlabTargetBranch", new StringParameterValue("gitlabTargetBranch", branch));
|
values.put("gitlabTargetBranch", new StringParameterValue("gitlabTargetBranch", branch));
|
||||||
values.put("gitlabBranch", new StringParameterValue("gitlabBranch", branch));
|
values.put("gitlabBranch", new StringParameterValue("gitlabBranch", branch));
|
||||||
|
|
||||||
List<ParameterValue> listValues = new ArrayList<ParameterValue>(values.values());
|
List<ParameterValue> listValues = new ArrayList<ParameterValue>(values.values());
|
||||||
|
|
||||||
ParametersAction parametersAction = new ParametersAction(listValues);
|
ParametersAction parametersAction = new ParametersAction(listValues);
|
||||||
actions.add(parametersAction);
|
actions.add(parametersAction);
|
||||||
|
|
||||||
RevisionParameterAction revision = new RevisionParameterAction(req.getLastCommit().getId());
|
RevisionParameterAction revision = new RevisionParameterAction(req.getLastCommit().getId());
|
||||||
actions.add(revision);
|
actions.add(revision);
|
||||||
|
|
||||||
Action[] actionsArray = actions.toArray(new Action[0]);
|
Action[] actionsArray = actions.toArray(new Action[0]);
|
||||||
|
|
||||||
return actionsArray;
|
return actionsArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onPost(final GitLabMergeRequest req) {
|
public void onPost(final GitLabMergeRequest req) {
|
||||||
getDescriptor().queue.execute(new Runnable() {
|
if (triggerOnMergeRequest) {
|
||||||
public void run() {
|
getDescriptor().queue.execute(new Runnable() {
|
||||||
LOGGER.log(Level.INFO, "{0} triggered.", job.getName());
|
public void run() {
|
||||||
String name = " #" + job.getNextBuildNumber();
|
LOGGER.log(Level.INFO, "{0} triggered.", job.getName());
|
||||||
GitLabMergeCause cause = createGitLabMergeCause(req);
|
String name = " #" + job.getNextBuildNumber();
|
||||||
Action[] actions = createActions(req);
|
GitLabMergeCause cause = createGitLabMergeCause(req);
|
||||||
|
Action[] actions = createActions(req);
|
||||||
|
|
||||||
if (job.scheduleBuild(job.getQuietPeriod(), cause, actions)) {
|
if (job.scheduleBuild(job.getQuietPeriod(), cause, actions)) {
|
||||||
LOGGER.log(Level.INFO, "GitLab Merge Request detected in {0}. Triggering {1}", new String[]{job.getName(), name});
|
LOGGER.log(Level.INFO, "GitLab Merge Request detected in {0}. Triggering {1}", new String[]{job.getName(), name});
|
||||||
} else {
|
} else {
|
||||||
LOGGER.log(Level.INFO, "GitLab Merge Request detected in {0}. Job is already in the queue.", job.getName());
|
LOGGER.log(Level.INFO, "GitLab Merge Request detected in {0}. Job is already in the queue.", job.getName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private GitLabMergeCause createGitLabMergeCause(GitLabMergeRequest req) {
|
private GitLabMergeCause createGitLabMergeCause(GitLabMergeRequest req) {
|
||||||
GitLabMergeCause cause;
|
GitLabMergeCause cause;
|
||||||
try {
|
try {
|
||||||
cause = new GitLabMergeCause(req, getLogFile());
|
cause = new GitLabMergeCause(req, getLogFile());
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
cause = new GitLabMergeCause(req);
|
cause = new GitLabMergeCause(req);
|
||||||
|
}
|
||||||
|
return cause;
|
||||||
}
|
}
|
||||||
return cause;
|
|
||||||
}
|
|
||||||
|
|
||||||
private Action[] createActions(GitLabMergeRequest req) {
|
private Action[] createActions(GitLabMergeRequest req) {
|
||||||
List<Action> actions = new ArrayList<Action>();
|
List<Action> actions = new ArrayList<Action>();
|
||||||
|
|
||||||
Map<String, ParameterValue> values = new HashMap<String, ParameterValue>();
|
Map<String, ParameterValue> values = new HashMap<String, ParameterValue>();
|
||||||
values.put("gitlabSourceBranch", new StringParameterValue("gitlabSourceBranch", String.valueOf(req.getObjectAttribute().getSourceBranch())));
|
values.put("gitlabSourceBranch", new StringParameterValue("gitlabSourceBranch", String.valueOf(req.getObjectAttribute().getSourceBranch())));
|
||||||
values.put("gitlabTargetBranch", new StringParameterValue("gitlabTargetBranch", String.valueOf(req.getObjectAttribute().getTargetBranch())));
|
values.put("gitlabTargetBranch", new StringParameterValue("gitlabTargetBranch", String.valueOf(req.getObjectAttribute().getTargetBranch())));
|
||||||
|
|
||||||
List<ParameterValue> listValues = new ArrayList<ParameterValue>(values.values());
|
List<ParameterValue> listValues = new ArrayList<ParameterValue>(values.values());
|
||||||
|
|
||||||
ParametersAction parametersAction = new ParametersAction(listValues);
|
ParametersAction parametersAction = new ParametersAction(listValues);
|
||||||
actions.add(parametersAction);
|
actions.add(parametersAction);
|
||||||
|
|
||||||
Action[] actionsArray = actions.toArray(new Action[0]);
|
Action[] actionsArray = actions.toArray(new Action[0]);
|
||||||
|
|
||||||
return actionsArray;
|
return actionsArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
|
||||||
|
<f:entry title="Build on Push Events" field="triggerOnPush">
|
||||||
|
<f:checkbox default="true" />
|
||||||
|
</f:entry>
|
||||||
|
<f:entry title="Build on Merge Request Events" field="triggerOnMergeRequest">
|
||||||
|
<f:checkbox default="true" />
|
||||||
|
</f:entry>
|
||||||
|
</j:jelly>
|
Loading…
Reference in New Issue