Skip to content

Commit 809bcb4

Browse files
committed
Wire up UI for custom content
1 parent cff01a4 commit 809bcb4

12 files changed

Lines changed: 170 additions & 33 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ public SlackNotificationConfig(Element e) {
128128
setHasCustomContent(true);
129129
Element eContent = e.getChild("content");
130130

131+
this.content.setEnabled(true);
132+
131133
if (eContent.getAttribute("iconUrl") != null){
132134
this.content.setIconUrl(eContent.getAttributeValue("iconUrl"));
133135
}
@@ -173,6 +175,7 @@ public SlackNotificationConfig(String channel,
173175
Set<String> enabledBuildTypes,
174176
boolean mentionChannelEnabled,
175177
boolean mentionSlackUserEnabled) {
178+
this.content = new SlackNotificationContentConfig();
176179
int Min = 1000000, Max = 1000000000;
177180
Integer Rand = Min + (int) (Math.random() * ((Max - Min) + 1));
178181
this.uniqueKey = Rand.toString();

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44
* Created by petegoo on 24/02/15.
55
*/
66
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;
712
private String iconUrl = SlackNotificationMainConfig.DEFAULT_ICONURL;
813
private String botName = SlackNotificationMainConfig.DEFAULT_BOTNAME;
914
private Boolean showBuildAgent;
1015
private Boolean showElapsedBuildTime;
1116
private Boolean showCommits = true;
1217
private Boolean showCommitters = true;
1318
private int maxCommitsToDisplay = 5;
19+
private boolean enabled;
1420

1521
public String getIconUrl() {
1622
return iconUrl;
@@ -67,4 +73,12 @@ public int getMaxCommitsToDisplay() {
6773
public void setMaxCommitsToDisplay(int maxCommitsToDisplay) {
6874
this.maxCommitsToDisplay = maxCommitsToDisplay;
6975
}
76+
77+
public boolean isEnabled() {
78+
return enabled;
79+
}
80+
81+
public void setEnabled(boolean enabled) {
82+
this.enabled = enabled;
83+
}
7084
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ public boolean getConfigFileExists() {
321321

322322
void readConfigurationFromXmlElement(Element slackNotificationsElement) {
323323
if(slackNotificationsElement != null){
324+
content.setEnabled(true);
324325
if(slackNotificationsElement.getAttribute("enabled") != null)
325326
{
326327
setEnabled(Boolean.parseBoolean(slackNotificationsElement.getAttributeValue("enabled")));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ public void writeTo(Element parentElement)
7272
Loggers.SERVER.debug(NAME + ":writeTo :: channel " + whc.getChannel());
7373
Loggers.SERVER.debug(NAME + ":writeTo :: enabled " + String.valueOf(whc.getEnabled()));
7474
}
75-
7675
}
7776
}
7877

@@ -130,7 +129,7 @@ public void deleteSlackNotification(String slackNotificationId, String ProjectId
130129
}
131130
}
132131

133-
public void updateSlackNotification(String ProjectId, String slackNotificationId, String channel, Boolean enabled, BuildState buildState, boolean buildTypeAll, boolean buildSubProjects, Set<String> buildTypesEnabled, boolean mentionChannelEnabled, boolean mentionSlackUserEnabled) {
132+
public void updateSlackNotification(String ProjectId, String slackNotificationId, String channel, Boolean enabled, BuildState buildState, boolean buildTypeAll, boolean buildSubProjects, Set<String> buildTypesEnabled, boolean mentionChannelEnabled, boolean mentionSlackUserEnabled, SlackNotificationContentConfig content) {
134133
if(this.slackNotificationsConfigs != null)
135134
{
136135
updateSuccess = false;
@@ -145,6 +144,7 @@ public void updateSlackNotification(String ProjectId, String slackNotificationId
145144
whc.setBuildStates(buildState);
146145
whc.enableForSubProjects(buildSubProjects);
147146
whc.enableForAllBuildsInProject(buildTypeAll);
147+
whc.setContent(content);
148148
if (!buildTypeAll){
149149
whc.clearAllEnabledBuildsInProject();
150150
for (String bt : buildTypesEnabled){

tcslackbuildnotifier-web-ui/src/main/java/slacknotifications/teamcity/extension/SlackNotificationAjaxEditPageController.java

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
import jetbrains.buildServer.web.util.SessionUser;
1212
import org.jetbrains.annotations.Nullable;
1313
import org.springframework.web.servlet.ModelAndView;
14+
import slacknotifications.SlackNotification;
1415
import slacknotifications.teamcity.BuildState;
1516
import slacknotifications.teamcity.BuildStateEnum;
1617
import slacknotifications.teamcity.TeamCityIdResolver;
1718
import slacknotifications.teamcity.extension.bean.ProjectSlackNotificationsBean;
1819
import slacknotifications.teamcity.extension.bean.ProjectSlackNotificationsBeanJsonSerialiser;
1920
import slacknotifications.teamcity.payload.SlackNotificationPayloadManager;
21+
import slacknotifications.teamcity.settings.SlackNotificationContentConfig;
22+
import slacknotifications.teamcity.settings.SlackNotificationMainSettings;
2023
import slacknotifications.teamcity.settings.SlackNotificationProjectSettings;
2124

2225
import javax.servlet.http.HttpServletRequest;
@@ -37,20 +40,22 @@ public class SlackNotificationAjaxEditPageController extends BaseController {
3740
protected static final String BUILD_SUCCESSFUL = "BuildSuccessful";
3841

3942
private final WebControllerManager myWebManager;
40-
private SBuildServer myServer;
43+
private final SlackNotificationMainSettings myMainSettings;
44+
private SBuildServer myServer;
4145
private ProjectSettingsManager mySettings;
4246
private final String myPluginPath;
4347
private final SlackNotificationPayloadManager myManager;
4448

4549
public SlackNotificationAjaxEditPageController(SBuildServer server, WebControllerManager webManager,
4650
ProjectSettingsManager settings, SlackNotificationProjectSettings whSettings, SlackNotificationPayloadManager manager,
47-
PluginDescriptor pluginDescriptor) {
51+
PluginDescriptor pluginDescriptor, SlackNotificationMainSettings mainSettings) {
4852
super(server);
4953
myWebManager = webManager;
5054
myServer = server;
5155
mySettings = settings;
5256
myPluginPath = pluginDescriptor.getPluginResourcesPath();
5357
myManager = manager;
58+
myMainSettings = mainSettings;
5459
}
5560

5661
public void register(){
@@ -117,19 +122,56 @@ protected ModelAndView doHandle(HttpServletRequest request, HttpServletResponse
117122
Boolean mentionSlackUserEnabled = false;
118123
Boolean buildTypeAll = false;
119124
Boolean buildTypeSubProjects = false;
125+
SlackNotificationContentConfig content = new SlackNotificationContentConfig();
120126
Set<String> buildTypes = new HashSet<String>();
121127
if ((request.getParameter("slackNotificationsEnabled") != null )
122128
&& (request.getParameter("slackNotificationsEnabled").equalsIgnoreCase("on"))){
123129
enabled = true;
124130
}
125131
if ((request.getParameter("mentionChannelEnabled") != null )
126132
&& (request.getParameter("mentionChannelEnabled").equalsIgnoreCase("on"))){
133+
127134
mentionChannelEnabled = true;
128135
}
129136
if ((request.getParameter("mentionSlackUserEnabled") != null )
130137
&& (request.getParameter("mentionSlackUserEnabled").equalsIgnoreCase("on"))){
131138
mentionSlackUserEnabled = true;
132139
}
140+
141+
content.setEnabled((request.getParameter("customContentEnabled") != null )
142+
&& (request.getParameter("customContentEnabled").equalsIgnoreCase("on")));
143+
144+
if (content.isEnabled()){
145+
146+
if ((request.getParameter("maxCommitsToDisplay") != null )
147+
&& (request.getParameter("maxCommitsToDisplay").length() > 0)){
148+
content.setMaxCommitsToDisplay(convertToInt(request.getParameter("maxCommitsToDisplay"), SlackNotificationContentConfig.DEFAULT_MAX_COMMITS));
149+
}
150+
151+
content.setShowBuildAgent((request.getParameter("showBuildAgent") != null )
152+
&& (request.getParameter("showBuildAgent").equalsIgnoreCase("on")));
153+
154+
content.setShowCommits((request.getParameter("showCommits") != null )
155+
&& (request.getParameter("showCommits").equalsIgnoreCase("on")));
156+
157+
content.setShowCommitters((request.getParameter("showCommitters") != null)
158+
&& (request.getParameter("showCommitters").equalsIgnoreCase("on")));
159+
160+
content.setShowElapsedBuildTime((request.getParameter("showElapsedBuildTime") != null)
161+
&& (request.getParameter("showElapsedBuildTime").equalsIgnoreCase("on")));
162+
163+
164+
if ((request.getParameter("botName") != null )
165+
&& (request.getParameter("botName").length() > 0)){
166+
content.setBotName(request.getParameter("botName"));
167+
}
168+
169+
if ((request.getParameter("iconUrl") != null )
170+
&& (request.getParameter("iconUrl").length() > 0)){
171+
content.setIconUrl(request.getParameter("iconUrl"));
172+
}
173+
}
174+
133175
BuildState states = new BuildState();
134176

135177
checkAndAddBuildState(request, states, BuildStateEnum.BUILD_SUCCESSFUL, BUILD_SUCCESSFUL);
@@ -168,7 +210,8 @@ protected ModelAndView doHandle(HttpServletRequest request, HttpServletResponse
168210
} else {
169211
projSettings.updateSlackNotification(myProject.getProjectId(),request.getParameter("slackNotificationId"),
170212
request.getParameter("channel"), enabled,
171-
states, buildTypeAll, buildTypeSubProjects, buildTypes, mentionChannelEnabled, mentionSlackUserEnabled);
213+
states, buildTypeAll, buildTypeSubProjects, buildTypes, mentionChannelEnabled, mentionSlackUserEnabled,
214+
content);
172215
if(projSettings.updateSuccessful()){
173216
myProject.persist();
174217
params.put("messages", "<errors />");
@@ -217,12 +260,22 @@ protected ModelAndView doHandle(HttpServletRequest request, HttpServletResponse
217260
params.put("slackNotificationList", projSettings.getSlackNotificationsAsList());
218261
params.put("slackNotificationsDisabled", !projSettings.isEnabled());
219262
params.put("slackNotificationsEnabledAsChecked", projSettings.isEnabledAsChecked());
220-
params.put("projectSlackNotificationsAsJson", ProjectSlackNotificationsBeanJsonSerialiser.serialise(ProjectSlackNotificationsBean.build(projSettings, project)));
263+
params.put("projectSlackNotificationsAsJson", ProjectSlackNotificationsBeanJsonSerialiser.serialise(ProjectSlackNotificationsBean.build(projSettings, project, myMainSettings)));
221264
}
222265
} else {
223266
params.put("haveProject", "false");
224267
}
225268

226269
return new ModelAndView(myPluginPath + "SlackNotification/ajaxEdit.jsp", params);
227270
}
271+
272+
273+
private int convertToInt(String s, int defaultValue){
274+
try{
275+
int myInt = Integer.parseInt(s);
276+
return myInt;
277+
} catch (NumberFormatException e){
278+
return defaultValue;
279+
}
280+
}
228281
}

tcslackbuildnotifier-web-ui/src/main/java/slacknotifications/teamcity/extension/SlackNotificationAjaxSettingsListPageController.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import slacknotifications.teamcity.extension.bean.ProjectSlackNotificationsBean;
1414
import slacknotifications.teamcity.extension.bean.ProjectSlackNotificationsBeanJsonSerialiser;
1515
import slacknotifications.teamcity.payload.SlackNotificationPayloadManager;
16+
import slacknotifications.teamcity.settings.SlackNotificationMainSettings;
1617
import slacknotifications.teamcity.settings.SlackNotificationProjectSettings;
1718

1819
import javax.servlet.http.HttpServletRequest;
@@ -22,20 +23,24 @@
2223

2324
public class SlackNotificationAjaxSettingsListPageController extends BaseController {
2425

25-
private final WebControllerManager myWebManager;
26-
private SBuildServer myServer;
27-
private ProjectSettingsManager mySettings;
28-
private PluginDescriptor myPluginDescriptor;
29-
private final SlackNotificationPayloadManager myManager;
26+
private final WebControllerManager myWebManager;
27+
private final SlackNotificationMainSettings myMainSettings;
28+
29+
private SBuildServer myServer;
30+
private ProjectSettingsManager mySettings;
31+
private PluginDescriptor myPluginDescriptor;
32+
private final SlackNotificationPayloadManager myManager;
3033

3134
public SlackNotificationAjaxSettingsListPageController(SBuildServer server, WebControllerManager webManager,
32-
ProjectSettingsManager settings, SlackNotificationPayloadManager manager, PluginDescriptor pluginDescriptor) {
35+
ProjectSettingsManager settings, SlackNotificationPayloadManager manager, PluginDescriptor pluginDescriptor,
36+
SlackNotificationMainSettings mainSettings) {
3337
super(server);
3438
myWebManager = webManager;
3539
myServer = server;
3640
mySettings = settings;
3741
myPluginDescriptor = pluginDescriptor;
3842
myManager = manager;
43+
myMainSettings = mainSettings;
3944
}
4045

4146
public void register(){
@@ -55,15 +60,15 @@ protected ModelAndView doHandle(HttpServletRequest request, HttpServletResponse
5560
SlackNotificationProjectSettings projSettings = (SlackNotificationProjectSettings)
5661
mySettings.getSettings(request.getParameter("projectId"), "slackNotifications");
5762

58-
params.put("projectSlackNotificationsAsJson", ProjectSlackNotificationsBeanJsonSerialiser.serialise(ProjectSlackNotificationsBean.build(projSettings, project)));
63+
params.put("projectSlackNotificationsAsJson", ProjectSlackNotificationsBeanJsonSerialiser.serialise(ProjectSlackNotificationsBean.build(projSettings, project, myMainSettings)));
5964
} else if (request.getParameter("buildTypeId") != null){
6065
SBuildType sBuildType = TeamCityIdResolver.findBuildTypeById(this.myServer.getProjectManager(), request.getParameter("buildTypeId"));
6166
if (sBuildType != null){
6267
SProject project = sBuildType.getProject();
6368
if (project != null){
6469
SlackNotificationProjectSettings projSettings = (SlackNotificationProjectSettings)
6570
mySettings.getSettings(project.getProjectId(), "slackNotifications");
66-
params.put("projectSlackNotificationsAsJson", ProjectSlackNotificationsBeanJsonSerialiser.serialise(ProjectSlackNotificationsBean.build(projSettings, sBuildType, project)));
71+
params.put("projectSlackNotificationsAsJson", ProjectSlackNotificationsBeanJsonSerialiser.serialise(ProjectSlackNotificationsBean.build(projSettings, sBuildType, project, myMainSettings)));
6772
}
6873
}
6974

tcslackbuildnotifier-web-ui/src/main/java/slacknotifications/teamcity/extension/SlackNotificationIndexPageController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ protected ModelAndView doHandle(HttpServletRequest request, HttpServletResponse
9999
if (projSettings.getSlackNotificationsCount() == 0){
100100
params.put("noSlackNotifications", "true");
101101
params.put("slackNotifications", "false");
102-
params.put("projectSlackNotificationsAsJson", ProjectSlackNotificationsBeanJsonSerialiser.serialise(ProjectSlackNotificationsBean.build(projSettings, project)));
102+
params.put("projectSlackNotificationsAsJson", ProjectSlackNotificationsBeanJsonSerialiser.serialise(ProjectSlackNotificationsBean.build(projSettings, project, myMainSettings)));
103103
} else {
104104
params.put("noSlackNotifications", "false");
105105
params.put("slackNotifications", "true");
106106
params.put("slackNotificationList", projSettings.getSlackNotificationsAsList());
107107
params.put("slackNotificationsDisabled", !projSettings.isEnabled());
108108
params.put("slackNotificationsEnabledAsChecked", projSettings.isEnabledAsChecked());
109-
params.put("projectSlackNotificationsAsJson", ProjectSlackNotificationsBeanJsonSerialiser.serialise(ProjectSlackNotificationsBean.build(projSettings, project)));
109+
params.put("projectSlackNotificationsAsJson", ProjectSlackNotificationsBeanJsonSerialiser.serialise(ProjectSlackNotificationsBean.build(projSettings, project, myMainSettings)));
110110

111111
}
112112
} else {
@@ -139,7 +139,7 @@ protected ModelAndView doHandle(HttpServletRequest request, HttpServletResponse
139139
params.put("noSlackNotifications", configs.size() == 0);
140140
params.put("slackNotifications", configs.size() != 0);
141141

142-
params.put("projectSlackNotificationsAsJson", ProjectSlackNotificationsBeanJsonSerialiser.serialise(ProjectSlackNotificationsBean.build(projSettings, sBuildType, project)));
142+
params.put("projectSlackNotificationsAsJson", ProjectSlackNotificationsBeanJsonSerialiser.serialise(ProjectSlackNotificationsBean.build(projSettings, sBuildType, project, myMainSettings)));
143143
}
144144
} else {
145145
params.put("haveProject", "false");

0 commit comments

Comments
 (0)