Skip to content

Commit

Permalink
Refactor CI for forked repositories - Fix & enhance (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoanm authored Mar 24, 2024
1 parent 13ba822 commit 4156918
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ runs:
- name: Upload to codacy
id: upload
# Temporarily rely on internal fork, waiting for updates to be merges on original action
uses: yoanm/codacy-coverage-reporter-action@e8bf406ddc38eb22a7d302eb1d20df5090d36da1
uses: yoanm/codacy-coverage-reporter-action@d641c0af7ba34e9ddd18422ca95827100307ad71
with:
coverage-reports: ${{ steps.build-uploader-options.outputs.coverage-reports }}
project-token: ${{ inputs.project-token }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,21 @@ outputs:
commit-sha:
description: |
Full commit SHA.
- `push` event => Branch where the push happened
- `pull_request` event => Head branch name
- `workflow_run` event => Commit upon which the triggering workflow has been executed (should match behaviors described on other cases then)
- Other events => Fallback on `github.sha` value
branch:
description: |
Branch name
- `push` event => latest commit pushed
- `pull_request` event => latest commit pushed on the pull request
- Other events => null
- `workflow_run` event => Branch where the triggering workflow has been executed (should match behaviors described on other cases then)
- Other events => Fallback on `github.ref` value if it's a branch ref, else the repository default branch is returned
pull-request:
description: Pull request number. Available only in case of `pull_request` event !
is-pr-from-fork:
description: Weither PR head is outside of current repository. Obviously accurate only in case of `pull_request` event !
workflow-name:
description: Name of the workflow, either current one or the triggering one (based on `from-triggering-workflow` value).
run-id:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ async function run() {
core.setOutput('repository-owner', context.repositoryOwner);
core.setOutput('repository-name', context.repositoryName);
core.setOutput('commit-sha', context.commitSha);
core.setOutput('branch', context.branch);
core.setOutput('pull-request', context.prNumber ?? null); // Ensure `null` rather than `undefined` (better/easier for end-user)!
core.setOutput('is-pr-from-fork', context.isPrFromFork);
core.setOutput('workflow-name', context.workflowName);
core.setOutput('run-id', context.runId);
core.setOutput('server-url', context.serverUrl);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {buildWorkflowRunUrl} from "./common";

const {context: ghaContext} = require('@actions/github');
const {payload: ghaEvent} = ghaContext;

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

/**
* @type {GHAContextGetter}
Expand All @@ -16,7 +15,9 @@ export const getContext = () => {
repositoryOwner: ghaContext.repo.owner,
repositoryName: ghaContext.repo.repo,
commitSha: getCommitSha(),
branch: getBranch(),
prNumber: prNumber,
isPrFromFork: isPRFromFork(),
workflowName: getWorkflowName(),
serverUrl: ghaContext.serverUrl,
runId: runId,
Expand Down Expand Up @@ -52,3 +53,21 @@ export const getWorkflowName = () => ghaContext.workflow;
* @returns {string}
*/
export const getRunId = () => ghaContext.runId.toString();

/**
* @return {string}
*/
export const getBranch = () => {
if (isPullRequestEvent()) {
return ghaEvent.pull_request.head.ref;
}

// In case ref is not a branch (e.g. a tag), fallback to repository default branch
return ghaEvent.ref.startsWith('refs/heads') ? ghaEvent.ref.replace('refs/heads/', '') : ghaEvent.repository.default_branch;
};


/**
* @return {boolean}
*/
export const isPRFromFork = () => isPullRequestEvent() && ghaEvent.pull_request.head.repo.id === ghaEvent.pull_request.base.repo.id;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @typedef {{repositoryOwner: string, repositoryName: string, commitSha: string, prNumber: number|undefined, workflowName: string, runId: string, workflowRunUrl: string}} GHAContext
* @typedef {{repositoryOwner: string, repositoryName: string, commitSha: string, branch: string, prNumber: number|undefined, isPrFromFork: boolean, workflowName: string, runId: string, workflowRunUrl: string}} GHAContext
*/
/**
* @typedef {() => GHAContext} GHAContextGetter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export const getContext = () => {
repositoryOwner: ghaContext.repo.owner,
repositoryName: ghaContext.repo.repo,
commitSha: getCommitSha(),
branch: getBranch(),
prNumber: prNumber,
isPrFromFork: isPRFromFork(),
workflowName: getWorkflowName(),
serverUrl: ghaContext.serverUrl,
runId: runId,
Expand All @@ -42,3 +44,13 @@ export const getWorkflowName = () => ghaEvent.workflow.name;
* @returns {string}
*/
export const getRunId = () => ghaEvent.workflow_run.id.toString();

/**
* @return {string}
*/
export const getBranch = () => ghaEvent.workflow_run.head_branch;

/**
* @return {boolean}
*/
export const isPRFromFork = () => isPullRequestEvent() && ghaEvent.workflow_run.head_repository.id === ghaEvent.workflow_run.repository.id;
5 changes: 5 additions & 0 deletions .github/workflows/coverage-upload.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:

- uses: ./custom-action-repo/.github/actions/reports-group/attach-check-run-to-triggering-workflow-action
with:
name: "Fetch coverage info"
github-token: ${{ github.token }}
job-status: ${{ job.status }}
fails-on-triggering-workflow-failure: true
Expand Down Expand Up @@ -51,6 +52,10 @@ jobs:
artifacts-pattern: coverage-groups-*
run-id: ${{ needs.fetch-info.outputs.run-id }}
override-commit: ${{ needs.fetch-info.outputs.commit-sha }}
override-branch: ${{ needs.fetch-info.outputs.branch }}
override-pr: ${{ needs.fetch-info.outputs.pr-number }}
override-build: ${{ needs.fetch-info.outputs.run-id }}
override-build-url: ${{ needs.fetch-info.outputs.run-url }}
permissions:
contents: read
checks: write # For the check run creation !
Expand Down

0 comments on commit 4156918

Please sign in to comment.