1
- const github = require ( '@actions/github' ) ; // @TODO move to 'imports from' when moved to TS !
1
+ const { getOctokit } = require ( '@actions/github' ) ; // @TODO move to 'imports from' when moved to TS !
2
2
const core = require ( '@actions/core' ) ;
3
3
4
- const { GITHUB_REPOSITORY , RUNNER_NAME } = process . env ;
4
+ const { GITHUB_REPOSITORY } = process . env ;
5
5
6
- /**
7
- * @returns {number|undefined }
8
- */
9
- function guessTriggeringPrNumber ( ) {
10
- if ( 'pull_request' === github . context . eventName ) {
11
- return github . context . payload . number ;
12
- } else if ( 'workflow_run' === github . context . eventName && 'pull_request' === github . context . payload . workflow_run . event ) {
13
- return github . context . payload . workflow_run . pull_requests [ 0 ] ?. number ;
14
- }
15
-
16
- return undefined ;
17
- }
18
-
19
- /**
20
- * @returns {string|undefined }
21
- */
22
- function guessTriggeringCommitSha ( ) {
23
- if ( 'pull_request' === github . context . eventName ) {
24
- return github . context . payload . pull_request . head . sha ;
25
- }
26
- if ( 'push' === github . context . eventName ) {
27
- return github . context . payload . after ;
28
- }
29
- if ( 'workflow_run' === github . context . eventName && [ 'pull_request' , 'push' ] . includes ( github . context . payload . workflow_run . event ) ) {
30
- return github . context . payload . workflow_run . head_sha ;
31
- }
32
-
33
- throw new Error ( 'Unable to guess the commit SHA !' ) ;
34
- }
35
-
36
- /**
37
- * @returns {string }
38
- */
39
- function guessTriggeringWorkflowName ( ) {
40
- if ( 'workflow_run' === github . context . eventName ) {
41
- return github . context . payload . workflow . name ;
42
- }
43
-
44
- return github . context . workflow ;
45
- }
46
-
47
- /**
48
- * @returns {string }
49
- */
50
- function guessTriggeringRunId ( ) {
51
- if ( 'workflow_run' === github . context . eventName ) {
52
- return github . context . payload . workflow . id . toString ( ) ;
53
- }
54
-
55
- return github . context . runId . toString ( ) ;
56
- }
57
-
58
- /**
59
- * @returns {Promise<Record<string, any>|undefined> }
60
- */
61
- async function retrieveCurrentJob ( octokit , owner , repo , runId ) {
62
- const jobList = await getWorkflowJobsForRunId ( octokit , owner , repo , runId ) ;
63
- core . info ( 'TMP DEBUG jobsForCurrentWorkflow=' + JSON . stringify ( jobList ) ) ;
64
- const candidateList = [ ] ;
65
- for ( const job of jobList ) {
66
- if ( RUNNER_NAME === job . runner_name && 'in_progress' === job . status ) {
67
- candidateList . push ( job ) ;
68
- }
69
- }
70
- if ( candidateList . length === 0 ) {
71
- core . info ( 'Unable to retrieve the current job !' ) ;
72
- return undefined ;
73
- }
74
- if ( candidateList . length > 1 ) {
75
- core . warning (
76
- 'Multiple running jobs rely on runners with the same name, unable to retrieve the current job !'
77
- + '\nCandidates: ' + Object . entries ( candidateList ) . map ( ( [ k , v ] ) => v . name + '(' + k + ')' ) . join ( ', ' )
78
- ) ;
79
- return undefined ;
80
- }
81
-
82
- return candidateList . shift ( ) ;
83
- }
84
-
85
- async function getWorkflowJobsForRunId ( octokit , owner , repo , runId ) {
86
- return octokit . paginate (
87
- 'GET /repos/{owner}/{repo}/actions/runs/{run_id}/jobs' ,
88
- {
89
- //filter: 'latest',
90
- // Url path parameters
91
- owner : owner ,
92
- repo : repo ,
93
- run_id : runId
94
- }
95
- ) ;
96
- }
6
+ const ghaHelpers = require ( '../node-gha-helpers' ) ;
97
7
98
8
async function run ( ) {
99
9
/** INPUTS **/
@@ -102,60 +12,43 @@ async function run() {
102
12
const checkName = core . getInput ( 'name' ) ;
103
13
104
14
const isSuccessfulJobAsOfNow = 'success' === jobStatus ;
105
- const octokit = github . getOctokit ( githubToken ) ;
15
+ const octokit = getOctokit ( githubToken ) ;
106
16
107
17
const requestParams = await core . group (
108
18
'Build API params' ,
109
19
async ( ) => {
110
- const repoInfo = github . context . repo ;
111
- const triggeringWorkflowRunId = guessTriggeringRunId ( ) ;
112
- core . info ( 'TMP DEBUG context=' + JSON . stringify ( github . context ) ) ;
113
- //const jobsForCurrentWorkflow = await getWorkflowJobsForRunId(octokit, repoInfo.owner, repoInfo.repo, github.context.runId);
114
- //core.info('TMP DEBUG jobsForCurrentWorkflow=' + JSON.stringify(jobsForCurrentWorkflow));
115
- //const jobsForTriggeringWorkflow = await getWorkflowJobsForRunId(octokit, repoInfo.owner, repoInfo.repo, triggeringWorkflowRunId);
116
- //core.info('TMP DEBUG jobsForTriggeringWorkflow=' + JSON.stringify(jobsForTriggeringWorkflow));
117
- core . info ( 'TMP DEBUG GITHUB_ACTION=' + process . env . GITHUB_ACTION ) ;
118
- core . info ( 'TMP DEBUG GITHUB_ACTION_PATH=' + process . env . GITHUB_ACTION_PATH ) ;
119
- core . info ( 'TMP DEBUG GITHUB_ACTION_REPOSITORY=' + process . env . GITHUB_ACTION_REPOSITORY ) ;
120
- core . info ( 'TMP DEBUG GITHUB_JOB=' + process . env . GITHUB_JOB ) ;
121
- core . info ( 'TMP DEBUG GITHUB_RUN_ATTEMPT=' + process . env . GITHUB_RUN_ATTEMPT ) ;
122
- core . info ( 'TMP DEBUG GITHUB_WORKFLOW=' + process . env . GITHUB_WORKFLOW ) ;
123
- core . info ( 'TMP DEBUG GITHUB_WORKFLOW_REF=' + process . env . GITHUB_WORKFLOW_REF ) ;
124
- core . info ( 'TMP DEBUG RUNNER_ARCH=' + process . env . RUNNER_ARCH ) ;
125
- core . info ( 'TMP DEBUG RUNNER_NAME=' + process . env . RUNNER_NAME ) ;
126
- core . info ( 'TMP DEBUG RUNNER_OS=' + process . env . RUNNER_OS ) ;
127
- const currentJob = await retrieveCurrentJob ( octokit , repoInfo . owner , repoInfo . repo , github . context . runId ) ;
128
- core . info ( 'TMP DEBUG CURRENT JOB=' + JSON . stringify ( currentJob ) ) ;
129
- const commitSha = guessTriggeringCommitSha ( ) ;
20
+ const currentWorkflowContext = ghaHelpers . getContext ( ) ;
21
+ const triggeringWorkflowContext = ghaHelpers . triggeringWorkflow . getContext ( ) ;
22
+ if ( ! triggeringWorkflowContext . commitSha ) {
23
+ throw new Error ( 'Unable to guess the commit SHA !' ) ;
24
+ }
25
+ const currentJob = await ghaHelpers . fetchCurrentJob ( octokit ) ;
26
+
130
27
const startedAt = ( new Date ( ) ) . toISOString ( ) ;
131
- const prNumber = guessTriggeringPrNumber ( ) ;
132
- //const originalWorkflowName = guessTriggeringWorkflowName();
133
- const currentWorkflowName = github . context . workflow ;
134
- const outputTitle = '🔔 ' + currentWorkflowName ;
135
- const prLink = ( undefined !== prNumber ? '?pr=' + prNumber : '' ) ;
136
- const currentWorkflowUrl = github . context . serverUrl + '/' + GITHUB_REPOSITORY + '/actions/runs/' + github . context . runId . toString ( ) + prLink ;
28
+ const prLink = ( undefined !== triggeringWorkflowContext . prNumber ? '?pr=' + triggeringWorkflowContext . prNumber : '' ) ;
29
+ const currentWorkflowUrl = currentWorkflowContext . serverUrl + '/' + GITHUB_REPOSITORY + '/actions/runs/' + currentWorkflowContext . runId + prLink ;
137
30
const outputSummary = '🪢 Check added by '
138
31
+ ( currentJob ? '<a href="' + currentJob . html_url + prLink + '" target="blank">**' + currentJob . name + '**</a>' : '' )
139
- + ( currentJob ? ' (' : '' ) + '<a href="' + currentWorkflowUrl + '" target="blank">**' + currentWorkflowName + '** workflow</a>' + ( currentJob ? ')' : '' )
32
+ + ( currentJob ? ' (' : '' ) + '<a href="' + currentWorkflowUrl + '" target="blank">**' + currentWorkflowContext . workflowName + '** workflow</a>' + ( currentJob ? ')' : '' )
140
33
;
141
34
142
35
return {
143
- name : checkName ? checkName : ( currentJob ?. name ?? currentWorkflowName + ' Check run' ) ,
144
- head_sha : commitSha ,
36
+ name : ! ! checkName ? checkName : ( currentJob ?. name ?? currentWorkflowContext . workflowName + ' Check run' ) ,
37
+ head_sha : triggeringWorkflowContext . commitSha ,
145
38
//details_url: detailsUrl,
146
- external_id : triggeringWorkflowRunId ?. toString ( ) ,
39
+ external_id : triggeringWorkflowContext . runId ,
147
40
status : isSuccessfulJobAsOfNow ? 'in_progress' : 'completed' ,
148
41
output : {
149
- title : outputTitle ,
42
+ title : '🔔 ' + currentWorkflowContext . workflowName ,
150
43
summary : outputSummary ,
151
44
} ,
152
45
// Conclusion
153
46
conclusion : isSuccessfulJobAsOfNow ? undefined : jobStatus ,
154
47
started_at : startedAt ,
155
48
completed_at : isSuccessfulJobAsOfNow ? undefined : startedAt ,
156
49
// Url path parameters
157
- owner : repoInfo . owner ,
158
- repo : repoInfo . repo
50
+ owner : currentWorkflowContext . repositoryOwner ,
51
+ repo : currentWorkflowContext . repositoryName
159
52
} ;
160
53
}
161
54
) ;
0 commit comments