Skip to content

Commit

Permalink
Merge pull request #10 from Quantco/work-around-github-actions-bug
Browse files Browse the repository at this point in the history
  • Loading branch information
JaninaWibkerQC authored Sep 28, 2023
2 parents 3c06e52 + 4831caf commit 14270fb
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
version: 6.32.2
- uses: actions/setup-node@v3
with:
node-version: 16.x
node-version: 18.x
cache: pnpm

- run: pnpm install
Expand Down Expand Up @@ -47,7 +47,7 @@ jobs:
version: 6.32.2
- uses: actions/setup-node@v3
with:
node-version: 16.x
node-version: 18.x
cache: pnpm

- run: pnpm install
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr-gate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
version: 6.32.2
- uses: actions/setup-node@v3
with:
node-version: 16.x
node-version: 18.x
cache: pnpm

- run: pnpm install
Expand Down
2 changes: 1 addition & 1 deletion publish/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "publish",
"version": "1.0.11",
"version": "1.0.12",
"description": "An action that decides if a new release should be published",
"scripts": {
"build": "tsup",
Expand Down
2 changes: 1 addition & 1 deletion version-metadata/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "version-metadata",
"version": "1.0.11",
"version": "1.0.12",
"description": "An action that checks wether your package.json version has been updated with a bit of additional metadata",
"scripts": {
"build": "tsup",
Expand Down
15 changes: 14 additions & 1 deletion version-metadata/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getSemverDiffType,
computeResponseFromChanges,
determineBaseAndHead,
getParentCommitSha,
deduplicateConsecutive,
categorizeChangedFiles,
parseVersionFromFileContents
Expand Down Expand Up @@ -96,10 +97,22 @@ async function run(): Promise<VersionMetadataResponse> {
// all resolved correctly, just not "the bigger picture" (meaning all useful types in one place).
const octokit = getOctokit(token)

const { base, head } = determineBaseAndHead(context)
const { base: maybeBase, head } = determineBaseAndHead(context)

if (maybeBase === undefined) {
core.info(`no base commit found, assuming this is a new branch, fetching parent commit of ${head} from api`)
}

const base =
maybeBase === undefined
? await getParentCommitSha(octokit, context, head).catch((error) => {
throw new Error(`could not retrieve parent commit of ${head}: ${error.message}`)
})
: maybeBase

core.info(`base SHA: ${base}`)
core.info(`head SHA: ${head}`)

if (extractionMethod.type === 'command') {
core.info(`version extraction command: ${extractionMethod.command}`)
}
Expand Down
29 changes: 27 additions & 2 deletions version-metadata/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { spawnSync } from 'child_process'
import type { Context } from '@actions/github/lib/context'
import type { getOctokit } from '@actions/github'

export type VersionDiffType = 'major' | 'minor' | 'patch' | 'pre-release'

Expand Down Expand Up @@ -178,12 +179,14 @@ const computeResponseFromChanges = (
* - context.payload.pull_request?.head?.sha
*
* For pushes:
* - context.payload.before
* - context.payload.before (*)
* - context.payload.after
*
* For merge queues:
* - context.payload.merge_group?.base_sha
* - context.payload.merge_group?.head_sha
*
* (*): For pushes which create a new branch, context.payload.before is all zeroes (40 to be exact), in this case base is returned as undefined
*/
const determineBaseAndHead = (context: Context) => {
// Define the base and head commits to be extracted from the payload.
Expand All @@ -198,6 +201,13 @@ const determineBaseAndHead = (context: Context) => {
case 'push':
base = context.payload.before
head = context.payload.after

// when pushing a new branch, the case commit sha is all zeroes (40 to be exact)
// don't think this is all too correct but it's what the payload looks like
// (additionally the GITHUB_BASE_REF env variable is empty)
if (base === '0'.repeat(40)) {
base = undefined
}
break
case 'merge_group':
base = context.payload.merge_group?.base_sha
Expand All @@ -211,7 +221,7 @@ const determineBaseAndHead = (context: Context) => {
}

// Ensure that the base and head properties are set on the payload.
if (!base || !head) {
if (!head) {
throw new Error(
`The base and head commits are missing from the payload for this ${context.eventName} event. ` +
"Please submit an issue on this action's GitHub repo."
Expand All @@ -221,6 +231,20 @@ const determineBaseAndHead = (context: Context) => {
return { base, head }
}

/**
* Gets the SHA of the (first) parent commit of a commit
*
* This assumes that only one parent exists for the specified commit.
*/
const getParentCommitSha = (octokit: ReturnType<typeof getOctokit>, context: Context, ref: string) =>
octokit.rest.repos
.getCommit({
owner: context.repo.owner,
repo: context.repo.repo,
ref
})
.then((res) => res.data.parents[0].sha)

/**
* deduplicates consecutive elements in an array
* Consecutive elements are determined by the result of the accessor function, meaning that it is
Expand Down Expand Up @@ -379,6 +403,7 @@ export {
getSemverDiffType,
computeResponseFromChanges,
determineBaseAndHead,
getParentCommitSha,
deduplicateConsecutive,
categorizeChangedFiles,
parseVersionFromFileContents
Expand Down
13 changes: 13 additions & 0 deletions version-metadata/test/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ const tests = [
newVersion: '1.0.5'
}
},
{
description: 'Handles "missing" / all zeroes `before` value correctly on push',
base: '0000000000000000000000000000000000000000',
head: '4cedd64e7615d69f7bcd229fc960d3e1a6f4b496',
repo: 'Quantco/ui-actions',
event: 'push',
file: 'version-metadata/package.json',
expected: {
changed: true,
oldVersion: '1.0.11',
newVersion: '1.0.12'
}
},
{
description:
'fallback 0.0.0 version works with custom extractors (https://github.com/Quantco/slim-trees/actions/runs/5436331701/jobs/9886105283)',
Expand Down

0 comments on commit 14270fb

Please sign in to comment.