Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add GitHub context in reporter #15

Merged
merged 2 commits into from
Dec 21, 2023

Conversation

devpow112
Copy link
Contributor

@devpow112 devpow112 commented Dec 20, 2023

This means that at report generation time the github context will sourced. Helps with data consistency/accuracy.

Base automatically changed from depowell/chalk to main December 20, 2023 22:47
@devpow112 devpow112 force-pushed the depowell/add-context-to-reporter branch from d9a0595 to 612c6c6 Compare December 20, 2023 23:08
@devpow112 devpow112 marked this pull request as ready for review December 21, 2023 00:15
@devpow112 devpow112 requested a review from a team December 21, 2023 00:15
@devpow112 devpow112 added the enhancement New feature or request label Dec 21, 2023
@devpow112 devpow112 enabled auto-merge (squash) December 21, 2023 02:47
@devpow112

This comment was marked as resolved.

Comment on lines +7 to +41
const hasContext = () => {
const { env: { GITHUB_ACTIONS } } = process;

return !!GITHUB_ACTIONS;
};

const getContext = () => {
if (!hasContext()) {
throw new GitHubActionsUnavailableError();
}

const { env: {
GITHUB_REPOSITORY,
GITHUB_WORKFLOW_REF,
GITHUB_RUN_ID,
GITHUB_RUN_ATTEMPT,
GITHUB_HEAD_REF,
GITHUB_REF,
GITHUB_SHA
} } = process;
const [owner, repo] = GITHUB_REPOSITORY.split('/');
const [workflowPath] = GITHUB_WORKFLOW_REF.split('@');
const workflowRegex = new RegExp(`^${owner}/${repo}/.github/workflows/`);
const branchRef = GITHUB_HEAD_REF || GITHUB_REF;

return {
githubOrganization: owner,
githubRepository: repo,
githubWorkflow: workflowPath.replace(workflowRegex, ''),
githubRunId: parseInt(GITHUB_RUN_ID, 10),
githubRunAttempt: parseInt(GITHUB_RUN_ATTEMPT, 10),
gitBranch: branchRef.replace(/^refs\/heads\//i, ''),
gitSha: GITHUB_SHA
};
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These purposefully don't use the @actions/github package since it's harder to test. This is modeled after what it does internally at global script level but within a function so testing is easier and errors are better caught.

@devpow112 devpow112 merged commit 48fcaa4 into main Dec 21, 2023
3 checks passed
@devpow112 devpow112 deleted the depowell/add-context-to-reporter branch December 21, 2023 13:50
@ghost
Copy link

ghost commented Dec 21, 2023

🎉 This PR is included in version 0.0.7 🎉

The release is available on:

Your semantic-release bot 📦🚀

@ghost ghost added the released label Dec 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants