From 88fb390484fe2ef66bd49018d425c7ddd4669893 Mon Sep 17 00:00:00 2001 From: Jason Connery Date: Thu, 31 May 2018 09:43:56 +0100 Subject: [PATCH] Adds commit messages in build as a field in the slack post. Has the potential to be too messy / big perhaps --- README.markdown | 2 +- build.gradle | 2 +- .../slacknotifier/SlackServerAdapter.java | 39 +++++++++++++++++-- teamcity-plugin.xml | 2 +- 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/README.markdown b/README.markdown index 3f0f8a4..d11c691 100644 --- a/README.markdown +++ b/README.markdown @@ -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-.zip buildserver:.BuildServer/plugins/ diff --git a/build.gradle b/build.gradle index 5923eef..cb3f1a7 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ apply plugin: 'java' sourceCompatibility = 1.8 -version = '3.0.0' +version = '4.0.2' configurations { deploy diff --git a/src/main/java/com/tapadoo/slacknotifier/SlackServerAdapter.java b/src/main/java/com/tapadoo/slacknotifier/SlackServerAdapter.java index 044e284..7fd0991 100644 --- a/src/main/java/com/tapadoo/slacknotifier/SlackServerAdapter.java +++ b/src/main/java/com/tapadoo/slacknotifier/SlackServerAdapter.java @@ -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; @@ -22,6 +23,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.Collection; +import java.util.List; /** @@ -181,6 +183,24 @@ else if( projectSettings != null && projectSettings.getChannel() != null && proj UserSet committers = build.getCommitters(SelectPrevBuildPolicy.SINCE_LAST_BUILD); StringBuilder committersString = new StringBuilder(); + List 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) @@ -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(); @@ -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(" "); } @@ -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")); diff --git a/teamcity-plugin.xml b/teamcity-plugin.xml index ff87e80..8bc51f7 100644 --- a/teamcity-plugin.xml +++ b/teamcity-plugin.xml @@ -4,7 +4,7 @@ slackNotifier Slack Notifier - 3.0.0 + 4.0.2 Post build success notifications to Slack Tapadoo