Skip to content

Commit 67c4e67

Browse files
committed
Merge pull request #32 from PeteGoo/CustomContent
Add support for customizing content per notification
2 parents 7446437 + 955b33b commit 67c4e67

17 files changed

Lines changed: 482 additions & 141 deletions

tcslackbuildnotifier-core/src/main/java/slacknotifications/teamcity/SlackNotificationListener.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ public void getFromConfig(SlackNotification slackNotification, SlackNotification
7676
slackNotification.setMaxCommitsToDisplay(myMainSettings.getMaxCommitsToDisplay());
7777
slackNotification.setMentionChannelEnabled(slackNotificationConfig.getMentionChannelEnabled());
7878
slackNotification.setMentionSlackUserEnabled(slackNotificationConfig.getMentionSlackUserEnabled());
79+
if(slackNotificationConfig.getContent().isEnabled()) {
80+
slackNotification.setBotName(slackNotificationConfig.getContent().getBotName());
81+
slackNotification.setIconUrl(slackNotificationConfig.getContent().getIconUrl());
82+
slackNotification.setMaxCommitsToDisplay(slackNotificationConfig.getContent().getMaxCommitsToDisplay());
83+
slackNotification.setShowBuildAgent(slackNotificationConfig.getContent().getShowBuildAgent());
84+
slackNotification.setShowElapsedBuildTime(slackNotificationConfig.getContent().getShowElapsedBuildTime());
85+
slackNotification.setShowCommits(slackNotificationConfig.getContent().getShowCommits());
86+
slackNotification.setShowCommitters(slackNotificationConfig.getContent().getShowCommitters());
87+
}
7988
Loggers.ACTIVITIES.debug("SlackNotificationListener :: SlackNotification proxy set to "
8089
+ slackNotification.getProxyHost() + " for " + slackNotificationConfig.getChannel());
8190
}

tcslackbuildnotifier-core/src/main/java/slacknotifications/teamcity/settings/SlackNotificationConfig.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ public class SlackNotificationConfig {
2626
private Set<String> enabledBuildTypesSet = new HashSet<String>();
2727
private boolean mentionChannelEnabled;
2828
private boolean mentionSlackUserEnabled;
29+
private boolean customContent;
30+
private SlackNotificationContentConfig content;
2931

3032
@SuppressWarnings("unchecked")
3133
public SlackNotificationConfig(Element e) {
32-
34+
this.content = new SlackNotificationContentConfig();
3335
int Min = 1000000, Max = 1000000000;
3436
Integer Rand = Min + (int)(Math.random() * ((Max - Min) + 1));
3537
this.uniqueKey = Rand.toString();
@@ -121,6 +123,36 @@ public SlackNotificationConfig(Element e) {
121123
}
122124
}
123125
}
126+
127+
if(e.getChild("content") != null) {
128+
setHasCustomContent(true);
129+
Element eContent = e.getChild("content");
130+
131+
this.content.setEnabled(true);
132+
133+
if (eContent.getAttribute("iconUrl") != null){
134+
this.content.setIconUrl(eContent.getAttributeValue("iconUrl"));
135+
}
136+
if (eContent.getAttribute("botName") != null){
137+
this.content.setBotName(eContent.getAttributeValue("botName"));
138+
}
139+
if (eContent.getAttribute("showBuildAgent") != null){
140+
this.content.setShowBuildAgent(Boolean.parseBoolean(eContent.getAttributeValue("showBuildAgent")));
141+
}
142+
if (eContent.getAttribute("showElapsedBuildTime") != null){
143+
this.content.setShowElapsedBuildTime(Boolean.parseBoolean(eContent.getAttributeValue("showElapsedBuildTime")));
144+
}
145+
if (eContent.getAttribute("showCommits") != null){
146+
this.content.setShowCommits(Boolean.parseBoolean(eContent.getAttributeValue("showCommits")));
147+
}
148+
if (eContent.getAttribute("showCommitters") != null){
149+
this.content.setShowCommitters(Boolean.parseBoolean(eContent.getAttributeValue("showCommitters")));
150+
}
151+
if (eContent.getAttribute("maxCommitsToDisplay") != null){
152+
this.content.setMaxCommitsToDisplay(Integer.parseInt(eContent.getAttributeValue("maxCommitsToDisplay")));
153+
}
154+
}
155+
124156

