Files
labeler/src/branch.ts
2023-01-28 15:58:00 -05:00

43 lines
1.1 KiB
TypeScript

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;
}