mirror of
https://github.com/actions/labeler.git
synced 2025-12-13 13:07:24 +00:00
Add the changedFiles key and update logic in labeler
This commit is contained in:
@@ -1,2 +1,3 @@
|
|||||||
touched-a-pdf-file:
|
touched-a-pdf-file:
|
||||||
- any: ['*.pdf']
|
- changed-files:
|
||||||
|
- any: ['*.pdf']
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ beforeAll(() => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const matchConfig = [{any: ['*.txt']}];
|
const matchConfig = [{changedFiles: {any: ['*.txt']}}];
|
||||||
|
|
||||||
describe('checkGlobs', () => {
|
describe('checkGlobs', () => {
|
||||||
it('returns true when our pattern does match changed files', () => {
|
it('returns true when our pattern does match changed files', () => {
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ type BranchBase = 'base' | 'head';
|
|||||||
|
|
||||||
export function toBranchMatchConfig(config: any): BranchMatchConfig {
|
export function toBranchMatchConfig(config: any): BranchMatchConfig {
|
||||||
if (!config['head-branch'] || config['base-branch']) {
|
if (!config['head-branch'] || config['base-branch']) {
|
||||||
return config;
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
const branchConfig = {
|
const branchConfig = {
|
||||||
|
|||||||
@@ -5,18 +5,15 @@ import {Minimatch} from 'minimatch';
|
|||||||
|
|
||||||
import {checkBranch, toBranchMatchConfig, BranchMatchConfig} from './branch';
|
import {checkBranch, toBranchMatchConfig, BranchMatchConfig} from './branch';
|
||||||
|
|
||||||
interface FilesChangedMatchConfig {
|
interface ChangedFilesMatchConfig {
|
||||||
all?: string[];
|
changedFiles?: {
|
||||||
any?: string[];
|
|
||||||
filesChanged?: {
|
|
||||||
all?: string[];
|
all?: string[];
|
||||||
any?: string[];
|
any?: string[];
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
type MatchConfig = FilesChangedMatchConfig & BranchMatchConfig;
|
export type MatchConfig = ChangedFilesMatchConfig & BranchMatchConfig;
|
||||||
|
|
||||||
type StringOrMatchConfig = string | MatchConfig;
|
|
||||||
type ClientType = ReturnType<typeof github.getOctokit>;
|
type ClientType = ReturnType<typeof github.getOctokit>;
|
||||||
|
|
||||||
export async function run() {
|
export async function run() {
|
||||||
@@ -41,7 +38,7 @@ export async function run() {
|
|||||||
|
|
||||||
core.debug(`fetching changed files for pr #${prNumber}`);
|
core.debug(`fetching changed files for pr #${prNumber}`);
|
||||||
const changedFiles: string[] = await getChangedFiles(client, prNumber);
|
const changedFiles: string[] = await getChangedFiles(client, prNumber);
|
||||||
const labelGlobs: Map<string, StringOrMatchConfig[]> = await getLabelGlobs(
|
const labelGlobs: Map<string, MatchConfig[]> = await getLabelGlobs(
|
||||||
client,
|
client,
|
||||||
configPath
|
configPath
|
||||||
);
|
);
|
||||||
@@ -103,7 +100,7 @@ async function getChangedFiles(
|
|||||||
async function getLabelGlobs(
|
async function getLabelGlobs(
|
||||||
client: ClientType,
|
client: ClientType,
|
||||||
configurationPath: string
|
configurationPath: string
|
||||||
): Promise<Map<string, StringOrMatchConfig[]>> {
|
): Promise<Map<string, MatchConfig[]>> {
|
||||||
const configurationContent: string = await fetchContent(
|
const configurationContent: string = await fetchContent(
|
||||||
client,
|
client,
|
||||||
configurationPath
|
configurationPath
|
||||||
@@ -132,8 +129,8 @@ async function fetchContent(
|
|||||||
|
|
||||||
function getLabelGlobMapFromObject(
|
function getLabelGlobMapFromObject(
|
||||||
configObject: any
|
configObject: any
|
||||||
): Map<string, StringOrMatchConfig[]> {
|
): Map<string, MatchConfig[]> {
|
||||||
const labelGlobs: Map<string, StringOrMatchConfig[]> = new Map();
|
const labelGlobs: Map<string, MatchConfig[]> = new Map();
|
||||||
for (const label in configObject) {
|
for (const label in configObject) {
|
||||||
if (typeof configObject[label] === 'string') {
|
if (typeof configObject[label] === 'string') {
|
||||||
labelGlobs.set(label, [configObject[label]]);
|
labelGlobs.set(label, [configObject[label]]);
|
||||||
@@ -149,17 +146,48 @@ function getLabelGlobMapFromObject(
|
|||||||
return labelGlobs;
|
return labelGlobs;
|
||||||
}
|
}
|
||||||
|
|
||||||
function toMatchConfig(config: StringOrMatchConfig): MatchConfig {
|
function toChangedFilesMatchConfig(config: any): ChangedFilesMatchConfig {
|
||||||
if (typeof config === 'string') {
|
if (!config['changed-files']) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
const changedFiles = config['changed-files'];
|
||||||
|
|
||||||
|
// If the value provided is a string or an array of strings then default to `any` matching
|
||||||
|
if (typeof changedFiles === 'string') {
|
||||||
return {
|
return {
|
||||||
any: [config]
|
changedFiles: {
|
||||||
|
any: [changedFiles]
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const changedFilesMatchConfig = {
|
||||||
|
changedFiles: {}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (Array.isArray(changedFiles)) {
|
||||||
|
if (changedFiles.every(entry => typeof entry === 'string')) {
|
||||||
|
changedFilesMatchConfig.changedFiles = {
|
||||||
|
any: changedFiles
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
// If it is not an array of strings then it should be array of further config options
|
||||||
|
// so assign them to our `changedFilesMatchConfig`
|
||||||
|
changedFiles.forEach(element => {
|
||||||
|
Object.assign(changedFilesMatchConfig.changedFiles, element);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return changedFilesMatchConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
function toMatchConfig(config: MatchConfig): MatchConfig {
|
||||||
|
const changedFilesConfig = toChangedFilesMatchConfig(config);
|
||||||
const branchConfig = toBranchMatchConfig(config);
|
const branchConfig = toBranchMatchConfig(config);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...config,
|
...changedFilesConfig,
|
||||||
...branchConfig
|
...branchConfig
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -170,7 +198,7 @@ function printPattern(matcher: Minimatch): string {
|
|||||||
|
|
||||||
export function checkGlobs(
|
export function checkGlobs(
|
||||||
changedFiles: string[],
|
changedFiles: string[],
|
||||||
globs: StringOrMatchConfig[]
|
globs: MatchConfig[]
|
||||||
): boolean {
|
): boolean {
|
||||||
for (const glob of globs) {
|
for (const glob of globs) {
|
||||||
core.debug(` checking pattern ${JSON.stringify(glob)}`);
|
core.debug(` checking pattern ${JSON.stringify(glob)}`);
|
||||||
@@ -227,14 +255,14 @@ function checkAll(changedFiles: string[], globs: string[]): boolean {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function checkMatch(changedFiles: string[], matchConfig: MatchConfig): boolean {
|
function checkMatch(changedFiles: string[], matchConfig: MatchConfig): boolean {
|
||||||
if (matchConfig.all !== undefined) {
|
if (matchConfig.changedFiles?.all !== undefined) {
|
||||||
if (!checkAll(changedFiles, matchConfig.all)) {
|
if (!checkAll(changedFiles, matchConfig.changedFiles.all)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (matchConfig.any !== undefined) {
|
if (matchConfig.changedFiles?.any !== undefined) {
|
||||||
if (!checkAny(changedFiles, matchConfig.any)) {
|
if (!checkAny(changedFiles, matchConfig.changedFiles.any)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user