125157
}
126158

@@ -143,6 +175,7 @@ public SlackNotificationConfig(String channel,
143175
Set<String> enabledBuildTypes,
144176
boolean mentionChannelEnabled,
145177
boolean mentionSlackUserEnabled) {
178+
this.content = new SlackNotificationContentConfig();
146179
int Min = 1000000, Max = 1000000000;
147180
Integer Rand = Min + (int) (Math.random() * ((Max - Min) + 1));
148181
this.uniqueKey = Rand.toString();
@@ -201,6 +234,18 @@ public Element getAsElement(){
201234
}
202235
el.addContent(templatesEl);
203236
}
237+
238+
if(this.hasCustomContent()){
239+
Element customContentEl = new Element("content");
240+
customContentEl.setAttribute("iconUrl", this.content.getIconUrl());
241+
customContentEl.setAttribute("botName", this.content.getBotName());
242+
customContentEl.setAttribute("maxCommitsToDisplay", Integer.toString(this.content.getMaxCommitsToDisplay()));
243+
customContentEl.setAttribute("showBuildAgent", this.content.getShowBuildAgent().toString());
244+
customContentEl.setAttribute("showElapsedBuildTime", this.content.getShowElapsedBuildTime().toString());
245+
customContentEl.setAttribute("showCommits", this.content.getShowCommits().toString());
246+
customContentEl.setAttribute("showCommitters", this.content.getShowCommitters().toString());
247+
el.addContent(customContentEl);
248+
}
204249

205250
return el;
206251
}
@@ -441,4 +486,19 @@ public boolean getMentionSlackUserEnabled() {
441486
return mentionSlackUserEnabled;
442487
}
443488

489+
public boolean hasCustomContent() {
490+
return customContent;
491+
}
492+
493+
public void setHasCustomContent(boolean customContent) {
494+
this.customContent = customContent;
495+
}
496+
497+
public SlackNotificationContentConfig getContent() {
498+
return content;
499+
}
500+
501+
public void setContent(SlackNotificationContentConfig content) {
502+
this.content = content;
503+
}
444504
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package slacknotifications.teamcity.settings;
2+
3+
/**
4+
* Created by petegoo on 24/02/15.
5+
*/
6+
public class SlackNotificationContentConfig {
7+
public static final int DEFAULT_MAX_COMMITS = 5;
8+
public static final boolean DEFAULT_SHOW_BUILD_AGENT = true;
9+
public static final boolean DEFAULT_SHOW_ELAPSED_BUILD_TIME = true;
10+
public static final boolean DEFAULT_SHOW_COMMITS = true;
11+
public static final boolean DEFAULT_SHOW_COMMITTERS = true;
12+
private String iconUrl = SlackNotificationMainConfig.DEFAULT_ICONURL;
13+
private String botName = SlackNotificationMainConfig.DEFAULT_BOTNAME;
14+
private Boolean showBuildAgent;
15+
private Boolean showElapsedBuildTime;
16+
private Boolean showCommits = true;
17+
private Boolean showCommitters = true;
18+
private int maxCommitsToDisplay = 5;
19+
private boolean enabled;
20+
21+
public String getIconUrl() {
22+
return iconUrl;
23+
}
24+
25+
public void setIconUrl(String iconUrl) {
26+
this.iconUrl = iconUrl;
27+
}
28+
29+
public String getBotName() {
30+
return botName;
31+
}
32+
33+
public void setBotName(String botName) {
34+
this.botName = botName;
35+
}
36+
37+
public Boolean getShowBuildAgent() {
38+
return showBuildAgent;
39+
}
40+
41+
public void setShowBuildAgent(Boolean showBuildAgent) {
42+
this.showBuildAgent = showBuildAgent;
43+
}
44+
45+
public Boolean getShowElapsedBuildTime() {
46+
return showElapsedBuildTime;
47+
}
48+
49+
public void setShowElapsedBuildTime(Boolean showElapsedBuildTime) {
50+
this.showElapsedBuildTime = showElapsedBuildTime;
51+
}
52+
53+
public Boolean getShowCommits() {
54+
return showCommits;
55+
}
56+
57+
public void setShowCommits(Boolean showCommits) {
58+
this.showCommits = showCommits;
59+
}
60+
61+
public Boolean getShowCommitters() {
62+
return showCommitters;
63+
}
64+
65+
public void setShowCommitters(Boolean showCommitters) {
66+
this.showCommitters = showCommitters;
67+
}
68+
69+
public int getMaxCommitsToDisplay() {
70+
return maxCommitsToDisplay;
71+
}
72+
73+
public void setMaxCommitsToDisplay(int maxCommitsToDisplay) {
74+
this.maxCommitsToDisplay = maxCommitsToDisplay;
75+
}
76+
77+
public boolean isEnabled() {
78+
return enabled;
79+
}
80+
81+
public void setEnabled(boolean enabled) {
82+
this.enabled = enabled;
83+
}
84+
}

