Skip to content
This repository was archived by the owner on Feb 13, 2020. It is now read-only.

Commit

Permalink
Extract support class
Browse files Browse the repository at this point in the history
  • Loading branch information
ikikko committed Feb 11, 2015
1 parent 4e898c6 commit bf24a39
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import hudson.tasks.BuildWrapperDescriptor;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.typetalk.api.Typetalk;
import org.jenkinsci.plugins.typetalk.api.TypetalkMessage;
import org.jenkinsci.plugins.typetalk.support.Emoji;
import org.jenkinsci.plugins.typetalk.support.TypetalkMessage;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;
Expand Down Expand Up @@ -41,8 +42,8 @@ public Environment setUp(AbstractBuild build, Launcher launcher, BuildListener l

String message;
if (StringUtils.isBlank(notifyStartMessage)) {
TypetalkMessage typetalkMessage = new TypetalkMessage(TypetalkMessage.Emoji.LOUDSPEAKER, "Build start");
message = typetalkMessage.messageWithBuildInfo(build);
TypetalkMessage typetalkMessage = new TypetalkMessage(Emoji.LOUDSPEAKER, "Build start");
message = typetalkMessage.buildMessageWithBuild(build);
} else{
message = build.getEnvironment(listener).expand(notifyStartMessage);
}
Expand All @@ -59,8 +60,8 @@ public boolean tearDown(AbstractBuild build, BuildListener listener) throws IOEx

String message;
if (StringUtils.isBlank(notifyEndMessage)) {
TypetalkMessage typetalkMessage = new TypetalkMessage(TypetalkMessage.Emoji.MEGA, "Build end");
message = typetalkMessage.messageWithBuildInfo(build);
TypetalkMessage typetalkMessage = new TypetalkMessage(Emoji.MEGA, "Build end");
message = typetalkMessage.buildMessageWithBuild(build);
} else {
message = build.getEnvironment(listener).expand(notifyEndMessage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import hudson.util.Secret;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.typetalk.api.Typetalk;
import org.jenkinsci.plugins.typetalk.api.TypetalkMessage;
import org.jenkinsci.plugins.typetalk.support.ResultSupport;
import org.jenkinsci.plugins.typetalk.support.TypetalkMessage;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

Expand Down Expand Up @@ -47,8 +48,8 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

listener.getLogger().println("Notifying build result to Typetalk...");

TypetalkMessage typetalkMessage = TypetalkMessage.convertFromResult(build);
String message = typetalkMessage.messageWithBuildInfo(build);
TypetalkMessage typetalkMessage = new ResultSupport().convertBuildToMessage(build);
String message = typetalkMessage.buildMessageWithBuild(build);
Long topicId = Long.valueOf(topicNumber);

Typetalk.createFromName(name).postMessage(topicId, message);
Expand Down
10 changes: 1 addition & 9 deletions src/main/java/org/jenkinsci/plugins/typetalk/api/Typetalk.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,12 @@
import com.google.api.client.auth.oauth2.ClientCredentialsTokenRequest;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.auth.oauth2.TokenResponse;
import com.google.api.client.http.BasicAuthentication;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpContent;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.*;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.http.json.JsonHttpContent;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.JsonObjectParser;
import com.google.api.client.json.jackson.JacksonFactory;
import hudson.model.AbstractBuild;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.typetalk.TypetalkNotifier;
Expand Down
117 changes: 0 additions & 117 deletions src/main/java/org/jenkinsci/plugins/typetalk/api/TypetalkMessage.java

This file was deleted.

23 changes: 23 additions & 0 deletions src/main/java/org/jenkinsci/plugins/typetalk/support/Emoji.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.jenkinsci.plugins.typetalk.support;

public enum Emoji {
LOUDSPEAKER(":loudspeaker:"),
MEGA(":mega:"),
ASTONISHED(":astonished:"),
RAGE(":rage:"),
CRY(":cry:"),
SMILEY(":smiley:"),
MASK(":mask:"),
BOOK(":book:"),
PAGE_FACING_UP(":page_facing_up:");

private String symbol;

public String getSymbol() {
return symbol;
}

Emoji(String symbol) {
this.symbol = symbol;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.jenkinsci.plugins.typetalk.support;

import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Result;

public class ResultSupport {

public TypetalkMessage convertBuildToMessage(AbstractBuild build) {
if (build.getResult().equals(Result.ABORTED)) {
return new TypetalkMessage(Emoji.ASTONISHED, "Build aborted");
} else if (build.getResult().equals(Result.NOT_BUILT)) {
return new TypetalkMessage(Emoji.ASTONISHED, "Not built");
} else if (build.getResult().equals(Result.FAILURE)) {
return new TypetalkMessage(Emoji.RAGE, "Build failure");
} else if (build.getResult().equals(Result.UNSTABLE)) {
return new TypetalkMessage(Emoji.CRY, "Build unstable");
} else if (build.getResult().equals(Result.SUCCESS)) {
if (recoverSuccess(build)) {
return new TypetalkMessage(Emoji.SMILEY, "Build recovery");
} else {
return new TypetalkMessage(Emoji.SMILEY, "Build success");
}
}

throw new IllegalArgumentException("Unknown build result.");
}

public boolean recoverSuccess(AbstractBuild build) {
if (build.getPreviousBuild() == null) {
return false;
} else {
return build.getResult().equals(Result.SUCCESS)
&& build.getPreviousBuild().getResult().isWorseThan(Result.SUCCESS);
}
}

public Emoji convertProjectToEmoji(AbstractProject project) {
switch (project.getIconColor()) {
case RED:
case RED_ANIME:
return Emoji.RAGE;
case YELLOW:
case YELLOW_ANIME:
return Emoji.CRY;
case BLUE:
case BLUE_ANIME:
return Emoji.SMILEY;
case DISABLED:
case DISABLED_ANIME:
case NOTBUILT:
case NOTBUILT_ANIME:
return Emoji.MASK;
default:
return Emoji.ASTONISHED;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.jenkinsci.plugins.typetalk.support;

import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;

public class TypetalkMessage {

private final Emoji emoji;
private final String message;

public Emoji getEmoji() {
return emoji;
}

public String getMessage() {
return message;
}

public TypetalkMessage(Emoji emoji, String message) {
this.emoji = emoji;
this.message = message;
}

public String buildMessageWithBuild(AbstractBuild<?, ?> build) {
final String rootUrl = Jenkins.getInstance().getRootUrl();
if (StringUtils.isEmpty(rootUrl)) {
throw new IllegalStateException("Root URL isn't configured yet. Cannot compute absolute URL.");
}

final StringBuilder builder = new StringBuilder();
builder.append(emoji.getSymbol());
builder.append(" ");
builder.append(message);
builder.append(" [ ");
builder.append(build.getProject().getDisplayName());
builder.append(" ]");
builder.append("\n");
builder.append(rootUrl);
builder.append(build.getUrl());

return builder.toString();
}

public String buildMessageWithProject(AbstractProject project) {
final String rootUrl = Jenkins.getInstance().getRootUrl();
if (StringUtils.isEmpty(rootUrl)) {
throw new IllegalStateException("Root URL isn't configured yet. Cannot compute absolute URL.");
}

final StringBuilder builder = new StringBuilder();
builder.append(emoji.getSymbol());
builder.append(" ");
builder.append(message);
builder.append("\n");
builder.append(rootUrl);
if (project != null) {
builder.append(project.getUrl());
}

return builder.toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.jenkinsci.plugins.typetalk.webhookaction;

import hudson.model.AbstractProject;
import org.jenkinsci.plugins.typetalk.api.TypetalkMessage;
import org.jenkinsci.plugins.typetalk.support.Emoji;

import java.util.List;

Expand All @@ -23,13 +23,13 @@ public String getMessage() {
return message;
}

private TypetalkMessage.Emoji emoji;
private Emoji emoji;

public TypetalkMessage.Emoji getEmoji() {
return emoji != null ? emoji : TypetalkMessage.Emoji.SMILEY;
public Emoji getEmoji() {
return emoji != null ? emoji : Emoji.SMILEY;
}

public void setEmoji(TypetalkMessage.Emoji emoji) {
public void setEmoji(Emoji emoji) {
this.emoji = emoji;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.jenkinsci.plugins.typetalk.webhookaction;

import net.sf.json.JSONObject;
import org.jenkinsci.plugins.typetalk.api.TypetalkMessage;
import org.jenkinsci.plugins.typetalk.support.Emoji;
import org.jenkinsci.plugins.typetalk.support.TypetalkMessage;
import org.kohsuke.stapler.StaplerResponse;

import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -30,7 +31,7 @@ protected void output(ResponseParameter parameter) {
}

protected void outputError(ResponseParameter parameter) {
parameter.setEmoji(TypetalkMessage.Emoji.CRY);
parameter.setEmoji(Emoji.CRY);
outputInternal(Level.WARNING, parameter);
}

Expand All @@ -50,7 +51,7 @@ private String buildResponseMessage(ResponseParameter parameter) {
JSONObject jsonObject = new JSONObject();

TypetalkMessage typetalkMessage = new TypetalkMessage(parameter.getEmoji(), parameter.getMessage());
jsonObject.element("message", typetalkMessage.messageWithProjectInfo(parameter.getProject()));
jsonObject.element("message", typetalkMessage.buildMessageWithProject(parameter.getProject()));
jsonObject.element("replyTo", req.getPostId());

return jsonObject.toString();
Expand Down
Loading

0 comments on commit bf24a39

Please sign in to comment.