From 79d2717121d25f299356ebba720dc4e9bc200f78 Mon Sep 17 00:00:00 2001 From: Simon Gerber Date: Fri, 3 Nov 2023 11:04:17 +0100 Subject: [PATCH] tmp get labels from context? --- __tests__/bump-labels.test.ts | 28 ++++++++++++++++++++++++---- badges/coverage.svg | 2 +- dist/index.js | 5 ++++- src/bump-labels.ts | 9 ++++++++- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/__tests__/bump-labels.test.ts b/__tests__/bump-labels.test.ts index fd9a813..ab4eb13 100644 --- a/__tests__/bump-labels.test.ts +++ b/__tests__/bump-labels.test.ts @@ -101,7 +101,10 @@ describe('prBumpLabel', () => { }) it('identifies bump:patch label', async () => { - getOctokitMock.mockImplementation(makePROctokitMock('bump:patch')) + //getOctokitMock.mockImplementation(makePROctokitMock('bump:patch')) + if (github.context.payload.pull_request) { + github.context.payload.pull_request['labels'] = [{ name: 'bump:patch' }] + } await expect(bump_labels.prBumpLabel(bumpLabels)).resolves.toStrictEqual({ bump: 'patch' as ReleaseType, @@ -110,7 +113,10 @@ describe('prBumpLabel', () => { }) it('identifies bump:minor label', async () => { - getOctokitMock.mockImplementation(makePROctokitMock('bump:minor')) + //getOctokitMock.mockImplementation(makePROctokitMock('bump:minor')) + if (github.context.payload.pull_request) { + github.context.payload.pull_request.labels = [{ name: 'bump:minor' }] + } await expect(bump_labels.prBumpLabel(bumpLabels)).resolves.toStrictEqual({ bump: 'minor' as ReleaseType, @@ -119,7 +125,10 @@ describe('prBumpLabel', () => { }) it('identifies bump:major label', async () => { - getOctokitMock.mockImplementation(makePROctokitMock('bump:major')) + //getOctokitMock.mockImplementation(makePROctokitMock('bump:major')) + if (github.context.payload.pull_request) { + github.context.payload.pull_request.labels = [{ name: 'bump:major' }] + } await expect(bump_labels.prBumpLabel(bumpLabels)).resolves.toStrictEqual({ bump: 'major' as ReleaseType, @@ -128,7 +137,10 @@ describe('prBumpLabel', () => { }) it('logs a message when no bump labels are present', async () => { - getOctokitMock.mockImplementation(makePROctokitMock()) + //getOctokitMock.mockImplementation(makePROctokitMock()) + if (github.context.payload.pull_request) { + github.context.payload.pull_request.labels = [] + } await expect(bump_labels.prBumpLabel(bumpLabels)).resolves.toStrictEqual({ bump: null, @@ -139,9 +151,17 @@ describe('prBumpLabel', () => { }) it('logs a warning when multiple bump labels are present', async () => { + /* getOctokitMock.mockImplementation( makePROctokitMock('bump:patch', 'bump:minor') ) + */ + if (github.context.payload.pull_request) { + github.context.payload.pull_request.labels = [ + { name: 'bump:patch' }, + { name: 'bump:minor' } + ] + } await expect(bump_labels.prBumpLabel(bumpLabels)).resolves.toStrictEqual({ bump: null, diff --git a/badges/coverage.svg b/badges/coverage.svg index 5bb55be..f0f1e25 100644 --- a/badges/coverage.svg +++ b/badges/coverage.svg @@ -1 +1 @@ -Coverage: 100%Coverage100% \ No newline at end of file +Coverage: 69.03%Coverage69.03% \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index 1dd1178..4b0a933 100644 --- a/dist/index.js +++ b/dist/index.js @@ -34432,7 +34432,10 @@ function bumpFromLabel(b, bump) { } exports.bumpFromLabel = bumpFromLabel; async function prBumpLabel(b) { - const prLabels = await getPRLabels(); + if (!github.context.payload.pull_request) { + throw new Error(`Action is running on a '${github.context.eventName}' event, only 'pull_request' events are supported`); + } + const prLabels = github.context.payload.labels.map((lbl) => lbl.name); const bumpLabels = prLabels.filter((l) => l === b.patch || l === b.minor || l === b.major); if (bumpLabels.length === 0) { core.info('No bump labels found'); diff --git a/src/bump-labels.ts b/src/bump-labels.ts index d495cfb..f1916fb 100644 --- a/src/bump-labels.ts +++ b/src/bump-labels.ts @@ -62,7 +62,14 @@ interface BumpAction { } export async function prBumpLabel(b: BumpLabels): Promise { - const prLabels = await getPRLabels() + if (!github.context.payload.pull_request) { + throw new Error( + `Action is running on a '${github.context.eventName}' event, only 'pull_request' events are supported` + ) + } + const prLabels = github.context.payload.labels.map( + (lbl: { name: string }) => lbl.name + ) const bumpLabels = prLabels.filter( (l: string) => l === b.patch || l === b.minor || l === b.major )