Skip to content

Commit 57d3407

Browse files
committed
Better check for empty configs in checkAll
1 parent a256a58 commit 57d3407

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

__tests__/fixtures/any_and_all.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
tests:
2+
- any:
3+
- head-branch: ['^tests/', '^test/']
4+
- changed-files: ['tests/**/*']
5+
- all:
6+
- changed-files: ['!tests/requirements.txt']

__tests__/main.test.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const getPullMock = jest.spyOn(gh.rest.pulls, 'get');
1717
const yamlFixtures = {
1818
'branches.yml': fs.readFileSync('__tests__/fixtures/branches.yml'),
1919
'only_pdfs.yml': fs.readFileSync('__tests__/fixtures/only_pdfs.yml'),
20-
'not_supported.yml': fs.readFileSync('__tests__/fixtures/not_supported.yml')
20+
'not_supported.yml': fs.readFileSync('__tests__/fixtures/not_supported.yml'),
21+
'any_and_all.yml': fs.readFileSync('__tests__/fixtures/any_and_all.yml')
2122
};
2223

2324
afterAll(() => jest.restoreAllMocks());
@@ -167,19 +168,28 @@ describe('run', () => {
167168
});
168169
});
169170

170-
it('can support multiple branches by providing an array', async () => {
171-
github.context.payload.pull_request!.head = {ref: 'array/123'};
172-
usingLabelerConfigYaml('branches.yml');
171+
it('adds a label when matching any and all patterns are provided', async () => {
172+
usingLabelerConfigYaml('any_and_all.yml');
173+
mockGitHubResponseChangedFiles('tests/test.ts');
173174
await run();
174175

175176
expect(addLabelsMock).toHaveBeenCalledTimes(1);
176177
expect(addLabelsMock).toHaveBeenCalledWith({
177178
owner: 'monalisa',
178179
repo: 'helloworld',
179180
issue_number: 123,
180-
labels: ['array-branch']
181+
labels: ['tests']
181182
});
182183
});
184+
185+
it('does not add a label when not all any and all patterns are matched', async () => {
186+
usingLabelerConfigYaml('any_and_all.yml');
187+
mockGitHubResponseChangedFiles('tests/requirements.txt');
188+
await run();
189+
190+
expect(addLabelsMock).toHaveBeenCalledTimes(0);
191+
expect(removeLabelMock).toHaveBeenCalledTimes(0);
192+
});
183193
});
184194

185195
function usingLabelerConfigYaml(fixtureName: keyof typeof yamlFixtures): void {

dist/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,8 @@ function checkMatch(changedFiles, matchConfig) {
439439
// equivalent to "Array.some()" but expanded for debugging and clarity
440440
function checkAny(matchConfigs, changedFiles) {
441441
core.debug(` checking "any" patterns`);
442-
if (!matchConfigs.length) {
442+
if (!matchConfigs.length ||
443+
!matchConfigs.some(configOption => Object.keys(configOption).length)) {
443444
core.debug(` no "any" patterns to check`);
444445
return false;
445446
}
@@ -468,8 +469,7 @@ exports.checkAny = checkAny;
468469
function checkAll(matchConfigs, changedFiles) {
469470
core.debug(` checking "all" patterns`);
470471
if (!matchConfigs.length ||
471-
// Make sure that all the configs have keys that we can check for
472-
!matchConfigs.some(configOption => ALLOWED_CONFIG_KEYS.includes(Object.keys(configOption)[0]))) {
472+
!matchConfigs.some(configOption => Object.keys(configOption).length)) {
473473
core.debug(` no "all" patterns to check`);
474474
return false;
475475
}

src/labeler.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,10 @@ export function checkAny(
225225
changedFiles: string[]
226226
): boolean {
227227
core.debug(` checking "any" patterns`);
228-
if (!matchConfigs.length) {
228+
if (
229+
!matchConfigs.length ||
230+
!matchConfigs.some(configOption => Object.keys(configOption).length)
231+
) {
229232
core.debug(` no "any" patterns to check`);
230233
return false;
231234
}
@@ -262,10 +265,7 @@ export function checkAll(
262265
core.debug(` checking "all" patterns`);
263266
if (
264267
!matchConfigs.length ||
265-
// Make sure that all the configs have keys that we can check for
266-
!matchConfigs.some(configOption =>
267-
ALLOWED_CONFIG_KEYS.includes(Object.keys(configOption)[0])
268-
)
268+
!matchConfigs.some(configOption => Object.keys(configOption).length)
269269
) {
270270
core.debug(` no "all" patterns to check`);
271271
return false;

0 commit comments

Comments
 (0)