Skip to content

Commit

Permalink
Add list workflows paging for monorepos
Browse files Browse the repository at this point in the history
  • Loading branch information
eerichmond committed Aug 4, 2020
1 parent 7ff1caf commit 5bdddda
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 29 deletions.
21 changes: 7 additions & 14 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 9 additions & 14 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
Expand All @@ -9,7 +10,9 @@ async function run(): Promise<void> {
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 = {}
Expand All @@ -22,24 +25,16 @@ async function run(): Promise<void> {
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<string, string>) => {
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
})
Expand Down

0 comments on commit 5bdddda

Please sign in to comment.