From cc130d3e4d183ff0f7570dfb39bf184e76c71039 Mon Sep 17 00:00:00 2001 From: Timon van Spronsen Date: Sun, 27 Oct 2019 17:21:12 +0100 Subject: [PATCH 1/4] Improve Context mocking in tests --- __tests__/action.test.ts | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/__tests__/action.test.ts b/__tests__/action.test.ts index 904a1de8..44c6300f 100644 --- a/__tests__/action.test.ts +++ b/__tests__/action.test.ts @@ -2,6 +2,8 @@ import nock from 'nock' import fs from 'fs' import path from 'path' import action from '../src/action' +import { Context } from '@actions/github/lib/context' +import { WebhookPayload } from '@actions/github/lib/interfaces' nock.disableNetConnect() @@ -24,9 +26,7 @@ describe('pr-labeler-action', () => { }) .reply(200) - await action({ - payload: pullRequestOpenedFixture({ ref: 'fix/510-logging' }) - }) + await action(new MockContext(pullRequestOpenedFixture({ ref: 'fix/510-logging' }))) expect.assertions(1) }) @@ -42,9 +42,7 @@ describe('pr-labeler-action', () => { }) .reply(200) - await action({ - payload: pullRequestOpenedFixture({ ref: 'feature/sign-in-page/101' }) - }) + await action(new MockContext(pullRequestOpenedFixture({ ref: 'feature/sign-in-page/101' }))) expect.assertions(1) }) @@ -60,9 +58,7 @@ describe('pr-labeler-action', () => { }) .reply(200) - await action({ - payload: pullRequestOpenedFixture({ ref: 'release/2.0' }) - }) + await action(new MockContext(pullRequestOpenedFixture({ ref: 'release/2.0' }))) expect.assertions(1) }) @@ -78,9 +74,7 @@ describe('pr-labeler-action', () => { }) .reply(200) - await action({ - payload: pullRequestOpenedFixture({ ref: 'fix/510-logging' }) - }) + await action(new MockContext(pullRequestOpenedFixture({ ref: 'fix/510-logging' }))) expect.assertions(1) }) @@ -93,12 +87,17 @@ describe('pr-labeler-action', () => { }) .reply(200) - await action({ - payload: pullRequestOpenedFixture({ ref: 'hello_world' }) - }) + await action(new MockContext(pullRequestOpenedFixture({ ref: 'hello_world' }))) }) }) +class MockContext extends Context { + constructor(payload: WebhookPayload) { + super() + this.payload = payload + } +} + function encodeContent(content: Buffer) { return Buffer.from(content).toString('base64') } From ff1dcea1f203e49802dcea261300f1a06552a07e Mon Sep 17 00:00:00 2001 From: Timon van Spronsen Date: Sun, 27 Oct 2019 17:22:02 +0100 Subject: [PATCH 2/4] Simplify code by using context.repo to get repo owner and name --- src/action.ts | 13 ++++--------- src/utils/config.ts | 2 +- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/action.ts b/src/action.ts index 458c68e4..1617544e 100644 --- a/src/action.ts +++ b/src/action.ts @@ -3,7 +3,6 @@ import * as github from '@actions/github' import { Context } from '@actions/github/lib/context' import matcher from 'matcher' import getConfig from './utils/config' -import { RepoInfo } from './utils/config' const defaultConfig = { feature: ['feature/*', 'feat/*'], @@ -11,14 +10,10 @@ const defaultConfig = { chore: 'chore/*' } -async function action(context: Pick = github.context) { +async function action(context: Context = github.context) { try { const GITHUB_TOKEN = process.env.GITHUB_TOKEN! const octokit = new github.GitHub(GITHUB_TOKEN) - const repoInfo: RepoInfo = { - owner: context.payload.repository!.owner.login, - repo: context.payload.repository!.name - } const configPath = core.getInput('configuration-path', { required: true }) if (!context.payload.pull_request) { @@ -28,7 +23,7 @@ async function action(context: Pick = github.context) { } const ref: string = context.payload.pull_request.head.ref - const config = await getConfig(octokit, configPath, repoInfo, ref, defaultConfig) + const config = await getConfig(octokit, configPath, context.repo, ref, defaultConfig) const labelsToAdd = Object.entries(config).reduce( (labels, [label, patterns]) => { @@ -47,9 +42,9 @@ async function action(context: Pick = github.context) { if (labelsToAdd.length > 0) { await octokit.issues.addLabels({ + ...context.repo, number: context.payload.pull_request.number, - labels: labelsToAdd, - ...repoInfo + labels: labelsToAdd }) } } catch (error) { diff --git a/src/utils/config.ts b/src/utils/config.ts index 78455af7..9f7b4bb8 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -1,7 +1,7 @@ import yaml from 'js-yaml' import { GitHub } from '@actions/github' -export interface RepoInfo { +interface RepoInfo { owner: string repo: string } From cb6a98fa0ad1262bf54b89015289c4f47ea3ce90 Mon Sep 17 00:00:00 2001 From: Timon van Spronsen Date: Sun, 27 Oct 2019 17:34:32 +0100 Subject: [PATCH 3/4] Extract getLabelsToAdd function --- src/action.ts | 35 +++++++++++++++++++---------------- src/utils/config.ts | 2 +- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/action.ts b/src/action.ts index 1617544e..09a6b19d 100644 --- a/src/action.ts +++ b/src/action.ts @@ -2,7 +2,7 @@ import * as core from '@actions/core' import * as github from '@actions/github' import { Context } from '@actions/github/lib/context' import matcher from 'matcher' -import getConfig from './utils/config' +import getConfig, { Config } from './utils/config' const defaultConfig = { feature: ['feature/*', 'feat/*'], @@ -24,21 +24,7 @@ async function action(context: Context = github.context) { const ref: string = context.payload.pull_request.head.ref const config = await getConfig(octokit, configPath, context.repo, ref, defaultConfig) - - const labelsToAdd = Object.entries(config).reduce( - (labels, [label, patterns]) => { - if ( - Array.isArray(patterns) - ? patterns.some(pattern => matcher.isMatch(ref, pattern)) - : matcher.isMatch(ref, patterns) - ) { - labels.push(label) - } - - return labels - }, - [] as string[] - ) + const labelsToAdd = getLabelsToAdd(config, ref) if (labelsToAdd.length > 0) { await octokit.issues.addLabels({ @@ -57,4 +43,21 @@ async function action(context: Context = github.context) { } } +function getLabelsToAdd(config: Config, branchName: string): string[] { + return Object.entries(config).reduce( + (labels, [label, patterns]) => { + if ( + Array.isArray(patterns) + ? patterns.some(pattern => matcher.isMatch(branchName, pattern)) + : matcher.isMatch(branchName, patterns) + ) { + labels.push(label) + } + + return labels + }, + [] as string[] + ) +} + export default action diff --git a/src/utils/config.ts b/src/utils/config.ts index 9f7b4bb8..cc379f9d 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -6,7 +6,7 @@ interface RepoInfo { repo: string } -interface Config { +export interface Config { [k: string]: string | string[] } From ffc746525d6dcce38bb324d2ce11ee448e249fe5 Mon Sep 17 00:00:00 2001 From: Timon van Spronsen Date: Sun, 27 Oct 2019 17:47:14 +0100 Subject: [PATCH 4/4] Reset environment variables in test --- __tests__/action.test.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/__tests__/action.test.ts b/__tests__/action.test.ts index 44c6300f..475f150d 100644 --- a/__tests__/action.test.ts +++ b/__tests__/action.test.ts @@ -9,9 +9,7 @@ nock.disableNetConnect() describe('pr-labeler-action', () => { beforeEach(() => { - // configuration-path parameter is required - // parameters are exposed as environment variables: https://help.github.com/en/github/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#jobsjob_idstepswith - process.env['INPUT_CONFIGURATION-PATH'] = '.github/pr-labeler.yml' + setupEnvironmentVariables() }) it('adds the "fix" label for "fix/510-logging" branch', async () => { @@ -139,3 +137,12 @@ function pullRequestOpenedFixture({ ref }: { ref: string }) { } } } + +function setupEnvironmentVariables() { + // reset process.env otherwise `Context` will use those variables + process.env = {} + + // configuration-path parameter is required + // parameters are exposed as environment variables: https://help.github.com/en/github/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#jobsjob_idstepswith + process.env['INPUT_CONFIGURATION-PATH'] = '.github/pr-labeler.yml' +}