Merge pull request #102 from TomAndrews/configurable-rebuild

Configurable rebuild options
This commit is contained in:
bassrock 2015-08-26 10:31:51 -07:00
commit 019d82087a
4 changed files with 31 additions and 10 deletions

View File

@ -95,7 +95,8 @@ public class GitLabMergeRequest extends GitLabRequest {
private Branch target;
private LastCommit lastCommit;
private String action;
public ObjectAttributes() {
}
@ -240,6 +241,14 @@ public class GitLabMergeRequest extends GitLabRequest {
public void setLastCommit(LastCommit lastCommit) {
this.lastCommit = lastCommit;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
}
public static class Branch{

View File

@ -23,6 +23,8 @@ import hudson.triggers.TriggerDescriptor;
import hudson.util.FormValidation;
import hudson.util.SequentialExecutionQueue;
import hudson.util.XStream2;
import hudson.util.ListBoxModel;
import hudson.util.ListBoxModel.Option;
import java.io.File;
import java.io.IOException;
@ -76,7 +78,7 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
private static final Logger LOGGER = Logger.getLogger(GitLabPushTrigger.class.getName());
private boolean triggerOnPush = true;
private boolean triggerOnMergeRequest = true;
private boolean triggerOpenMergeRequestOnPush = true;
private final String triggerOpenMergeRequestOnPush;
private boolean ciSkip = true;
private boolean setBuildDescription = true;
private boolean addNoteOnMergeRequest = true;
@ -86,7 +88,7 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
private final String excludeBranchesSpec;
@DataBoundConstructor
public GitLabPushTrigger(boolean triggerOnPush, boolean triggerOnMergeRequest, boolean triggerOpenMergeRequestOnPush, boolean ciSkip, boolean setBuildDescription, boolean addNoteOnMergeRequest, boolean addVoteOnMergeRequest, boolean allowAllBranches,
public GitLabPushTrigger(boolean triggerOnPush, boolean triggerOnMergeRequest, String triggerOpenMergeRequestOnPush, boolean ciSkip, boolean setBuildDescription, boolean addNoteOnMergeRequest, boolean addVoteOnMergeRequest, boolean allowAllBranches,
String includeBranchesSpec, String excludeBranchesSpec) {
this.triggerOnPush = triggerOnPush;
this.triggerOnMergeRequest = triggerOnMergeRequest;
@ -108,7 +110,7 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
return triggerOnMergeRequest;
}
public boolean getTriggerOpenMergeRequestOnPush() {
public String getTriggerOpenMergeRequestOnPush() {
return triggerOpenMergeRequestOnPush;
}
@ -313,7 +315,7 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
values.put("gitlabActionType", new StringParameterValue("gitlabActionType", "MERGE"));
LOGGER.log(Level.INFO, "Trying to get name and URL for job: {0} using project {1}", new String[]{job.getName(), getDesc().project.getName()});
LOGGER.log(Level.INFO, "Trying to get name and URL for job: {0}", job.getName());
String sourceRepoName = getDesc().getSourceRepoNameDefault(job);
String sourceRepoURL = getDesc().getSourceRepoURLDefault(job).toString();
@ -556,6 +558,12 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
gitlab = new GitLab();
return super.configure(req, formData);
}
public ListBoxModel doFillTriggerOpenMergeRequestOnPushItems(@QueryParameter String triggerOpenMergeRequestOnPush) {
return new ListBoxModel(new Option("Never", "never", triggerOpenMergeRequestOnPush.matches("never") ),
new Option("On push to source branch", "source", triggerOpenMergeRequestOnPush.matches("source") ),
new Option("On push to source or target branch", "both", triggerOpenMergeRequestOnPush.matches("both") ));
}
private List<String> getProjectBranches(final Job<?, ?> job) throws IOException, IllegalStateException {
final URIish sourceRepository = getSourceRepoURLDefault(job);

View File

@ -399,7 +399,7 @@ public class GitLabWebHook implements UnprotectedRootAction {
trigger.onPost(request);
if (trigger.getTriggerOpenMergeRequestOnPush()) {
if (!trigger.getTriggerOpenMergeRequestOnPush().equals("never")) {
// Fetch and build open merge requests with the same source branch
buildOpenMergeRequests(trigger, request.getProject_id(), request.getRef());
}
@ -416,7 +416,8 @@ public class GitLabWebHook implements UnprotectedRootAction {
List<GitlabMergeRequest> mergeRequests = api.instance().retrieve().getAll(tailUrl, GitlabMergeRequest[].class);
for (org.gitlab.api.models.GitlabMergeRequest mr : mergeRequests) {
if (projectRef.endsWith(mr.getSourceBranch()) || projectRef.endsWith(mr.getTargetBranch())) {
if (projectRef.endsWith(mr.getSourceBranch()) ||
(trigger.getTriggerOpenMergeRequestOnPush().equals("both") && projectRef.endsWith(mr.getTargetBranch()))) {
LOGGER.log(Level.FINE,
"Generating new merge trigger from "
+ mr.toString() + "\n source: "
@ -450,7 +451,6 @@ public class GitLabWebHook implements UnprotectedRootAction {
} finally {
SecurityContextHolder.getContext().setAuthentication(old);
}
return;
}
}
} catch (Exception e) {
@ -470,6 +470,10 @@ public class GitLabWebHook implements UnprotectedRootAction {
LOGGER.log(Level.INFO, "Accepted Merge Request, no build started");
return;
}
if(request.getObjectAttribute().getAction().equals("update")) {
LOGGER.log(Level.INFO, "Existing Merge Request, build will be trigged by buildOpenMergeRequests instead");
return;
}
if(request.getObjectAttribute().getLastCommit()!=null) {
Run mergeBuild = getBuildBySHA1(project, request.getObjectAttribute().getLastCommit().getId(), true);
if(mergeBuild!=null){

View File

@ -6,8 +6,8 @@
<f:entry title="Build on Push Events" field="triggerOnPush">
<f:checkbox default="true" />
</f:entry>
<f:entry title="Rebuild open Merge Requests on Push Events" field="triggerOpenMergeRequestOnPush">
<f:checkbox default="true" />
<f:entry title="Rebuild open Merge Requests" field="triggerOpenMergeRequestOnPush">
<f:select/>
</f:entry>
<f:entry title="Enable [ci-skip]" field="ciSkip">
<f:checkbox default="true" />