Skip to content

Commit 5bdddda

Browse files
committed
Add list workflows paging for monorepos
1 parent 7ff1caf commit 5bdddda

File tree

3 files changed

+17
-29
lines changed

3 files changed

+17
-29
lines changed

dist/index.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,9 @@ function run() {
540540
const workflowName = core.getInput('workflow');
541541
// Optional inputs, with defaults
542542
const ref = core.getInput('ref') || github.context.ref;
543-
const repo = core.getInput('repo') || `${github.context.repo.owner}/${github.context.repo.repo}`;
543+
const [owner, repo] = core.getInput('repo')
544+
? core.getInput('repo').split('/')
545+
: [github.context.repo.owner, github.context.repo.repo];
544546
// Decode inputs, these MUST be a valid JSON string
545547
let inputs = {};
546548
const inputsJson = core.getInput('inputs');
@@ -550,23 +552,14 @@ function run() {
550552
// Get octokit client for making API calls
551553
const octokit = github.getOctokit(token);
552554
// List workflows via API
553-
const listResp = yield octokit.request(`GET /repos/${repo}/actions/workflows`, {
554-
ref: ref,
555-
inputs: inputs
556-
});
557-
if (listResp.status != 200)
558-
throw new Error(`Got HTTP ${listResp.status} calling list workflows API 💩`);
559-
// Debug response if ACTIONS_STEP_DEBUG is enabled
560-
core.debug(listResp.data);
555+
const workflows = yield octokit.paginate(octokit.actions.listRepoWorkflows.endpoint.merge({ owner, repo, ref, inputs }));
561556
// Locate workflow by name as we need it's id
562-
const workflowFind = listResp.data.workflows.find((wf) => {
563-
return wf['name'] === workflowName;
564-
});
557+
const workflowFind = workflows.find((workflow) => workflow.name === workflowName);
565558
if (!workflowFind)
566-
throw new Error(`Unable to find workflow named '${workflowName}' in ${repo} 😥`);
559+
throw new Error(`Unable to find workflow named '${workflowName}' in ${owner}/${repo} 😥`);
567560
console.log(`Workflow id is: ${workflowFind.id}`);
568561
// Call workflow_dispatch API
569-
const dispatchResp = yield octokit.request(`POST /repos/${repo}/actions/workflows/${workflowFind.id}/dispatches`, {
562+
const dispatchResp = yield octokit.request(`POST /repos/${owner}/${repo}/actions/workflows/${workflowFind.id}/dispatches`, {
570563
ref: ref,
571564
inputs: inputs
572565
});

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: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as core from '@actions/core'
22
import * as github from '@actions/github'
3+
import { ActionsGetWorkflowResponseData } from '@octokit/types'
34

45
// async wrapper function
56
async function run(): Promise<void> {
@@ -9,7 +10,9 @@ async function run(): Promise<void> {
910
const workflowName = core.getInput('workflow')
1011
// Optional inputs, with defaults
1112
const ref = core.getInput('ref') || github.context.ref
12-
const repo = core.getInput('repo') || `${github.context.repo.owner}/${github.context.repo.repo}`
13+
const [owner, repo] = core.getInput('repo')
14+
? core.getInput('repo').split('/')
15+
: [github.context.repo.owner, github.context.repo.repo]
1316

1417
// Decode inputs, these MUST be a valid JSON string
1518
let inputs = {}
@@ -22,24 +25,16 @@ async function run(): Promise<void> {
2225
const octokit = github.getOctokit(token)
2326

2427
// List workflows via API
25-
const listResp = await octokit.request(`GET /repos/${repo}/actions/workflows`, {
26-
ref: ref,
27-
inputs: inputs
28-
})
29-
if(listResp.status != 200) throw new Error(`Got HTTP ${listResp.status} calling list workflows API 💩`)
30-
31-
// Debug response if ACTIONS_STEP_DEBUG is enabled
32-
core.debug(listResp.data)
28+
const workflows: ActionsGetWorkflowResponseData[] =
29+
await octokit.paginate(octokit.actions.listRepoWorkflows.endpoint.merge({ owner, repo, ref, inputs }))
3330

3431
// Locate workflow by name as we need it's id
35-
const workflowFind = listResp.data.workflows.find((wf: Record<string, string>) => {
36-
return wf['name'] === workflowName
37-
})
38-
if(!workflowFind) throw new Error(`Unable to find workflow named '${workflowName}' in ${repo} 😥`)
32+
const workflowFind = workflows.find((workflow) => workflow.name === workflowName)
33+
if(!workflowFind) throw new Error(`Unable to find workflow named '${workflowName}' in ${owner}/${repo} 😥`)
3934
console.log(`Workflow id is: ${workflowFind.id}`)
4035

4136
// Call workflow_dispatch API
42-
const dispatchResp = await octokit.request(`POST /repos/${repo}/actions/workflows/${workflowFind.id}/dispatches`, {
37+
const dispatchResp = await octokit.request(`POST /repos/${owner}/${repo}/actions/workflows/${workflowFind.id}/dispatches`, {
4338
ref: ref,
4439
inputs: inputs
4540
})

0 commit comments

Comments
 (0)