Skip to content

Commit

Permalink
ci(.github/action): change the generate-commit-list action
Browse files Browse the repository at this point in the history
  • Loading branch information
maksym-shynkarenko committed Apr 4, 2024
1 parent 658ab7a commit 1739a5e
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/actions/generate-commit-list-v1/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion .github/actions/generate-commit-list-v1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,52 @@ 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)
})
})

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({
Expand All @@ -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' })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export default async function doesBranchOrTagExist({
}: DoesBranchOrTagExistParams): Promise<boolean> {
try {
const subCommand = tag ? tag : `origin/${branchName}`

await getExecOutput(`git rev-parse --verify ${subCommand}`)

return true
} catch (error) {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default async function getFallbackID(
])

const trimmedFallbackID = fallbackID.trim()

return trimmedFallbackID ? trimmedFallbackID : null
} catch (error) {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,55 @@ 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)
})
})

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
Expand Down
12 changes: 3 additions & 9 deletions .github/actions/generate-commit-list-v1/src/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -160,9 +158,7 @@ describe('run', () => {
})
.onSecondCall()
.throws({
exitCode: 1,
stdout: '',
stderr: 'welp, we tried'
stdout: ''
})

const core = {
Expand Down Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion .github/actions/generate-commit-list-v1/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
)
Expand Down

0 comments on commit 1739a5e

Please sign in to comment.