mirror of
https://github.com/actions/labeler.git
synced 2025-12-13 13:07:24 +00:00
Merge pull request #3 from joshdales/new-config-structure
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: {
|
||||||
|
|||||||
155
__tests__/branch.test.ts
Normal file
155
__tests__/branch.test.ts
Normal file
@@ -0,0 +1,155 @@
|
|||||||
|
import {
|
||||||
|
getBranchName,
|
||||||
|
checkBranch,
|
||||||
|
toBranchMatchConfig,
|
||||||
|
BranchMatchConfig
|
||||||
|
} from '../src/branch';
|
||||||
|
import * as github from '@actions/github';
|
||||||
|
|
||||||
|
jest.mock('@actions/core');
|
||||||
|
jest.mock('@actions/github');
|
||||||
|
|
||||||
|
describe('getBranchName', () => {
|
||||||
|
describe('when the pull requests base branch is requested', () => {
|
||||||
|
it('returns the base branch name', () => {
|
||||||
|
const result = getBranchName('base');
|
||||||
|
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();
|
||||||
|
expect(result).toEqual('head-branch-name');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('checkBranch', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
github.context.payload.pull_request!.head = {
|
||||||
|
ref: 'test/feature/123'
|
||||||
|
};
|
||||||
|
github.context.payload.pull_request!.base = {
|
||||||
|
ref: 'main'
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when a single pattern is provided', () => {
|
||||||
|
describe('and the pattern matches the head branch', () => {
|
||||||
|
it('returns true', () => {
|
||||||
|
const result = checkBranch(['^test']);
|
||||||
|
expect(result).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('and the pattern does not match the head branch', () => {
|
||||||
|
it('returns false', () => {
|
||||||
|
const result = checkBranch(['^feature/']);
|
||||||
|
expect(result).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when multiple patterns are provided', () => {
|
||||||
|
describe('and at least one pattern matches', () => {
|
||||||
|
it('returns true', () => {
|
||||||
|
const result = checkBranch(['^test/', '^feature/']);
|
||||||
|
expect(result).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('and all patterns match', () => {
|
||||||
|
it('returns true', () => {
|
||||||
|
const result = checkBranch(['^test/', '/feature/']);
|
||||||
|
expect(result).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('and no patterns match', () => {
|
||||||
|
it('returns false', () => {
|
||||||
|
const result = checkBranch(['^feature/', '/test$']);
|
||||||
|
expect(result).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when the branch to check is specified as the base branch', () => {
|
||||||
|
describe('and the pattern matches the base branch', () => {
|
||||||
|
it('returns true', () => {
|
||||||
|
const result = checkBranch(['^main$'], 'base');
|
||||||
|
expect(result).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('toBranchMatchConfig', () => {
|
||||||
|
describe('when there are no branch keys in the config', () => {
|
||||||
|
const config = {'changed-files': [{any: ['testing']}]};
|
||||||
|
it('returns an empty object', () => {
|
||||||
|
const result = toBranchMatchConfig(config);
|
||||||
|
expect(result).toMatchObject({});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when the config contains a head-branch option', () => {
|
||||||
|
const config = {'head-branch': ['testing']};
|
||||||
|
it('sets headBranch in the matchConfig', () => {
|
||||||
|
const result = toBranchMatchConfig(config);
|
||||||
|
expect(result).toMatchObject<BranchMatchConfig>({
|
||||||
|
headBranch: ['testing']
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('and the matching option is a string', () => {
|
||||||
|
const stringConfig = {'head-branch': 'testing'};
|
||||||
|
|
||||||
|
it('sets headBranch in the matchConfig', () => {
|
||||||
|
const result = toBranchMatchConfig(stringConfig);
|
||||||
|
expect(result).toMatchObject<BranchMatchConfig>({
|
||||||
|
headBranch: ['testing']
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when the config contains a base-branch option', () => {
|
||||||
|
const config = {'base-branch': ['testing']};
|
||||||
|
it('sets headBranch in the matchConfig', () => {
|
||||||
|
const result = toBranchMatchConfig(config);
|
||||||
|
expect(result).toMatchObject<BranchMatchConfig>({
|
||||||
|
baseBranch: ['testing']
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('and the matching option is a string', () => {
|
||||||
|
const stringConfig = {'base-branch': 'testing'};
|
||||||
|
|
||||||
|
it('sets headBranch in the matchConfig', () => {
|
||||||
|
const result = toBranchMatchConfig(stringConfig);
|
||||||
|
expect(result).toMatchObject<BranchMatchConfig>({
|
||||||
|
baseBranch: ['testing']
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('when the config contains both a base-branch and head-branch option', () => {
|
||||||
|
const config = {'base-branch': ['testing'], 'head-branch': ['testing']};
|
||||||
|
it('sets headBranch in the matchConfig', () => {
|
||||||
|
const result = toBranchMatchConfig(config);
|
||||||
|
expect(result).toMatchObject<BranchMatchConfig>({
|
||||||
|
baseBranch: ['testing'],
|
||||||
|
headBranch: ['testing']
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
test-branch:
|
test-branch:
|
||||||
- branch: "test/**"
|
- head-branch: "^test/"
|
||||||
|
|
||||||
feature-branch:
|
feature-branch:
|
||||||
- branch: "*/feature/*"
|
- head-branch: "/feature/"
|
||||||
|
|
||||||
bug-branch:
|
bug-branch:
|
||||||
- branch: "{bug,fix}/*"
|
- head-branch: "^bug/|fix/"
|
||||||
|
|
||||||
array-branch:
|
array-branch:
|
||||||
- branch: ["array/*"]
|
- head-branch: ["^array/"]
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
touched-a-pdf-file:
|
touched-a-pdf-file:
|
||||||
|
- changed-files:
|
||||||
- any: ['*.pdf']
|
- any: ['*.pdf']
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import {checkGlobs} from '../src/labeler';
|
import {checkGlobs, MatchConfig} from '../src/labeler';
|
||||||
|
|
||||||
import * as core from '@actions/core';
|
import * as core from '@actions/core';
|
||||||
|
|
||||||
@@ -10,7 +10,11 @@ beforeAll(() => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const matchConfig = [{any: ['*.txt']}];
|
// I have to double cast here as this is what the output from js-yaml looks like which then gets
|
||||||
|
// transformed in toMatchConfig
|
||||||
|
const matchConfig = [
|
||||||
|
{'changed-files': [{any: ['*.txt']}]}
|
||||||
|
] as unknown as MatchConfig[];
|
||||||
|
|
||||||
describe('checkGlobs', () => {
|
describe('checkGlobs', () => {
|
||||||
it('returns true when our pattern does match changed files', () => {
|
it('returns true when our pattern does match changed files', () => {
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ describe('run', () => {
|
|||||||
expect(removeLabelMock).toHaveBeenCalledTimes(0);
|
expect(removeLabelMock).toHaveBeenCalledTimes(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('adds labels based on the branch names that match the glob pattern', async () => {
|
it('adds labels based on the branch names that match the regexp pattern', async () => {
|
||||||
github.context.payload.pull_request!.head = {ref: 'test/testing-time'};
|
github.context.payload.pull_request!.head = {ref: 'test/testing-time'};
|
||||||
usingLabelerConfigYaml('branches.yml');
|
usingLabelerConfigYaml('branches.yml');
|
||||||
await run();
|
await run();
|
||||||
@@ -118,7 +118,7 @@ describe('run', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('adds multiple labels based on branch names that match different glob patterns', async () => {
|
it('adds multiple labels based on branch names that match different regexp patterns', async () => {
|
||||||
github.context.payload.pull_request!.head = {
|
github.context.payload.pull_request!.head = {
|
||||||
ref: 'test/feature/123'
|
ref: 'test/feature/123'
|
||||||
};
|
};
|
||||||
|
|||||||
232
dist/index.js
vendored
232
dist/index.js
vendored
@@ -1,6 +1,106 @@
|
|||||||
/******/ (() => { // webpackBootstrap
|
/******/ (() => { // webpackBootstrap
|
||||||
/******/ var __webpack_modules__ = ({
|
/******/ var __webpack_modules__ = ({
|
||||||
|
|
||||||
|
/***/ 8045:
|
||||||
|
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
|
||||||
|
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||||
|
if (k2 === undefined) k2 = k;
|
||||||
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||||
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||||
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||||
|
}
|
||||||
|
Object.defineProperty(o, k2, desc);
|
||||||
|
}) : (function(o, m, k, k2) {
|
||||||
|
if (k2 === undefined) k2 = k;
|
||||||
|
o[k2] = m[k];
|
||||||
|
}));
|
||||||
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||||||
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||||||
|
}) : function(o, v) {
|
||||||
|
o["default"] = v;
|
||||||
|
});
|
||||||
|
var __importStar = (this && this.__importStar) || function (mod) {
|
||||||
|
if (mod && mod.__esModule) return mod;
|
||||||
|
var result = {};
|
||||||
|
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
||||||
|
__setModuleDefault(result, mod);
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
||||||
|
exports.checkBranch = exports.getBranchName = exports.toBranchMatchConfig = void 0;
|
||||||
|
const core = __importStar(__nccwpck_require__(2186));
|
||||||
|
const github = __importStar(__nccwpck_require__(5438));
|
||||||
|
function toBranchMatchConfig(config) {
|
||||||
|
if (!config['head-branch'] && !config['base-branch']) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
const branchConfig = {
|
||||||
|
headBranch: config['head-branch'],
|
||||||
|
baseBranch: config['base-branch']
|
||||||
|
};
|
||||||
|
if (branchConfig.headBranch) {
|
||||||
|
const patterns = branchConfig.headBranch;
|
||||||
|
if (typeof patterns === 'string') {
|
||||||
|
branchConfig.headBranch = [patterns];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (branchConfig.baseBranch) {
|
||||||
|
const patterns = branchConfig.baseBranch;
|
||||||
|
if (typeof patterns === 'string') {
|
||||||
|
branchConfig.baseBranch = [patterns];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return branchConfig;
|
||||||
|
}
|
||||||
|
exports.toBranchMatchConfig = toBranchMatchConfig;
|
||||||
|
function getBranchName(branchBase) {
|
||||||
|
var _a, _b;
|
||||||
|
const pullRequest = github.context.payload.pull_request;
|
||||||
|
if (!pullRequest) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
if (branchBase === 'base') {
|
||||||
|
return (_a = pullRequest.base) === null || _a === void 0 ? void 0 : _a.ref;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return (_b = pullRequest.head) === null || _b === void 0 ? void 0 : _b.ref;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exports.getBranchName = getBranchName;
|
||||||
|
function checkBranch(regexps, branchBase) {
|
||||||
|
const branchName = getBranchName(branchBase);
|
||||||
|
if (!branchName) {
|
||||||
|
core.debug(` no branch name`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
core.debug(` checking "branch" pattern against ${branchName}`);
|
||||||
|
const matchers = regexps.map(regexp => new RegExp(regexp));
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
exports.checkBranch = checkBranch;
|
||||||
|
function matchBranchPattern(matcher, branchName) {
|
||||||
|
core.debug(` - ${matcher}`);
|
||||||
|
if (matcher.test(branchName)) {
|
||||||
|
core.debug(` "branch" pattern matched`);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
core.debug(` ${matcher} did not match`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/***/ }),
|
||||||
|
|
||||||
/***/ 5272:
|
/***/ 5272:
|
||||||
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
|
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
|
||||||
|
|
||||||
@@ -44,6 +144,7 @@ const core = __importStar(__nccwpck_require__(2186));
|
|||||||
const github = __importStar(__nccwpck_require__(5438));
|
const github = __importStar(__nccwpck_require__(5438));
|
||||||
const yaml = __importStar(__nccwpck_require__(1917));
|
const yaml = __importStar(__nccwpck_require__(1917));
|
||||||
const minimatch_1 = __nccwpck_require__(3973);
|
const minimatch_1 = __nccwpck_require__(3973);
|
||||||
|
const branch_1 = __nccwpck_require__(8045);
|
||||||
function run() {
|
function run() {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
try {
|
try {
|
||||||
@@ -63,12 +164,12 @@ function run() {
|
|||||||
});
|
});
|
||||||
core.debug(`fetching changed files for pr #${prNumber}`);
|
core.debug(`fetching changed files for pr #${prNumber}`);
|
||||||
const changedFiles = yield getChangedFiles(client, prNumber);
|
const changedFiles = yield getChangedFiles(client, prNumber);
|
||||||
const labelGlobs = yield getLabelGlobs(client, configPath);
|
const labelConfigs = yield getMatchConfigs(client, configPath);
|
||||||
const labels = [];
|
const labels = [];
|
||||||
const labelsToRemove = [];
|
const labelsToRemove = [];
|
||||||
for (const [label, globs] of labelGlobs.entries()) {
|
for (const [label, configs] of labelConfigs.entries()) {
|
||||||
core.debug(`processing ${label}`);
|
core.debug(`processing ${label}`);
|
||||||
if (checkGlobs(changedFiles, globs)) {
|
if (checkGlobs(changedFiles, configs)) {
|
||||||
labels.push(label);
|
labels.push(label);
|
||||||
}
|
}
|
||||||
else if (pullRequest.labels.find(l => l.name === label)) {
|
else if (pullRequest.labels.find(l => l.name === label)) {
|
||||||
@@ -96,14 +197,6 @@ function getPrNumber() {
|
|||||||
}
|
}
|
||||||
return pullRequest.number;
|
return pullRequest.number;
|
||||||
}
|
}
|
||||||
function getBranchName() {
|
|
||||||
var _a;
|
|
||||||
const pullRequest = github.context.payload.pull_request;
|
|
||||||
if (!pullRequest) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
return (_a = pullRequest.head) === null || _a === void 0 ? void 0 : _a.ref;
|
|
||||||
}
|
|
||||||
function getChangedFiles(client, prNumber) {
|
function getChangedFiles(client, prNumber) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
const listFilesOptions = client.rest.pulls.listFiles.endpoint.merge({
|
const listFilesOptions = client.rest.pulls.listFiles.endpoint.merge({
|
||||||
@@ -120,13 +213,13 @@ function getChangedFiles(client, prNumber) {
|
|||||||
return changedFiles;
|
return changedFiles;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
function getLabelGlobs(client, configurationPath) {
|
function getMatchConfigs(client, configurationPath) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
const configurationContent = yield fetchContent(client, configurationPath);
|
const configurationContent = yield fetchContent(client, configurationPath);
|
||||||
// loads (hopefully) a `{[label:string]: string | StringOrMatchConfig[]}`, but is `any`:
|
// loads (hopefully) a `{[label:string]: string | StringOrMatchConfig[]}`, but is `any`:
|
||||||
const configObject = yaml.load(configurationContent);
|
const configObject = yaml.load(configurationContent);
|
||||||
// transform `any` => `Map<string,StringOrMatchConfig[]>` or throw if yaml is malformed:
|
// transform `any` => `Map<string,StringOrMatchConfig[]>` or throw if yaml is malformed:
|
||||||
return getLabelGlobMapFromObject(configObject);
|
return getLabelConfigMapFromObject(configObject);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
function fetchContent(client, repoPath) {
|
function fetchContent(client, repoPath) {
|
||||||
@@ -140,7 +233,7 @@ function fetchContent(client, repoPath) {
|
|||||||
return Buffer.from(response.data.content, response.data.encoding).toString();
|
return Buffer.from(response.data.content, response.data.encoding).toString();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
function getLabelGlobMapFromObject(configObject) {
|
function getLabelConfigMapFromObject(configObject) {
|
||||||
const labelGlobs = new Map();
|
const labelGlobs = new Map();
|
||||||
for (const label in configObject) {
|
for (const label in configObject) {
|
||||||
if (typeof configObject[label] === 'string') {
|
if (typeof configObject[label] === 'string') {
|
||||||
@@ -155,13 +248,48 @@ function getLabelGlobMapFromObject(configObject) {
|
|||||||
}
|
}
|
||||||
return labelGlobs;
|
return labelGlobs;
|
||||||
}
|
}
|
||||||
function toMatchConfig(config) {
|
function toChangedFilesMatchConfig(config) {
|
||||||
if (typeof config === 'string') {
|
if (!config['changed-files']) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
const changedFilesConfig = config['changed-files'];
|
||||||
|
// If the value provided is a string or an array of strings then default to `any` matching
|
||||||
|
if (typeof changedFilesConfig === 'string') {
|
||||||
return {
|
return {
|
||||||
any: [config]
|
changedFiles: {
|
||||||
|
any: [changedFilesConfig]
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return config;
|
const changedFilesMatchConfig = {
|
||||||
|
changedFiles: {}
|
||||||
|
};
|
||||||
|
if (Array.isArray(changedFilesConfig)) {
|
||||||
|
if (changedFilesConfig.every(entry => typeof entry === 'string')) {
|
||||||
|
changedFilesMatchConfig.changedFiles = {
|
||||||
|
any: changedFilesConfig
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// If it is not an array of strings then it should be array of further config options
|
||||||
|
// so assign them to our `changedFilesMatchConfig`
|
||||||
|
changedFilesConfig.forEach(config => {
|
||||||
|
// Make sure that the values that we assign to our match config are an array
|
||||||
|
Object.entries(config).forEach(([key, value]) => {
|
||||||
|
const element = {
|
||||||
|
[key]: Array.isArray(value) ? value : [value]
|
||||||
|
};
|
||||||
|
Object.assign(changedFilesMatchConfig.changedFiles, element);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return changedFilesMatchConfig;
|
||||||
|
}
|
||||||
|
function toMatchConfig(config) {
|
||||||
|
const changedFilesConfig = toChangedFilesMatchConfig(config);
|
||||||
|
const branchConfig = (0, branch_1.toBranchMatchConfig)(config);
|
||||||
|
return Object.assign(Object.assign({}, changedFilesConfig), branchConfig);
|
||||||
}
|
}
|
||||||
function printPattern(matcher) {
|
function printPattern(matcher) {
|
||||||
return (matcher.negate ? '!' : '') + matcher.pattern;
|
return (matcher.negate ? '!' : '') + matcher.pattern;
|
||||||
@@ -215,55 +343,29 @@ function checkAll(changedFiles, globs) {
|
|||||||
core.debug(` "all" patterns matched all files`);
|
core.debug(` "all" patterns matched all files`);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
function matchBranchPattern(matcher, branchName) {
|
|
||||||
core.debug(` - ${printPattern(matcher)}`);
|
|
||||||
if (matcher.match(branchName)) {
|
|
||||||
core.debug(` "branch" pattern matched`);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
core.debug(` ${printPattern(matcher)} did not match`);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
function checkBranch(glob) {
|
|
||||||
const branchName = getBranchName();
|
|
||||||
if (!branchName) {
|
|
||||||
core.debug(` no branch name`);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
core.debug(` checking "branch" pattern against ${branchName}`);
|
|
||||||
if (Array.isArray(glob)) {
|
|
||||||
const matchers = glob.map(g => new minimatch_1.Minimatch(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;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
const matcher = new minimatch_1.Minimatch(glob);
|
|
||||||
return matchBranchPattern(matcher, branchName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function checkMatch(changedFiles, matchConfig) {
|
function checkMatch(changedFiles, matchConfig) {
|
||||||
if (matchConfig.all !== undefined) {
|
var _a, _b;
|
||||||
if (!checkAll(changedFiles, matchConfig.all)) {
|
if (((_a = matchConfig.changedFiles) === null || _a === void 0 ? void 0 : _a.all) !== undefined) {
|
||||||
return false;
|
if (checkAll(changedFiles, matchConfig.changedFiles.all)) {
|
||||||
}
|
|
||||||
}
|
|
||||||
if (matchConfig.any !== undefined) {
|
|
||||||
if (!checkAny(changedFiles, matchConfig.any)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (matchConfig.branch !== undefined) {
|
|
||||||
if (!checkBranch(matchConfig.branch)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (((_b = matchConfig.changedFiles) === null || _b === void 0 ? void 0 : _b.any) !== undefined) {
|
||||||
|
if (checkAny(changedFiles, matchConfig.changedFiles.any)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (matchConfig.headBranch !== undefined) {
|
||||||
|
if ((0, branch_1.checkBranch)(matchConfig.headBranch, 'head')) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (matchConfig.baseBranch !== undefined) {
|
||||||
|
if ((0, branch_1.checkBranch)(matchConfig.baseBranch, 'base')) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
function addLabels(client, prNumber, labels) {
|
function addLabels(client, prNumber, labels) {
|
||||||
return __awaiter(this, void 0, void 0, function* () {
|
return __awaiter(this, void 0, void 0, function* () {
|
||||||
|
|||||||
83
src/branch.ts
Normal file
83
src/branch.ts
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
import * as core from '@actions/core';
|
||||||
|
import * as github from '@actions/github';
|
||||||
|
|
||||||
|
export interface BranchMatchConfig {
|
||||||
|
headBranch?: string[];
|
||||||
|
baseBranch?: string[];
|
||||||
|
}
|
||||||
|
|
||||||
|
type BranchBase = 'base' | 'head';
|
||||||
|
|
||||||
|
export function toBranchMatchConfig(config: any): BranchMatchConfig {
|
||||||
|
if (!config['head-branch'] && !config['base-branch']) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const branchConfig = {
|
||||||
|
headBranch: config['head-branch'],
|
||||||
|
baseBranch: config['base-branch']
|
||||||
|
};
|
||||||
|
|
||||||
|
if (branchConfig.headBranch) {
|
||||||
|
const patterns = branchConfig.headBranch;
|
||||||
|
if (typeof patterns === 'string') {
|
||||||
|
branchConfig.headBranch = [patterns];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (branchConfig.baseBranch) {
|
||||||
|
const patterns = branchConfig.baseBranch;
|
||||||
|
if (typeof patterns === 'string') {
|
||||||
|
branchConfig.baseBranch = [patterns];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return branchConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getBranchName(branchBase?: BranchBase): string | undefined {
|
||||||
|
const pullRequest = github.context.payload.pull_request;
|
||||||
|
if (!pullRequest) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (branchBase === 'base') {
|
||||||
|
return pullRequest.base?.ref;
|
||||||
|
} else {
|
||||||
|
return pullRequest.head?.ref;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function checkBranch(
|
||||||
|
regexps: string[],
|
||||||
|
branchBase?: BranchBase
|
||||||
|
): boolean {
|
||||||
|
const branchName = getBranchName(branchBase);
|
||||||
|
if (!branchName) {
|
||||||
|
core.debug(` no branch name`);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
core.debug(` checking "branch" pattern against ${branchName}`);
|
||||||
|
const matchers = regexps.map(regexp => new RegExp(regexp));
|
||||||
|
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;
|
||||||
|
}
|
||||||
173
src/labeler.ts
173
src/labeler.ts
@@ -1,15 +1,19 @@
|
|||||||
import * as core from '@actions/core';
|
import * as core from '@actions/core';
|
||||||
import * as github from '@actions/github';
|
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} from 'minimatch';
|
||||||
|
|
||||||
interface MatchConfig {
|
import {checkBranch, toBranchMatchConfig, BranchMatchConfig} from './branch';
|
||||||
|
|
||||||
|
interface ChangedFilesMatchConfig {
|
||||||
|
changedFiles?: {
|
||||||
all?: string[];
|
all?: string[];
|
||||||
any?: string[];
|
any?: string[];
|
||||||
branch?: string | string[];
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
type StringOrMatchConfig = string | MatchConfig;
|
export type MatchConfig = ChangedFilesMatchConfig & BranchMatchConfig;
|
||||||
|
|
||||||
type ClientType = ReturnType<typeof github.getOctokit>;
|
type ClientType = ReturnType<typeof github.getOctokit>;
|
||||||
|
|
||||||
export async function run() {
|
export async function run() {
|
||||||
@@ -34,16 +38,16 @@ 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 labelConfigs: Map<string, MatchConfig[]> = await getMatchConfigs(
|
||||||
client,
|
client,
|
||||||
configPath
|
configPath
|
||||||
);
|
);
|
||||||
|
|
||||||
const labels: string[] = [];
|
const labels: string[] = [];
|
||||||
const labelsToRemove: string[] = [];
|
const labelsToRemove: string[] = [];
|
||||||
for (const [label, globs] of labelGlobs.entries()) {
|
for (const [label, configs] of labelConfigs.entries()) {
|
||||||
core.debug(`processing ${label}`);
|
core.debug(`processing ${label}`);
|
||||||
if (checkGlobs(changedFiles, globs)) {
|
if (checkGlobs(changedFiles, configs)) {
|
||||||
labels.push(label);
|
labels.push(label);
|
||||||
} else if (pullRequest.labels.find(l => l.name === label)) {
|
} else if (pullRequest.labels.find(l => l.name === label)) {
|
||||||
labelsToRemove.push(label);
|
labelsToRemove.push(label);
|
||||||
@@ -72,15 +76,6 @@ function getPrNumber(): number | undefined {
|
|||||||
return pullRequest.number;
|
return pullRequest.number;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getBranchName(): 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
|
||||||
@@ -102,10 +97,10 @@ async function getChangedFiles(
|
|||||||
return changedFiles;
|
return changedFiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getLabelGlobs(
|
async function getMatchConfigs(
|
||||||
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
|
||||||
@@ -115,7 +110,7 @@ async function getLabelGlobs(
|
|||||||
const configObject: any = yaml.load(configurationContent);
|
const configObject: any = yaml.load(configurationContent);
|
||||||
|
|
||||||
// transform `any` => `Map<string,StringOrMatchConfig[]>` or throw if yaml is malformed:
|
// transform `any` => `Map<string,StringOrMatchConfig[]>` or throw if yaml is malformed:
|
||||||
return getLabelGlobMapFromObject(configObject);
|
return getLabelConfigMapFromObject(configObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchContent(
|
async function fetchContent(
|
||||||
@@ -132,10 +127,10 @@ async function fetchContent(
|
|||||||
return Buffer.from(response.data.content, response.data.encoding).toString();
|
return Buffer.from(response.data.content, response.data.encoding).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLabelGlobMapFromObject(
|
function getLabelConfigMapFromObject(
|
||||||
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]]);
|
||||||
@@ -151,23 +146,65 @@ 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 changedFilesConfig = config['changed-files'];
|
||||||
|
|
||||||
|
// If the value provided is a string or an array of strings then default to `any` matching
|
||||||
|
if (typeof changedFilesConfig === 'string') {
|
||||||
return {
|
return {
|
||||||
any: [config]
|
changedFiles: {
|
||||||
|
any: [changedFilesConfig]
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return config;
|
const changedFilesMatchConfig = {
|
||||||
|
changedFiles: {}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (Array.isArray(changedFilesConfig)) {
|
||||||
|
if (changedFilesConfig.every(entry => typeof entry === 'string')) {
|
||||||
|
changedFilesMatchConfig.changedFiles = {
|
||||||
|
any: changedFilesConfig
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
// If it is not an array of strings then it should be array of further config options
|
||||||
|
// so assign them to our `changedFilesMatchConfig`
|
||||||
|
changedFilesConfig.forEach(config => {
|
||||||
|
// Make sure that the values that we assign to our match config are an array
|
||||||
|
Object.entries(config).forEach(([key, value]) => {
|
||||||
|
const element = {
|
||||||
|
[key]: Array.isArray(value) ? value : [value]
|
||||||
|
};
|
||||||
|
Object.assign(changedFilesMatchConfig.changedFiles, element);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return changedFilesMatchConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
function printPattern(matcher: IMinimatch): string {
|
function toMatchConfig(config: MatchConfig): MatchConfig {
|
||||||
|
const changedFilesConfig = toChangedFilesMatchConfig(config);
|
||||||
|
const branchConfig = toBranchMatchConfig(config);
|
||||||
|
|
||||||
|
return {
|
||||||
|
...changedFilesConfig,
|
||||||
|
...branchConfig
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function printPattern(matcher: Minimatch): string {
|
||||||
return (matcher.negate ? '!' : '') + matcher.pattern;
|
return (matcher.negate ? '!' : '') + matcher.pattern;
|
||||||
}
|
}
|
||||||
|
|
||||||
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)}`);
|
||||||
@@ -179,7 +216,7 @@ export function checkGlobs(
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isMatch(changedFile: string, matchers: IMinimatch[]): boolean {
|
function isMatch(changedFile: string, matchers: Minimatch[]): boolean {
|
||||||
core.debug(` matching patterns against file ${changedFile}`);
|
core.debug(` matching patterns against file ${changedFile}`);
|
||||||
for (const matcher of matchers) {
|
for (const matcher of matchers) {
|
||||||
core.debug(` - ${printPattern(matcher)}`);
|
core.debug(` - ${printPattern(matcher)}`);
|
||||||
@@ -223,62 +260,32 @@ function checkAll(changedFiles: string[], globs: string[]): boolean {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function matchBranchPattern(matcher: IMinimatch, branchName: string): boolean {
|
|
||||||
core.debug(` - ${printPattern(matcher)}`);
|
|
||||||
if (matcher.match(branchName)) {
|
|
||||||
core.debug(` "branch" pattern matched`);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
core.debug(` ${printPattern(matcher)} did not match`);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function checkBranch(glob: string | string[]): boolean {
|
|
||||||
const branchName = getBranchName();
|
|
||||||
if (!branchName) {
|
|
||||||
core.debug(` no branch name`);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
core.debug(` checking "branch" pattern against ${branchName}`);
|
|
||||||
if (Array.isArray(glob)) {
|
|
||||||
const matchers = glob.map(g => new Minimatch(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;
|
|
||||||
} else {
|
|
||||||
const matcher = new Minimatch(glob);
|
|
||||||
return matchBranchPattern(matcher, branchName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (matchConfig.any !== undefined) {
|
|
||||||
if (!checkAny(changedFiles, matchConfig.any)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (matchConfig.branch !== undefined) {
|
|
||||||
if (!checkBranch(matchConfig.branch)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (matchConfig.changedFiles?.any !== undefined) {
|
||||||
|
if (checkAny(changedFiles, matchConfig.changedFiles.any)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (matchConfig.headBranch !== undefined) {
|
||||||
|
if (checkBranch(matchConfig.headBranch, 'head')) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (matchConfig.baseBranch !== undefined) {
|
||||||
|
if (checkBranch(matchConfig.baseBranch, 'base')) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function addLabels(
|
async function addLabels(
|
||||||
|
|||||||
Reference in New Issue
Block a user