-
Notifications
You must be signed in to change notification settings - Fork 46
/
NotificationStrategy.java
166 lines (146 loc) · 4.7 KB
/
NotificationStrategy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package hudson.plugins.im;
import hudson.model.ResultTrend;
import hudson.model.Run;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static hudson.plugins.im.tools.BuildHelper.*;
/**
* Represents the notification strategy.
*
* @author Uwe Schaefer
*/
public enum NotificationStrategy {
// Note that the order of the constants also specifies the display order!
/**
* No matter what, notifications should always be send.
*/
ALL("all") {
/**
* {@inheritDoc}
*/
@Override
public boolean notificationWanted(final Run<?, ?> run) {
return true;
}
},
/**
* Whenever there is a failure, a notification should be send.
*/
ANY_FAILURE("failure") {
/**
* {@inheritDoc}
*/
@Override
public boolean notificationWanted(final Run<?, ?> run) {
return !isSuccessOrInProgress(run);
}
},
/**
* Whenever there is a failure or a failure was fixed, a notification should be send.
*/
FAILURE_AND_FIXED("failure and fixed") {
/**
* {@inheritDoc}
*/
@Override
public boolean notificationWanted(final Run<?, ?> run) {
if (!isSuccessOrInProgress(run)) {
return true;
}
return isFix(run);
}
},
/**
* Whenever there is a new failure or a failure was fixed, a notification should be send.
* Similar to #FAILURE_AND_FIXED, but repeated failures do not trigger a notification.
*/
NEW_FAILURE_AND_FIXED("new failure and fixed") {
/**
* {@inheritDoc}
*/
@Override
public boolean notificationWanted(final Run<?, ?> run) {
ResultTrend trend = getResultTrend(run);
return trend == ResultTrend.FAILURE || trend == ResultTrend.FIXED;
}
},
/**
* Notifications should be send only if there was a change in the build
* state, or this was the first build.
*/
STATECHANGE_ONLY("change") {
/**
* {@inheritDoc}
*/
@Override
public boolean notificationWanted(final Run<?, ?> run) {
final Run<?, ?> previousBuild = run.getPreviousBuild();
return (previousBuild == null)
|| (run.getResult() != previousBuild.getResult());
}
};
private static final String[] DISPLAY_NAMES;
static {
DISPLAY_NAMES = new String[NotificationStrategy.values().length];
int i = 0;
for (NotificationStrategy strategy : NotificationStrategy.values()) {
DISPLAY_NAMES[i++] = strategy.getDisplayName();
}
}
private final String displayName;
private NotificationStrategy(String displayName) {
this.displayName = displayName;
}
/**
* Signals if the given build qualifies to send a notification according to
* the current strategy.
*
* @param run
* The build for which it should be decided, if notification is
* wanted or not.
* @return true if, according to the given strategy, a notification should
* be sent.
*/
public abstract boolean notificationWanted(Run<?, ?> run);
/**
* Returns the name of the strategy to display in dialogs etc.
*
* @return the display name
*/
public String getDisplayName() {
return this.displayName;
}
/**
* Returns the notification strategy with the given display name.
*
* @param displayName the display name
* @return the notification strategy or null
*/
public static NotificationStrategy forDisplayName(String displayName) {
for (NotificationStrategy strategy : values()) {
if (strategy.getDisplayName().equals(displayName)) {
return strategy;
}
}
return null;
}
/**
* Returns the display names of all notification strategies as a List.
*/
public static List<String> getDisplayNamesList() {
// Return an immutable list copy of public array, not reference to it directly (findbugs MS - MS_EXPOSE_REP)
List<String> resultList = Arrays.asList(DISPLAY_NAMES.clone());
return Collections.unmodifiableList(resultList);
}
/**
* Returns the display names of all notification strategies as an array.
* Deprecated because String[] array is not immutable and upsets findbugs.
*/
@Deprecated
public static String[] getDisplayNames() {
// Return a copy of public array, not reference to if directly (findbugs MS - MS_EXPOSE_REP)
return (String[])DISPLAY_NAMES.clone();
}
}