Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ The base match object is defined as:
- all-globs-to-all-files: ['list', 'of', 'globs']
- base-branch: ['list', 'of', 'regexps']
- head-branch: ['list', 'of', 'regexps']
- authors: ['list', 'of', 'authors'] # github users
```

There are two top-level keys, `any` and `all`, which both accept the same configuration options:
Expand Down
3 changes: 3 additions & 0 deletions __mocks__/@actions/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ export const context = {
payload: {
pull_request: {
number: 123,
user: {
login: 'monalisa'
},
head: {
ref: 'head-branch-name'
},
Expand Down
44 changes: 42 additions & 2 deletions __tests__/labeler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,14 @@ describe('toMatchConfig', () => {
const config = {
'changed-files': [{'any-glob-to-any-file': ['testing-files']}],
'head-branch': ['testing-head'],
'base-branch': ['testing-base']
'base-branch': ['testing-base'],
authors: ['testing-author']
};
const expected: BaseMatchConfig = {
changedFiles: [{anyGlobToAnyFile: ['testing-files']}],
headBranch: ['testing-head'],
baseBranch: ['testing-base']
baseBranch: ['testing-base'],
authors: ['testing-author']
};

it('returns a MatchConfig object with all options', () => {
Expand Down Expand Up @@ -232,4 +234,42 @@ describe('labeler error handling', () => {
);
expect(core.setFailed).toHaveBeenCalledWith(error.message);
});

it('returns true when PR author is in the list of authors', () => {
const matchConfigWithAuthor: MatchConfig[] = [
{
any: [
{changedFiles: [{anyGlobToAnyFile: ['*.txt']}]},
{authors: ['monalisa', 'hubot']}
]
}
];
const changedFiles = ['not_match.pdf'];

const result = checkMatchConfigs(
changedFiles,
matchConfigWithAuthor,
false
);
expect(result).toBeTruthy();
});

it('returns false when PR author is not in the list of authors', () => {
const matchConfigWithAuthor: MatchConfig[] = [
{
any: [
{changedFiles: [{anyGlobToAnyFile: ['*.txt']}]},
{authors: ['foo', 'bar']}
]
}
];
const changedFiles = ['not_match.pdf'];

const result = checkMatchConfigs(
changedFiles,
matchConfigWithAuthor,
false
);
expect(result).toBeFalsy();
});
});
36 changes: 34 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,12 @@ const fs_1 = __importDefault(__nccwpck_require__(9896));
const get_content_1 = __nccwpck_require__(6519);
const changedFiles_1 = __nccwpck_require__(5145);
const branch_1 = __nccwpck_require__(2234);
const ALLOWED_CONFIG_KEYS = ['changed-files', 'head-branch', 'base-branch'];
const ALLOWED_CONFIG_KEYS = [
'changed-files',
'head-branch',
'base-branch',
'authors'
];
const getLabelConfigs = (client, configurationPath) => Promise.resolve()
.then(() => {
if (!fs_1.default.existsSync(configurationPath)) {
Expand Down Expand Up @@ -352,7 +357,8 @@ function getLabelConfigMapFromObject(configObject) {
function toMatchConfig(config) {
const changedFilesConfig = (0, changedFiles_1.toChangedFilesMatchConfig)(config);
const branchConfig = (0, branch_1.toBranchMatchConfig)(config);
return Object.assign(Object.assign({}, changedFilesConfig), branchConfig);
const authorsConfig = config['authors'] ? { authors: config['authors'] } : {};
return Object.assign(Object.assign(Object.assign({}, changedFilesConfig), branchConfig), authorsConfig);
}


Expand Down Expand Up @@ -1170,6 +1176,12 @@ function checkAny(matchConfigs, changedFiles, dot) {
return true;
}
}
if (matchConfig.authors) {
if (checkAuthors(matchConfig.authors)) {
core.debug(` "any" patterns matched`);
return true;
}
}
}
core.debug(` "any" patterns did not match any configs`);
return false;
Expand Down Expand Up @@ -1205,10 +1217,30 @@ function checkAll(matchConfigs, changedFiles, dot) {
return false;
}
}
if (matchConfig.authors) {
if (!checkAuthors(matchConfig.authors)) {
core.debug(` "all" patterns did not match`);
return false;
}
}
}
core.debug(` "all" patterns matched all configs`);
return true;
}
function checkAuthors(authors) {
var _a, _b;
const prAuthor = (_b = (_a = github.context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.user) === null || _b === void 0 ? void 0 : _b.login;
if (!prAuthor) {
core.info('Could not get pull request author from context, exiting');
return false;
}
if (authors.includes(prAuthor)) {
core.debug(` author ${prAuthor} is on the list`);
return true;
}
core.debug(` author ${prAuthor} is not on the list`);
return false;
}


/***/ }),
Expand Down
19 changes: 16 additions & 3 deletions src/api/get-label-configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,25 @@ import {

import {toBranchMatchConfig, BranchMatchConfig} from '../branch';

export interface AuthorsMatchConfig {
authors?: string[];
}

export interface MatchConfig {
all?: BaseMatchConfig[];
any?: BaseMatchConfig[];
}

export type BaseMatchConfig = BranchMatchConfig & ChangedFilesMatchConfig;
export type BaseMatchConfig = BranchMatchConfig &
ChangedFilesMatchConfig &
AuthorsMatchConfig;

const ALLOWED_CONFIG_KEYS = ['changed-files', 'head-branch', 'base-branch'];
const ALLOWED_CONFIG_KEYS = [
'changed-files',
'head-branch',
'base-branch',
'authors'
];

export const getLabelConfigs = (
client: ClientType,
Expand Down Expand Up @@ -118,9 +129,11 @@ export function getLabelConfigMapFromObject(
export function toMatchConfig(config: any): BaseMatchConfig {
const changedFilesConfig = toChangedFilesMatchConfig(config);
const branchConfig = toBranchMatchConfig(config);
const authorsConfig = config['authors'] ? {authors: config['authors']} : {};

return {
...changedFilesConfig,
...branchConfig
...branchConfig,
...authorsConfig
};
}
30 changes: 30 additions & 0 deletions src/labeler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ export function checkAny(
return true;
}
}

if (matchConfig.authors) {
if (checkAuthors(matchConfig.authors)) {
core.debug(` "any" patterns matched`);
return true;
}
}
}

core.debug(` "any" patterns did not match any configs`);
Expand Down Expand Up @@ -230,8 +237,31 @@ export function checkAll(
return false;
}
}

if (matchConfig.authors) {
if (!checkAuthors(matchConfig.authors)) {
core.debug(` "all" patterns did not match`);
return false;
}
}
}

core.debug(` "all" patterns matched all configs`);
return true;
}

function checkAuthors(authors: string[]): boolean {
const prAuthor = github.context.payload.pull_request?.user?.login;
if (!prAuthor) {
core.info('Could not get pull request author from context, exiting');
return false;
}

if (authors.includes(prAuthor)) {
core.debug(` author ${prAuthor} is on the list`);
return true;
}

core.debug(` author ${prAuthor} is not on the list`);
return false;
}