Make sure that empty config options don't accidently label things

This commit is contained in:
Josh Dales
2023-03-20 17:39:57 -04:00
parent 92990c0c57
commit d31255f7be
4 changed files with 26 additions and 17 deletions

View File

@@ -0,0 +1,2 @@
label:
- unknown-check: 'this-check-is-not-supported'

View File

@@ -17,17 +17,18 @@ describe('toMatchConfig', () => {
'head-branch': ['testing-head'],
'base-branch': ['testing-base']
};
const expected: MatchConfig = {
changedFiles: {
all: ['testing-all'],
any: ['testing-any']
},
headBranch: ['testing-head'],
baseBranch: ['testing-base']
};
it('returns a MatchConfig object with all options', () => {
const result = toMatchConfig(config);
expect(result).toEqual<MatchConfig>({
changedFiles: {
all: ['testing-all'],
any: ['testing-any']
},
headBranch: ['testing-head'],
baseBranch: ['testing-base']
});
expect(result).toEqual(expected);
});
describe('and there are also unexpected options present', () => {
@@ -35,14 +36,7 @@ describe('toMatchConfig', () => {
it('does not include the unexpected items in the returned MatchConfig object', () => {
const result = toMatchConfig(config);
expect(result).toEqual<MatchConfig>({
changedFiles: {
all: ['testing-all'],
any: ['testing-any']
},
headBranch: ['testing-head'],
baseBranch: ['testing-base']
});
expect(result).toEqual(expected);
});
});
});

View File

@@ -16,7 +16,8 @@ const getPullMock = jest.spyOn(gh.rest.pulls, 'get');
const yamlFixtures = {
'branches.yml': fs.readFileSync('__tests__/fixtures/branches.yml'),
'only_pdfs.yml': fs.readFileSync('__tests__/fixtures/only_pdfs.yml')
'only_pdfs.yml': fs.readFileSync('__tests__/fixtures/only_pdfs.yml'),
'not_supported.yml': fs.readFileSync('__tests__/fixtures/not_supported.yml')
};
afterAll(() => jest.restoreAllMocks());
@@ -48,6 +49,14 @@ describe('run', () => {
expect(addLabelsMock).toHaveBeenCalledTimes(0);
});
it('does not add a label when no match config options match', async () => {
usingLabelerConfigYaml('not_supported.yml');
await run();
expect(addLabelsMock).toHaveBeenCalledTimes(0);
expect(removeLabelMock).toHaveBeenCalledTimes(0);
});
it('(with sync-labels: true) it deletes preexisting PR labels that no longer match the glob pattern', async () => {
const mockInput = {
'repo-token': 'foo',

View File

@@ -151,6 +151,10 @@ export function checkMatchConfigs(
}
function checkMatch(changedFiles: string[], matchConfig: MatchConfig): boolean {
if (!Object.keys(matchConfig).length) {
return false;
}
if (matchConfig.changedFiles?.all) {
if (!checkAll(changedFiles, matchConfig.changedFiles.all)) {
return false;