Skip to content

Commit 337a447

Browse files
authored
Refactor CI for forked repositories - fetch-gha-metadata (yoanm#115)
1 parent a2dae75 commit 337a447

32 files changed

+1940
-131
lines changed

.github/actions/reports-group/attach-check-run-to-triggering-workflow-action/dist/LICENSE

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.github/actions/reports-group/attach-check-run-to-triggering-workflow-action/dist/index.js

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.github/actions/reports-group/attach-check-run-to-triggering-workflow-action/dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../node-gha-helpers

.github/actions/reports-group/attach-check-run-to-triggering-workflow-action/src/main.js

Lines changed: 20 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,9 @@
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 !
22
const 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

988
async 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
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/** -diff linguist-generated=true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.yarn
2+
node_modules
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
20
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.PHONY: install
2+
install:
3+
yarn install
4+
5+
.PHONY: build
6+
build: install package
7+
8+
.PHONY: package
9+
package:
10+
rm -Rf dist/* && ./node_modules/.bin/ncc build index.js --minify --source-map --license LICENSE --out dist
11+
12+
.PHONY: lint
13+
lint:
14+
./node_modules/.bin/eslint index.js
15+
16+
.PHONY: test
17+
test:
18+
echo "Error: no test specified"
19+
exit 1
20+
21+
get-action-nodejs-version: ## Display node version configured on action.yml
22+
@grep -E "using:\s*'?node" action.yml | sed -e "s/^.*using: '*node\([0-9][0-9]\)'*.*$$/\1/"
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: TODO
2+
description: TODO
3+
4+
inputs:
5+
from-triggering-workflow:
6+
description: |
7+
Weither to look for triggering workflow context or just current workflow context (default `true`).
8+
- True: In case current workflow event is `workflow_run`, the context of the workflow which **triggered** the current one will be returned.
9+
- False: Return the context for the current workflow.
10+
default: 'true'
11+
12+
outputs:
13+
commit-sha:
14+
description: |
15+
Full commit SHA.
16+
- `push` event => latest commit pushed
17+
- `pull_request` event => latest commit pushed on the pull request
18+
- Other events => null
19+
pull-request:
20+
description: Pull request number. Available only in case of `pull_request` event !
21+
workflow-name:
22+
description: Name of the workflow, either current one or the triggering one (based on `from-triggering-workflow` value).
23+
run-id:
24+
description: Workflow run ID, either current one or the triggering one (based on `from-triggering-workflow` value).
25+
26+
27+
runs:
28+
using: 'node20'
29+
main: 'dist/index.js'

0 commit comments

Comments
 (0)