diff --git a/src/main/java/jenkins/plugins/hipchat/ActiveNotifier.java b/src/main/java/jenkins/plugins/hipchat/ActiveNotifier.java index c0918b3..2d05188 100644 --- a/src/main/java/jenkins/plugins/hipchat/ActiveNotifier.java +++ b/src/main/java/jenkins/plugins/hipchat/ActiveNotifier.java @@ -79,29 +79,8 @@ String getChanges(AbstractBuild r) { logger.info("No change set computed..."); return null; } - ChangeLogSet changeSet = r.getChangeSet(); - List entries = new LinkedList(); - Set files = new HashSet(); - for (Object o : changeSet.getItems()) { - Entry entry = (Entry) o; - logger.info("Entry " + o); - entries.add(entry); - files.addAll(entry.getAffectedFiles()); - } - if (entries.isEmpty()) { - logger.info("Empty change..."); - return null; - } - Set authors = new HashSet(); - for (Entry entry : entries) { - authors.add(entry.getAuthor().getDisplayName()); - } MessageBuilder message = new MessageBuilder(notifier, r); - message.append("Started by changes from "); - message.append(StringUtils.join(authors, ", ")); - message.append(" ("); - message.append(files.size()); - message.append(" file(s) changed)"); + message.appendChanges(); return message.appendOpenLink().toString(); } @@ -120,6 +99,7 @@ String getBuildStatusMessage(AbstractBuild r) { MessageBuilder message = new MessageBuilder(notifier, r); message.appendStatusMessage(); message.appendDuration(); + message.appendChanges(); return message.appendOpenLink().toString(); } @@ -135,6 +115,51 @@ public MessageBuilder(HipChatNotifier notifier, AbstractBuild build) { startMessage(); } + public void appendChanges() { + AbstractProject project = build.getProject(); + HipChatNotifier.HipChatJobProperty jobProperty = project.getProperty(HipChatNotifier.HipChatJobProperty.class); + + ChangeLogSet changeSet = build.getChangeSet(); + + if (changeSet.isEmptySet()) { + logger.info("Empty change..."); + return; + } + + List entries = new LinkedList(); + Set files = new HashSet(); + for (Object o : changeSet.getItems()) { + Entry entry = (Entry) o; + logger.info("Entry " + o); + entries.add(entry); + files.addAll(entry.getAffectedFiles()); + } + + if (jobProperty != null && jobProperty.getIncludeChangeDetails()) { + message.append(" Started by changes:"); + for (Entry entry : entries) { + final String commitMsg = entry.getMsg().length() > 45 + ? entry.getMsg().substring(0, 45) + "..." + : entry.getMsg(); + final Integer fileCount = entry.getAffectedFiles().size(); + message.append("
  " + commitMsg + + " [" + entry.getAuthor().getDisplayName() + + " / " + fileCount + " file(s)" + + "]"); + } + } else { + Set authors = new HashSet(); + for (Entry entry : entries) { + authors.add(entry.getAuthor().getDisplayName()); + } + message.append(" Started by changes from "); + message.append(StringUtils.join(authors, ", ")); + message.append(" ("); + message.append(files.size()); + message.append(" file(s) changed)"); + } + } + public MessageBuilder appendStatusMessage() { message.append(getStatusMessage(build)); return this; diff --git a/src/main/java/jenkins/plugins/hipchat/HipChatNotifier.java b/src/main/java/jenkins/plugins/hipchat/HipChatNotifier.java index e0f83b3..208d6f1 100644 --- a/src/main/java/jenkins/plugins/hipchat/HipChatNotifier.java +++ b/src/main/java/jenkins/plugins/hipchat/HipChatNotifier.java @@ -157,10 +157,11 @@ public static class HipChatJobProperty extends hudson.model.JobProperty + + + + diff --git a/src/test/java/jenkins/plugins/hipchat/StandardHipChatServiceTest.java b/src/test/java/jenkins/plugins/hipchat/StandardHipChatServiceTest.java index d771b3b..ba238a0 100644 --- a/src/test/java/jenkins/plugins/hipchat/StandardHipChatServiceTest.java +++ b/src/test/java/jenkins/plugins/hipchat/StandardHipChatServiceTest.java @@ -1,18 +1,40 @@ package jenkins.plugins.hipchat; +import java.util.logging.Level; +import java.util.logging.Logger; + import jenkins.plugins.hipchat.StandardHipChatService; + +import org.junit.After; import org.junit.Before; import org.junit.Test; public class StandardHipChatServiceTest { + private Level loggingLevel; + + @Before + public void saveLoggingLevel() { + loggingLevel = Logger.getLogger(StandardHipChatService.class.getName()).getLevel(); + } + /** * Publish should generally not rethrow exceptions, or it will cause a build job to fail at end. */ @Test public void publishWithBadHostShouldNotRethrowExceptions() { + dropLoggingLevel(); StandardHipChatService service = new StandardHipChatService("token", "room", "from"); service.setHost("hostvaluethatwillcausepublishtofail"); service.publish("message"); } + + private void dropLoggingLevel() { + Logger.getLogger(StandardHipChatService.class.getName()).setLevel(Level.SEVERE); + } + + @After + public void restoreLoggingLevel() { + Logger.getLogger(StandardHipChatService.class.getName()).setLevel(loggingLevel); + } }