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
194 additions
and
19 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
72 changes: 68 additions & 4 deletions
72
src/main/java/org/jenkinsci/plugins/typetalk/webhookaction/executorimpl/HelpExecutor.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,77 @@ | ||
package org.jenkinsci.plugins.typetalk.webhookaction.executorimpl; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.jenkinsci.plugins.typetalk.api.TypetalkMessage; | ||
import org.jenkinsci.plugins.typetalk.webhookaction.WebhookExecutor; | ||
import org.jenkinsci.plugins.typetalk.webhookaction.WebhookRequest; | ||
import org.kohsuke.stapler.StaplerResponse; | ||
|
||
// TODO add help executor | ||
public class HelpExecutor extends UndefinedExecutor { | ||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
public HelpExecutor(WebhookRequest req, StaplerResponse rsp, String command) { | ||
super(req, rsp, command); | ||
public class HelpExecutor extends WebhookExecutor { | ||
|
||
private String botUser; | ||
private LinkedList<String> parameters; | ||
|
||
public static HelpExecutor createBuildHelpExecutor(WebhookRequest req, StaplerResponse rsp, String botUser) { | ||
LinkedList<String> parameters = new LinkedList<>(); | ||
parameters.add("build"); | ||
return new HelpExecutor(req, rsp, botUser, parameters); | ||
} | ||
|
||
public HelpExecutor(WebhookRequest req, StaplerResponse rsp, String botUser, LinkedList<String> parameters) { | ||
super(req, rsp, "help"); | ||
this.botUser = botUser; | ||
this.parameters = parameters; | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
output("Command [ help ] is executed", getMessages(), TypetalkMessage.Emoji.BOOK); | ||
} | ||
|
||
private List<String> getMessages() { | ||
String command = parameters.poll(); | ||
if (StringUtils.isBlank(command)) { | ||
return getDefaultMessages(); | ||
} | ||
|
||
switch (command) { | ||
case "build": | ||
return getBuildMessages(); | ||
default: | ||
return getDefaultMessages(); | ||
} | ||
} | ||
|
||
private List<String> getDefaultMessages() { | ||
List<String> messages = new ArrayList<>(); | ||
messages.add("Usage"); | ||
messages.add("```"); | ||
messages.add(botUser + " build <project> (<key=value>)"); | ||
messages.add(botUser + " help (<sub command>)"); | ||
messages.add("```"); | ||
|
||
return messages; | ||
} | ||
|
||
private List<String> getBuildMessages() { | ||
List<String> messages = new ArrayList<>(); | ||
messages.add("Usage"); | ||
messages.add("```"); | ||
messages.add(botUser + " build <project> (<key=value>)"); | ||
messages.add("```"); | ||
messages.add(TypetalkMessage.Emoji.BOOK.getSymbol() + " Sample"); | ||
messages.add("```"); | ||
messages.add(botUser + " build helloWorldProject | build without parameters"); | ||
messages.add(botUser + " build helloWorldProject 1.0.0 | build with only single parameter"); | ||
messages.add(botUser + " build helloWorldProject version=1.0.0 | build with single parameter"); | ||
messages.add(botUser + " build helloWorldProject version=1.0.0 env=stage | build with multiple parameters"); | ||
messages.add("```"); | ||
|
||
return messages; | ||
} | ||
|
||
} |
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
68 changes: 68 additions & 0 deletions
68
.../groovy/org/jenkinsci/plugins/typetalk/webhookaction/executorimpl/HelpExecutorSpec.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,68 @@ | ||
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 | ||
|
||
import javax.servlet.http.HttpServletResponse | ||
|
||
class HelpExecutorSpec extends Specification { | ||
|
||
@Rule JenkinsRule j | ||
|
||
def req = Mock(WebhookRequest) | ||
def res = Mock(StaplerResponse) | ||
|
||
WebhookExecutor executor | ||
def writer = new StringWriter() | ||
|
||
def setup() { | ||
res.writer >> new PrintWriter(writer) | ||
} | ||
|
||
@Unroll | ||
def "execute : parameters #parameters"() { | ||
setup: | ||
setUpRootUrl() | ||
executor = new HelpExecutor(req, res, "@jenkins+", parameters as LinkedList) | ||
|
||
when: | ||
executor.execute() | ||
|
||
then: | ||
1 * res.setStatus(HttpServletResponse.SC_OK) | ||
writer.toString().contains("build <project>") | ||
writer.toString().contains("help") | ||
|
||
where: | ||
parameters || result | ||
[] || true | ||
["dummy"] || true | ||
} | ||
|
||
def "execute : parameters [build]"() { | ||
setup: | ||
setUpRootUrl() | ||
executor = new HelpExecutor(req, res, "@jenkins+", ["build"] as LinkedList) | ||
|
||
when: | ||
executor.execute() | ||
|
||
then: | ||
1 * res.setStatus(HttpServletResponse.SC_OK) | ||
writer.toString().contains("build <project> (<key=value>)") | ||
writer.toString().contains("build helloWorldProject version=1.0.0 env=stage") | ||
} | ||
|
||
// --- helper method --- | ||
|
||
def setUpRootUrl() { | ||
JenkinsLocationConfiguration.get().url = "http://localhost:8080/" | ||
} | ||
|
||
} |