Skip to content

Commit f4eefdc

Browse files
authored
[Beta] Implement the new structure of the match object for the changed-files section (#680)
* Implement the new structure of the match object for changed files section * Replace inner loops with the find method
1 parent 4f05277 commit f4eefdc

8 files changed

+612
-115
lines changed

__tests__/changedFiles.test.ts

+152-38
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import {
22
ChangedFilesMatchConfig,
33
checkAllChangedFiles,
44
checkAnyChangedFiles,
5-
toChangedFilesMatchConfig
5+
toChangedFilesMatchConfig,
6+
checkIfAnyGlobMatchesAnyFile,
7+
checkIfAllGlobsMatchAnyFile,
8+
checkIfAnyGlobMatchesAllFiles,
9+
checkIfAllGlobsMatchAllFiles
610
} from '../src/changedFiles';
711

812
jest.mock('@actions/core');
@@ -11,20 +15,28 @@ jest.mock('@actions/github');
1115
describe('checkAllChangedFiles', () => {
1216
const changedFiles = ['foo.txt', 'bar.txt'];
1317

14-
describe('when the globs match every file that has been changed', () => {
15-
const globs = ['*.txt'];
18+
describe('when all given glob pattern configs matched', () => {
19+
const globPatternsConfigs = [
20+
{AnyGlobToAnyFile: ['foo.txt']},
21+
{AnyGlobToAllFiles: ['*.txt']},
22+
{AllGlobsToAllFiles: ['**']}
23+
];
1624

1725
it('returns true', () => {
18-
const result = checkAllChangedFiles(changedFiles, globs);
26+
const result = checkAllChangedFiles(changedFiles, globPatternsConfigs);
1927
expect(result).toBe(true);
2028
});
2129
});
2230

23-
describe(`when the globs don't match every file that has changed`, () => {
24-
const globs = ['foo.txt'];
31+
describe(`when some given glob pattern config did not match`, () => {
32+
const globPatternsConfigs = [
33+
{AnyGlobToAnyFile: ['*.md']},
34+
{AnyGlobToAllFiles: ['*.txt']},
35+
{AllGlobsToAllFiles: ['**']}
36+
];
2537

2638
it('returns false', () => {
27-
const result = checkAllChangedFiles(changedFiles, globs);
39+
const result = checkAllChangedFiles(changedFiles, globPatternsConfigs);
2840
expect(result).toBe(false);
2941
});
3042
});
@@ -33,20 +45,26 @@ describe('checkAllChangedFiles', () => {
3345
describe('checkAnyChangedFiles', () => {
3446
const changedFiles = ['foo.txt', 'bar.txt'];
3547

36-
describe('when any glob matches any of the files that have changed', () => {
37-
const globs = ['*.txt', '*.md'];
48+
describe('when any given glob pattern config matched', () => {
49+
const globPatternsConfigs = [
50+
{AnyGlobToAnyFile: ['*.md']},
51+
{AnyGlobToAllFiles: ['*.txt']}
52+
];
3853

3954
it('returns true', () => {
40-
const result = checkAnyChangedFiles(changedFiles, globs);
55+
const result = checkAnyChangedFiles(changedFiles, globPatternsConfigs);
4156
expect(result).toBe(true);
4257
});
4358
});
4459

45-
describe('when none of the globs match any files that have changed', () => {
46-
const globs = ['*.md'];
60+
describe('when none of the given glob pattern configs matched', () => {
61+
const globPatternsConfigs = [
62+
{AnyGlobToAnyFile: ['*.md']},
63+
{AnyGlobToAllFiles: ['!*.txt']}
64+
];
4765

4866
it('returns false', () => {
49-
const result = checkAnyChangedFiles(changedFiles, globs);
67+
const result = checkAnyChangedFiles(changedFiles, globPatternsConfigs);
5068
expect(result).toBe(false);
5169
});
5270
});
@@ -63,44 +81,140 @@ describe('toChangedFilesMatchConfig', () => {
6381
});
6482

6583
describe(`when there is a 'changed-files' key in the config`, () => {
66-
describe('and the value is an array of strings', () => {
67-
const config = {'changed-files': ['testing']};
84+
describe('but the glob pattern config key is not provided', () => {
85+
const config = {'changed-files': ['bar']};
86+
87+
it('throws the error', () => {
88+
expect(() => {
89+
toChangedFilesMatchConfig(config);
90+
}).toThrow(
91+
`The "changed-files" section must have a valid config structure. Please read the action documentation for more information`
92+
);
93+
});
94+
});
6895

69-
it('sets the value in the config object', () => {
70-
const result = toChangedFilesMatchConfig(config);
71-
expect(result).toEqual<ChangedFilesMatchConfig>({
72-
changedFiles: ['testing']
73-
});
96+
describe('but the glob pattern config key is not valid', () => {
97+
const config = {'changed-files': [{NotValidConfigKey: ['bar']}]};
98+
99+
it('throws the error', () => {
100+
expect(() => {
101+
toChangedFilesMatchConfig(config);
102+
}).toThrow(
103+
`Unknown config options were under "changed-files": NotValidConfigKey`
104+
);
74105
});
75106
});
76107

77-
describe('and the value is a string', () => {
78-
const config = {'changed-files': 'testing'};
108+
describe('and the glob pattern config key is provided', () => {
109+
describe('and the value is an array of strings', () => {
110+
const config = {'changed-files': [{AnyGlobToAnyFile: ['testing']}]};
79111

80-
it(`sets the string as an array in the config object`, () => {
81-
const result = toChangedFilesMatchConfig(config);
82-
expect(result).toEqual<ChangedFilesMatchConfig>({
83-
changedFiles: ['testing']
112+
it('sets the value in the config object', () => {
113+
const result = toChangedFilesMatchConfig(config);
114+
expect(result).toEqual<ChangedFilesMatchConfig>({
115+
changedFiles: [{AnyGlobToAnyFile: ['testing']}]
116+
});
84117
});
85118
});
86-
});
87119

88-
describe('but the value is an empty string', () => {
89-
const config = {'changed-files': ''};
120+
describe('and the value is a string', () => {
121+
const config = {'changed-files': [{AnyGlobToAnyFile: 'testing'}]};
90122

91-
it(`returns an empty object`, () => {
92-
const result = toChangedFilesMatchConfig(config);
93-
expect(result).toEqual<ChangedFilesMatchConfig>({});
123+
it(`sets the string as an array in the config object`, () => {
124+
const result = toChangedFilesMatchConfig(config);
125+
expect(result).toEqual<ChangedFilesMatchConfig>({
126+
changedFiles: [{AnyGlobToAnyFile: ['testing']}]
127+
});
128+
});
94129
});
95130
});
131+
});
132+
});
96133

97-
describe('but the value is an empty array', () => {
98-
const config = {'changed-files': []};
134+
describe('checkIfAnyGlobMatchesAnyFile', () => {
135+
const changedFiles = ['foo.txt', 'bar.txt'];
99136

100-
it(`returns an empty object`, () => {
101-
const result = toChangedFilesMatchConfig(config);
102-
expect(result).toEqual<ChangedFilesMatchConfig>({});
103-
});
137+
describe('when any given glob pattern matched any file', () => {
138+
const globPatterns = ['*.md', 'foo.txt'];
139+
140+
it('returns true', () => {
141+
const result = checkIfAnyGlobMatchesAnyFile(changedFiles, globPatterns);
142+
expect(result).toBe(true);
143+
});
144+
});
145+
146+
describe('when none of the given glob pattern matched any file', () => {
147+
const globPatterns = ['*.md', '!*.txt'];
148+
149+
it('returns false', () => {
150+
const result = checkIfAnyGlobMatchesAnyFile(changedFiles, globPatterns);
151+
expect(result).toBe(false);
152+
});
153+
});
154+
});
155+
156+
describe('checkIfAllGlobsMatchAnyFile', () => {
157+
const changedFiles = ['foo.txt', 'bar.txt'];
158+
159+
describe('when all given glob patterns matched any file', () => {
160+
const globPatterns = ['**/bar.txt', 'bar.txt'];
161+
162+
it('returns true', () => {
163+
const result = checkIfAllGlobsMatchAnyFile(changedFiles, globPatterns);
164+
expect(result).toBe(true);
165+
});
166+
});
167+
168+
describe('when some of the given glob patterns did not match any file', () => {
169+
const globPatterns = ['*.txt', '*.md'];
170+
171+
it('returns false', () => {
172+
const result = checkIfAllGlobsMatchAnyFile(changedFiles, globPatterns);
173+
expect(result).toBe(false);
174+
});
175+
});
176+
});
177+
178+
describe('checkIfAnyGlobMatchesAllFiles', () => {
179+
const changedFiles = ['foo.txt', 'bar.txt'];
180+
181+
describe('when any given glob pattern matched all files', () => {
182+
const globPatterns = ['*.md', '*.txt'];
183+
184+
it('returns true', () => {
185+
const result = checkIfAnyGlobMatchesAllFiles(changedFiles, globPatterns);
186+
expect(result).toBe(true);
187+
});
188+
});
189+
190+
describe('when none of the given glob patterns matched all files', () => {
191+
const globPatterns = ['*.md', 'bar.txt', 'foo.txt'];
192+
193+
it('returns false', () => {
194+
const result = checkIfAnyGlobMatchesAllFiles(changedFiles, globPatterns);
195+
expect(result).toBe(false);
196+
});
197+
});
198+
});
199+
200+
describe('checkIfAllGlobsMatchAllFiles', () => {
201+
const changedFiles = ['foo.txt', 'bar.txt'];
202+
203+
describe('when all given glob patterns matched all files', () => {
204+
const globPatterns = ['*.txt', '**'];
205+
206+
it('returns true', () => {
207+
const result = checkIfAllGlobsMatchAllFiles(changedFiles, globPatterns);
208+
expect(result).toBe(true);
209+
});
210+
});
211+
212+
describe('when some of the given glob patterns did not match all files', () => {
213+
const globPatterns = ['**', 'foo.txt'];
214+
215+
it('returns false', () => {
216+
const result = checkIfAllGlobsMatchAllFiles(changedFiles, globPatterns);
217+
expect(result).toBe(false);
104218
});
105219
});
106220
});

__tests__/fixtures/all_options.yml

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
label1:
22
- any:
3-
- changed-files: ['glob']
3+
- changed-files:
4+
- AnyGlobToAnyFile: ['glob']
45
- head-branch: ['regexp']
56
- base-branch: ['regexp']
67
- all:
7-
- changed-files: ['glob']
8+
- changed-files:
9+
- AllGlobsToAllFiles: ['glob']
810
- head-branch: ['regexp']
911
- base-branch: ['regexp']
1012

1113
label2:
12-
- changed-files: ['glob']
14+
- changed-files:
15+
- AnyGlobToAnyFile: ['glob']
1316
- head-branch: ['regexp']
1417
- base-branch: ['regexp']

__tests__/fixtures/any_and_all.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
tests:
22
- any:
33
- head-branch: ['^tests/', '^test/']
4-
- changed-files: ['tests/**/*']
4+
- changed-files:
5+
- AnyGlobToAnyFile: ['tests/**/*']
56
- all:
6-
- changed-files: ['!tests/requirements.txt']
7+
- changed-files:
8+
- AllGlobsToAllFiles: ['!tests/requirements.txt']

__tests__/fixtures/only_pdfs.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
touched-a-pdf-file:
2-
- changed-files: ['*.pdf']
2+
- changed-files:
3+
- AnyGlobToAnyFile: ['*.pdf']

__tests__/labeler.test.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ describe('getLabelConfigMapFromObject', () => {
2929
expected.set('label1', [
3030
{
3131
any: [
32-
{changedFiles: ['glob']},
32+
{changedFiles: [{AnyGlobToAnyFile: ['glob']}]},
3333
{baseBranch: undefined, headBranch: ['regexp']},
3434
{baseBranch: ['regexp'], headBranch: undefined}
3535
]
3636
},
3737
{
3838
all: [
39-
{changedFiles: ['glob']},
39+
{changedFiles: [{AllGlobsToAllFiles: ['glob']}]},
4040
{baseBranch: undefined, headBranch: ['regexp']},
4141
{baseBranch: ['regexp'], headBranch: undefined}
4242
]
@@ -45,7 +45,7 @@ describe('getLabelConfigMapFromObject', () => {
4545
expected.set('label2', [
4646
{
4747
any: [
48-
{changedFiles: ['glob']},
48+
{changedFiles: [{AnyGlobToAnyFile: ['glob']}]},
4949
{baseBranch: undefined, headBranch: ['regexp']},
5050
{baseBranch: ['regexp'], headBranch: undefined}
5151
]
@@ -61,12 +61,12 @@ describe('getLabelConfigMapFromObject', () => {
6161
describe('toMatchConfig', () => {
6262
describe('when all expected config options are present', () => {
6363
const config = {
64-
'changed-files': ['testing-files'],
64+
'changed-files': [{AnyGlobToAnyFile: ['testing-files']}],
6565
'head-branch': ['testing-head'],
6666
'base-branch': ['testing-base']
6767
};
6868
const expected: BaseMatchConfig = {
69-
changedFiles: ['testing-files'],
69+
changedFiles: [{AnyGlobToAnyFile: ['testing-files']}],
7070
headBranch: ['testing-head'],
7171
baseBranch: ['testing-base']
7272
};
@@ -89,7 +89,9 @@ describe('toMatchConfig', () => {
8989

9090
describe('checkMatchConfigs', () => {
9191
describe('when a single match config is provided', () => {
92-
const matchConfig: MatchConfig[] = [{any: [{changedFiles: ['*.txt']}]}];
92+
const matchConfig: MatchConfig[] = [
93+
{any: [{changedFiles: [{AnyGlobToAnyFile: ['*.txt']}]}]}
94+
];
9395

9496
it('returns true when our pattern does match changed files', () => {
9597
const changedFiles = ['foo.txt', 'bar.txt'];
@@ -107,7 +109,12 @@ describe('checkMatchConfigs', () => {
107109

108110
it('returns true when either the branch or changed files patter matches', () => {
109111
const matchConfig: MatchConfig[] = [
110-
{any: [{changedFiles: ['*.txt']}, {headBranch: ['some-branch']}]}
112+
{
113+
any: [
114+
{changedFiles: [{AnyGlobToAnyFile: ['*.txt']}]},
115+
{headBranch: ['some-branch']}
116+
]
117+
}
111118
];
112119
const changedFiles = ['foo.txt', 'bar.txt'];
113120

@@ -118,7 +125,7 @@ describe('checkMatchConfigs', () => {
118125

119126
describe('when multiple MatchConfigs are supplied', () => {
120127
const matchConfig: MatchConfig[] = [
121-
{any: [{changedFiles: ['*.txt']}]},
128+
{any: [{changedFiles: [{AnyGlobToAnyFile: ['*.txt']}]}]},
122129
{any: [{headBranch: ['some-branch']}]}
123130
];
124131
const changedFiles = ['foo.txt', 'bar.md'];
@@ -130,7 +137,7 @@ describe('checkMatchConfigs', () => {
130137

131138
it('returns true when only both config matches', () => {
132139
const matchConfig: MatchConfig[] = [
133-
{any: [{changedFiles: ['*.txt']}]},
140+
{any: [{changedFiles: [{AnyGlobToAnyFile: ['*.txt']}]}]},
134141
{any: [{headBranch: ['head-branch']}]}
135142
];
136143
const result = checkMatchConfigs(changedFiles, matchConfig);

0 commit comments

Comments
 (0)