Skip to content

Commit 416d98c

Browse files
author
Ataxexe
committed
add a way to choose if the notification will be sent in case of success (issue #1)
1 parent f4581ad commit 416d98c

File tree

8 files changed

+59
-16
lines changed

8 files changed

+59
-16
lines changed

.idea/compiler.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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.0.2
4+
5+
- Option to not send notifications in case of success (issue #1)
6+
37
## 1.0.1
48

59
- Changed build step name

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</parent>
1313
<groupId>tools.devnull</groupId>
1414
<artifactId>jenkins-notifier</artifactId>
15-
<version>1.0.1</version>
15+
<version>1.0.2</version>
1616
<packaging>hpi</packaging>
1717

1818
<name>Jenkins Notifier Plugin</name>

src/main/java/tools/devnull/jenkinsnotifier/BuildNotifier.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,33 +45,43 @@ public class BuildNotifier {
4545
private final AbstractBuild build;
4646
private final Result result;
4747
private final String baseUrl;
48+
private final boolean sendIfSuccess;
4849

4950
/**
5051
* Constructs a new BuildNotifier based on the given objects
5152
*
5253
* @param message the message to populate and send
5354
* @param build the target build
5455
*/
55-
public BuildNotifier(Message message, AbstractBuild build) {
56+
public BuildNotifier(Message message, AbstractBuild build, boolean sendIfSuccess) {
5657
this.message = message;
5758
this.build = build;
5859
this.result = build.getResult();
5960
this.baseUrl = JenkinsLocationConfiguration.get().getUrl();
61+
this.sendIfSuccess = sendIfSuccess;
6062
}
6163

6264
/**
6365
* Sends the notification through the given message object.
6466
*/
6567
public void sendNotification() {
68+
if (result.ordinal == 0) {
69+
if (sendIfSuccess) {
70+
sendMessage();
71+
}
72+
} else {
73+
sendMessage();
74+
}
75+
}
76+
77+
private void sendMessage() {
78+
LOGGER.info("Sending push notification...");
79+
6680
setPriority();
6781
setContent();
6882
setTitle();
6983
setUrl();
70-
sendMessage();
71-
}
7284

73-
private void sendMessage() {
74-
LOGGER.info("Sending push notification...");
7585
message.send();
7686
}
7787

src/main/java/tools/devnull/jenkinsnotifier/PushoverNotifier.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,20 @@ public class PushoverNotifier extends Notifier {
5151

5252
private final String userToken;
5353
private final String device;
54+
private final boolean sendIfSuccess;
5455

5556
/**
5657
* Creates a new notifier based on the given parameters
5758
*
58-
* @param userToken the pushover target token
59-
* @param device the optional device to send the message
59+
* @param userToken the pushover target token
60+
* @param device the optional device to send the message
61+
* @param sendIfSuccess if the notification should be sent if the build succeed
6062
*/
6163
@DataBoundConstructor
62-
public PushoverNotifier(String userToken, String device) {
64+
public PushoverNotifier(String userToken, String device, boolean sendIfSuccess) {
6365
this.userToken = userToken;
6466
this.device = device;
67+
this.sendIfSuccess = sendIfSuccess;
6568
}
6669

6770
public String getUserToken() {
@@ -72,6 +75,10 @@ public String getDevice() {
7275
return device;
7376
}
7477

78+
public boolean isSendIfSuccess() {
79+
return sendIfSuccess;
80+
}
81+
7582
@Override
7683
public BuildStepMonitor getRequiredMonitorService() {
7784
return BuildStepMonitor.BUILD;
@@ -83,7 +90,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
8390
PushoverDescriptor descriptor = (PushoverDescriptor) Jenkins.getInstance()
8491
.getDescriptor(PushoverNotifier.class);
8592
Message message = new PushoverMessage(userToken, descriptor.appToken, device);
86-
BuildNotifier notifier = new BuildNotifier(message, build);
93+
BuildNotifier notifier = new BuildNotifier(message, build, sendIfSuccess);
8794
notifier.sendNotification();
8895
return true;
8996
}

src/main/java/tools/devnull/jenkinsnotifier/TelegramNotifier.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,28 @@
5050
public class TelegramNotifier extends Notifier {
5151

5252
private final String chatId;
53+
private final boolean sendIfSuccess;
5354

55+
/**
56+
* Creates a new notifier based on the given parameters
57+
*
58+
* @param chatId the telegram chat id
59+
* @param sendIfSuccess if the notification should be sent if the build succeed
60+
*/
5461
@DataBoundConstructor
55-
public TelegramNotifier(String chatId) {
62+
public TelegramNotifier(String chatId, boolean sendIfSuccess) {
5663
this.chatId = chatId;
64+
this.sendIfSuccess = sendIfSuccess;
5765
}
5866

5967
public String getChatId() {
6068
return chatId;
6169
}
6270

71+
public boolean isSendIfSuccess() {
72+
return sendIfSuccess;
73+
}
74+
6375
@Override
6476
public BuildStepMonitor getRequiredMonitorService() {
6577
return BuildStepMonitor.BUILD;
@@ -71,7 +83,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
7183
TelegramDescriptor descriptor = (TelegramDescriptor) Jenkins.getInstance()
7284
.getDescriptor(TelegramNotifier.class);
7385
Message message = new TelegramMessage(descriptor.getBotToken(), chatId);
74-
BuildNotifier notifier = new BuildNotifier(message, build);
86+
BuildNotifier notifier = new BuildNotifier(message, build, sendIfSuccess);
7587
notifier.sendNotification();
7688
return true;
7789
}

src/main/resources/tools/devnull/jenkinsnotifier/PushoverNotifier/config.jelly

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
xmlns:f="/lib/form">
33

44
<f:entry title="User Token" field="userToken" description="The user token from Pushover">
5-
<f:textbox field="userToken"/>
5+
<f:textbox/>
66
</f:entry>
77

88
<f:entry title="Device (optional)" field="device" description="The device to which the message should be sent.">
9-
<f:textbox field="device"/>
9+
<f:textbox/>
10+
</f:entry>
11+
12+
<f:entry title="Send if success?" field="sendIfSuccess"
13+
description="Sends the notification even if the build succeed">
14+
<f:checkbox/>
1015
</f:entry>
1116

1217
</j:jelly>

src/main/resources/tools/devnull/jenkinsnotifier/TelegramNotifier/config.jelly

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
xmlns:f="/lib/form">
33

44
<f:entry title="Chat ID" field="chatId" description="The target ID to send notifications">
5-
<f:textbox field="chatId"/>
5+
<f:textbox/>
6+
</f:entry>
7+
8+
<f:entry title="Send if success?" field="sendIfSuccess"
9+
description="Sends the notification even if the build succeed">
10+
<f:checkbox />
611
</f:entry>
712

813
</j:jelly>

0 commit comments

Comments
 (0)