This repository was archived by the owner on Feb 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
190 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 78 additions & 4 deletions
82
src/main/java/org/jenkinsci/plugins/typetalk/webhookaction/executorimpl/ListExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
.../groovy/org/jenkinsci/plugins/typetalk/webhookaction/executorimpl/ListExecutorSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:") | ||
} | ||
|
||
} |