Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds option to include details about each commit in the message #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 47 additions & 22 deletions src/main/java/jenkins/plugins/hipchat/ActiveNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,29 +79,8 @@ String getChanges(AbstractBuild r) {
logger.info("No change set computed...");
return null;
}
ChangeLogSet changeSet = r.getChangeSet();
List<Entry> entries = new LinkedList<Entry>();
Set<AffectedFile> files = new HashSet<AffectedFile>();
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<String> authors = new HashSet<String>();
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();
}

Expand All @@ -120,6 +99,7 @@ String getBuildStatusMessage(AbstractBuild r) {
MessageBuilder message = new MessageBuilder(notifier, r);
message.appendStatusMessage();
message.appendDuration();
message.appendChanges();
return message.appendOpenLink().toString();
}

Expand All @@ -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<Entry> entries = new LinkedList<Entry>();
Set<AffectedFile> files = new HashSet<AffectedFile>();
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("<br>&nbsp;&nbsp;" + commitMsg
+ " [" + entry.getAuthor().getDisplayName()
+ " / " + fileCount + " file(s)"
+ "]");
}
} else {
Set<String> authors = new HashSet<String>();
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;
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/jenkins/plugins/hipchat/HipChatNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,19 @@ public static class HipChatJobProperty extends hudson.model.JobProperty<Abstract
private boolean notifyNotBuilt;
private boolean notifyUnstable;
private boolean notifyFailure;
private boolean includeChangeDetails;


@DataBoundConstructor
public HipChatJobProperty(String room, boolean startNotification, boolean notifyAborted, boolean notifyFailure, boolean notifyNotBuilt, boolean notifySuccess, boolean notifyUnstable) {
public HipChatJobProperty(String room, boolean startNotification, boolean notifyAborted, boolean notifyFailure, boolean notifyNotBuilt, boolean notifySuccess, boolean notifyUnstable, boolean includeChangeDetails) {
this.room = room;
this.startNotification = startNotification;
this.notifyAborted = notifyAborted;
this.notifyFailure = notifyFailure;
this.notifyNotBuilt = notifyNotBuilt;
this.notifySuccess = notifySuccess;
this.notifyUnstable = notifyUnstable;
this.includeChangeDetails = includeChangeDetails;
}

@Exported
Expand Down Expand Up @@ -219,6 +221,11 @@ public boolean getNotifyUnstable() {
return notifyUnstable;
}

@Exported
public boolean getIncludeChangeDetails() {
return includeChangeDetails;
}

@Extension
public static final class DescriptorImpl extends JobPropertyDescriptor {
public String getDisplayName() {
Expand All @@ -238,7 +245,8 @@ public HipChatJobProperty newInstance(StaplerRequest sr, JSONObject formData) th
sr.getParameter("hipChatNotifyFailure") != null,
sr.getParameter("hipChatNotifyNotBuilt") != null,
sr.getParameter("hipChatNotifySuccess") != null,
sr.getParameter("hipChatNotifyUnstable") != null);
sr.getParameter("hipChatNotifyUnstable") != null,
sr.getParameter("hipChatIncludeChangeDetails") != null);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
<f:textbox name="hipChatProjectRoom" value="${instance.getRoom()}"/>
</f:entry>

<f:entry title="Include Change Details">
<f:checkbox name="hipChatIncludeChangeDetails" value="true" checked="${instance.getIncludeChangeDetails()}"/>
</f:entry>

<f:entry title="Notify Build Start">
<f:checkbox name="hipChatStartNotification" value="true" checked="${instance.getStartNotification()}"/>
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}