77
88import * as core from '@actions/core'
99import * 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