From 5bdddda1b0062f7cde6d598b8d59fae4f40a14bd Mon Sep 17 00:00:00 2001 From: Eli Richmond Date: Mon, 3 Aug 2020 23:58:57 -0700 Subject: [PATCH] Add list workflows paging for monorepos --- dist/index.js | 21 +++++++-------------- package.json | 2 +- src/main.ts | 23 +++++++++-------------- 3 files changed, 17 insertions(+), 29 deletions(-) diff --git a/dist/index.js b/dist/index.js index 5a18a49..64a332b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -540,7 +540,9 @@ function run() { const workflowName = core.getInput('workflow'); // Optional inputs, with defaults const ref = core.getInput('ref') || github.context.ref; - const repo = core.getInput('repo') || `${github.context.repo.owner}/${github.context.repo.repo}`; + const [owner, repo] = core.getInput('repo') + ? core.getInput('repo').split('/') + : [github.context.repo.owner, github.context.repo.repo]; // Decode inputs, these MUST be a valid JSON string let inputs = {}; const inputsJson = core.getInput('inputs'); @@ -550,23 +552,14 @@ function run() { // Get octokit client for making API calls const octokit = github.getOctokit(token); // List workflows via API - const listResp = yield octokit.request(`GET /repos/${repo}/actions/workflows`, { - ref: ref, - inputs: inputs - }); - if (listResp.status != 200) - throw new Error(`Got HTTP ${listResp.status} calling list workflows API 💩`); - // Debug response if ACTIONS_STEP_DEBUG is enabled - core.debug(listResp.data); + const workflows = yield octokit.paginate(octokit.actions.listRepoWorkflows.endpoint.merge({ owner, repo, ref, inputs })); // Locate workflow by name as we need it's id - const workflowFind = listResp.data.workflows.find((wf) => { - return wf['name'] === workflowName; - }); + const workflowFind = workflows.find((workflow) => workflow.name === workflowName); if (!workflowFind) - throw new Error(`Unable to find workflow named '${workflowName}' in ${repo} 😥`); + throw new Error(`Unable to find workflow named '${workflowName}' in ${owner}/${repo} 😥`); console.log(`Workflow id is: ${workflowFind.id}`); // Call workflow_dispatch API - const dispatchResp = yield octokit.request(`POST /repos/${repo}/actions/workflows/${workflowFind.id}/dispatches`, { + const dispatchResp = yield octokit.request(`POST /repos/${owner}/${repo}/actions/workflows/${workflowFind.id}/dispatches`, { ref: ref, inputs: inputs }); diff --git a/package.json b/package.json index 83ff318..fdbdc60 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "dist/index.js", "scripts": { "build": "ncc build src/main.ts -o dist", - "lint": "npm run eslint" + "lint": "eslint src/" }, "keywords": [ "github", diff --git a/src/main.ts b/src/main.ts index ad32bf8..266b630 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,5 +1,6 @@ import * as core from '@actions/core' import * as github from '@actions/github' +import { ActionsGetWorkflowResponseData } from '@octokit/types' // async wrapper function async function run(): Promise { @@ -9,7 +10,9 @@ async function run(): Promise { const workflowName = core.getInput('workflow') // Optional inputs, with defaults const ref = core.getInput('ref') || github.context.ref - const repo = core.getInput('repo') || `${github.context.repo.owner}/${github.context.repo.repo}` + const [owner, repo] = core.getInput('repo') + ? core.getInput('repo').split('/') + : [github.context.repo.owner, github.context.repo.repo] // Decode inputs, these MUST be a valid JSON string let inputs = {} @@ -22,24 +25,16 @@ async function run(): Promise { const octokit = github.getOctokit(token) // List workflows via API - const listResp = await octokit.request(`GET /repos/${repo}/actions/workflows`, { - ref: ref, - inputs: inputs - }) - if(listResp.status != 200) throw new Error(`Got HTTP ${listResp.status} calling list workflows API 💩`) - - // Debug response if ACTIONS_STEP_DEBUG is enabled - core.debug(listResp.data) + const workflows: ActionsGetWorkflowResponseData[] = + await octokit.paginate(octokit.actions.listRepoWorkflows.endpoint.merge({ owner, repo, ref, inputs })) // Locate workflow by name as we need it's id - const workflowFind = listResp.data.workflows.find((wf: Record) => { - return wf['name'] === workflowName - }) - if(!workflowFind) throw new Error(`Unable to find workflow named '${workflowName}' in ${repo} 😥`) + const workflowFind = workflows.find((workflow) => workflow.name === workflowName) + if(!workflowFind) throw new Error(`Unable to find workflow named '${workflowName}' in ${owner}/${repo} 😥`) console.log(`Workflow id is: ${workflowFind.id}`) // Call workflow_dispatch API - const dispatchResp = await octokit.request(`POST /repos/${repo}/actions/workflows/${workflowFind.id}/dispatches`, { + const dispatchResp = await octokit.request(`POST /repos/${owner}/${repo}/actions/workflows/${workflowFind.id}/dispatches`, { ref: ref, inputs: inputs })