Skip to content

Commit 761b72d

Browse files
committed
feat: support yml or yaml
test: support yml or yaml chore: lint
1 parent a94182a commit 761b72d

File tree

5 files changed

+73
-5
lines changed

5 files changed

+73
-5
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Hello yaml Command
2+
on:
3+
repository_dispatch:
4+
types: [hello-yaml-local-command]
5+
jobs:
6+
helloYaml:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: Add reaction
10+
uses: peter-evans/create-or-update-comment@v4
11+
with:
12+
comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
13+
reactions: hooray
14+
15+
- name: Create URL to the run output
16+
id: vars
17+
run: echo "run-url=https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" >> $GITHUB_OUTPUT
18+
19+
- name: Create comment
20+
uses: peter-evans/create-or-update-comment@v4
21+
with:
22+
issue-number: ${{ github.event.client_payload.github.payload.issue.number }}
23+
body: |
24+
Hello @${{ github.event.client_payload.github.actor }}!
25+
26+
This command was in a workflow file with the `.yaml` extension.
27+
28+
[Click here to see the command run output][1]
29+
30+
[1]: ${{ steps.vars.outputs.run-url }}

.github/workflows/slash-command-dispatch.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ jobs:
1919
with:
2020
token: ${{ secrets.REPO_ACCESS_TOKEN }}
2121
commands: |
22+
hello-yaml-local
2223
hello-world-local
2324
ping-local
2425
permission: none

dist/index.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ class GitHubHelper {
350350
}
351351
createWorkflowDispatch(cmd, clientPayload) {
352352
return __awaiter(this, void 0, void 0, function* () {
353-
const workflow = `${cmd.command}${cmd.event_type_suffix}.yml`;
353+
const workflowName = `${cmd.command}${cmd.event_type_suffix}`;
354+
const workflow = yield this.getWorkflow(cmd.repository, workflowName);
354355
const slashCommand = clientPayload.slash_command;
355356
const ref = slashCommand.args.named.ref
356357
? slashCommand.args.named.ref
@@ -366,8 +367,21 @@ class GitHubHelper {
366367
if (count == 10)
367368
break;
368369
}
369-
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 }));
370-
core.info(`Command '${cmd.command}' dispatched to workflow '${workflow}' in '${cmd.repository}'`);
370+
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 }));
371+
core.info(`Command '${cmd.command}' dispatched to workflow '${workflowName}' in '${cmd.repository}'`);
372+
});
373+
}
374+
getWorkflow(repository, workflowName) {
375+
return __awaiter(this, void 0, void 0, function* () {
376+
core.debug(`Getting workflow ${workflowName} for repository ${repository}`);
377+
const { data: workflows } = yield this.octokit.rest.actions.listRepoWorkflows(Object.assign({}, this.parseRepository(repository)));
378+
for (const workflow of workflows.workflows) {
379+
if (workflow.path === `${workflowName}.yml` || workflow.path === `${workflowName}.yaml`) {
380+
core.info(`Selecting workflow file ${workflow.path}`);
381+
return workflow.path;
382+
}
383+
}
384+
throw new Error(`Workflow ${workflowName} not found`);
371385
});
372386
}
373387
getDefaultBranch(repository) {

docs/workflow-dispatch.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ There are significant differences in the action's behaviour when using `workflow
1919

2020
It is important to name the `workflow_dispatch` event workflow correctly since the action targets the workflow based on its filename.
2121
The target filename is a combination of the command name and the `event-type-suffix`.
22-
Additionally, the file extension must be `.yml`.
22+
The file extensions `.yml` and `.yaml` are supported.
2323

2424
For the following example configuration, the target workflows are:
2525
- `deploy-command.yml`

src/github-helper.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ export class GitHubHelper {
150150
cmd: Command,
151151
clientPayload: ClientPayload
152152
): Promise<void> {
153-
const workflow = `${cmd.command}${cmd.event_type_suffix}.yml`
153+
const workflowName = `${cmd.command}${cmd.event_type_suffix}`
154+
const workflow = await this.getWorkflow(cmd.repository, workflowName)
154155
const slashCommand: SlashCommandPayload = clientPayload.slash_command
155156
const ref = slashCommand.args.named.ref
156157
? slashCommand.args.named.ref
@@ -181,6 +182,28 @@ export class GitHubHelper {
181182
)
182183
}
183184

185+
private async getWorkflow(
186+
repository: string,
187+
workflowName: string
188+
): Promise<string> {
189+
core.debug(`Getting workflow ${workflowName} for repository ${repository}`)
190+
const {data: workflows} = await this.octokit.rest.actions.listRepoWorkflows(
191+
{
192+
...this.parseRepository(repository)
193+
}
194+
)
195+
for (const workflow of workflows.workflows) {
196+
if (
197+
workflow.path === `${workflowName}.yml` ||
198+
workflow.path === `${workflowName}.yaml`
199+
) {
200+
core.debug(`Selecting workflow file ${workflow.path}`)
201+
return workflow.path
202+
}
203+
}
204+
throw new Error(`Workflow ${workflowName} not found`)
205+
}
206+
184207
private async getDefaultBranch(repository: string): Promise<string> {
185208
const {data: repo} = await this.octokit.rest.repos.get({
186209
...this.parseRepository(repository)

0 commit comments

Comments
 (0)