Skip to content
This repository has been archived by the owner on Nov 17, 2018. It is now read-only.

Commit

Permalink
Added environment parsing to Webhook Publisher, added field to edit t…
Browse files Browse the repository at this point in the history
…itle, branchname
  • Loading branch information
Gohrel committed Jul 30, 2018
1 parent 549b645 commit 9d89a8c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<groupId>nz.co.jammehcow</groupId>
<artifactId>jenkins-discord</artifactId>
<version>1.1.0</version>
<version>1.1.1</version>
<packaging>hpi</packaging>

<properties>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/nz/co/jammehcow/jenkinsdiscord/DiscordWebhook.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ public DiscordWebhook setTitle(String title) {
return this;
}

/**
* Sets the branch name.
*
* @param name the branch name
* @return this
*/
public DiscordWebhook setBranchName(String branchName) {
this.embed.put("branchName", branchName);
return this;
}

/**
* Sets the embed title url.
*
Expand Down
43 changes: 34 additions & 9 deletions src/main/java/nz/co/jammehcow/jenkinsdiscord/WebhookPublisher.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nz.co.jammehcow.jenkinsdiscord;

import hudson.EnvVars;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
Expand All @@ -14,6 +15,9 @@
import jenkins.model.JenkinsLocationConfiguration;
import nz.co.jammehcow.jenkinsdiscord.exception.WebhookException;
import nz.co.jammehcow.jenkinsdiscord.util.EmbedDescription;

import java.io.IOException;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

Expand All @@ -24,23 +28,29 @@

public class WebhookPublisher extends Notifier {
private final String webhookURL;
private final String branchName;
private final String statusTitle;
private final boolean sendOnStateChange;
private boolean enableUrlLinking;
private final boolean enableArtifactList;
private final boolean enableFooterInfo;
private static final String NAME = "Discord Notifier";
private static final String VERSION = "1.1.0";
private static final String NAME = "Discord Notifier";
private static final String VERSION = "1.1.1";

@DataBoundConstructor
public WebhookPublisher(String webhookURL, boolean sendOnStateChange, boolean enableUrlLinking, boolean enableArtifactList, boolean enableFooterInfo) {
public WebhookPublisher(String webhookURL, boolean sendOnStateChange, String statusTitle, String branchName, boolean enableUrlLinking, boolean enableArtifactList, boolean enableFooterInfo) {
this.webhookURL = webhookURL;
this.sendOnStateChange = sendOnStateChange;
this.enableUrlLinking = enableUrlLinking;
this.enableArtifactList = enableArtifactList;
this.enableFooterInfo = enableFooterInfo;
this.branchName = branchName;
this.statusTitle = statusTitle;
}

public String getWebhookURL() { return this.webhookURL; }
public String getBranchName() { return this.branchName; }
public String getStatusTitle() { return this.statusTitle; }
public boolean isSendOnStateChange() { return this.sendOnStateChange; }
public boolean isEnableUrlLinking() { return this.enableUrlLinking; }
public boolean isEnableArtifactList() { return this.enableArtifactList; }
Expand All @@ -50,12 +60,13 @@ public WebhookPublisher(String webhookURL, boolean sendOnStateChange, boolean en
public boolean needsToRunAfterFinalized() { return true; }

@Override
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) {
// The global configuration, used to fetch the instance url
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
final EnvVars env = build.getEnvironment(listener);
// The global configuration, used to fetch the instance url
JenkinsLocationConfiguration globalConfig = new JenkinsLocationConfiguration();

// Create a new webhook payload
DiscordWebhook wh = new DiscordWebhook(this.webhookURL);
DiscordWebhook wh = new DiscordWebhook(env.expand(this.webhookURL));

if (this.webhookURL.isEmpty()) {
// Stop the plugin from continuing when the webhook URL isn't set. Shouldn't happen due to form validation
Expand All @@ -77,20 +88,34 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
}

boolean buildStatus = build.getResult().isBetterOrEqualTo(Result.SUCCESS);
wh.setTitle(build.getProject().getDisplayName() + " #" + build.getId());

if (!this.statusTitle.isEmpty()) {
wh.setTitle(env.expand(this.statusTitle));
}
else {
wh.setTitle(build.getProject().getDisplayName() + " #" + build.getId());
}


String descriptionPrefix;

String branchNameString ="";
if (!branchName.isEmpty()) {
branchNameString = "**Branch:** "+env.expand(branchName)+"\n";
}

// Adds links to the description and title if enableUrlLinking is enabled
if (this.enableUrlLinking) {
String url = globalConfig.getUrl() + build.getUrl();
descriptionPrefix = "**Build:** "
descriptionPrefix = branchNameString
+"**Build:** "
+ getMarkdownHyperlink(build.getId(), url)
+ "\n**Status:** "
+ getMarkdownHyperlink(build.getResult().toString().toLowerCase(), url);
wh.setURL(url);
} else {
descriptionPrefix = "**Build:** "
descriptionPrefix = branchNameString
+ "**Build:** "
+ build.getId()
+ "\n**Status:** "
+ build.getResult().toString().toLowerCase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
<f:checkbox />
</f:entry>
<f:advanced>
<f:entry title="Title" field="statusTitle">
<f:textbox />
</f:entry>
<f:entry title="Branch name" field="branchName">
<f:textbox />
</f:entry>
<f:entry title="Enable URL linking" field="enableUrlLinking">
<f:checkbox />
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
The branch that was used to build. Leave empty to omit.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
The name that gets displayed on top of the message. Leave empty to use standard naming
</div>

0 comments on commit 9d89a8c

Please sign in to comment.