diff --git a/.github/workflows/hello-yaml-command.yaml b/.github/workflows/hello-yaml-command.yaml new file mode 100644 index 000000000..a8e895efb --- /dev/null +++ b/.github/workflows/hello-yaml-command.yaml @@ -0,0 +1,30 @@ +name: Hello yaml Command +on: + repository_dispatch: + types: [hello-yaml-local-command] +jobs: + helloYaml: + runs-on: ubuntu-latest + steps: + - name: Add reaction + uses: peter-evans/create-or-update-comment@v4 + with: + comment-id: ${{ github.event.client_payload.github.payload.comment.id }} + reactions: hooray + + - name: Create URL to the run output + id: vars + run: echo "run-url=https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" >> $GITHUB_OUTPUT + + - name: Create comment + uses: peter-evans/create-or-update-comment@v4 + with: + issue-number: ${{ github.event.client_payload.github.payload.issue.number }} + body: | + Hello @${{ github.event.client_payload.github.actor }}! + + This command was in a workflow file with the `.yaml` extension. + + [Click here to see the command run output][1] + + [1]: ${{ steps.vars.outputs.run-url }} diff --git a/.github/workflows/slash-command-dispatch.yml b/.github/workflows/slash-command-dispatch.yml index d7f810e1a..f972e7db8 100644 --- a/.github/workflows/slash-command-dispatch.yml +++ b/.github/workflows/slash-command-dispatch.yml @@ -19,6 +19,7 @@ jobs: with: token: ${{ secrets.REPO_ACCESS_TOKEN }} commands: | + hello-yaml-local hello-world-local ping-local permission: none diff --git a/dist/index.js b/dist/index.js index faf637755..a807129b7 100644 --- a/dist/index.js +++ b/dist/index.js @@ -350,7 +350,8 @@ class GitHubHelper { } createWorkflowDispatch(cmd, clientPayload) { return __awaiter(this, void 0, void 0, function* () { - const workflow = `${cmd.command}${cmd.event_type_suffix}.yml`; + const workflowName = `${cmd.command}${cmd.event_type_suffix}`; + const workflow = yield this.getWorkflow(cmd.repository, workflowName); const slashCommand = clientPayload.slash_command; const ref = slashCommand.args.named.ref ? slashCommand.args.named.ref @@ -366,8 +367,21 @@ class GitHubHelper { if (count == 10) break; } - yield this.octokit.request('POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches', Object.assign(Object.assign({}, this.parseRepository(cmd.repository)), { workflow_id: workflow, ref: ref, inputs: inputs })); - core.info(`Command '${cmd.command}' dispatched to workflow '${workflow}' in '${cmd.repository}'`); + yield this.octokit.request('POST /repos/{owner}/{repo}/actions/workflows/{workflow}/dispatches', Object.assign(Object.assign({}, this.parseRepository(cmd.repository)), { workflow: workflow, ref: ref, inputs: inputs })); + core.info(`Command '${cmd.command}' dispatched to workflow '${workflowName}' in '${cmd.repository}'`); + }); + } + getWorkflow(repository, workflowName) { + return __awaiter(this, void 0, void 0, function* () { + core.debug(`Getting workflow ${workflowName} for repository ${repository}`); + const { data: workflows } = yield this.octokit.rest.actions.listRepoWorkflows(Object.assign({}, this.parseRepository(repository))); + for (const workflow of workflows.workflows) { + if (workflow.path === `${workflowName}.yml` || workflow.path === `${workflowName}.yaml`) { + core.info(`Selecting workflow file ${workflow.path}`); + return workflow.path; + } + } + throw new Error(`Workflow ${workflowName} not found`); }); } getDefaultBranch(repository) { diff --git a/docs/workflow-dispatch.md b/docs/workflow-dispatch.md index 166d31367..ab957622b 100644 --- a/docs/workflow-dispatch.md +++ b/docs/workflow-dispatch.md @@ -19,7 +19,7 @@ There are significant differences in the action's behaviour when using `workflow It is important to name the `workflow_dispatch` event workflow correctly since the action targets the workflow based on its filename. The target filename is a combination of the command name and the `event-type-suffix`. -Additionally, the file extension must be `.yml`. +The file extensions `.yml` and `.yaml` are supported. For the following example configuration, the target workflows are: - `deploy-command.yml` diff --git a/src/github-helper.ts b/src/github-helper.ts index 31b96237f..387633c82 100644 --- a/src/github-helper.ts +++ b/src/github-helper.ts @@ -150,7 +150,8 @@ export class GitHubHelper { cmd: Command, clientPayload: ClientPayload ): Promise { - const workflow = `${cmd.command}${cmd.event_type_suffix}.yml` + const workflowName = `${cmd.command}${cmd.event_type_suffix}` + const workflow = await this.getWorkflow(cmd.repository, workflowName) const slashCommand: SlashCommandPayload = clientPayload.slash_command const ref = slashCommand.args.named.ref ? slashCommand.args.named.ref @@ -181,6 +182,28 @@ export class GitHubHelper { ) } + private async getWorkflow( + repository: string, + workflowName: string + ): Promise { + core.debug(`Getting workflow ${workflowName} for repository ${repository}`) + const {data: workflows} = await this.octokit.rest.actions.listRepoWorkflows( + { + ...this.parseRepository(repository) + } + ) + for (const workflow of workflows.workflows) { + if ( + workflow.path === `${workflowName}.yml` || + workflow.path === `${workflowName}.yaml` + ) { + core.debug(`Selecting workflow file ${workflow.path}`) + return workflow.path + } + } + throw new Error(`Workflow ${workflowName} not found`) + } + private async getDefaultBranch(repository: string): Promise { const {data: repo} = await this.octokit.rest.repos.get({ ...this.parseRepository(repository)