Skip to content

Commit

Permalink
Adds commit messages in build as a field in the slack post. Has the p…
Browse files Browse the repository at this point in the history
…otential to be too messy / big perhaps
  • Loading branch information
jasonconnery committed May 31, 2018
1 parent f53b9be commit 88fb390
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ this will generate a zip file with the right meta data in the right folder struc

# Install Plugin

Copy the zip file into TeamCity plugin directory inside the data directory, usually `.BuildServer`
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`

```
scp build/distributions/TCSlackNotifierPlugin-<version>.zip buildserver:.BuildServer/plugins/
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ apply plugin: 'java'


sourceCompatibility = 1.8
version = '3.0.0'
version = '4.0.2'

configurations {
deploy
Expand Down
39 changes: 35 additions & 4 deletions src/main/java/com/tapadoo/slacknotifier/SlackServerAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import jetbrains.buildServer.serverSide.settings.ProjectSettingsManager;
import jetbrains.buildServer.users.SUser;
import jetbrains.buildServer.users.UserSet;
import jetbrains.buildServer.vcs.SVcsModification;
import jetbrains.buildServer.vcs.SelectPrevBuildPolicy;
import jetbrains.buildServer.vcs.VcsRoot;
import org.joda.time.Duration;
Expand All @@ -22,6 +23,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.List;


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

List<SVcsModification> changes = build.getChanges(SelectPrevBuildPolicy.SINCE_LAST_SUCCESSFULLY_FINISHED_BUILD, true);

StringBuilder commitMessage = new StringBuilder();

/*
* 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
*
*/
for ( int i = 0 ; i < changes.size() ; i++ ){
SVcsModification modification = changes.get(i);
String desc = modification.getDescription();
commitMessage.append(desc);

if( i < changes.size() - 1 ) {
commitMessage.append("\n------\n");
}
}

for( SUser committer : committers.getUsers() )
{
if( committer != null)
Expand All @@ -204,7 +224,7 @@ else if( projectSettings != null && projectSettings.getChannel() != null && proj
committersString.deleteCharAt(committersString.length()-1); //remove the last ,
}

String commitMsg = committersString.toString();
String committersMsg = committersString.toString();


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

StringBuilder fallbackMessage = new StringBuilder();

if( commitMsg.length() > 0 )
if( committersMsg.length() > 0 )
{
JsonObject field = new JsonObject() ;

field.addProperty("title","Changes By");
field.addProperty("value",commitMsg);
field.addProperty("value",committersMsg);
field.addProperty("short", true);

fields.add(field);

fallbackMessage.append("Changes by ");
fallbackMessage.append(commitMsg);
fallbackMessage.append(committersMsg);
fallbackMessage.append(" ");

}
Expand Down Expand Up @@ -294,6 +314,17 @@ else if( projectSettings != null && projectSettings.getChannel() != null && proj
fallbackMessage.append(issueIds.toString());
}

/* TODO: Consider adding a setting to SlackProjectSettings perhaps to enable / disable commit messages at a project level? */
if( commitMessage.length() > 0 ) {
JsonObject field = new JsonObject() ;

field.addProperty("title", "Commit Messages");
field.addProperty("value",commitMessage.toString());
field.addProperty("short", false);

fields.add(field);
}

attachment.addProperty("color", (goodColor ? "good" : "danger"));


Expand Down
2 changes: 1 addition & 1 deletion teamcity-plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<info>
<name>slackNotifier</name> <!-- the name of plugin used in teamcity -->
<display-name>Slack Notifier</display-name>
<version>3.0.0</version>
<version>4.0.2</version>
<description>Post build success notifications to Slack</description>
<vendor>
<name>Tapadoo</name>
Expand Down

0 comments on commit 88fb390

Please sign in to comment.