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 , tagGitCheckRun ) => {
15+ const commitSHA = tagGitCheckRun . head_sha
16+ const conclusion = tagGitCheckRun . conclusion
17+ const text = tagGitCheckRun . output . text
18+
19+ if ( conclusion !== 'success' ) {
20+ throw new Error ( `tag-git run ${ tagGitCheckRun . id } completed with ${ conclusion } : ${ tagGitCheckRun . html_url } ` )
21+ }
22+
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 ${ tagGitCheckRun . id } : ${ tagGitCheckRun . 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 ${ tagGitCheckRun . id } : ${ tagGitCheckRun . url } ` )
30+ }
31+
32+ const gitVersionMatch = tagGitCheckRun . 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 '${ tagGitCheckRun . output . summary } ' of tag-git run ${ tagGitCheckRun . id } : ${ tagGitCheckRun . url } ` )
35+ }
36+ if ( commitSHA !== gitVersionMatch [ 2 ] ) {
37+ throw new Error ( `Expected ${ commitSHA } in summary '${ tagGitCheckRun . output . summary } ' of tag-git run ${ tagGitCheckRun . id } : ${ tagGitCheckRun . url } ` )
38+ }
39+ const gitVersion = gitVersionMatch [ 1 ]
40+
41+ let res = ''
42+
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+ }
66+
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+ }
84+
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
96+ }
97+ )
98+ res = `${ res } The \`git-artifacts-${ architecture } \` workflow run [was started](${ run . html_url } ).\n`
99+ }
100+
101+ return res
102+ }
103+
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
110+
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 } ` )
115+ }
116+
117+ return await triggerGitArtifactsRuns ( context , checkRunOwner , checkRunRepo , checkRun )
118+ }
119+ return `Not a cascading run: ${ name } ; Doing nothing.`
120+ }
121+ return `Unhandled action: ${ action } `
122+ }
123+
124+ module . exports = {
125+ triggerGitArtifactsRuns,
126+ cascadingRuns
127+ }
0 commit comments