Skip to content

Commit 4156918

Browse files
authored
Refactor CI for forked repositories - Fix & enhance (yoanm#122)
1 parent 13ba822 commit 4156918

File tree

7 files changed

+53
-5
lines changed

7 files changed

+53
-5
lines changed

.github/actions/reports-group/codacy-uploader-action/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ runs:
7979
- name: Upload to codacy
8080
id: upload
8181
# Temporarily rely on internal fork, waiting for updates to be merges on original action
82-
uses: yoanm/codacy-coverage-reporter-action@e8bf406ddc38eb22a7d302eb1d20df5090d36da1
82+
uses: yoanm/codacy-coverage-reporter-action@d641c0af7ba34e9ddd18422ca95827100307ad71
8383
with:
8484
coverage-reports: ${{ steps.build-uploader-options.outputs.coverage-reports }}
8585
project-token: ${{ inputs.project-token }}

.github/actions/reports-group/fetch-workflow-metadata-action/action.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,21 @@ outputs:
1818
commit-sha:
1919
description: |
2020
Full commit SHA.
21+
- `push` event => Branch where the push happened
22+
- `pull_request` event => Head branch name
23+
- `workflow_run` event => Commit upon which the triggering workflow has been executed (should match behaviors described on other cases then)
24+
- Other events => Fallback on `github.sha` value
25+
branch:
26+
description: |
27+
Branch name
2128
- `push` event => latest commit pushed
2229
- `pull_request` event => latest commit pushed on the pull request
23-
- Other events => null
30+
- `workflow_run` event => Branch where the triggering workflow has been executed (should match behaviors described on other cases then)
31+
- Other events => Fallback on `github.ref` value if it's a branch ref, else the repository default branch is returned
2432
pull-request:
2533
description: Pull request number. Available only in case of `pull_request` event !
34+
is-pr-from-fork:
35+
description: Weither PR head is outside of current repository. Obviously accurate only in case of `pull_request` event !
2636
workflow-name:
2737
description: Name of the workflow, either current one or the triggering one (based on `from-triggering-workflow` value).
2838
run-id:

.github/actions/reports-group/fetch-workflow-metadata-action/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ async function run() {
88
core.setOutput('repository-owner', context.repositoryOwner);
99
core.setOutput('repository-name', context.repositoryName);
1010
core.setOutput('commit-sha', context.commitSha);
11+
core.setOutput('branch', context.branch);
1112
core.setOutput('pull-request', context.prNumber ?? null); // Ensure `null` rather than `undefined` (better/easier for end-user)!
13+
core.setOutput('is-pr-from-fork', context.isPrFromFork);
1214
core.setOutput('workflow-name', context.workflowName);
1315
core.setOutput('run-id', context.runId);
1416
core.setOutput('server-url', context.serverUrl);

.github/actions/reports-group/node-gha-helpers/src/current-workflow-context.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import {buildWorkflowRunUrl} from "./common";
2-
31
const {context: ghaContext} = require('@actions/github');
42
const {payload: ghaEvent} = ghaContext;
53

64
const {isPullRequestEvent, isPushEvent} = require('./current-workflow-event');
5+
import {buildWorkflowRunUrl} from "./common";
76

87
/**
98
* @type {GHAContextGetter}
@@ -16,7 +15,9 @@ export const getContext = () => {
1615
repositoryOwner: ghaContext.repo.owner,
1716
repositoryName: ghaContext.repo.repo,
1817
commitSha: getCommitSha(),
18+
branch: getBranch(),
1919
prNumber: prNumber,
20+
isPrFromFork: isPRFromFork(),
2021
workflowName: getWorkflowName(),
2122
serverUrl: ghaContext.serverUrl,
2223
runId: runId,
@@ -52,3 +53,21 @@ export const getWorkflowName = () => ghaContext.workflow;
5253
* @returns {string}
5354
*/
5455
export const getRunId = () => ghaContext.runId.toString();
56+
57+
/**
58+
* @return {string}
59+
*/
60+
export const getBranch = () => {
61+
if (isPullRequestEvent()) {
62+
return ghaEvent.pull_request.head.ref;
63+
}
64+
65+
// In case ref is not a branch (e.g. a tag), fallback to repository default branch
66+
return ghaEvent.ref.startsWith('refs/heads') ? ghaEvent.ref.replace('refs/heads/', '') : ghaEvent.repository.default_branch;
67+
};
68+
69+
70+
/**
71+
* @return {boolean}
72+
*/
73+
export const isPRFromFork = () => isPullRequestEvent() && ghaEvent.pull_request.head.repo.id === ghaEvent.pull_request.base.repo.id;

.github/actions/reports-group/node-gha-helpers/src/interfaces.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @typedef {{repositoryOwner: string, repositoryName: string, commitSha: string, prNumber: number|undefined, workflowName: string, runId: string, workflowRunUrl: string}} GHAContext
2+
* @typedef {{repositoryOwner: string, repositoryName: string, commitSha: string, branch: string, prNumber: number|undefined, isPrFromFork: boolean, workflowName: string, runId: string, workflowRunUrl: string}} GHAContext
33
*/
44
/**
55
* @typedef {() => GHAContext} GHAContextGetter

.github/actions/reports-group/node-gha-helpers/src/workflow-run-context.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ export const getContext = () => {
1515
repositoryOwner: ghaContext.repo.owner,
1616
repositoryName: ghaContext.repo.repo,
1717
commitSha: getCommitSha(),
18+
branch: getBranch(),
1819
prNumber: prNumber,
20+
isPrFromFork: isPRFromFork(),
1921
workflowName: getWorkflowName(),
2022
serverUrl: ghaContext.serverUrl,
2123
runId: runId,
@@ -42,3 +44,13 @@ export const getWorkflowName = () => ghaEvent.workflow.name;
4244
* @returns {string}
4345
*/
4446
export const getRunId = () => ghaEvent.workflow_run.id.toString();
47+
48+
/**
49+
* @return {string}
50+
*/
51+
export const getBranch = () => ghaEvent.workflow_run.head_branch;
52+
53+
/**
54+
* @return {boolean}
55+
*/
56+
export const isPRFromFork = () => isPullRequestEvent() && ghaEvent.workflow_run.head_repository.id === ghaEvent.workflow_run.repository.id;

.github/workflows/coverage-upload.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jobs:
2222

2323
- uses: ./custom-action-repo/.github/actions/reports-group/attach-check-run-to-triggering-workflow-action
2424
with:
25+
name: "Fetch coverage info"
2526
github-token: ${{ github.token }}
2627
job-status: ${{ job.status }}
2728
fails-on-triggering-workflow-failure: true
@@ -51,6 +52,10 @@ jobs:
5152
artifacts-pattern: coverage-groups-*
5253
run-id: ${{ needs.fetch-info.outputs.run-id }}
5354
override-commit: ${{ needs.fetch-info.outputs.commit-sha }}
55+
override-branch: ${{ needs.fetch-info.outputs.branch }}
56+
override-pr: ${{ needs.fetch-info.outputs.pr-number }}
57+
override-build: ${{ needs.fetch-info.outputs.run-id }}
58+
override-build-url: ${{ needs.fetch-info.outputs.run-url }}
5459
permissions:
5560
contents: read
5661
checks: write # For the check run creation !

0 commit comments

Comments
 (0)