Merge branch 'main'

This commit is contained in:
MaksimZhukov
2023-10-24 12:11:31 +02:00
35 changed files with 7607 additions and 1338 deletions

View File

@@ -0,0 +1,24 @@
import * as core from '@actions/core';
import * as github from '@actions/github';
import {ClientType} from './types';
export const getChangedFiles = async (
client: ClientType,
prNumber: number
): Promise<string[]> => {
const listFilesOptions = client.rest.pulls.listFiles.endpoint.merge({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: prNumber
});
const listFilesResponse = await client.paginate(listFilesOptions);
const changedFiles = listFilesResponse.map((f: any) => f.filename);
core.debug('found changed files:');
for (const file of changedFiles) {
core.debug(' ' + file);
}
return changedFiles;
};

View File

@@ -0,0 +1,38 @@
import * as core from '@actions/core';
import * as github from '@actions/github';
import {getChangedFiles} from './get-changed-files';
import {ClientType} from './types';
export async function* getPullRequests(
client: ClientType,
prNumbers: number[]
) {
for (const prNumber of prNumbers) {
core.debug(`looking for pr #${prNumber}`);
let prData: any;
try {
const result = await client.rest.pulls.get({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: prNumber
});
prData = result.data;
} catch (error: any) {
core.warning(`Could not find pull request #${prNumber}, skipping`);
continue;
}
core.debug(`fetching changed files for pr #${prNumber}`);
const changedFiles: string[] = await getChangedFiles(client, prNumber);
if (!changedFiles.length) {
core.warning(`Pull request #${prNumber} has no changed files, skipping`);
continue;
}
yield {
data: prData,
number: prNumber,
changedFiles
};
}
}

16
src/api/get-content.ts Normal file
View File

@@ -0,0 +1,16 @@
import * as github from '@actions/github';
import {ClientType} from './types';
export const getContent = async (
client: ClientType,
repoPath: string
): Promise<string> => {
const response: any = await client.rest.repos.getContent({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
path: repoPath,
ref: github.context.sha
});
return Buffer.from(response.data.content, response.data.encoding).toString();
};

View File

@@ -0,0 +1,126 @@
import * as core from '@actions/core';
import * as yaml from 'js-yaml';
import fs from 'fs';
import {ClientType} from './types';
import {getContent} from './get-content';
import {
ChangedFilesMatchConfig,
toChangedFilesMatchConfig
} from '../changedFiles';
import {toBranchMatchConfig, BranchMatchConfig} from '../branch';
export interface MatchConfig {
all?: BaseMatchConfig[];
any?: BaseMatchConfig[];
}
export type BaseMatchConfig = BranchMatchConfig & ChangedFilesMatchConfig;
const ALLOWED_CONFIG_KEYS = ['changed-files', 'head-branch', 'base-branch'];
export const getLabelConfigs = (
client: ClientType,
configurationPath: string
): Promise<Map<string, MatchConfig[]>> =>
Promise.resolve()
.then(() => {
if (!fs.existsSync(configurationPath)) {
core.info(
`The configuration file (path: ${configurationPath}) was not found locally, fetching via the api`
);
return getContent(client, configurationPath);
}
core.info(
`The configuration file (path: ${configurationPath}) was found locally, reading from the file`
);
return fs.readFileSync(configurationPath, {
encoding: 'utf8'
});
})
.catch(error => {
if (error.name == 'HttpError' || error.name == 'NotFound') {
core.warning(
`The config file was not found at ${configurationPath}. Make sure it exists and that this action has the correct access rights.`
);
}
return Promise.reject(error);
})
.then(configuration => {
// loads (hopefully) a `{[label:string]: MatchConfig[]}`, but is `any`:
const configObject: any = yaml.load(configuration);
// transform `any` => `Map<string,MatchConfig[]>` or throw if yaml is malformed:
return getLabelConfigMapFromObject(configObject);
});
export function getLabelConfigMapFromObject(
configObject: any
): Map<string, MatchConfig[]> {
const labelMap: Map<string, MatchConfig[]> = new Map();
for (const label in configObject) {
const configOptions = configObject[label];
if (
!Array.isArray(configOptions) ||
!configOptions.every(opts => typeof opts === 'object')
) {
throw Error(
`found unexpected type for label '${label}' (should be array of config options)`
);
}
const matchConfigs = configOptions.reduce<MatchConfig[]>(
(updatedConfig, configValue) => {
if (!configValue) {
return updatedConfig;
}
Object.entries(configValue).forEach(([key, value]) => {
// If the top level `any` or `all` keys are provided then set them, and convert their values to
// our config objects.
if (key === 'any' || key === 'all') {
if (Array.isArray(value)) {
const newConfigs = value.map(toMatchConfig);
updatedConfig.push({[key]: newConfigs});
}
} else if (ALLOWED_CONFIG_KEYS.includes(key)) {
const newMatchConfig = toMatchConfig({[key]: value});
// Find or set the `any` key so that we can add these properties to that rule,
// Or create a new `any` key and add that to our array of configs.
const indexOfAny = updatedConfig.findIndex(mc => !!mc['any']);
if (indexOfAny >= 0) {
updatedConfig[indexOfAny].any?.push(newMatchConfig);
} else {
updatedConfig.push({any: [newMatchConfig]});
}
} else {
// Log the key that we don't know what to do with.
core.info(`An unknown config option was under ${label}: ${key}`);
}
});
return updatedConfig;
},
[]
);
if (matchConfigs.length) {
labelMap.set(label, matchConfigs);
}
}
return labelMap;
}
export function toMatchConfig(config: any): BaseMatchConfig {
const changedFilesConfig = toChangedFilesMatchConfig(config);
const branchConfig = toBranchMatchConfig(config);
return {
...changedFilesConfig,
...branchConfig
};
}

