1- module . exports = async ( context , req ) => {
2- const action = req . body . action
3- const checkRunOwner = req . body . repository . owner . login
4- const checkRunRepo = req . body . repository . name
5- const checkRun = req . body . check_run
1+ const getToken = ( ( ) => {
2+ const tokens = { }
3+
4+ const get = async ( context , owner , repo ) => {
5+ const getInstallationIdForRepo = require ( './get-installation-id-for-repo' )
6+ const installationId = await getInstallationIdForRepo ( context , owner , repo )
7+ const getInstallationAccessToken = require ( './get-installation-access-token' )
8+ return await getInstallationAccessToken ( context , installationId )
9+ }
10+
11+ return async ( context , owner , repo ) => tokens [ [ owner , repo ] ] || ( tokens [ [ owner , repo ] ] = await get ( context , owner , repo ) )
12+ } ) ( )
13+
14+ const triggerGitArtifactsRuns = async ( context , checkRunOwner , checkRunRepo , checkRun ) => {
615 const commitSHA = checkRun . head_sha
7- const name = checkRun . name
816 const conclusion = checkRun . conclusion
917 const text = checkRun . output . text
1018
11- const getToken = ( ( ) => {
12- const tokens = { }
19+ if ( conclusion !== 'success' ) {
20+ throw new Error ( `tag-git run ${ checkRun . id } completed with ${ conclusion } : ${ checkRun . html_url } ` )
21+ }
1322
14- const get = async ( owner , repo ) => {
15- const getInstallationIdForRepo = require ( './get-installation-id-for-repo' )
16- const installationId = await getInstallationIdForRepo ( context , owner , repo )
17- const getInstallationAccessToken = require ( './get-installation-access-token' )
18- return await getInstallationAccessToken ( context , installationId )
19- }
23+ const match = text . match ( / F o r d e t a i l s , s e e \[ t h i s r u n \] \( h t t p s : \/ \/ g i t h u b .c o m \/ ( [ ^ / ] + ) \/ ( [ ^ / ] + ) \/ a c t i o n s \/ r u n s \/ ( \d + ) \) / )
24+ if ( ! match ) throw new Error ( `Unhandled 'text' attribute of tag-git run ${ checkRun . id } : ${ checkRun . url } ` )
25+ const owner = match [ 1 ]
26+ const repo = match [ 2 ]
27+ const workflowRunId = Number ( match [ 3 ] )
28+ if ( owner !== 'git-for-windows' || repo !== 'git-for-windows-automation' ) {
29+ throw new Error ( `Unexpected repository ${ owner } /${ repo } for tag-git run ${ checkRun . id } : ${ checkRun . url } ` )
30+ }
2031
21- return async ( owner , repo ) => tokens [ [ owner , repo ] ] || ( tokens [ [ owner , repo ] ] = await get ( owner , repo ) )
22- } ) ( )
32+ const gitVersionMatch = checkRun . output . summary . match ( / ^ T a g G i t ( \S + ) @ ( [ 0 - 9 a - f ] + ) $ / )
33+ if ( ! gitVersionMatch ) {
34+ throw new Error ( `Could not parse Git version from summary '${ checkRun . output . summary } ' of tag-git run ${ checkRun . id } : ${ checkRun . url } ` )
35+ }
36+ if ( commitSHA !== gitVersionMatch [ 2 ] ) {
37+ throw new Error ( `Expected ${ commitSHA } in summary '${ checkRun . output . summary } ' of tag-git run ${ checkRun . id } : ${ checkRun . url } ` )
38+ }
39+ const gitVersion = gitVersionMatch [ 1 ]
2340
24- if ( action === 'completed' ) {
25- if ( name === 'tag-git' ) {
26- if ( checkRunOwner !== 'git-for-windows' || checkRunRepo !== 'git' ) {
27- throw new Error ( `Refusing to handle cascading run in ${ checkRunOwner } /${ checkRunRepo } ` )
28- }
41+ let res = ''
2942
30- if ( conclusion !== 'success' ) {
31- throw new Error ( `tag-git run ${ checkRun . id } completed with ${ conclusion } : ${ checkRun . html_url } ` )
32- }
43+ const architecturesToTrigger = [ ]
44+ const { listCheckRunsForCommit, queueCheckRun } = require ( './check-runs' )
45+ for ( const architecture of [ 'x86_64' , 'i686' ] ) {
46+ const workflowName = `git-artifacts-${ architecture } `
47+ const runs = await listCheckRunsForCommit (
48+ context ,
49+ await getToken ( context , checkRunOwner , checkRunRepo ) ,
50+ checkRunOwner ,
51+ checkRunRepo ,
52+ commitSHA ,
53+ workflowName
54+ )
55+ const latest = runs
56+ . filter ( run => run . output . summary . endsWith ( `(tag-git run #${ workflowRunId } )` ) )
57+ . sort ( ( a , b ) => a . id - b . id )
58+ . pop ( )
59+ if ( latest && ( latest . status !== 'completed' || latest . conclusion === 'success' ) ) {
60+ // It either succeeded or is still running
61+ res = `${ res } ${ workflowName } run already exists at ${ latest . html_url } .\n`
62+ } else {
63+ architecturesToTrigger . push ( architecture )
64+ }
65+ }
3366
34- const match = text . match ( / F o r d e t a i l s , s e e \[ t h i s r u n \] \( h t t p s : \/ \/ g i t h u b .c o m \/ ( [ ^ / ] + ) \/ ( [ ^ / ] + ) \/ a c t i o n s \/ r u n s \/ ( \d + ) \) / )
35- if ( ! match ) throw new Error ( `Unhandled 'text' attribute of tag-git run ${ checkRun . id } : ${ checkRun . url } ` )
36- const owner = match [ 1 ]
37- const repo = match [ 2 ]
38- const workflowRunId = Number ( match [ 3 ] )
39- if ( owner !== 'git-for-windows' || repo !== 'git-for-windows-automation' ) {
40- throw new Error ( `Unexpected repository ${ owner } /${ repo } for tag-git run ${ checkRun . id } : ${ checkRun . url } ` )
41- }
67+ if ( architecturesToTrigger . length === 0 ) return `${ res } No workflows need to be run!\n`
68+
69+ for ( const architecture of architecturesToTrigger ) {
70+ const workflowName = `git-artifacts-${ architecture } `
71+ const title = `Build Git ${ gitVersion } artifacts`
72+ const summary = `Build Git ${ gitVersion } artifacts from commit ${ commitSHA } (tag-git run #${ workflowRunId } )`
73+ await queueCheckRun (
74+ context ,
75+ await getToken ( context , checkRunOwner , checkRunRepo ) ,
76+ checkRunOwner ,
77+ checkRunRepo ,
78+ commitSHA ,
79+ workflowName ,
80+ title ,
81+ summary
82+ )
83+ }
4284
43- let res = ''
44-
45- const architecturesToTrigger = [ ]
46- const { listCheckRunsForCommit, queueCheckRun } = require ( './check-runs' )
47- for ( const architecture of [ 'x86_64' , 'i686' ] ) {
48- const workflowName = `git-artifacts-${ architecture } `
49- const runs = await listCheckRunsForCommit (
50- context ,
51- await getToken ( checkRunOwner , checkRunRepo ) ,
52- checkRunOwner ,
53- checkRunRepo ,
54- commitSHA ,
55- workflowName
56- )
57- const latest = runs
58- . filter ( run => run . output . summary . endsWith ( `(tag-git run #${ workflowRunId } )` ) )
59- . sort ( ( a , b ) => a . id - b . id )
60- . pop ( )
61- if ( latest && ( latest . status !== 'completed' || latest . conclusion === 'success' ) ) {
62- // It either succeeded or is still running
63- res = `${ res } ${ workflowName } run already exists at ${ latest . html_url } .\n`
64- } else {
65- architecturesToTrigger . push ( architecture )
66- }
85+ const triggerWorkflowDispatch = require ( './trigger-workflow-dispatch' )
86+ for ( const architecture of architecturesToTrigger ) {
87+ const run = await triggerWorkflowDispatch (
88+ context ,
89+ await getToken ( context , owner , repo ) ,
90+ owner ,
91+ repo ,
92+ 'git-artifacts.yml' ,
93+ 'main' , {
94+ architecture,
95+ tag_git_workflow_run_id : workflowRunId
6796 }
97+ )
98+ res = `${ res } The \`git-artifacts-${ architecture } \` workflow run [was started](${ run . html_url } ).\n`
99+ }
68100
69- if ( architecturesToTrigger . length === 0 ) return `${ res } No workflows need to be run!\n`
101+ return res
102+ }
70103
71- const gitVersionMatch = checkRun . output . summary . match ( / ^ T a g G i t ( \S + ) @ ( [ 0 - 9 a - f ] + ) $ / )
72- if ( ! gitVersionMatch ) {
73- throw new Error ( `Could not parse Git version from summary '${ checkRun . output . summary } ' of tag-git run ${ checkRun . id } : ${ checkRun . url } ` )
74- }
75- if ( commitSHA !== gitVersionMatch [ 2 ] ) {
76- throw new Error ( `Expected ${ commitSHA } in summary '${ checkRun . output . summary } ' of tag-git run ${ checkRun . id } : ${ checkRun . url } ` )
77- }
78- const gitVersion = gitVersionMatch [ 1 ]
79-
80- for ( const architecture of architecturesToTrigger ) {
81- const workflowName = `git-artifacts-${ architecture } `
82- const title = `Build Git ${ gitVersion } artifacts`
83- const summary = `Build Git ${ gitVersion } artifacts from commit ${ commitSHA } (tag-git run #${ workflowRunId } )`
84- await queueCheckRun (
85- context ,
86- await getToken ( checkRunOwner , checkRunRepo ) ,
87- checkRunOwner ,
88- checkRunRepo ,
89- commitSHA ,
90- workflowName ,
91- title ,
92- summary
93- )
94- }
104+ const cascadingRuns = async ( context , req ) => {
105+ const action = req . body . action
106+ const checkRunOwner = req . body . repository . owner . login
107+ const checkRunRepo = req . body . repository . name
108+ const checkRun = req . body . check_run
109+ const name = checkRun . name
95110
96- const triggerWorkflowDispatch = require ( './trigger-workflow-dispatch' )
97- for ( const architecture of architecturesToTrigger ) {
98- const run = await triggerWorkflowDispatch (
99- context ,
100- await getToken ( owner , repo ) ,
101- owner ,
102- repo ,
103- 'git-artifacts.yml' ,
104- 'main' , {
105- architecture,
106- tag_git_workflow_run_id : workflowRunId
107- }
108- )
109- res = `${ res } The \`git-artifacts-${ architecture } \` workflow run [was started](${ run . html_url } ).\n`
111+ if ( action === 'completed' ) {
112+ if ( name === 'tag-git' ) {
113+ if ( checkRunOwner !== 'git-for-windows' || checkRunRepo !== 'git' ) {
114+ throw new Error ( `Refusing to handle cascading run in ${ checkRunOwner } /${ checkRunRepo } ` )
110115 }
111116
112- return res
117+ return await triggerGitArtifactsRuns ( context , checkRunOwner , checkRunRepo , checkRun )
113118 }
114119 return `Not a cascading run: ${ name } ; Doing nothing.`
115120 }
116121 return `Unhandled action: ${ action } `
122+ }
123+
124+ module . exports = {
125+ triggerGitArtifactsRuns,
126+ cascadingRuns
117127}
0 commit comments