Skip to content

Commit cff01a4

Browse files
committed
Introduce content config element and add support in project settings
1 parent 4dd6fcf commit cff01a4

7 files changed

Lines changed: 228 additions & 108 deletions

File tree

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

Lines changed: 58 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,34 @@ 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+
if (eContent.getAttribute("iconUrl") != null){
132+
this.content.setIconUrl(eContent.getAttributeValue("iconUrl"));
133+
}
134+
if (eContent.getAttribute("botName") != null){
135+
this.content.setBotName(eContent.getAttributeValue("botName"));
136+
}
137+
if (eContent.getAttribute("showBuildAgent") != null){
138+
this.content.setShowBuildAgent(Boolean.parseBoolean(eContent.getAttributeValue("showBuildAgent")));
139+
}
140+
if (eContent.getAttribute("showElapsedBuildTime") != null){
141+
this.content.setShowElapsedBuildTime(Boolean.parseBoolean(eContent.getAttributeValue("showElapsedBuildTime")));
142+
}
143+
if (eContent.getAttribute("showCommits") != null){
144+
this.content.setShowCommits(Boolean.parseBoolean(eContent.getAttributeValue("showCommits")));
145+
}
146+
if (eContent.getAttribute("showCommitters") != null){
147+
this.content.setShowCommitters(Boolean.parseBoolean(eContent.getAttributeValue("showCommitters")));
148+
}
149+
if (eContent.getAttribute("maxCommitsToDisplay") != null){
150+
this.content.setMaxCommitsToDisplay(Integer.parseInt(eContent.getAttributeValue("maxCommitsToDisplay")));
151+
}
152+
}
153+
124154

125155
}
126156

@@ -201,6 +231,18 @@ public Element getAsElement(){
201231
}
202232
el.addContent(templatesEl);
203233
}
234+
235+
if(this.hasCustomContent()){
236+
Element customContentEl = new Element("content");
237+
customContentEl.setAttribute("iconUrl", this.content.getIconUrl());
238+
customContentEl.setAttribute("botName", this.content.getBotName());
239+
customContentEl.setAttribute("maxCommitsToDisplay", Integer.toString(this.content.getMaxCommitsToDisplay()));
240+
customContentEl.setAttribute("showBuildAgent", this.content.getShowBuildAgent().toString());
241+
customContentEl.setAttribute("showElapsedBuildTime", this.content.getShowElapsedBuildTime().toString());
242+
customContentEl.setAttribute("showCommits", this.content.getShowCommits().toString());
243+
customContentEl.setAttribute("showCommitters", this.content.getShowCommitters().toString());
244+
el.addContent(customContentEl);
245+
}
204246

205247
return el;
206248
}
@@ -441,4 +483,19 @@ public boolean getMentionSlackUserEnabled() {
441483
return mentionSlackUserEnabled;
442484
}
443485

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

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

