Skip to content

Commit 0ac510f

Browse files
committed
Fix tests
1 parent f1709fd commit 0ac510f

File tree

4 files changed

+89
-10
lines changed

4 files changed

+89
-10
lines changed

__tests__/labeler.test.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,21 +235,39 @@ describe('labeler error handling', () => {
235235

236236
it('returns true when PR author is in the list of authors', () => {
237237
const matchConfigWithAuthor: MatchConfig[] = [
238-
{any: ['*.txt'], authors: ['monalisa', 'hubot']}
238+
{
239+
any: [
240+
{changedFiles: [{anyGlobToAnyFile: ['*.txt']}]},
241+
{authors: ['monalisa', 'hubot']}
242+
]
243+
}
239244
];
240-
const changedFiles = ['foo.txt'];
245+
const changedFiles = ['not_match.pdf'];
241246

242-
const result = checkGlobs(changedFiles, matchConfigWithAuthor);
247+
const result = checkMatchConfigs(
248+
changedFiles,
249+
matchConfigWithAuthor,
250+
false
251+
);
243252
expect(result).toBeTruthy();
244253
});
245254

246255
it('returns false when PR author is not in the list of authors', () => {
247256
const matchConfigWithAuthor: MatchConfig[] = [
248-
{any: ['*.txt'], authors: ['foo', 'bar']}
257+
{
258+
any: [
259+
{changedFiles: [{anyGlobToAnyFile: ['*.txt']}]},
260+
{authors: ['foo', 'bar']}
261+
]
262+
}
249263
];
250-
const changedFiles = ['foo.txt'];
264+
const changedFiles = ['not_match.pdf'];
251265

252-
const result = checkGlobs(changedFiles, matchConfigWithAuthor);
266+
const result = checkMatchConfigs(
267+
changedFiles,
268+
matchConfigWithAuthor,
269+
false
270+
);
253271
expect(result).toBeFalsy();
254272
});
255273
});

dist/index.js

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,12 @@ const fs_1 = __importDefault(__nccwpck_require__(9896));
277277
const get_content_1 = __nccwpck_require__(6519);
278278
const changedFiles_1 = __nccwpck_require__(5145);
279279
const branch_1 = __nccwpck_require__(2234);
280-
const ALLOWED_CONFIG_KEYS = ['changed-files', 'head-branch', 'base-branch'];
280+
const ALLOWED_CONFIG_KEYS = [
281+
'changed-files',
282+
'head-branch',
283+
'base-branch',
284+
'authors'
285+
];
281286
const getLabelConfigs = (client, configurationPath) => Promise.resolve()
282287
.then(() => {
283288
if (!fs_1.default.existsSync(configurationPath)) {
@@ -1117,6 +1122,13 @@ function labeler() {
11171122
}
11181123
});
11191124
}
1125+
function getPrAuthor() {
1126+
const pullRequest = github.context.payload.pull_request;
1127+
if (!pullRequest) {
1128+
return undefined;
1129+
}
1130+
return pullRequest.user.login;
1131+
}
11201132
function checkMatchConfigs(changedFiles, matchConfigs, dot) {
11211133
for (const config of matchConfigs) {
11221134
core.debug(` checking config ${JSON.stringify(config)}`);
@@ -1170,6 +1182,12 @@ function checkAny(matchConfigs, changedFiles, dot) {
11701182
return true;
11711183
}
11721184
}
1185+
if (matchConfig.authors) {
1186+
if (checkAuthors(matchConfig.authors)) {
1187+
core.debug(` "any" patterns matched`);
1188+
return true;
1189+
}
1190+
}
11731191
}
11741192
core.debug(` "any" patterns did not match any configs`);
11751193
return false;
@@ -1205,10 +1223,29 @@ function checkAll(matchConfigs, changedFiles, dot) {
12051223
return false;
12061224
}
12071225
}
1226+
if (matchConfig.authors) {
1227+
if (!checkAuthors(matchConfig.authors)) {
1228+
core.debug(` "all" patterns did not match`);
1229+
return false;
1230+
}
1231+
}
12081232
}
12091233
core.debug(` "all" patterns matched all configs`);
12101234
return true;
12111235
}
1236+
function checkAuthors(authors) {
1237+
const prAuthor = getPrAuthor();
1238+
if (!prAuthor) {
1239+
core.info('Could not get pull request author from context, exiting');
1240+
return false;
1241+
}
1242+
if (authors.includes(prAuthor)) {
1243+
core.debug(` author ${prAuthor} is on the list`);
1244+
return true;
1245+
}
1246+
core.debug(` author ${prAuthor} is not on the list`);
1247+
return false;
1248+
}
12121249

12131250

12141251
/***/ }),

src/api/get-label-configs.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,25 @@ import {
1111

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

14+
export interface AuthorsMatchConfig {
15+
authors?: string[];
16+
}
17+
1418
export interface MatchConfig {
1519
all?: BaseMatchConfig[];
1620
any?: BaseMatchConfig[];
1721
}
1822

19-
export type BaseMatchConfig = BranchMatchConfig & ChangedFilesMatchConfig;
23+
export type BaseMatchConfig = BranchMatchConfig &
24+
ChangedFilesMatchConfig &
25+
AuthorsMatchConfig;
2026

21-
const ALLOWED_CONFIG_KEYS = ['changed-files', 'head-branch', 'base-branch'];
27+
const ALLOWED_CONFIG_KEYS = [
28+
'changed-files',
29+
'head-branch',
30+
'base-branch',
31+
'authors'
32+
];
2233

2334
export const getLabelConfigs = (
2435
client: ClientType,

src/labeler.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ export function checkAny(
191191
return true;
192192
}
193193
}
194+
195+
if (matchConfig.authors) {
196+
if (checkAuthors(matchConfig.authors)) {
197+
core.debug(` "any" patterns matched`);
198+
return true;
199+
}
200+
}
194201
}
195202

196203
core.debug(` "any" patterns did not match any configs`);
@@ -238,13 +245,19 @@ export function checkAll(
238245
return false;
239246
}
240247
}
248+
249+
if (matchConfig.authors) {
250+
if (!checkAuthors(matchConfig.authors)) {
251+
core.debug(` "all" patterns did not match`);
252+
return false;
253+
}
254+
}
241255
}
242256

243257
core.debug(` "all" patterns matched all configs`);
244258
return true;
245259
}
246260

247-
248261
function checkAuthors(authors: string[]): boolean {
249262
const prAuthor = getPrAuthor();
250263
if (!prAuthor) {

0 commit comments

Comments
 (0)