Add named args command config

This commit is contained in:
Peter Evans
2020-01-04 17:23:41 +09:00
parent 899b263c41
commit 00faff1292
3 changed files with 23 additions and 6 deletions
+5 -1
View File
@@ -10,7 +10,8 @@ const commandDefaults = Object.freeze({
issue_type: "both",
allow_edits: false,
repository: process.env.GITHUB_REPOSITORY,
event_type_suffix: "-command"
event_type_suffix: "-command",
named_args: false
});
function toBool(input, defaultVal) {
@@ -34,6 +35,7 @@ function getInputs() {
allowEdits: core.getInput("allow-edits"),
repository: core.getInput("repository"),
eventTypeSuffix: core.getInput("event-type-suffix"),
namedArgs: core.getInput("named-args"),
config: core.getInput("config"),
configFromFile: core.getInput("config-from-file")
};
@@ -77,6 +79,7 @@ function getCommandsConfigFromInputs(inputs) {
cmd.event_type_suffix = inputs.eventTypeSuffix
? inputs.eventTypeSuffix
: cmd.event_type_suffix;
cmd.named_args = toBool(inputs.namedArgs, cmd.named_args);
config.push(cmd);
}
return config;
@@ -98,6 +101,7 @@ function getCommandsConfigFromJson(json) {
cmd.event_type_suffix = jc.event_type_suffix
? jc.event_type_suffix
: cmd.event_type_suffix;
cmd.named_args = toBool(jc.named_args, cmd.named_args);
config.push(cmd);
}
return config;
+8 -2
View File
@@ -23,6 +23,7 @@ test("building config with required inputs only", async () => {
expect(config[i].event_type_suffix).toEqual(
commandDefaults.event_type_suffix
);
expect(config[i].named_args).toEqual(commandDefaults.named_args);
}
});
@@ -33,7 +34,8 @@ test("building config with optional inputs", async () => {
issueType: "pull-request",
allowEdits: true,
repository: "owner/repo",
eventTypeSuffix: "-cmd"
eventTypeSuffix: "-cmd",
namedArgs: true
};
const commands = inputs.commands.replace(/\s+/g, "").split(",");
const config = getCommandsConfigFromInputs(inputs);
@@ -45,6 +47,7 @@ test("building config with optional inputs", async () => {
expect(config[i].allow_edits).toEqual(inputs.allowEdits);
expect(config[i].repository).toEqual(inputs.repository);
expect(config[i].event_type_suffix).toEqual(inputs.eventTypeSuffix);
expect(config[i].named_args).toEqual(inputs.namedArgs);
}
});
@@ -69,6 +72,7 @@ test("building config with required JSON only", async () => {
expect(config[i].event_type_suffix).toEqual(
commandDefaults.event_type_suffix
);
expect(config[i].named_args).toEqual(commandDefaults.named_args);
}
});
@@ -80,7 +84,8 @@ test("building config with optional JSON properties", async () => {
"issue_type": "pull-request",
"allow_edits": true,
"repository": "owner/repo",
"event_type_suffix": "-cmd"
"event_type_suffix": "-cmd",
"named_args": true
},
{
"command": "test-all-the-things",
@@ -96,6 +101,7 @@ test("building config with optional JSON properties", async () => {
expect(config[0].allow_edits).toBeTruthy();
expect(config[0].repository).toEqual("owner/repo");
expect(config[0].event_type_suffix).toEqual("-cmd");
expect(config[0].named_args).toBeTruthy();
expect(config[1].command).toEqual(commands[1]);
expect(config[1].permission).toEqual("read");
expect(config[1].issue_type).toEqual(commandDefaults.issue_type);
+10 -3
View File
@@ -136,10 +136,8 @@ async function run() {
core.info(`Command '${commentWords[0]}' to be dispatched.`);
// Define payload
const slashCommandPayload = getSlashCommandPayload(commentWords);
core.debug(`Slash command payload: ${inspect(slashCommandPayload)}`);
var clientPayload = {
slash_command: slashCommandPayload,
slash_command: {},
github: github.context
};
@@ -154,6 +152,15 @@ async function run() {
// Dispatch for each matching configuration
for (const cmd of configMatches) {
// Generate slash command payload
clientPayload.slash_command = getSlashCommandPayload(
commentWords,
cmd.named_args
);
core.debug(
`Slash command payload: ${inspect(clientPayload.slash_command)}`
);
// Dispatch the command
const dispatchRepo = cmd.repository.split("/");
const eventType = cmd.command + cmd.event_type_suffix;
await octokit.repos.createDispatchEvent({