tcslackbuildnotifier-core/src/main/java/slacknotifications/teamcity/settings/SlackNotificationMainConfig.java

Lines changed: 28 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import java.io.IOException;
1717

1818
public class SlackNotificationMainConfig implements ChangeListener {
19-
public final String DEFAULT_BOTNAME = "TeamCity";
19+
public static final String DEFAULT_BOTNAME = "TeamCity";
2020
public static final String DEFAULT_ICONURL = "https://raw.githubusercontent.com/PeteGoo/tcSlackBuildNotifier/master/docs/TeamCity32.png";
2121

2222

@@ -38,19 +38,14 @@ public class SlackNotificationMainConfig implements ChangeListener {
3838

3939
public final String SINGLE_HOST_REGEX = "^[^./~`'\"]+(?:/.*)?$";
4040
public final String HOSTNAME_ONLY_REGEX = "^([^/]+)(?:/.*)?$";
41-
private String iconUrl = DEFAULT_ICONURL;
42-
private String botName = DEFAULT_BOTNAME;
43-
private Boolean showBuildAgent;
44-
private Boolean showElapsedBuildTime;
45-
private Boolean showCommits = true;
46-
private Boolean showCommitters = true;
47-
private int maxCommitsToDisplay = 5;
48-
private boolean configFileExists;
41+
private SlackNotificationContentConfig content;
42+
private boolean configFileExists;
4943

5044

5145

52-
public SlackNotificationMainConfig(ServerPaths serverPaths) {
5346

47+
public SlackNotificationMainConfig(ServerPaths serverPaths) {
48+
this.content = new SlackNotificationContentConfig();
5449
this.myConfigDir = new File(serverPaths.getConfigDir(), "slack");
5550
this.myConfigFile = new File(this.myConfigDir, "slack-config.xml");
5651
configFileExists = this.myConfigFile.exists();
@@ -186,26 +181,6 @@ public void setToken(String token) {
186181
this.token = token;
187182
}
188183

189-
public String getIconUrl()
190-
{
191-
return iconUrl;
192-
}
193-
194-
public void setIconUrl(String iconUrl)
195-
{
196-
this.iconUrl = iconUrl;
197-
}
198-
199-
public String getBotName()
200-
{
201-
return botName;
202-
}
203-
204-
public void setBotName(String botName)
205-
{
206-
this.botName = botName;
207-
}
208-
209184
public Integer getProxyPort() {
210185
return proxyPort;
211186
}
@@ -279,46 +254,6 @@ public Boolean getSlackNotificationShowFurtherReading() {
279254
}
280255

281256

282-
public Boolean getShowBuildAgent() {
283-
return showBuildAgent;
284-
}
285-
286-
public void setShowBuildAgent(Boolean showBuildAgent) {
287-
this.showBuildAgent = showBuildAgent;
288-
}
289-
290-
public Boolean getShowElapsedBuildTime() {
291-
return showElapsedBuildTime;
292-
}
293-
294-
public void setShowElapsedBuildTime(Boolean showElapsedBuildTime) {
295-
this.showElapsedBuildTime = showElapsedBuildTime;
296-
}
297-
298-
public boolean getShowCommits() {
299-
return showCommits;
300-
}
301-
302-
public void setShowCommits(boolean showCommits) {
303-
this.showCommits = showCommits;
304-
}
305-
306-
public boolean getShowCommitters() {
307-
return showCommitters;
308-
}
309-
310-
public void setShowCommitters(boolean showCommitters) {
311-
this.showCommitters = showCommitters;
312-
}
313-
314-
public int getMaxCommitsToDisplay() {
315-
return maxCommitsToDisplay;
316-
}
317-
318-
public void setMaxCommitsToDisplay(int maxCommitsToDisplay) {
319-
this.maxCommitsToDisplay = maxCommitsToDisplay;
320-
}
321-
322257
public synchronized void save()
323258
{
324259
this.myChangeObserver.runActionWithDisabledObserver(new Runnable()
@@ -332,21 +267,21 @@ public void process(Element rootElement) {
332267
rootElement.setAttribute("defaultChannel", emptyIfNull(SlackNotificationMainConfig.this.defaultChannel));
333268
rootElement.setAttribute("teamName", emptyIfNull(SlackNotificationMainConfig.this.teamName));
334269
rootElement.setAttribute("token", emptyIfNull(SlackNotificationMainConfig.this.token));
335-
rootElement.setAttribute("iconurl", emptyIfNull(SlackNotificationMainConfig.this.iconUrl));
336-
rootElement.setAttribute("botname", emptyIfNull(SlackNotificationMainConfig.this.botName));
337-
if(SlackNotificationMainConfig.this.showBuildAgent != null){
338-
rootElement.setAttribute("showBuildAgent", Boolean.toString(SlackNotificationMainConfig.this.showBuildAgent));
270+
rootElement.setAttribute("iconurl", emptyIfNull(SlackNotificationMainConfig.this.content.getIconUrl()));
271+
rootElement.setAttribute("botname", emptyIfNull(SlackNotificationMainConfig.this.content.getBotName()));
272+
if(SlackNotificationMainConfig.this.content.getShowBuildAgent() != null){
273+
rootElement.setAttribute("showBuildAgent", Boolean.toString(SlackNotificationMainConfig.this.content.getShowBuildAgent()));
339274
}
340-
if(SlackNotificationMainConfig.this.showElapsedBuildTime != null) {
341-
rootElement.setAttribute("showElapsedBuildTime", Boolean.toString(SlackNotificationMainConfig.this.showElapsedBuildTime));
275+
if(SlackNotificationMainConfig.this.content.getShowElapsedBuildTime() != null) {
276+
rootElement.setAttribute("showElapsedBuildTime", Boolean.toString(SlackNotificationMainConfig.this.content.getShowElapsedBuildTime()));
342277
}
343-
if(SlackNotificationMainConfig.this.showCommits != null) {
344-
rootElement.setAttribute("showCommits", Boolean.toString(SlackNotificationMainConfig.this.showCommits));
278+
if(SlackNotificationMainConfig.this.content.getShowCommits() != null) {
279+
rootElement.setAttribute("showCommits", Boolean.toString(SlackNotificationMainConfig.this.content.getShowCommits()));
345280
}
346-
if(SlackNotificationMainConfig.this.showCommitters != null) {
347-
rootElement.setAttribute("showCommitters", Boolean.toString(SlackNotificationMainConfig.this.showCommitters));
281+
if(SlackNotificationMainConfig.this.content.getShowCommitters() != null) {
282+
rootElement.setAttribute("showCommitters", Boolean.toString(SlackNotificationMainConfig.this.content.getShowCommitters()));
348283
}
349-
rootElement.setAttribute("maxCommitsToDisplay", Integer.toString(SlackNotificationMainConfig.this.maxCommitsToDisplay));
284+
rootElement.setAttribute("maxCommitsToDisplay", Integer.toString(SlackNotificationMainConfig.this.content.getMaxCommitsToDisplay()));
350285

351286
rootElement.removeChildren("proxy");
352287
rootElement.removeChildren("info");
@@ -386,6 +321,7 @@ public boolean getConfigFileExists() {
386321

387322
void readConfigurationFromXmlElement(Element slackNotificationsElement) {
388323
if(slackNotificationsElement != null){
324+
content.setEnabled(true);
389325
if(slackNotificationsElement.getAttribute("enabled") != null)
390326
{
391327
setEnabled(Boolean.parseBoolean(slackNotificationsElement.getAttributeValue("enabled")));
@@ -404,31 +340,31 @@ void readConfigurationFromXmlElement(Element slackNotificationsElement) {
404340
}
405341
if(slackNotificationsElement.getAttribute("iconurl") != null)
406342
{
407-
setIconUrl(slackNotificationsElement.getAttributeValue("iconurl"));
343+
content.setIconUrl(slackNotificationsElement.getAttributeValue("iconurl"));
408344
}
409345
if(slackNotificationsElement.getAttribute("botname") != null)
410346
{
411-
setBotName(slackNotificationsElement.getAttributeValue("botname"));
347+
content.setBotName(slackNotificationsElement.getAttributeValue("botname"));
412348
}
413349
if(slackNotificationsElement.getAttribute("showBuildAgent") != null)
414350
{
415-
setShowBuildAgent(Boolean.parseBoolean(slackNotificationsElement.getAttributeValue("showBuildAgent")));
351+
content.setShowBuildAgent(Boolean.parseBoolean(slackNotificationsElement.getAttributeValue("showBuildAgent")));
416352
}
417353
if(slackNotificationsElement.getAttribute("showElapsedBuildTime") != null)
418354
{
419-
setShowElapsedBuildTime(Boolean.parseBoolean(slackNotificationsElement.getAttributeValue("showElapsedBuildTime")));
355+
content.setShowElapsedBuildTime(Boolean.parseBoolean(slackNotificationsElement.getAttributeValue("showElapsedBuildTime")));
420356
}
421357
if(slackNotificationsElement.getAttribute("showCommits") != null)
422358
{
423-
setShowCommits(Boolean.parseBoolean(slackNotificationsElement.getAttributeValue("showCommits")));
359+
content.setShowCommits(Boolean.parseBoolean(slackNotificationsElement.getAttributeValue("showCommits")));
424360
}
425361
if(slackNotificationsElement.getAttribute("showCommitters") != null)
426362
{
427-
setShowCommitters(Boolean.parseBoolean(slackNotificationsElement.getAttributeValue("showCommitters")));
363+
content.setShowCommitters(Boolean.parseBoolean(slackNotificationsElement.getAttributeValue("showCommitters")));
428364
}
429365
if(slackNotificationsElement.getAttribute("maxCommitsToDisplay") != null)
430366
{
431-
setMaxCommitsToDisplay(Integer.parseInt(slackNotificationsElement.getAttributeValue("maxCommitsToDisplay")));
367+
content.setMaxCommitsToDisplay(Integer.parseInt(slackNotificationsElement.getAttributeValue("maxCommitsToDisplay")));
432368
}
433369

434370
Element proxyElement = slackNotificationsElement.getChild("proxy");
@@ -466,4 +402,8 @@ void readConfigurationFromXmlElement(Element slackNotificationsElement) {
466402
public SlackNotificationProxyConfig getProxyConfig() {
467403
return new SlackNotificationProxyConfig(proxyHost, proxyPort, proxyUsername, proxyPassword);
468404
}
405+
406+
public SlackNotificationContentConfig getContent() {
407+
return content;
408+
}
469409
}

0 commit comments

Comments
 (0)