Skip to content

Commit

Permalink
Fix unstable build notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
butchyyyy committed Feb 5, 2019
1 parent dd06663 commit 8a616e8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
17 changes: 12 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
<jenkins.version>1.653</jenkins.version>
<java.level>7</java.level>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<workflow.version>1.11</workflow.version>
<structs.version>1.2</structs.version>
<jenkins.plugin.workflow.version>1.11</jenkins.plugin.workflow.version>
<jenkins.plugin.structs.version>1.2</jenkins.plugin.structs.version>
<jenkins.plugin.junit.version>1.19</jenkins.plugin.junit.version>
<junit.version>4.12</junit.version>
<mockito.version>1.10.19</mockito.version>
<powermock.version>1.7.4</powermock.version>
Expand All @@ -39,19 +40,25 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>${workflow.version}</version>
<version>${jenkins.plugin.workflow.version}</version>
</dependency>
<!-- For @Symbol named pipeline steps support -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>${structs.version}</version>
<version>${jenkins.plugin.structs.version}</version>
</dependency>
<!-- Support for reading test results -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>junit</artifactId>
<version>${jenkins.plugin.junit.version}</version>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-aggregator</artifactId>
<version>${workflow.version}</version>
<version>${jenkins.plugin.workflow.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/jenkins/plugins/zulip/ZulipNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import hudson.scm.ChangeLogSet;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.tasks.test.AbstractTestResultAction;
import jenkins.tasks.SimpleBuildStep;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.kohsuke.stapler.DataBoundConstructor;
Expand Down Expand Up @@ -100,10 +101,9 @@ private boolean publish(Run<?, ?> build) {
}
else if (result == Result.UNSTABLE) {
message += " :warning:";

/*int failCount = build.getTestResultAction().getFailCount();
message += " (" + failCount + " broken tests)";*/
AbstractTestResultAction testResultAction = build.getAction(AbstractTestResultAction.class);
String failCount = testResultAction != null ? Integer.toString(testResultAction.getFailCount()) : "?";
message += " (" + failCount + " broken tests)";
} else {
message += " :x:";
}
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/jenkins/plugins/zulip/ZulipNotifierTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import hudson.model.Run;
import hudson.model.User;
import hudson.scm.ChangeLogSet;
import hudson.tasks.test.AbstractTestResultAction;
import jenkins.model.Jenkins;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -37,6 +38,9 @@
AbstractBuild.class, Job.class})
public class ZulipNotifierTest {

private static final int TOTAL_TEST_COUNT = 100;
private static final int FAILED_TEST_COUNT = 50;

@Mock
private Jenkins jenkins;

Expand Down Expand Up @@ -122,6 +126,16 @@ public void testFailedBuild() throws Exception {
assertEquals("Message should be failed build", "**Project: **TestJob : **Build: **#1: **FAILURE** :x:", messageCaptor.getValue());
}

@Test
public void testUnstableBuild() throws Exception {
ZulipNotifier notifier = new ZulipNotifier();
when(build.getResult()).thenReturn(Result.UNSTABLE);
when(build.getAction(AbstractTestResultAction.class)).thenReturn(new FakeTestResultAction());
notifier.perform(build, null, null);
verify(zulip).sendStreamMessage(streamCaptor.capture(), topicCaptor.capture(), messageCaptor.capture());
assertEquals("Message should be unstable build", "**Project: **TestJob : **Build: **#1: **UNSTABLE** :warning: (50 broken tests)", messageCaptor.getValue());
}

@Test
public void testShouldUseProjectConfig() throws Exception {
ZulipNotifier notifier = new ZulipNotifier();
Expand Down Expand Up @@ -227,4 +241,21 @@ private FakeChangeLogSCM.EntryImpl createChange(String author, String msg) {
return new FakeChangeLogSCM.EntryImpl().withAuthor(author).withMsg(msg);
}

private class FakeTestResultAction extends AbstractTestResultAction {
@Override
public int getFailCount() {
return FAILED_TEST_COUNT;
}

@Override
public int getTotalCount() {
return TOTAL_TEST_COUNT;
}

@Override
public Object getResult() {
return null;
}
}

}

0 comments on commit 8a616e8

Please sign in to comment.