Skip to content

Commit

Permalink
Merge pull request #31 from Tapadoo/include_commit_message
Browse files Browse the repository at this point in the history
Moved commit message to Attachment
  • Loading branch information
jasonconnery authored May 31, 2018
2 parents fdb7f26 + 0b3a848 commit e398b44
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 32 deletions.
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 = '4.0.2'
version = '4.1.1'

configurations {
deploy
Expand Down
71 changes: 41 additions & 30 deletions src/main/java/com/tapadoo/slacknotifier/SlackServerAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,24 +183,6 @@ 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 Down Expand Up @@ -314,21 +296,14 @@ 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"));
attachment.add("fields",fields);

JsonObject commitAttachment = createCommitAttachment(build);

attachment.add("fields",fields);
if(commitAttachment != null) {
attachmentsObj.add(commitAttachment);
}

if( attachmentsObj.size() > 0 ) {
payloadObj.add("attachments", attachmentsObj);
Expand Down Expand Up @@ -360,4 +335,40 @@ else if( projectSettings != null && projectSettings.getChannel() != null && proj
}
}

private JsonObject createCommitAttachment(SRunningBuild build) {

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("‣ ");
commitMessage.append(desc);

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

if (changes.size() < 1) {
return null;
}

String commitMessageString = commitMessage.toString();
JsonObject attachment = new JsonObject();
attachment.addProperty("title", "Commit Messages");
attachment.addProperty("fallback" , commitMessageString);
attachment.addProperty("text" , commitMessageString);
attachment.addProperty("color" , "#2FA8B9");

return attachment;

}

}
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>4.0.2</version>
<version>4.1.1</version>
<description>Post build success notifications to Slack</description>
<vendor>
<name>Tapadoo</name>
Expand Down

0 comments on commit e398b44

Please sign in to comment.