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

Commit

Permalink
Handle response back
Browse files Browse the repository at this point in the history
  • Loading branch information
ikikko committed Jan 31, 2015
1 parent 109748d commit 96c5c9f
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
@Extension
public class TypetalkWebhookAction implements RootAction {

// TODO handle response back ( if Typetalk supports it )
// TODO enable alias job name to simplify build parameter

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

import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Result;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -87,4 +88,24 @@ public String messageWithBuildInfo(AbstractBuild<?, ?> build) {
return builder.toString();
}

public String messageWithProjectInfo(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.symbol);
builder.append(" ");
builder.append(message);
builder.append("\n");
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,5 +1,8 @@
package org.jenkinsci.plugins.typetalk.webhookaction;

import hudson.model.AbstractProject;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.typetalk.api.TypetalkMessage;
import org.kohsuke.stapler.StaplerResponse;

import javax.servlet.http.HttpServletResponse;
Expand All @@ -24,26 +27,40 @@ protected WebhookExecutor(WebhookRequest req, StaplerResponse rsp, String comman
public abstract void execute();

protected void output(String message) {
outputInternal(Level.INFO, message, HttpServletResponse.SC_OK);
output(message, null);
}

protected void output(String message, AbstractProject project) {
outputInternal(Level.INFO, HttpServletResponse.SC_OK, TypetalkMessage.Emoji.SMILEY, message, project);
}

protected void outputError(String message) {
outputError(message, HttpServletResponse.SC_BAD_REQUEST);
}

protected void outputError(String message, int status) {
outputInternal(Level.WARNING, message, status);
outputInternal(Level.WARNING, status, TypetalkMessage.Emoji.CRY, message, null);
}

private void outputInternal(Level level, String message, int status) {
private void outputInternal(Level level, int status, TypetalkMessage.Emoji emoji, String message, AbstractProject project) {
try {
logger.log(level, message);

rsp.setContentType("text/plain");
rsp.setContentType("application/json");
rsp.setStatus(status);
rsp.getWriter().println(message);
rsp.getWriter().println(buildResponseMessage(emoji, message, project));
} catch (IOException e) {
throw new IllegalStateException(e);
}
}

private String buildResponseMessage(TypetalkMessage.Emoji emoji, String message, AbstractProject project) {
JSONObject jsonObject = new JSONObject();

TypetalkMessage typetalkMessage = new TypetalkMessage(emoji, message);
jsonObject.element("message", typetalkMessage.messageWithProjectInfo(project));
jsonObject.element("replyTo", req.getPostId());

return jsonObject.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ private void parseParameters() {
public void execute() {
TopLevelItem item = Jenkins.getInstance().getItemMap().get(job);
if (item == null || !(item instanceof AbstractProject)) {
outputError("'" + job + "' is not found");
outputError("Project [ " + job + " ] is not found");
return;
}
AbstractProject project = ((AbstractProject) item);

if (!project.hasPermission(Item.BUILD)) {
String name = Jenkins.getAuthentication().getName();
outputError(String.format("'%s' cannot be built by '%s'", job, name), HttpServletResponse.SC_FORBIDDEN);
outputError(String.format("Project [ %s ] cannot be built by '%s'", job, name), HttpServletResponse.SC_FORBIDDEN);
return;
}

Jenkins.getInstance().getQueue().schedule(project, project.getQuietPeriod(), getParametersAction(project), getCauseAction());
output("'" + job + "' has been scheduled");
output("Project [ " + job + " ] has been scheduled", project);
}

private Action getParametersAction(AbstractProject project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public UndefinedExecutor(WebhookRequest req, StaplerResponse rsp, String command

@Override
public void execute() {
outputError("command '" + command + "' is not defined");
outputError("Command [ " + command + " ] is not defined");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import hudson.security.ACL
import hudson.security.AuthorizationStrategy
import hudson.security.Permission
import hudson.util.OneShotEvent
import jenkins.model.JenkinsLocationConfiguration
import org.acegisecurity.Authentication
import org.acegisecurity.context.SecurityContextHolder
import org.acegisecurity.providers.TestingAuthenticationToken
Expand All @@ -22,7 +23,7 @@ import javax.servlet.http.HttpServletResponse

class BuildExecutorSpec extends Specification {

@Rule JenkinsRule j = new JenkinsRule()
@Rule JenkinsRule j

def req = Mock(WebhookRequest)
def res = Mock(StaplerResponse)
Expand All @@ -38,6 +39,7 @@ class BuildExecutorSpec extends Specification {

def "execute : project is not found"() {
setup:
setUpRootUrl()
executor = new BuildExecutor(req, res, "typetalk-plugin", [])

when:
Expand All @@ -49,6 +51,7 @@ class BuildExecutorSpec extends Specification {

def "execute : no parameter"() {
setup:
setUpRootUrl()
setUpProject([])
executor = new BuildExecutor(req, res, "typetalk-plugin", [])

Expand All @@ -61,6 +64,7 @@ class BuildExecutorSpec extends Specification {

def "execute : parameter without key"() {
setup:
setUpRootUrl()
setUpProject([
new StringParameterDefinition("version", null, null)
])
Expand All @@ -77,6 +81,7 @@ class BuildExecutorSpec extends Specification {

def "execute : single parameter"() {
setup:
setUpRootUrl()
setUpProject([
new StringParameterDefinition("version", null, null)
])
Expand All @@ -93,6 +98,7 @@ class BuildExecutorSpec extends Specification {

def "execute : multiple parameters"() {
setup:
setUpRootUrl()
setUpProject([
new StringParameterDefinition("version", null, null),
new StringParameterDefinition("env", null, null)
Expand All @@ -111,6 +117,7 @@ class BuildExecutorSpec extends Specification {

def "execute : default parameter"() {
setup:
setUpRootUrl()
setUpProject([
new StringParameterDefinition("version", "1.0.0-SNAPSHOT", null)
])
Expand All @@ -127,6 +134,7 @@ class BuildExecutorSpec extends Specification {

def "execute : illegal parameter format"() {
setup:
setUpRootUrl()
setUpProject([
new StringParameterDefinition("version", null, null),
new StringParameterDefinition("env", null, null)
Expand All @@ -145,6 +153,7 @@ class BuildExecutorSpec extends Specification {

def "execute : not set without key when multiple parameters are defined"() {
setup:
setUpRootUrl()
setUpProject([
new StringParameterDefinition("version", null, null),
new StringParameterDefinition("env", null, null)
Expand All @@ -164,6 +173,7 @@ class BuildExecutorSpec extends Specification {
@Unroll
def "execute : #username"() {
setup:
setUpRootUrl()
setUpProject([])
executor = new BuildExecutor(req, res, "typetalk-plugin", [])

Expand All @@ -187,6 +197,10 @@ class BuildExecutorSpec extends Specification {

// --- helper method ---

def setUpRootUrl() {
JenkinsLocationConfiguration.get().url = "http://localhost:8080/"
}

def setUpProject(spds) {
project = j.createFreeStyleProject("typetalk-plugin")

Expand Down

0 comments on commit 96c5c9f

Please sign in to comment.