Add possibility to add build status only for failed builds to MR (Fixes #345)
This commit is contained in:
parent
342a6bbcfd
commit
d566923021
|
@ -25,6 +25,7 @@ import java.util.logging.Logger;
|
|||
*/
|
||||
public class GitLabMessagePublisher extends MergeRequestNotifier {
|
||||
private static final Logger LOGGER = Logger.getLogger(GitLabMessagePublisher.class.getName());
|
||||
private boolean onlyForFailure = false;
|
||||
private boolean replaceSuccessNote = false;
|
||||
private boolean replaceFailureNote = false;
|
||||
private boolean replaceAbortNote = false;
|
||||
|
@ -33,8 +34,9 @@ public class GitLabMessagePublisher extends MergeRequestNotifier {
|
|||
private String abortNoteText;
|
||||
|
||||
@DataBoundConstructor
|
||||
public GitLabMessagePublisher(boolean replaceSuccessNote, boolean replaceFailureNote, boolean replaceAbortNote,
|
||||
public GitLabMessagePublisher(boolean onlyForFailure, boolean replaceSuccessNote, boolean replaceFailureNote, boolean replaceAbortNote,
|
||||
String successNoteText, String failureNoteText, String abortNoteText) {
|
||||
this.onlyForFailure = onlyForFailure;
|
||||
this.replaceSuccessNote = replaceSuccessNote;
|
||||
this.replaceFailureNote = replaceFailureNote;
|
||||
this.replaceAbortNote = replaceAbortNote;
|
||||
|
@ -45,15 +47,19 @@ public class GitLabMessagePublisher extends MergeRequestNotifier {
|
|||
|
||||
public GitLabMessagePublisher() { }
|
||||
|
||||
public boolean getReplaceSuccessNote() {
|
||||
public boolean isOnlyForFailure() {
|
||||
return onlyForFailure;
|
||||
}
|
||||
|
||||
public boolean isReplaceSuccessNote() {
|
||||
return replaceSuccessNote;
|
||||
}
|
||||
|
||||
public boolean getReplaceFailureNote() {
|
||||
public boolean isReplaceFailureNote() {
|
||||
return replaceFailureNote;
|
||||
}
|
||||
|
||||
public boolean getReplaceAbortNote() {
|
||||
public boolean isReplaceAbortNote() {
|
||||
return replaceAbortNote;
|
||||
}
|
||||
|
||||
|
@ -91,7 +97,9 @@ public class GitLabMessagePublisher extends MergeRequestNotifier {
|
|||
@Override
|
||||
protected void perform(Run<?, ?> build, TaskListener listener, GitLabApi client, Integer projectId, Integer mergeRequestId) {
|
||||
try {
|
||||
client.createMergeRequestNote(projectId, mergeRequestId, getNote(build, listener));
|
||||
if (!onlyForFailure || build.getResult() == Result.FAILURE) {
|
||||
client.createMergeRequestNote(projectId, mergeRequestId, getNote(build, listener));
|
||||
}
|
||||
} catch (WebApplicationException | ProcessingException e) {
|
||||
listener.getLogger().printf("Failed to add comment on Merge Request for project '%s': %s%n", projectId, e.getMessage());
|
||||
LOGGER.log(Level.SEVERE, String.format("Failed to add comment on Merge Request for project '%s'", projectId), e);
|
||||
|
@ -139,11 +147,11 @@ public class GitLabMessagePublisher extends MergeRequestNotifier {
|
|||
|
||||
private String getNote(Run<?, ?> build, TaskListener listener) {
|
||||
String message;
|
||||
if (this.getReplaceSuccessNote() && build.getResult() == Result.SUCCESS) {
|
||||
if (this.replaceSuccessNote && build.getResult() == Result.SUCCESS) {
|
||||
message = replaceMacros(build, listener, this.getSuccessNoteText());
|
||||
} else if (this.getReplaceAbortNote() && build.getResult() == Result.ABORTED) {
|
||||
} else if (this.replaceAbortNote && build.getResult() == Result.ABORTED) {
|
||||
message = replaceMacros(build, listener, this.getAbortNoteText());
|
||||
} else if (this.getReplaceFailureNote() && build.getResult() == Result.FAILURE) {
|
||||
} else if (this.replaceFailureNote && build.getResult() == Result.FAILURE) {
|
||||
message = replaceMacros(build, listener, this.getFailureNoteText());
|
||||
} else {
|
||||
String icon = getResultIcon(build.getResult());
|
||||
|
|
|
@ -2,6 +2,9 @@
|
|||
<j:jelly xmlns:j="jelly:core"
|
||||
xmlns:f="/lib/form">
|
||||
<f:advanced>
|
||||
<f:entry title="${%Add message only for failed builds}" field="onlyForFailure">
|
||||
<f:checkbox default="false"/>
|
||||
</f:entry>
|
||||
<f:optionalBlock name="replaceSuccessNote" checked="${instance.replaceSuccessNote}" title="Custom message on success" inline="true">
|
||||
<f:entry>
|
||||
<f:textarea name="successNoteText" field="successNoteText"/>
|
||||
|
|
|
@ -104,7 +104,7 @@ public class GitLabMessagePublisherTest {
|
|||
prepareSendMessageWithSuccessResponse(projectId, mergeRequestId, defaultNote)
|
||||
};
|
||||
|
||||
GitLabMessagePublisher publisher = spy(new GitLabMessagePublisher(false, false, false, null, null, null));
|
||||
GitLabMessagePublisher publisher = spy(new GitLabMessagePublisher(false, false, false, false, null, null, null));
|
||||
doReturn(projectId).when(publisher).getProjectId(build);
|
||||
doReturn(mergeRequestId).when(publisher).getMergeRequestId(build);
|
||||
publisher.perform(build, null, listener);
|
||||
|
@ -126,7 +126,7 @@ public class GitLabMessagePublisherTest {
|
|||
prepareSendMessageWithSuccessResponse(projectId, mergeRequestId, defaultNote)
|
||||
};
|
||||
|
||||
GitLabMessagePublisher publisher = spy(new GitLabMessagePublisher(false, false, false, null, null, null));
|
||||
GitLabMessagePublisher publisher = spy(new GitLabMessagePublisher(false, false, false, false, null, null, null));
|
||||
doReturn(projectId).when(publisher).getProjectId(build);
|
||||
doReturn(mergeRequestId).when(publisher).getMergeRequestId(build);
|
||||
publisher.perform(build, null, listener);
|
||||
|
@ -134,6 +134,21 @@ public class GitLabMessagePublisherTest {
|
|||
mockServerClient.verify(requests);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void success_withOnlyForFailure() throws IOException, InterruptedException {
|
||||
Integer buildNumber = 1;
|
||||
Integer projectId = 3;
|
||||
Integer mergeRequestId = 1;
|
||||
AbstractBuild build = mockBuild("/build/123", GIT_LAB_CONNECTION, Result.SUCCESS, buildNumber);
|
||||
|
||||
GitLabMessagePublisher publisher = spy(new GitLabMessagePublisher(true, false, false, false, null, null, null));
|
||||
doReturn(projectId).when(publisher).getProjectId(build);
|
||||
doReturn(mergeRequestId).when(publisher).getMergeRequestId(build);
|
||||
publisher.perform(build, null, listener);
|
||||
|
||||
mockServerClient.verifyZeroInteractions();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void failed() throws IOException, InterruptedException {
|
||||
Integer buildNumber = 1;
|
||||
|
@ -148,7 +163,29 @@ public class GitLabMessagePublisherTest {
|
|||
prepareSendMessageWithSuccessResponse(projectId, mergeRequestId, defaultNote)
|
||||
};
|
||||
|
||||
GitLabMessagePublisher publisher = spy(new GitLabMessagePublisher(false, false, false, null, null, null));
|
||||
GitLabMessagePublisher publisher = spy(new GitLabMessagePublisher(false, false, false, false, null, null, null));
|
||||
doReturn(projectId).when(publisher).getProjectId(build);
|
||||
doReturn(mergeRequestId).when(publisher).getMergeRequestId(build);
|
||||
publisher.perform(build, null, listener);
|
||||
|
||||
mockServerClient.verify(requests);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void failed_withOnlyForFailed() throws IOException, InterruptedException {
|
||||
Integer buildNumber = 1;
|
||||
Integer projectId = 3;
|
||||
Integer mergeRequestId = 1;
|
||||
AbstractBuild build = mockBuild("/build/123", GIT_LAB_CONNECTION, Result.FAILURE, buildNumber);
|
||||
String buildUrl = Jenkins.getInstance().getRootUrl() + build.getUrl();
|
||||
String defaultNote = MessageFormat.format(":negative_squared_cross_mark: Jenkins Build {0}\n\nResults available at: [Jenkins [{1} #{2}]]({3})",
|
||||
Result.FAILURE, build.getParent().getDisplayName(), buildNumber, buildUrl);
|
||||
|
||||
HttpRequest[] requests = new HttpRequest[] {
|
||||
prepareSendMessageWithSuccessResponse(projectId, mergeRequestId, defaultNote)
|
||||
};
|
||||
|
||||
GitLabMessagePublisher publisher = spy(new GitLabMessagePublisher(true, false, false, false, null, null, null));
|
||||
doReturn(projectId).when(publisher).getProjectId(build);
|
||||
doReturn(mergeRequestId).when(publisher).getMergeRequestId(build);
|
||||
publisher.perform(build, null, listener);
|
||||
|
@ -168,7 +205,7 @@ public class GitLabMessagePublisherTest {
|
|||
prepareSendMessageWithSuccessResponse(projectId, mergeRequestId, defaultNote)
|
||||
};
|
||||
|
||||
GitLabMessagePublisher publisher = spy(new GitLabMessagePublisher(false, false, true, null, null, defaultNote));
|
||||
GitLabMessagePublisher publisher = spy(new GitLabMessagePublisher(false, false, false, true, null, null, defaultNote));
|
||||
doReturn(projectId).when(publisher).getProjectId(build);
|
||||
doReturn(mergeRequestId).when(publisher).getMergeRequestId(build);
|
||||
publisher.perform(build, null, listener);
|
||||
|
@ -188,7 +225,7 @@ public class GitLabMessagePublisherTest {
|
|||
prepareSendMessageWithSuccessResponse(projectId, mergeRequestId, defaultNote)
|
||||
};
|
||||
|
||||
GitLabMessagePublisher publisher = spy(new GitLabMessagePublisher(true, false, false, defaultNote, null, null));
|
||||
GitLabMessagePublisher publisher = spy(new GitLabMessagePublisher(false, true, false, false, defaultNote, null, null));
|
||||
doReturn(projectId).when(publisher).getProjectId(build);
|
||||
doReturn(mergeRequestId).when(publisher).getMergeRequestId(build);
|
||||
publisher.perform(build, null, listener);
|
||||
|
@ -208,7 +245,7 @@ public class GitLabMessagePublisherTest {
|
|||
prepareSendMessageWithSuccessResponse(projectId, mergeRequestId, defaultNote)
|
||||
};
|
||||
|
||||
GitLabMessagePublisher publisher = spy(new GitLabMessagePublisher(false, true, false, null, defaultNote, null));
|
||||
GitLabMessagePublisher publisher = spy(new GitLabMessagePublisher(false, false, true, false, null, defaultNote, null));
|
||||
doReturn(projectId).when(publisher).getProjectId(build);
|
||||
doReturn(mergeRequestId).when(publisher).getMergeRequestId(build);
|
||||
publisher.perform(build, null, listener);
|
||||
|
|
Loading…
Reference in New Issue