6
src/api/index.ts Normal file
View File

@@ -0,0 +1,6 @@
export * from './get-changed-files';
export * from './get-changed-pull-requests';
export * from './get-content';
export * from './get-label-configs';
export * from './set-labels';
export * from './types';

15
src/api/set-labels.ts Normal file
View File

@@ -0,0 +1,15 @@
import * as github from '@actions/github';
import {ClientType} from './types';
export const setLabels = async (
client: ClientType,
prNumber: number,
labels: string[]
) => {
await client.rest.issues.setLabels({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: prNumber,
labels: labels
});
};

2
src/api/types.ts Normal file
View File

@@ -0,0 +1,2 @@
import * as github from '@actions/github';
export type ClientType = ReturnType<typeof github.getOctokit>;

View File

@@ -99,7 +99,8 @@ function printPattern(matcher: Minimatch): string {
export function checkAnyChangedFiles(
changedFiles: string[],
globPatternsConfigs: ChangedFilesGlobPatternsConfig[]
globPatternsConfigs: ChangedFilesGlobPatternsConfig[],
dot: boolean
): boolean {
core.debug(` checking "changed-files" patterns`);
@@ -108,7 +109,8 @@ export function checkAnyChangedFiles(
if (
checkIfAnyGlobMatchesAnyFile(
changedFiles,
globPatternsConfig.AnyGlobToAnyFile
globPatternsConfig.AnyGlobToAnyFile,
dot
)
) {
core.debug(` "changed-files" matched`);
@@ -120,7 +122,8 @@ export function checkAnyChangedFiles(
if (
checkIfAnyGlobMatchesAllFiles(
changedFiles,
globPatternsConfig.AnyGlobToAllFiles
globPatternsConfig.AnyGlobToAllFiles,
dot
)
) {
core.debug(` "changed-files" matched`);
@@ -132,7 +135,8 @@ export function checkAnyChangedFiles(
if (
checkIfAllGlobsMatchAnyFile(
changedFiles,
globPatternsConfig.AllGlobsToAnyFile
globPatternsConfig.AllGlobsToAnyFile,
dot
)
) {
core.debug(` "changed-files" matched`);
@@ -144,7 +148,8 @@ export function checkAnyChangedFiles(
if (
checkIfAllGlobsMatchAllFiles(
changedFiles,
globPatternsConfig.AllGlobsToAllFiles
globPatternsConfig.AllGlobsToAllFiles,
dot
)
) {
core.debug(` "changed-files" matched`);
@@ -159,7 +164,8 @@ export function checkAnyChangedFiles(
export function checkAllChangedFiles(
changedFiles: string[],
globPatternsConfigs: ChangedFilesGlobPatternsConfig[]
globPatternsConfigs: ChangedFilesGlobPatternsConfig[],
dot: boolean
): boolean {
core.debug(` checking "changed-files" patterns`);
@@ -168,7 +174,8 @@ export function checkAllChangedFiles(
if (
!checkIfAnyGlobMatchesAnyFile(
changedFiles,
globPatternsConfig.AnyGlobToAnyFile
globPatternsConfig.AnyGlobToAnyFile,
dot
)
) {
core.debug(` "changed-files" did not match`);
@@ -180,7 +187,8 @@ export function checkAllChangedFiles(
if (
!checkIfAnyGlobMatchesAllFiles(
changedFiles,
globPatternsConfig.AnyGlobToAllFiles
globPatternsConfig.AnyGlobToAllFiles,
dot
)
) {
core.debug(` "changed-files" did not match`);
@@ -192,7 +200,8 @@ export function checkAllChangedFiles(
if (
!checkIfAllGlobsMatchAnyFile(
changedFiles,
globPatternsConfig.AllGlobsToAnyFile
globPatternsConfig.AllGlobsToAnyFile,
dot
)
) {
core.debug(` "changed-files" did not match`);
@@ -204,7 +213,8 @@ export function checkAllChangedFiles(
if (
!checkIfAllGlobsMatchAllFiles(
changedFiles,
globPatternsConfig.AllGlobsToAllFiles
globPatternsConfig.AllGlobsToAllFiles,
dot
)
) {
core.debug(` "changed-files" did not match`);
@@ -219,10 +229,11 @@ export function checkAllChangedFiles(
export function checkIfAnyGlobMatchesAnyFile(
changedFiles: string[],
globs: string[]
globs: string[],
dot: boolean
): boolean {
core.debug(` checking "AnyGlobToAnyFile" config patterns`);
const matchers = globs.map(g => new Minimatch(g));
const matchers = globs.map(g => new Minimatch(g, {dot}));
for (const matcher of matchers) {
const matchedFile = changedFiles.find(changedFile => {
@@ -249,10 +260,11 @@ export function checkIfAnyGlobMatchesAnyFile(
export function checkIfAllGlobsMatchAnyFile(
changedFiles: string[],
globs: string[]
globs: string[],
dot: boolean
): boolean {
core.debug(` checking "AllGlobsToAnyFile" config patterns`);
const matchers = globs.map(g => new Minimatch(g));
const matchers = globs.map(g => new Minimatch(g, {dot}));
for (const changedFile of changedFiles) {
const mismatchedGlob = matchers.find(matcher => {
@@ -285,10 +297,11 @@ export function checkIfAllGlobsMatchAnyFile(
export function checkIfAnyGlobMatchesAllFiles(
changedFiles: string[],
globs: string[]
globs: string[],
dot: boolean
): boolean {
core.debug(` checking "AnyGlobToAllFiles" config patterns`);
const matchers = globs.map(g => new Minimatch(g));
const matchers = globs.map(g => new Minimatch(g, {dot}));
for (const matcher of matchers) {
const mismatchedFile = changedFiles.find(changedFile => {
@@ -321,10 +334,11 @@ export function checkIfAnyGlobMatchesAllFiles(
export function checkIfAllGlobsMatchAllFiles(
changedFiles: string[],
globs: string[]
globs: string[],
dot: boolean
): boolean {
core.debug(` checking "AllGlobsToAllFiles" config patterns`);
const matchers = globs.map(g => new Minimatch(g));
const matchers = globs.map(g => new Minimatch(g, {dot}));
for (const changedFile of changedFiles) {
const mismatchedGlob = matchers.find(matcher => {

View File

@@ -0,0 +1,10 @@
import * as core from '@actions/core';
import {getPrNumbers} from './get-pr-numbers';
export const getInputs = () => ({
token: core.getInput('repo-token'),
configPath: core.getInput('configuration-path', {required: true}),
syncLabels: core.getBooleanInput('sync-labels'),
dot: core.getBooleanInput('dot'),
prNumbers: getPrNumbers()
});

View File

@@ -0,0 +1,28 @@
import * as core from '@actions/core';
import * as github from '@actions/github';
const getPrNumberFromContext = () =>
github.context.payload.pull_request?.number;
export const getPrNumbers = (): number[] => {
const prInput = core.getMultilineInput('pr-number');
if (!prInput?.length) {
return [getPrNumberFromContext()].filter(Boolean) as number[];
}
const result: number[] = [];
for (const line of prInput) {
const prNumber = parseInt(line, 10);
if (isNaN(prNumber) && prNumber <= 0) {
core.warning(`'${prNumber}' is not a valid pull request number`);
continue;
}
result.push(prNumber);
}
return result;
};

1
src/get-inputs/index.ts Normal file
View File

@@ -0,0 +1 @@
export * from './get-inputs';

View File

@@ -1,196 +1,110 @@
import * as core from '@actions/core';
import * as github from '@actions/github';
import * as yaml from 'js-yaml';
import * as pluginRetry from '@octokit/plugin-retry';
import * as api from './api';
import isEqual from 'lodash.isequal';
import {getInputs} from './get-inputs';
import {
ChangedFilesMatchConfig,
getChangedFiles,
toChangedFilesMatchConfig,
checkAllChangedFiles,
checkAnyChangedFiles
} from './changedFiles';
import {
checkAnyBranch,
checkAllBranch,
toBranchMatchConfig,
BranchMatchConfig
} from './branch';
import {BaseMatchConfig, MatchConfig} from './api/get-label-configs';
export type BaseMatchConfig = BranchMatchConfig & ChangedFilesMatchConfig;
import {checkAllChangedFiles, checkAnyChangedFiles} from './changedFiles';
export type MatchConfig = {
any?: BaseMatchConfig[];
all?: BaseMatchConfig[];
};
import {checkAnyBranch, checkAllBranch} from './branch';
type ClientType = ReturnType<typeof github.getOctokit>;
const ALLOWED_CONFIG_KEYS = ['changed-files', 'head-branch', 'base-branch'];
// GitHub Issues cannot have more than 100 labels
const GITHUB_MAX_LABELS = 100;
export async function run() {
try {
const token = core.getInput('repo-token');
const configPath = core.getInput('configuration-path', {required: true});
const syncLabels = core.getBooleanInput('sync-labels');
export const run = () =>
labeler().catch(error => {
core.error(error);
core.setFailed(error.message);
});
const prNumber = getPrNumber();
if (!prNumber) {
core.info('Could not get pull request number from context, exiting');
return;
}
async function labeler() {
const {token, configPath, syncLabels, dot, prNumbers} = getInputs();
const client: ClientType = github.getOctokit(token);
if (!prNumbers.length) {
core.warning('Could not get pull request number(s), exiting');
return;
}
const {data: pullRequest} = await client.rest.pulls.get({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: prNumber
});
const client: ClientType = github.getOctokit(token, {}, pluginRetry.retry);
core.debug(`fetching changed files for pr #${prNumber}`);
const changedFiles: string[] = await getChangedFiles(client, prNumber);
const labelConfigs: Map<string, MatchConfig[]> = await getMatchConfigs(
const pullRequests = api.getPullRequests(client, prNumbers);
for await (const pullRequest of pullRequests) {
const labelConfigs: Map<string, MatchConfig[]> = await api.getLabelConfigs(
client,
configPath
);
const preexistingLabels = pullRequest.data.labels.map(l => l.name);
const allLabels: Set<string> = new Set<string>(preexistingLabels);
const labels: string[] = [];
const labelsToRemove: string[] = [];
for (const [label, configs] of labelConfigs.entries()) {
core.debug(`processing ${label}`);
if (checkMatchConfigs(changedFiles, configs)) {
labels.push(label);
} else if (pullRequest.labels.find(l => l.name === label)) {
labelsToRemove.push(label);
if (checkMatchConfigs(pullRequest.changedFiles, configs, dot)) {
allLabels.add(label);
} else if (syncLabels) {
allLabels.delete(label);
}
}
if (labels.length > 0) {
await addLabels(client, prNumber, labels);
const labelsToAdd = [...allLabels].slice(0, GITHUB_MAX_LABELS);
const excessLabels = [...allLabels].slice(GITHUB_MAX_LABELS);
let newLabels: string[] = [];
try {
if (!isEqual(labelsToAdd, preexistingLabels)) {
await api.setLabels(client, pullRequest.number, labelsToAdd);
newLabels = labelsToAdd.filter(
label => !preexistingLabels.includes(label)
);
}
} catch (error: any) {
if (
error.name !== 'HttpError' ||
error.message !== 'Resource not accessible by integration'
) {
throw error;
}
core.warning(
`The action requires write permission to add labels to pull requests. For more information please refer to the action documentation: https://github.com/actions/labeler#permissions`,
{
title: `${process.env['GITHUB_ACTION_REPOSITORY']} running under '${github.context.eventName}' is misconfigured`
}
);
core.setFailed(error.message);
return;
}
if (syncLabels && labelsToRemove.length) {
await removeLabels(client, prNumber, labelsToRemove);
}
} catch (error: any) {
core.error(error);
core.setFailed(error.message);
}
}
core.setOutput('new-labels', newLabels.join(','));
core.setOutput('all-labels', labelsToAdd.join(','));
function getPrNumber(): number | undefined {
const pullRequest = github.context.payload.pull_request;
if (!pullRequest) {
return undefined;
}
return pullRequest.number;
}
async function getMatchConfigs(
client: ClientType,
configurationPath: string
): Promise<Map<string, MatchConfig[]>> {
const configurationContent: string = await fetchContent(
client,
configurationPath
);
// loads (hopefully) a `{[label:string]: MatchConfig[]}`, but is `any`:
const configObject: any = yaml.load(configurationContent);
// transform `any` => `Map<string,MatchConfig[]>` or throw if yaml is malformed:
return getLabelConfigMapFromObject(configObject);
}
async function fetchContent(
client: ClientType,
repoPath: string
): Promise<string> {
const response: any = await client.rest.repos.getContent({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
path: repoPath,
ref: github.context.sha
});
return Buffer.from(response.data.content, response.data.encoding).toString();
}
export function getLabelConfigMapFromObject(
configObject: any
): Map<string, MatchConfig[]> {
const labelMap: Map<string, MatchConfig[]> = new Map();
for (const label in configObject) {
const configOptions = configObject[label];
if (
!Array.isArray(configOptions) ||
!configOptions.every(opts => typeof opts === 'object')
) {
throw Error(
`found unexpected type for label '${label}' (should be array of config options)`
if (excessLabels.length) {
core.warning(
`Maximum of ${GITHUB_MAX_LABELS} labels allowed. Excess labels: ${excessLabels.join(
', '
)}`,
{title: 'Label limit for a PR exceeded'}
);
}
const matchConfigs = configOptions.reduce<MatchConfig[]>(
(updatedConfig, configValue) => {
if (!configValue) {
return updatedConfig;
}
Object.entries(configValue).forEach(([key, value]) => {
// If the top level `any` or `all` keys are provided then set them, and convert their values to
// our config objects.
if (key === 'any' || key === 'all') {
if (Array.isArray(value)) {
const newConfigs = value.map(toMatchConfig);
updatedConfig.push({[key]: newConfigs});
}
} else if (ALLOWED_CONFIG_KEYS.includes(key)) {
const newMatchConfig = toMatchConfig({[key]: value});
// Find or set the `any` key so that we can add these properties to that rule,
// Or create a new `any` key and add that to our array of configs.
const indexOfAny = updatedConfig.findIndex(mc => !!mc['any']);
if (indexOfAny >= 0) {
updatedConfig[indexOfAny].any?.push(newMatchConfig);
} else {
updatedConfig.push({any: [newMatchConfig]});
}
} else {
// Log the key that we don't know what to do with.
core.info(`An unknown config option was under ${label}: ${key}`);
}
});
return updatedConfig;
},
[]
);
if (matchConfigs.length) {
labelMap.set(label, matchConfigs);
}
}
return labelMap;
}
export function toMatchConfig(config: any): BaseMatchConfig {
const changedFilesConfig = toChangedFilesMatchConfig(config);
const branchConfig = toBranchMatchConfig(config);
return {
...changedFilesConfig,
...branchConfig
};
}
export function checkMatchConfigs(
changedFiles: string[],
matchConfigs: MatchConfig[]
matchConfigs: MatchConfig[],
dot: boolean
): boolean {
for (const config of matchConfigs) {
core.debug(` checking config ${JSON.stringify(config)}`);
if (!checkMatch(changedFiles, config)) {
if (!checkMatch(changedFiles, config, dot)) {
return false;
}
}
@@ -198,20 +112,24 @@ export function checkMatchConfigs(
return true;
}
function checkMatch(changedFiles: string[], matchConfig: MatchConfig): boolean {
function checkMatch(
changedFiles: string[],
matchConfig: MatchConfig,
dot: boolean
): boolean {
if (!Object.keys(matchConfig).length) {
core.debug(` no "any" or "all" patterns to check`);
return false;
}
if (matchConfig.all) {
if (!checkAll(matchConfig.all, changedFiles)) {
if (!checkAll(matchConfig.all, changedFiles, dot)) {
return false;
}
}
if (matchConfig.any) {
if (!checkAny(matchConfig.any, changedFiles)) {
if (!checkAny(matchConfig.any, changedFiles, dot)) {
return false;
}
}
@@ -222,7 +140,8 @@ function checkMatch(changedFiles: string[], matchConfig: MatchConfig): boolean {
// equivalent to "Array.some()" but expanded for debugging and clarity
export function checkAny(
matchConfigs: BaseMatchConfig[],
changedFiles: string[]
changedFiles: string[],
dot: boolean
): boolean {
core.debug(` checking "any" patterns`);
if (
@@ -242,7 +161,7 @@ export function checkAny(
}
if (matchConfig.changedFiles) {
if (checkAnyChangedFiles(changedFiles, matchConfig.changedFiles)) {
if (checkAnyChangedFiles(changedFiles, matchConfig.changedFiles, dot)) {
core.debug(` "any" patterns matched`);
return true;
}
@@ -263,7 +182,8 @@ export function checkAny(
// equivalent to "Array.every()" but expanded for debugging and clarity
export function checkAll(
matchConfigs: BaseMatchConfig[],
changedFiles: string[]
changedFiles: string[],
dot: boolean
): boolean {
core.debug(` checking "all" patterns`);
if (
@@ -288,7 +208,7 @@ export function checkAll(
return false;
}
if (!checkAllChangedFiles(changedFiles, matchConfig.changedFiles)) {
if (!checkAllChangedFiles(changedFiles, matchConfig.changedFiles, dot)) {
core.debug(` "all" patterns did not match`);
return false;
}
@@ -305,33 +225,3 @@ export function checkAll(
core.debug(` "all" patterns matched all configs`);
return true;
}
async function addLabels(
client: ClientType,
prNumber: number,
labels: string[]
) {
await client.rest.issues.addLabels({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: prNumber,
labels: labels
});
}
async function removeLabels(
client: ClientType,
prNumber: number,
labels: string[]
) {
await Promise.all(
labels.map(label =>
client.rest.issues.removeLabel({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: prNumber,
name: label
})
)
);
}

1
src/utils/index.ts Normal file
View File

@@ -0,0 +1 @@
export * from './print-pattern';

View File

@@ -0,0 +1,5 @@
import {Minimatch} from 'minimatch';
export const printPattern = (matcher: Minimatch): string => {
return (matcher.negate ? '!' : '') + matcher.pattern;
};