From 00faff1292c826bbf8015c115b58b63aec01ff4d Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Sat, 4 Jan 2020 17:23:41 +0900 Subject: [PATCH] Add named args command config --- src/func.js | 6 +++++- src/func.test.js | 10 ++++++++-- src/index.js | 13 ++++++++++--- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/func.js b/src/func.js index 9cb61ac..a5b1534 100644 --- a/src/func.js +++ b/src/func.js @@ -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; diff --git a/src/func.test.js b/src/func.test.js index eaf6e1f..b1769e5 100644 --- a/src/func.test.js +++ b/src/func.test.js @@ -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); diff --git a/src/index.js b/src/index.js index 96223f5..df57351 100644 --- a/src/index.js +++ b/src/index.js @@ -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({