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

Commit

Permalink
Implement list executor
Browse files Browse the repository at this point in the history
  • Loading branch information
ikikko committed Feb 7, 2015
1 parent 0d13024 commit f29d99f
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public enum Emoji {
RAGE(":rage:"),
CRY(":cry:"),
SMILEY(":smiley:"),
BOOK(":book:");
MASK(":mask:"),
BOOK(":book:"),
PAGE_FACING_UP(":page_facing_up:");

private String symbol;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public static WebhookExecutor create(WebhookRequest req, StaplerResponse rsp) {

return new BuildExecutor(req, rsp, job, parameters);
case "list":
return new ListExecutor(req, rsp, command);
String pattern = parameters.poll();
return new ListExecutor(req, rsp, pattern);
case "help":
return new HelpExecutor(req, rsp, botUser, parameters);
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ private List<String> getMessages() {
switch (command) {
case "build":
return getBuildMessages();
case "list":
return getListMessages();
default:
return getDefaultMessages();
}
Expand All @@ -56,6 +58,7 @@ private List<String> getDefaultMessages() {
messages.add("Usage");
messages.add("```");
messages.add(botUser + " build <project> (<key=value>)");
messages.add(botUser + " list (<regexp filter>)");
messages.add(botUser + " help (<sub command>)");
messages.add("```");

Expand All @@ -79,4 +82,20 @@ private List<String> getBuildMessages() {
return messages;
}

private List<String> getListMessages() {
List<String> messages = new ArrayList<>();
messages.add("Usage");
messages.add("```");
messages.add(botUser + " list (<regexp filter>)");
messages.add("```");
messages.add(TypetalkMessage.Emoji.BOOK.getSymbol() + " Sample");
messages.add("```");
messages.add(botUser + " list | list all projects ");
messages.add(botUser + " list helloWorld | list projects with simple filter");
messages.add(botUser + " list hel....rld | list projects with regexp filter");
messages.add("```");

return messages;
}

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

import hudson.model.AbstractProject;
import hudson.model.TopLevelItem;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.typetalk.api.TypetalkMessage;
import org.jenkinsci.plugins.typetalk.webhookaction.ResponseParameter;
import org.jenkinsci.plugins.typetalk.webhookaction.WebhookExecutor;
import org.jenkinsci.plugins.typetalk.webhookaction.WebhookRequest;
import org.kohsuke.stapler.StaplerResponse;

// TODO add list executor ( with regexp filter )
public class ListExecutor extends UndefinedExecutor {
import java.util.ArrayList;
import java.util.regex.Pattern;

public ListExecutor(WebhookRequest req, StaplerResponse rsp, String command) {
super(req, rsp, command);
public class ListExecutor extends WebhookExecutor {

private static final String PROJECT_MESSAGE_FORMAT = "%s [%s](%s)";

private Pattern pattern;

public ListExecutor(WebhookRequest req, StaplerResponse rsp, String pattern) {
super(req, rsp, "list");

if (StringUtils.isNotBlank(pattern)) {
this.pattern = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
}
}

@Override
public void execute() {
ArrayList<String> messages = new ArrayList<>();
TypetalkMessage.Emoji emoji;

for (TopLevelItem item : Jenkins.getInstance().getItems()) {
if (!(item instanceof AbstractProject)) {
continue;
}
AbstractProject project = ((AbstractProject) item);

if (pattern != null && !pattern.matcher(project.getName()).find()) {
continue;
}

TypetalkMessage.Emoji ball2emoji;
switch (project.getIconColor()) {
case RED:
case RED_ANIME:
ball2emoji = TypetalkMessage.Emoji.RAGE;
break;
case YELLOW:
case YELLOW_ANIME:
ball2emoji = TypetalkMessage.Emoji.CRY;
break;
case BLUE:
case BLUE_ANIME:
ball2emoji = TypetalkMessage.Emoji.SMILEY;
break;
case DISABLED:
case DISABLED_ANIME:
case NOTBUILT:
case NOTBUILT_ANIME:
ball2emoji = TypetalkMessage.Emoji.MASK;
break;
default:
ball2emoji = TypetalkMessage.Emoji.ASTONISHED;
}
messages.add(String.format(PROJECT_MESSAGE_FORMAT, ball2emoji.getSymbol(), project.getName(), project.getAbsoluteUrl()));
}

if (messages.isEmpty()) {
messages.add("Project is not found");
emoji = TypetalkMessage.Emoji.CRY;
} else {
messages.add(0, "Project list");
messages.add("");
emoji = TypetalkMessage.Emoji.PAGE_FACING_UP;
}

ResponseParameter responseParameter = new ResponseParameter(ResponseParameter.flatMessages(messages));
responseParameter.setDescription("Command [ list ] is executed");
responseParameter.setEmoji(emoji);

output(responseParameter);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class HelpExecutorSpec extends Specification {
then:
1 * res.setStatus(HttpServletResponse.SC_OK)
writer.toString().contains("build <project>")
writer.toString().contains("list")
writer.toString().contains("help")

where:
Expand All @@ -59,6 +60,20 @@ class HelpExecutorSpec extends Specification {
writer.toString().contains("build helloWorldProject version=1.0.0 env=stage")
}

def "execute : parameters [list]"() {
setup:
setUpRootUrl()
executor = new HelpExecutor(req, res, "@jenkins+", ["list"] as LinkedList)

when:
executor.execute()

then:
1 * res.setStatus(HttpServletResponse.SC_OK)
writer.toString().contains("list")
writer.toString().contains("list helloWorld")
}

// --- helper method ---

def setUpRootUrl() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.jenkinsci.plugins.typetalk.webhookaction.executorimpl

import jenkins.model.JenkinsLocationConfiguration
import org.jenkinsci.plugins.typetalk.webhookaction.WebhookExecutor
import org.jenkinsci.plugins.typetalk.webhookaction.WebhookRequest
import org.junit.Rule
import org.jvnet.hudson.test.JenkinsRule
import org.kohsuke.stapler.StaplerResponse
import spock.lang.Specification
import spock.lang.Unroll

class ListExecutorSpec extends Specification {

@Rule JenkinsRule j

def req = Mock(WebhookRequest)
def res = Mock(StaplerResponse)

def writer = new StringWriter()

WebhookExecutor executor

def setup() {
res.writer >> new PrintWriter(writer)
}

@Unroll
def "execute : pattern is '#pattern'"() {
setup:
setUpJenkins()
executor = new ListExecutor(req, res, pattern)

when:
executor.execute()

then:
countResultProjects() == result.size()
result.each { assert writer.toString().contains(it) }

where:
pattern || result
"typetalk" || ["typetalk-plugin", "typetalk2-plugin", "typetalk3-plugin"]
"typetalk2" || ["typetalk2-plugin"]
"typetalk(2|3)" || ["typetalk2-plugin", "typetalk3-plugin"]
}

def "execute : not found"() {
setup:
setUpJenkins()
executor = new ListExecutor(req, res, "notfound")

when:
executor.execute()

then:
countResultProjects() == 0
}

// --- helper method ---

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

["typetalk-plugin", "typetalk2-plugin", "typetalk3-plugin"].each {
j.createFreeStyleProject(it)
}
}

def countResultProjects() {
writer.toString().count(":astonished:")
}

}

0 comments on commit f29d99f

Please sign in to comment.