Skip to content

Commit 78a894b

Browse files
author
Ataxexe
committed
Merge branch 'release/issue-9'
2 parents 08eaad0 + ce8b3e0 commit 78a894b

11 files changed

Lines changed: 207 additions & 11 deletions

File tree

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.2.3
4+
5+
- Allowed different types of notifications (#9)
6+
37
## 1.2.2
48

59
- Updated code for new `boteco` versions

pom.xml

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</parent>
1212
<groupId>tools.devnull</groupId>
1313
<artifactId>build-notifications</artifactId>
14-
<version>1.2.2</version>
14+
<version>1.2.3</version>
1515
<packaging>hpi</packaging>
1616

1717
<name>Jenkins Build Notification Plugin</name>
@@ -22,6 +22,43 @@
2222
<url>http://opensource.org/licenses/MIT</url>
2323
</license>
2424
</licenses>
25+
26+
<properties>
27+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
28+
29+
<kodo.version>3.3.0</kodo.version>
30+
<mockito.version>2.7.13</mockito.version>
31+
</properties>
32+
33+
<dependencies>
34+
<dependency>
35+
<groupId>tools.devnull</groupId>
36+
<artifactId>kodo</artifactId>
37+
<version>${kodo.version}</version>
38+
<scope>test</scope>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.mockito</groupId>
43+
<artifactId>mockito-core</artifactId>
44+
<version>${mockito.version}</version>
45+
<scope>test</scope>
46+
</dependency>
47+
</dependencies>
48+
49+
<build>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.apache.maven.plugins</groupId>
53+
<artifactId>maven-compiler-plugin</artifactId>
54+
<configuration>
55+
<source>1.8</source>
56+
<target>1.8</target>
57+
</configuration>
58+
</plugin>
59+
</plugins>
60+
</build>
61+
2562
<repositories>
2663
<repository>
2764
<id>repo.jenkins-ci.org</id>

src/main/java/tools/devnull/jenkins/plugins/buildnotifications/BotecoMessage.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public void highPriority() {
7878
this.priority = "high";
7979
}
8080

81+
@Override
82+
public void normalPriority() {
83+
this.priority = "normal";
84+
}
85+
8186
@Override
8287
public void lowPriority() {
8388
this.priority = "low";
@@ -90,7 +95,7 @@ public void send() {
9095
post.setRequestHeader("Content-Type", "application/json; charset=utf-8");
9196
Map<String, String> values = new HashMap<String, String>();
9297
values.put("title", title);
93-
values.put("content", content);
98+
values.put("text", content);
9499
values.put("url", url);
95100
values.put("priority", priority);
96101
try {

src/main/java/tools/devnull/jenkins/plugins/buildnotifications/BotecoNotifier.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
8282
throws InterruptedException, IOException {
8383
BotecoDescriptor descriptor = (BotecoDescriptor) Jenkins.getInstance()
8484
.getDescriptor(BotecoNotifier.class);
85-
Message message = new BotecoMessage(eventId, descriptor.endpoint);
85+
Message message = new BotecoMessage(
86+
String.format("%s.%s",
87+
eventId,
88+
BuildStatus.of(build).name().toLowerCase().replaceAll("_", "-")),
89+
descriptor.endpoint);
8690
BuildNotifier notifier = new BuildNotifier(message, build, sendIfSuccess);
8791
notifier.sendNotification();
8892
return true;

src/main/java/tools/devnull/jenkins/plugins/buildnotifications/BuildNotifier.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public class BuildNotifier {
4545

4646
private final Message message;
4747
private final AbstractBuild build;
48+
private final BuildStatus status;
4849
private final Result result;
4950
private final String baseUrl;
5051
private final boolean sendIfSuccess;
@@ -58,6 +59,7 @@ public class BuildNotifier {
5859
public BuildNotifier(Message message, AbstractBuild build, boolean sendIfSuccess) {
5960
this.message = message;
6061
this.build = build;
62+
this.status = BuildStatus.of(build);
6163
this.result = build.getResult();
6264
this.baseUrl = JenkinsLocationConfiguration.get().getUrl();
6365
this.sendIfSuccess = sendIfSuccess;
@@ -67,7 +69,7 @@ public BuildNotifier(Message message, AbstractBuild build, boolean sendIfSuccess
6769
* Sends the notification through the given message object.
6870
*/
6971
public void sendNotification() {
70-
if (result.ordinal == 0) {
72+
if (status == BuildStatus.SUCCESSFUL) {
7173
if (sendIfSuccess) {
7274
sendMessage();
7375
}
@@ -92,12 +94,12 @@ private void setUrl() {
9294
}
9395

9496
private void setContent() {
95-
if(build.getChangeSet().getItems().length == 0){
97+
if (build.getChangeSet().getItems().length == 0) {
9698
message.setContent(result.toString());
9799
} else {
98100
StringBuilder changes = new StringBuilder();
99101

100-
for (Iterator<? extends ChangeLogSet.Entry> i = build.getChangeSet().iterator(); i.hasNext();) {
102+
for (Iterator<? extends ChangeLogSet.Entry> i = build.getChangeSet().iterator(); i.hasNext(); ) {
101103
ChangeLogSet.Entry change = i.next();
102104
changes.append("\n");
103105
changes.append(change.getMsg());
@@ -111,20 +113,25 @@ private void setContent() {
111113

112114
private void setTitle() {
113115
message.setTitle(String.format(
114-
"Build #%d of %s",
116+
"%s - Build #%d of %s",
117+
status.tag(),
115118
build.getNumber(),
116119
build.getProject().getName()
117120
));
118121
}
119122

120123
private void setPriority() {
121-
switch (result.ordinal) {
122-
case 0: //SUCCESS
123-
message.lowPriority();
124+
switch (status) {
125+
case FIXED:
126+
message.normalPriority();
124127
break;
125-
case 2: //FAILURE
128+
case BROKEN:
129+
case STILL_BROKEN:
126130
message.highPriority();
127131
break;
132+
case SUCCESSFUL:
133+
message.lowPriority();
134+
break;
128135
}
129136
}
130137

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package tools.devnull.jenkins.plugins.buildnotifications;
2+
3+
import hudson.model.AbstractBuild;
4+
import hudson.model.Result;
5+
6+
/**
7+
* Enumeration of the possible build status for notification purposes.
8+
*/
9+
public enum BuildStatus {
10+
11+
BROKEN("Broken"),
12+
STILL_BROKEN("Still Broken"),
13+
FIXED("Fixed"),
14+
SUCCESSFUL("Successful");
15+
16+
private final String tag;
17+
18+
BuildStatus(String tag) {
19+
this.tag = tag;
20+
}
21+
22+
public String tag() {
23+
return this.tag;
24+
}
25+
26+
public static BuildStatus of(AbstractBuild build) {
27+
AbstractBuild previousBuild = build.getPreviousBuild();
28+
if (build.getResult().ordinal == Result.SUCCESS.ordinal) {
29+
if (previousBuild != null) {
30+
return previousBuild.getResult().ordinal == Result.SUCCESS.ordinal ? SUCCESSFUL : FIXED;
31+
} else {
32+
return SUCCESSFUL;
33+
}
34+
} else {
35+
if (previousBuild != null) {
36+
return previousBuild.getResult().ordinal != Result.SUCCESS.ordinal ? STILL_BROKEN : BROKEN;
37+
} else {
38+
return BROKEN;
39+
}
40+
}
41+
}
42+
43+
}

src/main/java/tools/devnull/jenkins/plugins/buildnotifications/Message.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ public interface Message {
6565
*/
6666
void lowPriority();
6767

68+
/**
69+
* Indicates that this is a normal priority message
70+
*/
71+
void normalPriority();
72+
6873
/**
6974
* Sends the message
7075
*/

src/main/java/tools/devnull/jenkins/plugins/buildnotifications/PushoverMessage.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ public void highPriority() {
9898
this.priority = 1;
9999
}
100100

101+
@Override
102+
public void normalPriority() {
103+
this.priority = 0;
104+
}
105+
101106
public void lowPriority() {
102107
this.priority = -1;
103108
}

src/main/java/tools/devnull/jenkins/plugins/buildnotifications/TelegramMessage.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ public void highPriority() {
8282
// Not possible with Telegram
8383
}
8484

85+
@Override
86+
public void normalPriority() {
87+
// Not possible with Telegram
88+
}
89+
8590
@Override
8691
public void lowPriority() {
8792
// Not possible with Telegram
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package tools.devnull.jenkins.plugins.buildnotifications;
2+
3+
import hudson.model.AbstractBuild;
4+
import hudson.model.Result;
5+
6+
import static org.mockito.Mockito.mock;
7+
import static org.mockito.Mockito.when;
8+
9+
/**
10+
* Utility class for creating a build history for testing purposes
11+
*/
12+
public class BuildChain {
13+
14+
private final AbstractBuild build;
15+
16+
private BuildChain(AbstractBuild actual, AbstractBuild next, Result result) {
17+
this.build = next;
18+
when(build.getResult()).thenReturn(result);
19+
when(next.getPreviousBuild()).thenReturn(actual);
20+
when(actual.getNextBuild()).thenReturn(next);
21+
}
22+
23+
private BuildChain(Result result) {
24+
this.build = mock(AbstractBuild.class);
25+
when(build.getResult()).thenReturn(result);
26+
}
27+
28+
public BuildChain thenFailed() {
29+
return new BuildChain(this.build, mock(AbstractBuild.class), Result.FAILURE);
30+
}
31+
32+
public BuildChain thenSucceeded() {
33+
return new BuildChain(this.build, mock(AbstractBuild.class), Result.SUCCESS);
34+
}
35+
36+
public AbstractBuild get() {
37+
return this.build;
38+
}
39+
40+
public static BuildChain failed() {
41+
return new BuildChain(Result.FAILURE);
42+
}
43+
44+
public static BuildChain succeeded() {
45+
return new BuildChain(Result.SUCCESS);
46+
}
47+
48+
}

0 commit comments

Comments
 (0)