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

Commit

Permalink
Merge pull request #36 from KocproZ/unstable-builds
Browse files Browse the repository at this point in the history
Unstable builds and bugfixes
  • Loading branch information
KocproZ authored Sep 24, 2018
2 parents 36e8325 + e21b4cf commit 86b3085
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 27 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Discord Notifier supports Jenkins Pipeline. The only required parameter is webho
- The text in footer of the message.
- successful
- True makes the left-hand side of the embed green, false sets it to red.
- unstable
- True makes the left-hand side of the embed green (Only when successful: true).

### Example

Expand All @@ -74,7 +76,7 @@ pipeline {
post {
always {
discordSend description: 'Jenkins Pipeline Build', footer: 'Footer Text', link: env.BUILD_URL, successful: currentBuild.resultIsBetterOrEqualTo('SUCCESS'), title: JOB_NAME, webhookURL: 'Webhook URL'
discordSend description: 'Jenkins Pipeline Build', footer: 'Footer Text', link: env.BUILD_URL, successful: currentBuild.resultIsBetterOrEqualTo('SUCCESS'), unstable: false, title: JOB_NAME, webhookURL: 'Webhook URL'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import javax.annotation.Nonnull;
import javax.inject.Inject;

import static nz.co.jammehcow.jenkinsdiscord.DiscordWebhook.*;

public class DiscordPipelineStep extends AbstractStepImpl {
private final String webhookURL;

Expand All @@ -23,6 +25,7 @@ public class DiscordPipelineStep extends AbstractStepImpl {
private String image;
private String thumbnail;
private boolean successful;
private boolean unstable; //Backwards compatibility

@DataBoundConstructor
public DiscordPipelineStep(String webhookURL) {
Expand Down Expand Up @@ -78,6 +81,15 @@ public void setSuccessful(boolean successful) {
this.successful = successful;
}

public boolean isUnstable() {
return unstable;
}

@DataBoundSetter
public void setUnstable(boolean unstable) {
this.unstable = unstable;
}

@DataBoundSetter
public void setImage(String url) {
this.image = url;
Expand Down Expand Up @@ -107,21 +119,37 @@ public static class DiscordPipelineStepExecution extends AbstractSynchronousNonB
protected Void run() throws Exception {
listener.getLogger().println("Sending notification to Discord.");

DiscordWebhook.StatusColor statusColor = DiscordWebhook.StatusColor.YELLOW;
if (step.isSuccessful()) statusColor = DiscordWebhook.StatusColor.GREEN;
if (step.isSuccessful() && step.isUnstable()) statusColor = DiscordWebhook.StatusColor.YELLOW;
if (!step.isSuccessful() && !step.isUnstable()) statusColor = DiscordWebhook.StatusColor.RED;

DiscordWebhook wh = new DiscordWebhook(step.getWebhookURL());
wh.setTitle(step.getTitle());
wh.setTitle(checkLimitAndTruncate("title", step.getTitle(), TITLE_LIMIT));
wh.setURL(step.getLink());
wh.setThumbnail(step.getThumbnail());
wh.setDescription(step.getDescription());
wh.setDescription(checkLimitAndTruncate("description", step.getDescription(), DESCRIPTION_LIMIT));
wh.setImage(step.getImage());
wh.setFooter(step.getFooter());
wh.setStatus(step.isSuccessful());
wh.setFooter(checkLimitAndTruncate("footer", step.getFooter(), FOOTER_LIMIT));
wh.setStatus(statusColor);

try { wh.send(); }
catch (WebhookException e) { e.printStackTrace(listener.getLogger()); }

return null;
}

private String checkLimitAndTruncate(String fieldName, String value, int limit) {
if (value.length() > limit) {
listener.getLogger().printf("Warning: '%s' field has more than %d characters (%d). It will be truncated.%n",
fieldName,
limit,
value.length());
return value.substring(0, limit);
}
return value;
}

private static final long serialVersionUID = 1L;
}

Expand Down
41 changes: 22 additions & 19 deletions src/main/java/nz/co/jammehcow/jenkinsdiscord/DiscordWebhook.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package nz.co.jammehcow.jenkinsdiscord;

import com.google.common.primitives.UnsignedInteger;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.JsonNode;
import com.mashape.unirest.http.Unirest;
Expand All @@ -19,25 +18,37 @@ class DiscordWebhook {
private JSONObject obj;
private JSONObject embed;

private enum Color {
static final int TITLE_LIMIT = 256;
static final int DESCRIPTION_LIMIT = 2048;
static final int FOOTER_LIMIT = 2048;

enum StatusColor {
/**
* Green "you're sweet as" color.
*/
GREEN(1681177), /**
GREEN(1681177),
/**
* Yellow "go, but I'm watching you" color.
*/
YELLOW(16776970),
/**
* Red "something ain't right" color.
*/
RED(11278871);

private UnsignedInteger code;
Color(int code) { this.code = UnsignedInteger.asUnsigned(code); }
private long code;

StatusColor(int code) {
this.code = code;
}
}

/**
* Instantiates a new Discord webhook.
*
* @param url the webhook URL
*/
public DiscordWebhook(String url) {
DiscordWebhook(String url) {
this.webhookUrl = url;
this.obj = new JSONObject();
this.obj.put("username", "Jenkins");
Expand All @@ -56,17 +67,6 @@ 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 All @@ -84,8 +84,8 @@ public DiscordWebhook setURL(String buildUrl) {
* @param isSuccess if the build is successful
* @return this
*/
public DiscordWebhook setStatus(boolean isSuccess) {
this.embed.put("color", (isSuccess) ? Color.GREEN.code : Color.RED.code);
public DiscordWebhook setStatus(StatusColor isSuccess) {
this.embed.put("color", isSuccess.code);
return this;
}

Expand Down Expand Up @@ -141,6 +141,9 @@ public DiscordWebhook setFooter(String text) {
* @throws WebhookException the webhook exception
*/
public void send() throws WebhookException {
if (this.embed.toString().length() > 6000)
throw new WebhookException("Embed object larger than the limit (" + this.embed.toString().length() + ">6000).");

this.obj.put("embeds", new JSONArray().put(this.embed));

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public String getThumbnailURL() {
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();
JenkinsLocationConfiguration globalConfig = JenkinsLocationConfiguration.get();

// Create a new webhook payload
DiscordWebhook wh = new DiscordWebhook(env.expand(this.webhookURL));
Expand All @@ -92,7 +92,12 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
}
}

boolean buildStatus = build.getResult().isBetterOrEqualTo(Result.SUCCESS);
DiscordWebhook.StatusColor statusColor = DiscordWebhook.StatusColor.GREEN;
Result buildresult = build.getResult();
if (!buildresult.isCompleteBuild()) return true;
if (buildresult.isBetterOrEqualTo(Result.SUCCESS)) statusColor = DiscordWebhook.StatusColor.GREEN;
if (buildresult.isWorseThan(Result.SUCCESS)) statusColor = DiscordWebhook.StatusColor.YELLOW;
if (buildresult.isWorseThan(Result.UNSTABLE)) statusColor = DiscordWebhook.StatusColor.RED;

if (!this.statusTitle.isEmpty()) {
wh.setTitle(env.expand(this.statusTitle));
Expand Down Expand Up @@ -127,7 +132,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis

wh.setThumbnail(thumbnailURL);
wh.setDescription(new EmbedDescription(build, globalConfig, descriptionPrefix, this.enableArtifactList).toString());
wh.setStatus(buildStatus);
wh.setStatus(statusColor);

if (this.enableFooterInfo) wh.setFooter("Jenkins v" + build.getHudsonVersion() + ", " + getDescriptor().getDisplayName() + " v" + getDescriptor().getVersion());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@
<f:entry title="Successful Build" field="successful">
<f:checkbox />
</f:entry>
<f:entry title="Unstable Build" field="unstable">
<f:checkbox />
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Set to true to set the color to yellow (only when the build was successful, else this is omitted).
</div>

0 comments on commit 86b3085

Please sign in to comment.