Skip to content

Commit 88fb390

Browse files
committed
Adds commit messages in build as a field in the slack post. Has the potential to be too messy / big perhaps
1 parent f53b9be commit 88fb390

4 files changed

Lines changed: 38 additions & 7 deletions

File tree

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ this will generate a zip file with the right meta data in the right folder struc
1414

1515
# Install Plugin
1616

17-
Copy the zip file into TeamCity plugin directory inside the data directory, usually `.BuildServer`
17+
In the latest version of teamcity, the plugin zip can be uploaded via the admin page. In older versions, deploy manually by copying the zip file into TeamCity plugin directory inside the data directory, usually `.BuildServer`
1818

1919
```
2020
scp build/distributions/TCSlackNotifierPlugin-<version>.zip buildserver:.BuildServer/plugins/

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ apply plugin: 'java'
1111

1212

1313
sourceCompatibility = 1.8
14-
version = '3.0.0'
14+
version = '4.0.2'
1515

1616
configurations {
1717
deploy

src/main/java/com/tapadoo/slacknotifier/SlackServerAdapter.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import jetbrains.buildServer.serverSide.settings.ProjectSettingsManager;
1010
import jetbrains.buildServer.users.SUser;
1111
import jetbrains.buildServer.users.UserSet;
12+
import jetbrains.buildServer.vcs.SVcsModification;
1213
import jetbrains.buildServer.vcs.SelectPrevBuildPolicy;
1314
import jetbrains.buildServer.vcs.VcsRoot;
1415
import org.joda.time.Duration;
@@ -22,6 +23,7 @@
2223
import java.net.MalformedURLException;
2324
import java.net.URL;
2425
import java.util.Collection;
26+
import java.util.List;
2527

2628

2729
/**
@@ -181,6 +183,24 @@ else if( projectSettings != null && projectSettings.getChannel() != null && proj
181183
UserSet<SUser> committers = build.getCommitters(SelectPrevBuildPolicy.SINCE_LAST_BUILD);
182184
StringBuilder committersString = new StringBuilder();
183185

186+
List<SVcsModification> changes = build.getChanges(SelectPrevBuildPolicy.SINCE_LAST_SUCCESSFULLY_FINISHED_BUILD, true);
187+
188+
StringBuilder commitMessage = new StringBuilder();
189+
190+
/*
191+
* If this field starts to feel too long in slack, we should only use the first item in the array, which would be the latest change
192+
*
193+
*/
194+
for ( int i = 0 ; i < changes.size() ; i++ ){
195+
SVcsModification modification = changes.get(i);
196+
String desc = modification.getDescription();
197+
commitMessage.append(desc);
198+
199+
if( i < changes.size() - 1 ) {
200+
commitMessage.append("\n------\n");
201+
}
202+
}
203+
184204
for( SUser committer : committers.getUsers() )
185205
{
186206
if( committer != null)
@@ -204,7 +224,7 @@ else if( projectSettings != null && projectSettings.getChannel() != null && proj
204224
committersString.deleteCharAt(committersString.length()-1); //remove the last ,
205225
}
206226

207-
String commitMsg = committersString.toString();
227+
String committersMsg = committersString.toString();
208228

209229

210230
JsonObject payloadObj = new JsonObject();
@@ -230,18 +250,18 @@ else if( projectSettings != null && projectSettings.getChannel() != null && proj
230250

231251
StringBuilder fallbackMessage = new StringBuilder();
232252

233-
if( commitMsg.length() > 0 )
253+
if( committersMsg.length() > 0 )
234254
{
235255
JsonObject field = new JsonObject() ;
236256

237257
field.addProperty("title","Changes By");
238-
field.addProperty("value",commitMsg);
258+
field.addProperty("value",committersMsg);
239259
field.addProperty("short", true);
240260

241261
fields.add(field);
242262

243263
fallbackMessage.append("Changes by ");
244-
fallbackMessage.append(commitMsg);
264+
fallbackMessage.append(committersMsg);
245265
fallbackMessage.append(" ");
246266

247267
}
@@ -294,6 +314,17 @@ else if( projectSettings != null && projectSettings.getChannel() != null && proj
294314
fallbackMessage.append(issueIds.toString());
295315
}
296316

317+
/* TODO: Consider adding a setting to SlackProjectSettings perhaps to enable / disable commit messages at a project level? */
318+
if( commitMessage.length() > 0 ) {
319+
JsonObject field = new JsonObject() ;
320+
321+
field.addProperty("title", "Commit Messages");
322+
field.addProperty("value",commitMessage.toString());
323+
field.addProperty("short", false);
324+
325+
fields.add(field);
326+
}
327+
297328
attachment.addProperty("color", (goodColor ? "good" : "danger"));
298329

299330

teamcity-plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<info>
55
<name>slackNotifier</name> <!-- the name of plugin used in teamcity -->
66
<display-name>Slack Notifier</display-name>
7-
<version>3.0.0</version>
7+
<version>4.0.2</version>
88
<description>Post build success notifications to Slack</description>
99
<vendor>
1010
<name>Tapadoo</name>

0 commit comments

Comments
 (0)