mirror of
https://github.com/actions/labeler.git
synced 2025-12-11 12:07:32 +00:00
Add options for getting the head or base branch
This commit is contained in:
@@ -1,7 +1,13 @@
|
|||||||
export const context = {
|
export const context = {
|
||||||
payload: {
|
payload: {
|
||||||
pull_request: {
|
pull_request: {
|
||||||
number: 123
|
number: 123,
|
||||||
|
head: {
|
||||||
|
ref: 'head-branch-name'
|
||||||
|
},
|
||||||
|
base: {
|
||||||
|
ref: 'base-branch-name'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
repo: {
|
repo: {
|
||||||
|
|||||||
@@ -1,17 +1,40 @@
|
|||||||
import {checkBranch} from '../src/branch';
|
import {getBranchName, checkBranch} from '../src/branch';
|
||||||
import * as github from '@actions/github';
|
import * as github from '@actions/github';
|
||||||
|
|
||||||
jest.mock('@actions/core');
|
jest.mock('@actions/core');
|
||||||
jest.mock('@actions/github');
|
jest.mock('@actions/github');
|
||||||
|
|
||||||
describe('checkBranch', () => {
|
describe('getBranchName', () => {
|
||||||
describe('when a single pattern is provided', () => {
|
describe('when the pull requests base branch is requested', () => {
|
||||||
beforeEach(() => {
|
it('returns the base branch name', () => {
|
||||||
github.context.payload.pull_request!.head = {
|
const result = getBranchName('base');
|
||||||
ref: 'test/feature/123'
|
expect(result).toEqual('base-branch-name');
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when the pull requests head branch is requested', () => {
|
||||||
|
it('returns the head branch name', () => {
|
||||||
|
const result = getBranchName('head');
|
||||||
|
expect(result).toEqual('head-branch-name');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when no branch is specified', () => {
|
||||||
|
it('returns the head branch name', () => {
|
||||||
|
const result = getBranchName('base');
|
||||||
|
expect(result).toEqual('base-branch-name');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('checkBranch', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
github.context.payload.pull_request!.head = {
|
||||||
|
ref: 'test/feature/123'
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when a single pattern is provided', () => {
|
||||||
describe('and the pattern matches the head branch', () => {
|
describe('and the pattern matches the head branch', () => {
|
||||||
it('returns true', () => {
|
it('returns true', () => {
|
||||||
const result = checkBranch(['^test']);
|
const result = checkBranch(['^test']);
|
||||||
@@ -28,12 +51,6 @@ describe('checkBranch', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('when multiple patterns are provided', () => {
|
describe('when multiple patterns are provided', () => {
|
||||||
beforeEach(() => {
|
|
||||||
github.context.payload.pull_request!.head = {
|
|
||||||
ref: 'test/feature/123'
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('and at least one pattern matches', () => {
|
describe('and at least one pattern matches', () => {
|
||||||
it('returns true', () => {
|
it('returns true', () => {
|
||||||
const result = checkBranch(['^test/', '^feature/']);
|
const result = checkBranch(['^test/', '^feature/']);
|
||||||
|
|||||||
@@ -1,17 +1,23 @@
|
|||||||
import * as core from '@actions/core';
|
import * as core from '@actions/core';
|
||||||
import * as github from '@actions/github';
|
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;
|
const pullRequest = github.context.payload.pull_request;
|
||||||
if (!pullRequest) {
|
if (!pullRequest) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
return pullRequest.head?.ref;
|
if (branchBase === 'base') {
|
||||||
|
return pullRequest.base?.ref;
|
||||||
|
} else {
|
||||||
|
return pullRequest.head?.ref;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function checkBranch(glob: string[]): boolean {
|
export function checkBranch(glob: string[], branchBase?: BranchBase): boolean {
|
||||||
const branchName = getHeadBranchName();
|
const branchName = getBranchName(branchBase);
|
||||||
if (!branchName) {
|
if (!branchName) {
|
||||||
core.debug(` no branch name`);
|
core.debug(` no branch name`);
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ interface MatchConfig {
|
|||||||
all?: string[];
|
all?: string[];
|
||||||
any?: string[];
|
any?: string[];
|
||||||
branch?: string[];
|
branch?: string[];
|
||||||
|
headBranch?: string[];
|
||||||
|
baseBranch?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
type StringOrMatchConfig = string | MatchConfig;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user