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

Token expanded tags #3

Closed
wants to merge 3 commits into from
Closed
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
26 changes: 16 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@
<url>http://repo.jenkins-ci.org/public/</url>
</repository>
<repository>
<id>java.net-m2-repository</id>
<url>http://maven.jenkins-ci.org:8081/content/repositories/releases/
</url>
</repository>
<id>java.net-m2-repository</id>
<url>http://maven.jenkins-ci.org:8081/content/repositories/releases/</url>
</repository>
</repositories>

<pluginRepositories>
Expand All @@ -52,11 +51,18 @@
<url>http://repo.jenkins-ci.org/public/</url>
</pluginRepository>
<pluginRepository>
<id>jenkins-m2-repository</id>
<name>Jenkins Plugin Repository</name>
<url>http://maven.jenkins-
ci.org:8081/content/repositories/releases/</url>
<layout>default</layout>
</pluginRepository>
<id>jenkins-m2-repository</id>
<name>Jenkins Plugin Repository</name>
<url>http://maven.jenkins-ci.org:8081/content/repositories/releases/</url>
<layout>default</layout>
</pluginRepository>
</pluginRepositories>
<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>token-macro</artifactId>
<version>1.10</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>
27 changes: 22 additions & 5 deletions src/main/java/com/flowdock/jenkins/FlowdockNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.flowdock.jenkins.exception.FlowdockException;
import hudson.Extension;
import hudson.Launcher;
import hudson.Plugin;
import hudson.util.FormValidation;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
Expand All @@ -17,6 +18,7 @@
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.QueryParameter;

import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -35,6 +37,7 @@ public class FlowdockNotifier extends Notifier {
private final boolean notifyUnstable;
private final boolean notifyAborted;
private final boolean notifyNotBuilt;
private final Plugin tokenMacroPlugin;

// Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
@DataBoundConstructor
Expand All @@ -60,6 +63,9 @@ public FlowdockNotifier(String flowToken, String notificationTags, String chatNo
this.notifyMap.put(BuildResult.UNSTABLE, this.notifyUnstable);
this.notifyMap.put(BuildResult.ABORTED, this.notifyAborted);
this.notifyMap.put(BuildResult.NOT_BUILT, this.notifyNotBuilt);

// set up optional dependency plugins
tokenMacroPlugin = jenkins.model.Jenkins.getInstance().getPlugin("token-macro");
}

public String getFlowToken() {
Expand Down Expand Up @@ -103,7 +109,7 @@ public boolean needsToRunAfterFinalized() {
}

@Override
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) {
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
BuildResult buildResult = BuildResult.fromBuild(build);
if(shouldNotify(buildResult)) {
notifyFlowdock(build, buildResult, listener);
Expand All @@ -117,18 +123,29 @@ public boolean shouldNotify(BuildResult buildResult) {
return notifyMap.get(buildResult);
}

protected void notifyFlowdock(AbstractBuild build, BuildResult buildResult, BuildListener listener) {
protected void notifyFlowdock(AbstractBuild build, BuildResult buildResult, BuildListener listener) throws IOException, InterruptedException {
PrintStream logger = listener.getLogger();
try {
FlowdockAPI api = new FlowdockAPI(getDescriptor().apiUrl(), flowToken);
TeamInboxMessage msg = TeamInboxMessage.fromBuild(build, buildResult);
msg.setTags(notificationTags);
String expandedTags = null;

if(tokenMacroPlugin != null) {
try {
expandedTags = org.jenkinsci.plugins.tokenmacro.TokenMacro.expandAll(build, listener, notificationTags);
} catch(org.jenkinsci.plugins.tokenmacro.MacroEvaluationException e) {
logger.println("Error substituting with token-macro plugin: " + e);
}
} else {
expandedTags = notificationTags;
}
msg.setTags(expandedTags);
api.pushTeamInboxMessage(msg);
listener.getLogger().println("Flowdock: Team Inbox notification sent successfully");
logger.println("Flowdock: Team Inbox notification sent successfully");

if(build.getResult() != Result.SUCCESS && chatNotification) {
ChatMessage chatMsg = ChatMessage.fromBuild(build, buildResult);
chatMsg.setTags(notificationTags);
chatMsg.setTags(expandedTags);
api.pushChatMessage(chatMsg);
logger.println("Flowdock: Chat notification sent successfully");
}
Expand Down