From 1739a5eb92d0c2a2ad6fc148de5fc3af7591ca70 Mon Sep 17 00:00:00 2001 From: maksym-shynkarenko Date: Thu, 4 Apr 2024 14:23:26 +0300 Subject: [PATCH] ci(.github/action): change the generate-commit-list action --- .../generate-commit-list-v1/dist/index.js | 2 +- .../generate-commit-list-v1/package.json | 2 +- .../src/doesBranchOrTagExist.test.ts | 29 ++++++++------ .../src/doesBranchOrTagExist.ts | 2 + .../src/getFallbackID.test.ts | 8 +--- .../src/getFallbackID.ts | 1 + .../src/getRawCommitList.test.ts | 39 +++++++++++++++---- .../generate-commit-list-v1/src/run.test.ts | 12 ++---- .../generate-commit-list-v1/src/run.ts | 2 +- 9 files changed, 60 insertions(+), 37 deletions(-) diff --git a/.github/actions/generate-commit-list-v1/dist/index.js b/.github/actions/generate-commit-list-v1/dist/index.js index 0742e4de..27efdd53 100644 --- a/.github/actions/generate-commit-list-v1/dist/index.js +++ b/.github/actions/generate-commit-list-v1/dist/index.js @@ -31636,7 +31636,7 @@ async function run(core, github) { const base = core.getInput('base'); const head = core.getInput('head'); const tag = core.getInput('tag'); - if (!tag && (!base || !head)) { + if (!(base && head || tag)) { core.setFailed('You must provide either a tag or both a base and head branch.'); return; } diff --git a/.github/actions/generate-commit-list-v1/package.json b/.github/actions/generate-commit-list-v1/package.json index 25c863ef..a1df2b90 100644 --- a/.github/actions/generate-commit-list-v1/package.json +++ b/.github/actions/generate-commit-list-v1/package.json @@ -17,7 +17,7 @@ "test": "mocha src/*.test.ts", "coverage": "nyc npm run test", "fmt": "prettier --write \"src/**/*.{js,ts,tsx,json,md,css,html}\"", - "lint": "eslint \"src/**/*.{js,ts,tsx}\"" + "lint": "eslint \"src/**/*.{ts,tsx}\"" }, "nyc": { "checkCoverage": true, diff --git a/.github/actions/generate-commit-list-v1/src/doesBranchOrTagExist.test.ts b/.github/actions/generate-commit-list-v1/src/doesBranchOrTagExist.test.ts index 369be8fc..9407d34c 100644 --- a/.github/actions/generate-commit-list-v1/src/doesBranchOrTagExist.test.ts +++ b/.github/actions/generate-commit-list-v1/src/doesBranchOrTagExist.test.ts @@ -12,33 +12,44 @@ describe('doesBranchOrTagExist', () => { }) afterEach(() => { - getExecOutputStub.restore() + sinon.resetHistory() + sinon.restore() }) describe('when the branch exists', () => { it('returns true', async () => { + const branchName: string = 'my-branch-name' + getExecOutputStub.resolves({ exitCode: 0, stdout: 'refs/heads/my-branch-name' }) - const branchExists = await doesBranchOrTagExist({ - branchName: 'my-branch-name' - }) + const branchExists = await doesBranchOrTagExist({ branchName }) + assert.isTrue( + getExecOutputStub.calledOnceWithExactly( + `git rev-parse --verify origin/${branchName}` + ) + ) assert.isTrue(branchExists) }) }) describe('when the tag exists', () => { it('returns true', async () => { + const tag: string = 'my-tag-name' + getExecOutputStub.resolves({ exitCode: 0, stdout: 'refs/tags/my-tag-name' }) - const tagExists = await doesBranchOrTagExist({ tag: 'my-tag-name' }) + const tagExists = await doesBranchOrTagExist({ tag }) + assert.isTrue( + getExecOutputStub.calledOnceWithExactly(`git rev-parse --verify ${tag}`) + ) assert.isTrue(tagExists) }) }) @@ -46,9 +57,7 @@ describe('doesBranchOrTagExist', () => { describe('when the branch does not exist', () => { it('returns false', async () => { getExecOutputStub.throws({ - exitCode: 1, - stdout: '', - stderr: 'welp, we tried' + stdout: '' }) const branchExists = await doesBranchOrTagExist({ @@ -62,9 +71,7 @@ describe('doesBranchOrTagExist', () => { describe('when the tag does not exist', () => { it('returns false', async () => { getExecOutputStub.throws({ - exitCode: 1, - stdout: '', - stderr: 'welp, we tried' + stdout: '' }) const tagExists = await doesBranchOrTagExist({ tag: 'my-tag-name' }) diff --git a/.github/actions/generate-commit-list-v1/src/doesBranchOrTagExist.ts b/.github/actions/generate-commit-list-v1/src/doesBranchOrTagExist.ts index ef5f169e..ce8e2cca 100644 --- a/.github/actions/generate-commit-list-v1/src/doesBranchOrTagExist.ts +++ b/.github/actions/generate-commit-list-v1/src/doesBranchOrTagExist.ts @@ -18,7 +18,9 @@ export default async function doesBranchOrTagExist({ }: DoesBranchOrTagExistParams): Promise { try { const subCommand = tag ? tag : `origin/${branchName}` + await getExecOutput(`git rev-parse --verify ${subCommand}`) + return true } catch (error) { /** diff --git a/.github/actions/generate-commit-list-v1/src/getFallbackID.test.ts b/.github/actions/generate-commit-list-v1/src/getFallbackID.test.ts index a86cc36f..0c367ff3 100644 --- a/.github/actions/generate-commit-list-v1/src/getFallbackID.test.ts +++ b/.github/actions/generate-commit-list-v1/src/getFallbackID.test.ts @@ -30,9 +30,7 @@ describe('getFallbackID', () => { describe('when the fallback method fails to get a PR ID', () => { it('returns null', async () => { getExecOutputStub.throws({ - exitCode: 0, - stdout: '', - stderr: '' + stdout: '' }) const fallbackID = await getFallbackID('abc') @@ -44,9 +42,7 @@ describe('getFallbackID', () => { describe('when the fallback method throws an error', () => { it('returns null', async () => { getExecOutputStub.throws({ - exitCode: 1, - stdout: '', - stderr: 'welp, we tried' + stdout: '' }) const fallbackID = await getFallbackID('abc') diff --git a/.github/actions/generate-commit-list-v1/src/getFallbackID.ts b/.github/actions/generate-commit-list-v1/src/getFallbackID.ts index fc5fcf7e..ca252180 100644 --- a/.github/actions/generate-commit-list-v1/src/getFallbackID.ts +++ b/.github/actions/generate-commit-list-v1/src/getFallbackID.ts @@ -18,6 +18,7 @@ export default async function getFallbackID( ]) const trimmedFallbackID = fallbackID.trim() + return trimmedFallbackID ? trimmedFallbackID : null } catch (error) { /** diff --git a/.github/actions/generate-commit-list-v1/src/getRawCommitList.test.ts b/.github/actions/generate-commit-list-v1/src/getRawCommitList.test.ts index c66790bb..3aff4003 100644 --- a/.github/actions/generate-commit-list-v1/src/getRawCommitList.test.ts +++ b/.github/actions/generate-commit-list-v1/src/getRawCommitList.test.ts @@ -13,22 +13,47 @@ describe('getRawCommitList', () => { }) afterEach(() => { - getExecOutputStub.restore() + sinon.resetHistory() + sinon.restore() }) describe('when the raw commit list is successfully retrieved', () => { - it('returns the raw commit list', async () => { + it('returns the raw commit list from base and head branches', async () => { + const base: string = 'base-branch' + const head: string = 'head-branch' + getExecOutputStub.resolves({ exitCode: 0, stdout: rawCommitList, stderr: '' }) - const rawCommitListParsed = await getRawCommitList({ - base: 'base-branch', - head: 'head-branch' + const rawCommitListParsed = await getRawCommitList({ base, head }) + + assert.isTrue( + getExecOutputStub.calledOnceWithExactly( + `git log origin/${base}..origin/${head} --oneline --no-merges --abbrev-commit` + ) + ) + assert.deepEqual(rawCommitListParsed, expectedRawCommitList) + }) + + it('returns the raw commit list from a tag', async () => { + const tag: string = 'v1.0.0' + + getExecOutputStub.resolves({ + exitCode: 0, + stdout: rawCommitList, + stderr: '' }) + const rawCommitListParsed = await getRawCommitList({ tag }) + + assert.isTrue( + getExecOutputStub.calledOnceWithExactly( + `git log ${tag}..HEAD --oneline --no-merges --abbrev-commit` + ) + ) assert.deepEqual(rawCommitListParsed, expectedRawCommitList) }) }) @@ -36,9 +61,7 @@ describe('getRawCommitList', () => { describe('when the raw commit list cannot be retrieved', () => { it('throws an error', async () => { getExecOutputStub.throws({ - exitCode: 1, - stdout: '', - stderr: 'welp, guess something went wrong' + stdout: '' }) let error: Error | null = null diff --git a/.github/actions/generate-commit-list-v1/src/run.test.ts b/.github/actions/generate-commit-list-v1/src/run.test.ts index e3a3c09b..68a7c349 100644 --- a/.github/actions/generate-commit-list-v1/src/run.test.ts +++ b/.github/actions/generate-commit-list-v1/src/run.test.ts @@ -126,9 +126,7 @@ describe('run', () => { getInput.withArgs('base').returns('main') getInput.withArgs('head').returns('release') getExecOutputStub.throws({ - exitCode: 1, - stdout: '', - stderr: 'welp, we tried' + stdout: '' }) const core = { @@ -160,9 +158,7 @@ describe('run', () => { }) .onSecondCall() .throws({ - exitCode: 1, - stdout: '', - stderr: 'welp, we tried' + stdout: '' }) const core = { @@ -245,9 +241,7 @@ describe('run', () => { it('should throw an error', async () => { getInput.withArgs('tag').returns('v1.0.0') getExecOutputStub.throws({ - exitCode: 1, - stdout: '', - stderr: 'welp, we tried' + stdout: '' }) const core = { diff --git a/.github/actions/generate-commit-list-v1/src/run.ts b/.github/actions/generate-commit-list-v1/src/run.ts index 3a046034..024c9edc 100644 --- a/.github/actions/generate-commit-list-v1/src/run.ts +++ b/.github/actions/generate-commit-list-v1/src/run.ts @@ -9,7 +9,7 @@ export default async function run(core: Core, github: Github) { const head = core.getInput('head') const tag = core.getInput('tag') - if (!tag && (!base || !head)) { + if (!((base && head) || tag)) { core.setFailed( 'You must provide either a tag or both a base and head branch.' )