From 231de6bc242d90b525caa928ed430a2c83996fcb Mon Sep 17 00:00:00 2001 From: Josh Dales Date: Sat, 28 Jan 2023 22:30:05 -0500 Subject: [PATCH] Remove the branch option and replace with just head-branch and base-branch --- __tests__/fixtures/branches.yml | 8 ++++---- src/branch.ts | 32 ++++++++++++++++++++++++++++++++ src/labeler.ts | 16 ++-------------- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/__tests__/fixtures/branches.yml b/__tests__/fixtures/branches.yml index 266ef961..c4c2e7ba 100644 --- a/__tests__/fixtures/branches.yml +++ b/__tests__/fixtures/branches.yml @@ -1,11 +1,11 @@ test-branch: - - branch: "^test/" + - head-branch: "^test/" feature-branch: - - branch: "/feature/" + - head-branch: "/feature/" bug-branch: - - branch: "^bug/|fix/" + - head-branch: "^bug/|fix/" array-branch: - - branch: ["^array/"] + - head-branch: ["^array/"] diff --git a/src/branch.ts b/src/branch.ts index d6ccc3ad..1c7e87b3 100644 --- a/src/branch.ts +++ b/src/branch.ts @@ -3,6 +3,38 @@ import * as github from '@actions/github'; type BranchBase = 'base' | 'head'; +export function toMatchConfigWithBranches(config: any) { + if (!config['head-branch'] || config['base-branch']) { + return config; + } + + const branchConfig = { + headBranch: config['head-branch'], + baseBranch: config['base-branch'] + }; + + if (branchConfig.headBranch) { + const patterns = branchConfig.headBranch; + if (typeof patterns === 'string') { + branchConfig.headBranch = [patterns]; + } + } + + if (branchConfig.baseBranch) { + const patterns = branchConfig.baseBranch; + if (typeof patterns === 'string') { + branchConfig.baseBranch = [patterns]; + } + } + + return { + ...config, + ['head-branch']: undefined, + ['base-branch']: undefined, + ...branchConfig + }; +} + export function getBranchName(branchBase?: BranchBase): string | undefined { const pullRequest = github.context.payload.pull_request; if (!pullRequest) { diff --git a/src/labeler.ts b/src/labeler.ts index cf8e9d4d..0c24d558 100644 --- a/src/labeler.ts +++ b/src/labeler.ts @@ -3,12 +3,11 @@ import * as github from '@actions/github'; import * as yaml from 'js-yaml'; import {Minimatch, IMinimatch} from 'minimatch'; -import {checkBranch} from './branch'; +import {checkBranch, toMatchConfigWithBranches} from './branch'; interface MatchConfig { all?: string[]; any?: string[]; - branch?: string[]; headBranch?: string[]; baseBranch?: string[]; } @@ -151,14 +150,9 @@ function toMatchConfig(config: StringOrMatchConfig): MatchConfig { return { any: [config] }; - } else if (typeof config.branch === 'string') { - return { - ...config, - branch: [config.branch] - }; } - return config; + return toMatchConfigWithBranches(config); } function printPattern(matcher: IMinimatch): string { @@ -236,12 +230,6 @@ function checkMatch(changedFiles: string[], matchConfig: MatchConfig): boolean { } } - if (matchConfig.branch !== undefined) { - if (!checkBranch(matchConfig.branch)) { - return false; - } - } - if (matchConfig.headBranch !== undefined) { if (!checkBranch(matchConfig.headBranch, 'head')) { return false;