mirror of
https://github.com/actions/labeler.git
synced 2025-12-15 06:27:13 +00:00
Move all the branch checking into its own file
This commit is contained in:
42
src/branch.ts
Normal file
42
src/branch.ts
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import * as core from '@actions/core';
|
||||||
|
import * as github from '@actions/github';
|
||||||
|
|
||||||
|
function getHeadBranchName(): string | undefined {
|
||||||
|
const pullRequest = github.context.payload.pull_request;
|
||||||
|
if (!pullRequest) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pullRequest.head?.ref;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function checkBranch(glob: string[]): boolean {
|
||||||
|
const branchName = getHeadBranchName();
|
||||||
|
if (!branchName) {
|
||||||
|
core.debug(` no branch name`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
core.debug(` checking "branch" pattern against ${branchName}`);
|
||||||
|
const matchers = glob.map(g => new RegExp(g));
|
||||||
|
for (const matcher of matchers) {
|
||||||
|
if (matchBranchPattern(matcher, branchName)) {
|
||||||
|
core.debug(` "branch" patterns matched against ${branchName}`);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
core.debug(` "branch" patterns did not match against ${branchName}`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function matchBranchPattern(matcher: RegExp, branchName: string): boolean {
|
||||||
|
core.debug(` - ${matcher}`);
|
||||||
|
if (matcher.test(branchName)) {
|
||||||
|
core.debug(` "branch" pattern matched`);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
core.debug(` ${matcher} did not match`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
@@ -3,6 +3,8 @@ import * as github from '@actions/github';
|
|||||||
import * as yaml from 'js-yaml';
|
import * as yaml from 'js-yaml';
|
||||||
import {Minimatch, IMinimatch} from 'minimatch';
|
import {Minimatch, IMinimatch} from 'minimatch';
|
||||||
|
|
||||||
|
import {checkBranch} from './branch';
|
||||||
|
|
||||||
interface MatchConfig {
|
interface MatchConfig {
|
||||||
all?: string[];
|
all?: string[];
|
||||||
any?: string[];
|
any?: string[];
|
||||||
@@ -72,15 +74,6 @@ function getPrNumber(): number | undefined {
|
|||||||
return pullRequest.number;
|
return pullRequest.number;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getHeadBranchName(): string | undefined {
|
|
||||||
const pullRequest = github.context.payload.pull_request;
|
|
||||||
if (!pullRequest) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return pullRequest.head?.ref;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getChangedFiles(
|
async function getChangedFiles(
|
||||||
client: ClientType,
|
client: ClientType,
|
||||||
prNumber: number
|
prNumber: number
|
||||||
@@ -228,37 +221,6 @@ function checkAll(changedFiles: string[], globs: string[]): boolean {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function matchBranchPattern(matcher: RegExp, branchName: string): boolean {
|
|
||||||
core.debug(` - ${matcher}`);
|
|
||||||
if (matcher.test(branchName)) {
|
|
||||||
core.debug(` "branch" pattern matched`);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
core.debug(` ${matcher} did not match`);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function checkBranch(glob: string[]): boolean {
|
|
||||||
const branchName = getHeadBranchName();
|
|
||||||
if (!branchName) {
|
|
||||||
core.debug(` no branch name`);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
core.debug(` checking "branch" pattern against ${branchName}`);
|
|
||||||
const matchers = glob.map(g => new RegExp(g));
|
|
||||||
for (const matcher of matchers) {
|
|
||||||
if (matchBranchPattern(matcher, branchName)) {
|
|
||||||
core.debug(` "branch" patterns matched against ${branchName}`);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
core.debug(` "branch" patterns did not match against ${branchName}`);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function checkMatch(changedFiles: string[], matchConfig: MatchConfig): boolean {
|
function checkMatch(changedFiles: string[], matchConfig: MatchConfig): boolean {
|
||||||
if (matchConfig.all !== undefined) {
|
if (matchConfig.all !== undefined) {
|
||||||
if (!checkAll(changedFiles, matchConfig.all)) {
|
if (!checkAll(changedFiles, matchConfig.all)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user