Add option to mark unstable builds as success in GitLab (Fixes #190)

This commit is contained in:
Robin Müller 2016-09-04 17:49:41 +02:00
parent 9e67af1af2
commit f9577ca03a
4 changed files with 48 additions and 11 deletions

View File

@ -129,7 +129,7 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> {
GitLabPushTrigger trigger = project.getTrigger(GitLabPushTrigger.class);
if (trigger != null) {
if (trigger.addCiMessage) {
project.getPublishersList().add(new GitLabCommitStatusPublisher("jenkins"));
project.getPublishersList().add(new GitLabCommitStatusPublisher("jenkins", false));
}
project.addProperty(new GitLabConnectionProperty(defaultConnectionName));
project.save();

View File

@ -25,10 +25,12 @@ import java.io.IOException;
public class GitLabCommitStatusPublisher extends Notifier {
private String name;
private boolean markUnstableAsSuccess;
@DataBoundConstructor
public GitLabCommitStatusPublisher(String name) {
public GitLabCommitStatusPublisher(String name, boolean markUnstableAsSuccess) {
this.name = name;
this.markUnstableAsSuccess = markUnstableAsSuccess;
}
public BuildStepMonitor getRequiredMonitorService() {
@ -44,7 +46,7 @@ public class GitLabCommitStatusPublisher extends Notifier {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
Result buildResult = build.getResult();
if (buildResult == Result.SUCCESS) {
if (buildResult == Result.SUCCESS || (buildResult == Result.UNSTABLE && markUnstableAsSuccess)) {
CommitStatusUpdater.updateCommitStatus(build, listener, BuildState.success, name);
} else if (buildResult == Result.ABORTED) {
CommitStatusUpdater.updateCommitStatus(build, listener, BuildState.canceled, name);
@ -58,6 +60,10 @@ public class GitLabCommitStatusPublisher extends Notifier {
return name;
}
public boolean isMarkUnstableAsSuccess() {
return markUnstableAsSuccess;
}
protected GitLabCommitStatusPublisher readResolve() {
if (name == null) {
name = "jenkins";

View File

@ -4,5 +4,8 @@
<f:entry title="${%Build name}" field="name" help="/plugin/gitlab-plugin/help/help-buildName.html">
<f:textbox default="jenkins"/>
</f:entry>
<f:entry title="${%Mark unstable builds as success}" field="markUnstableAsSuccess">
<f:checkbox default="false"/>
</f:entry>
</f:advanced>
</j:jelly>

View File

@ -107,7 +107,7 @@ public class GitLabCommitStatusPublisherTest {
};
AbstractBuild build = mockBuild("123abc", "/build/123", GIT_LAB_CONNECTION, null, "test/project.git");
GitLabCommitStatusPublisher publisher = new GitLabCommitStatusPublisher("jenkins");
GitLabCommitStatusPublisher publisher = new GitLabCommitStatusPublisher("jenkins", false);
publisher.prebuild(build, listener);
mockServerClient.verify(requests);
@ -122,7 +122,7 @@ public class GitLabCommitStatusPublisherTest {
};
AbstractBuild build = mockBuild("123abc", "/build/123", GIT_LAB_CONNECTION, null, "test/project.test.git");
GitLabCommitStatusPublisher publisher = new GitLabCommitStatusPublisher("jenkins");
GitLabCommitStatusPublisher publisher = new GitLabCommitStatusPublisher("jenkins", false);
publisher.prebuild(build, listener);
mockServerClient.verify(requests);
@ -136,7 +136,7 @@ public class GitLabCommitStatusPublisherTest {
};
AbstractBuild build = mockBuild("123abc", "/build/123", GIT_LAB_CONNECTION, Result.ABORTED, "test/project.git");
GitLabCommitStatusPublisher publisher = new GitLabCommitStatusPublisher("jenkins");
GitLabCommitStatusPublisher publisher = new GitLabCommitStatusPublisher("jenkins", false);
publisher.perform(build, null, listener);
mockServerClient.verify(requests);
@ -150,7 +150,7 @@ public class GitLabCommitStatusPublisherTest {
};
AbstractBuild build = mockBuild("123abc", "/build/123", GIT_LAB_CONNECTION, Result.SUCCESS, "test/project.git");
GitLabCommitStatusPublisher publisher = new GitLabCommitStatusPublisher("jenkins");
GitLabCommitStatusPublisher publisher = new GitLabCommitStatusPublisher("jenkins", false);
publisher.perform(build, null, listener);
mockServerClient.verify(requests);
@ -164,7 +164,35 @@ public class GitLabCommitStatusPublisherTest {
};
AbstractBuild build = mockBuild("123abc", "/build/123", GIT_LAB_CONNECTION, Result.FAILURE, "test/project.git");
GitLabCommitStatusPublisher publisher = new GitLabCommitStatusPublisher("jenkins");
GitLabCommitStatusPublisher publisher = new GitLabCommitStatusPublisher("jenkins", false);
publisher.perform(build, null, listener);
mockServerClient.verify(requests);
}
@Test
public void unstable() throws IOException, InterruptedException {
HttpRequest[] requests = new HttpRequest[] {
prepareExistsCommitWithSuccessResponse("test/project", "123abc"),
prepareUpdateCommitStatusWithSuccessResponse("test/project", "123abc", jenkins.getInstance().getRootUrl() + "/build/123", BuildState.failed)
};
AbstractBuild build = mockBuild("123abc", "/build/123", GIT_LAB_CONNECTION, Result.UNSTABLE, "test/project.git");
GitLabCommitStatusPublisher publisher = new GitLabCommitStatusPublisher("jenkins", false);
publisher.perform(build, null, listener);
mockServerClient.verify(requests);
}
@Test
public void unstableAsSuccess() throws IOException, InterruptedException {
HttpRequest[] requests = new HttpRequest[] {
prepareExistsCommitWithSuccessResponse("test/project", "123abc"),
prepareUpdateCommitStatusWithSuccessResponse("test/project", "123abc", jenkins.getInstance().getRootUrl() + "/build/123", BuildState.success)
};
AbstractBuild build = mockBuild("123abc", "/build/123", GIT_LAB_CONNECTION, Result.UNSTABLE, "test/project.git");
GitLabCommitStatusPublisher publisher = new GitLabCommitStatusPublisher("jenkins", true);
publisher.perform(build, null, listener);
mockServerClient.verify(requests);
@ -180,7 +208,7 @@ public class GitLabCommitStatusPublisherTest {
};
AbstractBuild build = mockBuild("123abc", "/build/123", GIT_LAB_CONNECTION, null, "test/project-1.git", "test/project-2.git");
GitLabCommitStatusPublisher publisher = new GitLabCommitStatusPublisher("jenkins");
GitLabCommitStatusPublisher publisher = new GitLabCommitStatusPublisher("jenkins", false);
publisher.prebuild(build, listener);
mockServerClient.verify(requests);
@ -191,7 +219,7 @@ public class GitLabCommitStatusPublisherTest {
HttpRequest updateCommitStatus = prepareUpdateCommitStatusWithSuccessResponse("test/project", "123abc", jenkins.getInstance().getRootUrl() + "/build/123", BuildState.running);
AbstractBuild build = mockBuild("123abc", "/build/123", GIT_LAB_CONNECTION, null, "test/project.git");
GitLabCommitStatusPublisher publisher = new GitLabCommitStatusPublisher("jenkins");
GitLabCommitStatusPublisher publisher = new GitLabCommitStatusPublisher("jenkins", false);
publisher.prebuild(build, listener);
mockServerClient.verify(updateCommitStatus, VerificationTimes.exactly(0));
@ -207,7 +235,7 @@ public class GitLabCommitStatusPublisherTest {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
when(buildListener.getLogger()).thenReturn(new PrintStream(outputStream));
GitLabCommitStatusPublisher publisher = new GitLabCommitStatusPublisher("jenkins");
GitLabCommitStatusPublisher publisher = new GitLabCommitStatusPublisher("jenkins", false);
publisher.prebuild(build, buildListener);
assertThat(outputStream.toString(), CoreMatchers.containsString("Failed to update Gitlab commit status for project 'test/project': HTTP 403 Forbidden"));