Lines changed: 27 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");
@@ -404,31 +339,31 @@ void readConfigurationFromXmlElement(Element slackNotificationsElement) {
404339
}
405340
if(slackNotificationsElement.getAttribute("iconurl") != null)
406341
{
407-
setIconUrl(slackNotificationsElement.getAttributeValue("iconurl"));
342+
content.setIconUrl(slackNotificationsElement.getAttributeValue("iconurl"));
408343
}
409344
if(slackNotificationsElement.getAttribute("botname") != null)
410345
{
411-
setBotName(slackNotificationsElement.getAttributeValue("botname"));
346+
content.setBotName(slackNotificationsElement.getAttributeValue("botname"));
412347
}
413348
if(slackNotificationsElement.getAttribute("showBuildAgent") != null)
414349
{
415-
setShowBuildAgent(Boolean.parseBoolean(slackNotificationsElement.getAttributeValue("showBuildAgent")));
350+
content.setShowBuildAgent(Boolean.parseBoolean(slackNotificationsElement.getAttributeValue("showBuildAgent")));
416351
}
417352
if(slackNotificationsElement.getAttribute("showElapsedBuildTime") != null)
418353
{
419-
setShowElapsedBuildTime(Boolean.parseBoolean(slackNotificationsElement.getAttributeValue("showElapsedBuildTime")));
354+
content.setShowElapsedBuildTime(Boolean.parseBoolean(slackNotificationsElement.getAttributeValue("showElapsedBuildTime")));
420355
}
421356
if(slackNotificationsElement.getAttribute("showCommits") != null)
422357
{
423-
setShowCommits(Boolean.parseBoolean(slackNotificationsElement.getAttributeValue("showCommits")));
358+
content.setShowCommits(Boolean.parseBoolean(slackNotificationsElement.getAttributeValue("showCommits")));
424359
}
425360
if(slackNotificationsElement.getAttribute("showCommitters") != null)
426361
{
427-
setShowCommitters(Boolean.parseBoolean(slackNotificationsElement.getAttributeValue("showCommitters")));
362+
content.setShowCommitters(Boolean.parseBoolean(slackNotificationsElement.getAttributeValue("showCommitters")));
428363
}
429364
if(slackNotificationsElement.getAttribute("maxCommitsToDisplay") != null)
430365
{
431-
setMaxCommitsToDisplay(Integer.parseInt(slackNotificationsElement.getAttributeValue("maxCommitsToDisplay")));
366+
content.setMaxCommitsToDisplay(Integer.parseInt(slackNotificationsElement.getAttributeValue("maxCommitsToDisplay")));
432367
}
433368

434369
Element proxyElement = slackNotificationsElement.getChild("proxy");
@@ -466,4 +401,8 @@ void readConfigurationFromXmlElement(Element slackNotificationsElement) {
466401
public SlackNotificationProxyConfig getProxyConfig() {
467402
return new SlackNotificationProxyConfig(proxyHost, proxyPort, proxyUsername, proxyPassword);
468403
}
404+
405+
public SlackNotificationContentConfig getContent() {
406+
return content;
407+
}
469408
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ public String getToken() {
9191

9292
public String getIconUrl()
9393
{
94-
return this.slackNotificationMainConfig.getIconUrl();
94+
return this.slackNotificationMainConfig.getContent().getIconUrl();
9595
}
9696

9797
public String getBotName()
9898
{
99-
return this.slackNotificationMainConfig.getBotName();
99+
return this.slackNotificationMainConfig.getContent().getBotName();
100100
}
101101

102102
public boolean getEnabled(){
@@ -105,19 +105,19 @@ public boolean getEnabled(){
105105

106106

107107
public Boolean getShowBuildAgent() {
108-
return this.slackNotificationMainConfig.getShowBuildAgent();
108+
return this.slackNotificationMainConfig.getContent().getShowBuildAgent();
109109
}
110110

111111
public Boolean getShowElapsedBuildTime() {
112-
return this.slackNotificationMainConfig.getShowElapsedBuildTime();
112+
return this.slackNotificationMainConfig.getContent().getShowElapsedBuildTime();
113113
}
114114

115115
public boolean getShowCommits(){
116-
return this.slackNotificationMainConfig.getShowCommits();
116+
return this.slackNotificationMainConfig.getContent().getShowCommits();
117117
}
118118

119119
public boolean getShowCommitters(){
120-
return this.slackNotificationMainConfig.getShowCommitters();
120+
return this.slackNotificationMainConfig.getContent().getShowCommitters();
121121
}
122122

123123
public Boolean getSlackNotificationShowFurtherReading(){
@@ -133,7 +133,7 @@ public SlackNotificationProxyConfig getProxyConfig() {
133133

134134

135135
public int getMaxCommitsToDisplay() {
136-
return this.slackNotificationMainConfig.getMaxCommitsToDisplay();
136+
return this.slackNotificationMainConfig.getContent().getMaxCommitsToDisplay();
137137
}
138138

139139
public void refresh() {

0 commit comments

Comments
 (0)