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

Additional features #6

Open
wants to merge 7 commits 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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Flowdock Plugin for Jenkins
# Jenkins Flowdock Plugin

## Description
Flowdock Plugin is a tool for sending build notification from Jenkins to your flow. It hooks to Post-build Actions of the build,
so you can use with any number of different builds. All you need to configure is the API token of the flow where you want the notifications
to go. See [Tokens](https://www.flowdock.com/account/tokens) page for list of your API tokens.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.506</version><!-- which version of Jenkins is this plugin built against? -->
<version>1.580</version><!-- which version of Jenkins is this plugin built against? -->
</parent>

<groupId>com.flowdock.jenkins</groupId>
Expand Down
29 changes: 24 additions & 5 deletions src/main/java/com/flowdock/jenkins/ChatMessage.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.flowdock.jenkins;

import hudson.model.AbstractBuild;
import hudson.model.Cause;
import hudson.model.Hudson;
import java.io.UnsupportedEncodingException;
import java.util.Map;

public class ChatMessage extends FlowdockMessage {
protected String externalUserName;
Expand All @@ -26,13 +28,30 @@ public String asPostData() throws UnsupportedEncodingException {
public static ChatMessage fromBuild(AbstractBuild build, BuildResult buildResult) {
ChatMessage msg = new ChatMessage();
StringBuffer content = new StringBuffer();
String buildNo = build.getDisplayName().replaceAll("#", "");
content.append(build.getProject().getName()).append(" build ").append(buildNo);
content.append(" ").append(buildResult.getHumanResult());

Map<String,String> buildVars = build.getBuildVariables();
String rootUrl = Hudson.getInstance().getRootUrl();
String buildLink = (rootUrl == null) ? null : rootUrl + build.getUrl();
if(buildLink != null) content.append(" \n").append(buildLink);

Cause buildCause = build.getCause(hudson.triggers.SCMTrigger.SCMTriggerCause.class);
String pushedBy = (buildCause != null && buildCause.getShortDescription() != null ? buildCause.getShortDescription() : "Started by: Not Available").replaceAll("Started by GitHub push by ", "Change Pushed By GitHub UserId: ");

String buildNo = build.getDisplayName().replaceAll("#", "");
content.append(build.getProject().getName())
.append(" #")
.append(buildNo)
.append(" ")
.append(buildResult.getHumanResult().toUpperCase())
.append(" \n");

if (buildVars != null) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on quick testing, buildVars is usually not null but doesn't contain variables. This causes output like: Build Parameters: {} which could be omitted.

content.append("Build Parameters: ")
.append(buildVars.toString())
.append(" \n");
}
content.append("Build Link: ")
.append(buildLink)
.append(" \n")
.append(pushedBy);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to the above point, Started by: Not Available is always visible when not using SCM trigger. Feels a bit redundant especially in the chat message.


msg.setContent(content.toString());
return msg;
Expand Down
24 changes: 20 additions & 4 deletions src/main/java/com/flowdock/jenkins/FlowdockNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import hudson.tasks.Publisher;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import java.io.IOException;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
Expand All @@ -27,6 +28,7 @@ public class FlowdockNotifier extends Notifier {
private final String flowToken;
private final String notificationTags;
private final boolean chatNotification;
private final boolean chatNotificationBuildSuccess;

private final Map<BuildResult, Boolean> notifyMap;
private final boolean notifySuccess;
Expand All @@ -38,12 +40,13 @@ public class FlowdockNotifier extends Notifier {

// Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
@DataBoundConstructor
public FlowdockNotifier(String flowToken, String notificationTags, String chatNotification,
public FlowdockNotifier(String flowToken, String notificationTags, String chatNotification, String chatNotificationBuildSuccess,
String notifySuccess, String notifyFailure, String notifyFixed, String notifyUnstable,
String notifyAborted, String notifyNotBuilt) {
this.flowToken = flowToken;
this.notificationTags = notificationTags;
this.chatNotification = chatNotification != null && chatNotification.equals("true");
this.chatNotificationBuildSuccess = chatNotificationBuildSuccess != null && chatNotificationBuildSuccess.equals("true");

this.notifySuccess = notifySuccess != null && notifySuccess.equals("true");
this.notifyFailure = notifyFailure != null && notifyFailure.equals("true");
Expand Down Expand Up @@ -74,6 +77,10 @@ public boolean getChatNotification() {
return chatNotification;
}

public boolean getChatNotificationBuildSuccess() {
return chatNotificationBuildSuccess;
}

public boolean getNotifySuccess() {
return notifySuccess;
}
Expand Down Expand Up @@ -121,20 +128,29 @@ protected void notifyFlowdock(AbstractBuild build, BuildResult buildResult, Buil
PrintStream logger = listener.getLogger();
try {
FlowdockAPI api = new FlowdockAPI(getDescriptor().apiUrl(), flowToken);
String tags = build.getEnvironment(listener).expand(notificationTags);
TeamInboxMessage msg = TeamInboxMessage.fromBuild(build, buildResult);
msg.setTags(notificationTags);

msg.setTags(tags);
api.pushTeamInboxMessage(msg);
listener.getLogger().println("Flowdock: Team Inbox notification sent successfully");

if(build.getResult() != Result.SUCCESS && chatNotification) {
if ((build.getResult() != Result.SUCCESS && chatNotification) || (build.getResult() == Result.SUCCESS && chatNotificationBuildSuccess)) {
ChatMessage chatMsg = ChatMessage.fromBuild(build, buildResult);
chatMsg.setTags(notificationTags);

chatMsg.setTags(tags);
api.pushChatMessage(chatMsg);
logger.println("Flowdock: Chat notification sent successfully");
}
} catch(FlowdockException ex) {
logger.println("Flowdock: failed to send notification");
logger.println("Flowdock: " + ex.getMessage());
} catch (IOException ex) {
logger.println("Flowdock: failed to send notification");
logger.println("Flowdock: " + ex.getMessage());
} catch (InterruptedException ex) {
logger.println("Flowdock: failed to send notification");
logger.println("Flowdock: " + ex.getMessage());
}
}

Expand Down
20 changes: 12 additions & 8 deletions src/main/java/com/flowdock/jenkins/TeamInboxMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import hudson.scm.ChangeLogSet;
import hudson.scm.ChangeLogSet.Entry;
import java.io.UnsupportedEncodingException;
import java.util.Map;

public class TeamInboxMessage extends FlowdockMessage {

Expand Down Expand Up @@ -73,25 +74,28 @@ public static TeamInboxMessage fromBuild(AbstractBuild build, BuildResult buildR
TeamInboxMessage msg = new TeamInboxMessage();
msg.setProject(build.getProject().getName().replaceAll("[^a-zA-Z0-9\\-_ ]", ""));
String buildNo = build.getDisplayName().replaceAll("#", "");
msg.setSubject(build.getProject().getName() + " build " + buildNo + " " + buildResult.getHumanResult());

Map<String,String> buildVars = build.getBuildVariables();
String rootUrl = Hudson.getInstance().getRootUrl();
String buildLink = (rootUrl == null) ? null : rootUrl + build.getUrl();
if(buildLink != null) msg.setLink(buildLink);

msg.setSubject(build.getProject().getName() + " #" + buildNo + " " + buildResult.getHumanResult().toUpperCase());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why .toUpperCase()? I would prefer leaving this as it was.



if(build.getResult().isWorseThan(Result.SUCCESS))
msg.setFromAddress(FLOWDOCK_BUILD_FAIL_EMAIL);

StringBuffer content = new StringBuffer();
content.append("<h2>").append(build.getProject().getName()).append("</h2>");
content.append("Build: ").append(build.getDisplayName()).append("<br />");
content.append("Result: ").append(buildResult.toString()).append("<br />");
if(buildLink != null)
content.append("URL: <a href=\"").append(buildLink).append("\">").append(buildLink).append("</a>").append("<br />");
if (buildVars != null) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for removing everything but build parameters here? After this change the inbox messages are almost empty.

content.append("Build Parameters: ")
.append(buildVars.toString())
.append("<br/><br/>&nbsp;&nbsp;");
}

List<Entry> commits = parseCommits(build);
if(commits != null) {
content.append("<h3>Changes</h3><div class=\"commits\"><ul class=\"commit-list clean\">");
content.append("<h3> Changes</h3><div class=\"commits\"><ul class=\"commit-list clean\">");
for(Entry commit : commits) {
content.append("<li class=\"commit\"><span class=\"commit-details\">");
content.append("<span class=\"author-info\">").
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
<f:checkbox />
</f:entry>

<f:entry title="Chat notification when build succeeds" field="chatNotificationBuildSuccess">
<f:checkbox />
</f:entry>

<f:section title="Notify with following build statuses">
<f:entry title="Success" field="notifySuccess">
<f:checkbox default="true" value="true" />
Expand Down