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 !
22const core = require ( '@actions/core' ) ;
33
4- const { GITHUB_REPOSITORY , RUNNER_NAME } = process . env ;
4+ const { GITHUB_REPOSITORY } = process . env ;
55
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' ) ;
977
988async function run ( ) {
999 /** INPUTS **/
@@ -102,60 +12,43 @@ async function run() {
10212 const checkName = core . getInput ( 'name' ) ;
10313
10414 const isSuccessfulJobAsOfNow = 'success' === jobStatus ;
105- const octokit = github . getOctokit ( githubToken ) ;
15+ const octokit = getOctokit ( githubToken ) ;
10616
10717 const requestParams = await core . group (
10818 'Build API params' ,
10919 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+
13027 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 ;
13730 const outputSummary = '🪢 Check added by '
13831 + ( 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 ? ')' : '' )
14033 ;
14134
14235 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 ,
14538 //details_url: detailsUrl,
146- external_id : triggeringWorkflowRunId ?. toString ( ) ,
39+ external_id : triggeringWorkflowContext . runId ,
14740 status : isSuccessfulJobAsOfNow ? 'in_progress' : 'completed' ,
14841 output : {
149- title : outputTitle ,
42+ title : '🔔 ' + currentWorkflowContext . workflowName ,
15043 summary : outputSummary ,
15144 } ,
15245 // Conclusion
15346 conclusion : isSuccessfulJobAsOfNow ? undefined : jobStatus ,
15447 started_at : startedAt ,
15548 completed_at : isSuccessfulJobAsOfNow ? undefined : startedAt ,
15649 // Url path parameters
157- owner : repoInfo . owner ,
158- repo : repoInfo . repo
50+ owner : currentWorkflowContext . repositoryOwner ,
51+ repo : currentWorkflowContext . repositoryName
15952 } ;
16053 }
16154 ) ;
0 commit comments