Skip to content

Commit

Permalink
Merge branch 'list-workflows-paging' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
benc-uk committed Aug 9, 2020
2 parents 6b22730 + 5bdddda commit 4cedbac
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 42 deletions.
34 changes: 12 additions & 22 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,36 +548,26 @@ function run() {
const workflowReference = 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}`;
// Decode inputs, this MUST be a valid JSON string
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');
if (inputsJson) {
inputs = JSON.parse(inputsJson);
}
// Get octokit client for making API calls
const octokit = github.getOctokit(token);
// List workflows in repo 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('### START List Workflows response data');
core.debug(JSON.stringify(listResp.data, null, 3));
core.debug('### END: List Workflows response data');
// List workflows via API
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 foundWorkflow = listResp.data.workflows.find((wf) => {
// Match on name or id, there's a slim chance someone names their workflow 1803663 but they are crazy
return (wf['name'] === workflowReference || wf['id'].toString() === workflowReference);
});
if (!foundWorkflow)
throw new Error(`Unable to find workflow '${workflowReference}' in ${repo} 😥`);
console.log(`Workflow id is: ${foundWorkflow.id}`);
// Call workflow_dispatch API to trigger the workflow
const dispatchResp = yield octokit.request(`POST /repos/${repo}/actions/workflows/${foundWorkflow.id}/dispatches`, {
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 = 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
33 changes: 14 additions & 19 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import * as core from '@actions/core'
import * as github from '@actions/github'
import { ActionsGetWorkflowResponseData } from '@octokit/types'

//
// Main task function (async wrapper)
Expand All @@ -15,10 +16,12 @@ async function run(): Promise<void> {
try {
// Required inputs
const token = core.getInput('token')
const workflowReference = core.getInput('workflow')
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, this MUST be a valid JSON string
let inputs = {}
Expand All @@ -30,30 +33,22 @@ async function run(): Promise<void> {
// Get octokit client for making API calls
const octokit = github.getOctokit(token)

// List workflows in repo 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 💩`)
// List workflows via API
const workflows: ActionsGetWorkflowResponseData[] =
await octokit.paginate(octokit.actions.listRepoWorkflows.endpoint.merge({ owner, repo, ref, inputs }))

// Debug response if ACTIONS_STEP_DEBUG is enabled
core.debug('### START List Workflows response data')
core.debug(JSON.stringify(listResp.data, null, 3))
core.debug(JSON.stringify(workflows, null, 3))
core.debug('### END: List Workflows response data')

// Locate workflow by name as we need it's id
const foundWorkflow = listResp.data.workflows.find((wf: Record<string, string>) => {
// Match on name or id, there's a slim chance someone names their workflow 1803663 but they are crazy
return (wf['name'] === workflowReference || wf['id'].toString() === workflowReference)
})

if(!foundWorkflow) throw new Error(`Unable to find workflow '${workflowReference}' in ${repo} 😥`)

console.log(`Workflow id is: ${foundWorkflow.id}`)
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 to trigger the workflow
const dispatchResp = await octokit.request(`POST /repos/${repo}/actions/workflows/${foundWorkflow.id}/dispatches`, {
// Call workflow_dispatch API
const dispatchResp = await octokit.request(`POST /repos/${owner}/${repo}/actions/workflows/${workflowFind.id}/dispatches`, {
ref: ref,
inputs: inputs
})
Expand Down

0 comments on commit 4cedbac

Please sign in to comment.