Skip to content

Commit 64b7a44

Browse files
committed
Merge branch 'risk-and-safety-list-workflows-paging'
2 parents 6b22730 + 4cedbac commit 64b7a44

File tree

3 files changed

+27
-42
lines changed

3 files changed

+27
-42
lines changed

dist/index.js

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -548,36 +548,26 @@ function run() {
548548
const workflowReference = core.getInput('workflow');
549549
// Optional inputs, with defaults
550550
const ref = core.getInput('ref') || github.context.ref;
551-
const repo = core.getInput('repo') || `${github.context.repo.owner}/${github.context.repo.repo}`;
552-
// Decode inputs, this MUST be a valid JSON string
551+
const [owner, repo] = core.getInput('repo')
552+
? core.getInput('repo').split('/')
553+
: [github.context.repo.owner, github.context.repo.repo];
554+
// Decode inputs, these MUST be a valid JSON string
553555
let inputs = {};
554556
const inputsJson = core.getInput('inputs');
555557
if (inputsJson) {
556558
inputs = JSON.parse(inputsJson);
557559
}
558560
// Get octokit client for making API calls
559561
const octokit = github.getOctokit(token);
560-
// List workflows in repo via API
561-
const listResp = yield octokit.request(`GET /repos/${repo}/actions/workflows`, {
562-
ref: ref,
563-
inputs: inputs
564-
});
565-
if (listResp.status != 200)
566-
throw new Error(`Got HTTP ${listResp.status} calling list workflows API 💩`);
567-
// Debug response if ACTIONS_STEP_DEBUG is enabled
568-
core.debug('### START List Workflows response data');
569-
core.debug(JSON.stringify(listResp.data, null, 3));
570-
core.debug('### END: List Workflows response data');
562+
// List workflows via API
563+
const workflows = yield octokit.paginate(octokit.actions.listRepoWorkflows.endpoint.merge({ owner, repo, ref, inputs }));
571564
// Locate workflow by name as we need it's id
572-
const foundWorkflow = listResp.data.workflows.find((wf) => {
573-
// Match on name or id, there's a slim chance someone names their workflow 1803663 but they are crazy
574-
return (wf['name'] === workflowReference || wf['id'].toString() === workflowReference);
575-
});
576-
if (!foundWorkflow)
577-
throw new Error(`Unable to find workflow '${workflowReference}' in ${repo} 😥`);
578-
console.log(`Workflow id is: ${foundWorkflow.id}`);
579-
// Call workflow_dispatch API to trigger the workflow
580-
const dispatchResp = yield octokit.request(`POST /repos/${repo}/actions/workflows/${foundWorkflow.id}/dispatches`, {
565+
const workflowFind = workflows.find((workflow) => workflow.name === workflowName);
566+
if (!workflowFind)
567+
throw new Error(`Unable to find workflow named '${workflowName}' in ${owner}/${repo} 😥`);
568+
console.log(`Workflow id is: ${workflowFind.id}`);
569+
// Call workflow_dispatch API
570+
const dispatchResp = yield octokit.request(`POST /repos/${owner}/${repo}/actions/workflows/${workflowFind.id}/dispatches`, {
581571
ref: ref,
582572
inputs: inputs
583573
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "dist/index.js",
66
"scripts": {
77
"build": "ncc build src/main.ts -o dist",
8-
"lint": "npm run eslint"
8+
"lint": "eslint src/"
99
},
1010
"keywords": [
1111
"github",

src/main.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import * as core from '@actions/core'
99
import * as github from '@actions/github'
10+
import { ActionsGetWorkflowResponseData } from '@octokit/types'
1011

1112
//
1213
// Main task function (async wrapper)
@@ -15,10 +16,12 @@ async function run(): Promise<void> {
1516
try {
1617
// Required inputs
1718
const token = core.getInput('token')
18-
const workflowReference = core.getInput('workflow')
19+
const workflowName = core.getInput('workflow')
1920
// Optional inputs, with defaults
2021
const ref = core.getInput('ref') || github.context.ref
21-
const repo = core.getInput('repo') || `${github.context.repo.owner}/${github.context.repo.repo}`
22+
const [owner, repo] = core.getInput('repo')
23+
? core.getInput('repo').split('/')
24+
: [github.context.repo.owner, github.context.repo.repo]
2225

2326
// Decode inputs, this MUST be a valid JSON string
2427
let inputs = {}
@@ -30,30 +33,22 @@ async function run(): Promise<void> {
3033
// Get octokit client for making API calls
3134
const octokit = github.getOctokit(token)
3235

33-
// List workflows in repo via API
34-
const listResp = await octokit.request(`GET /repos/${repo}/actions/workflows`, {
35-
ref: ref,
36-
inputs: inputs
37-
})
38-
if(listResp.status != 200) throw new Error(`Got HTTP ${listResp.status} calling list workflows API 💩`)
36+
// List workflows via API
37+
const workflows: ActionsGetWorkflowResponseData[] =
38+
await octokit.paginate(octokit.actions.listRepoWorkflows.endpoint.merge({ owner, repo, ref, inputs }))
3939

4040
// Debug response if ACTIONS_STEP_DEBUG is enabled
4141
core.debug('### START List Workflows response data')
42-
core.debug(JSON.stringify(listResp.data, null, 3))
42+
core.debug(JSON.stringify(workflows, null, 3))
4343
core.debug('### END: List Workflows response data')
4444

4545
// Locate workflow by name as we need it's id
46-
const foundWorkflow = listResp.data.workflows.find((wf: Record<string, string>) => {
47-
// Match on name or id, there's a slim chance someone names their workflow 1803663 but they are crazy
48-
return (wf['name'] === workflowReference || wf['id'].toString() === workflowReference)
49-
})
50-
51-
if(!foundWorkflow) throw new Error(`Unable to find workflow '${workflowReference}' in ${repo} 😥`)
52-
53-
console.log(`Workflow id is: ${foundWorkflow.id}`)
46+
const workflowFind = workflows.find((workflow) => workflow.name === workflowName)
47+
if(!workflowFind) throw new Error(`Unable to find workflow named '${workflowName}' in ${owner}/${repo} 😥`)
48+
console.log(`Workflow id is: ${workflowFind.id}`)
5449

55-
// Call workflow_dispatch API to trigger the workflow
56-
const dispatchResp = await octokit.request(`POST /repos/${repo}/actions/workflows/${foundWorkflow.id}/dispatches`, {
50+
// Call workflow_dispatch API
51+
const dispatchResp = await octokit.request(`POST /repos/${owner}/${repo}/actions/workflows/${workflowFind.id}/dispatches`, {
5752
ref: ref,
5853
inputs: inputs
5954
})

0 commit comments

Comments
 (0)