mirror of
https://github.com/actions/labeler.git
synced 2025-12-14 13:47:02 +00:00
Add options for getting the head or base branch
This commit is contained in:
@@ -1,17 +1,23 @@
|
||||
import * as core from '@actions/core';
|
||||
import * as github from '@actions/github';
|
||||
|
||||
function getHeadBranchName(): string | undefined {
|
||||
type BranchBase = 'base' | 'head';
|
||||
|
||||
export function getBranchName(branchBase?: BranchBase): string | undefined {
|
||||
const pullRequest = github.context.payload.pull_request;
|
||||
if (!pullRequest) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return pullRequest.head?.ref;
|
||||
if (branchBase === 'base') {
|
||||
return pullRequest.base?.ref;
|
||||
} else {
|
||||
return pullRequest.head?.ref;
|
||||
}
|
||||
}
|
||||
|
||||
export function checkBranch(glob: string[]): boolean {
|
||||
const branchName = getHeadBranchName();
|
||||
export function checkBranch(glob: string[], branchBase?: BranchBase): boolean {
|
||||
const branchName = getBranchName(branchBase);
|
||||
if (!branchName) {
|
||||
core.debug(` no branch name`);
|
||||
return false;
|
||||
|
||||
@@ -9,6 +9,8 @@ interface MatchConfig {
|
||||
all?: string[];
|
||||
any?: string[];
|
||||
branch?: string[];
|
||||
headBranch?: string[];
|
||||
baseBranch?: string[];
|
||||
}
|
||||
|
||||
type StringOrMatchConfig = string | MatchConfig;
|
||||
@@ -240,6 +242,18 @@ function checkMatch(changedFiles: string[], matchConfig: MatchConfig): boolean {
|
||||
}
|
||||
}
|
||||
|
||||
if (matchConfig.headBranch !== undefined) {
|
||||
if (!checkBranch(matchConfig.headBranch, 'head')) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (matchConfig.baseBranch !== undefined) {
|
||||
if (!checkBranch(matchConfig.baseBranch, 'base')) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user