Skip to content

Commit 4f89302

Browse files
k0kubunnecojackarc
andauthored
Add options.last_files_match_only to simulate CODEOWNERS (#80)
* Add options.last_match_only to simulate CODEOWNERS * Add a test case for the last_match_only option * Update dist * Rename last_match_only to last_files_match_only Co-authored-by: necojackarc <[email protected]>
1 parent 5e89775 commit 4f89302

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ options:
175175
# Randomly pick reviewers up to this number.
176176
# Do not set this option if you'd like to assign all matching reviewers.
177177
number_of_reviewers: 3
178+
179+
# If it's true, the last matching files-change pattern takes the most precedence (CODEOWNERS-compatible)
180+
# See https://github.com/necojackarc/auto-request-review/pull/80 for more details.
181+
last_files_match_only: false
178182
```
179183

180184
The default configuration file location is `.github/auto_request_review.yml` but you can override it in your workflow configuration file.

dist/index.js

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/reviewer.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ function fetch_other_group_members({ author, config }) {
3434
}
3535

3636
function identify_reviewers_by_changed_files({ config, changed_files, excludes = [] }) {
37+
const DEFAULT_OPTIONS = {
38+
last_files_match_only: false,
39+
};
40+
41+
const { last_files_match_only } = {
42+
...DEFAULT_OPTIONS,
43+
...config.options,
44+
};
45+
3746
if (!config.files) {
3847
core.info('A "files" key does not exist in config; returning no reviewers for changed files.');
3948
return [];
@@ -43,6 +52,9 @@ function identify_reviewers_by_changed_files({ config, changed_files, excludes =
4352

4453
Object.entries(config.files).forEach(([ glob_pattern, reviewers ]) => {
4554
if (changed_files.some((changed_file) => minimatch(changed_file, glob_pattern))) {
55+
if (last_files_match_only) {
56+
matching_reviewers.length = 0; // clear previous matches
57+
}
4658
matching_reviewers.push(...reviewers);
4759
}
4860
});

test/reviewer.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ describe('reviewer', function() {
8080
files: {
8181
'**/super-star': [ 'mario', 'luigi' ],
8282
'backend/**/*': [ 'backend-engineers' ],
83+
'backend/**/some-specific-file': [ 'mario', 'someone-specific' ],
8384
'frontend/**/*': [ 'frontend-engineers', 'toad' ],
8485
},
8586
};
@@ -114,6 +115,17 @@ describe('reviewer', function() {
114115
const excludes = [ 'wario', 'waluigi' ];
115116
expect(identify_reviewers_by_changed_files({ config, changed_files, excludes })).to.have.members([ 'mario', 'luigi', 'princess-peach', 'toad' ]);
116117
});
118+
119+
it('uses the only last matching files-changed pattern with `last_files_match_only` `true` (CODEWONERS-compatible)', function() {
120+
const changed_files = [ 'backend/some-specific-file' ];
121+
const config_with_last_files_match_only = {
122+
...config,
123+
options: {
124+
last_files_match_only: true,
125+
},
126+
};
127+
expect(identify_reviewers_by_changed_files({ config: config_with_last_files_match_only, changed_files })).to.have.members([ 'mario', 'someone-specific' ]);
128+
});
117129
});
118130

119131
describe('identify_reviewers_by_author()', function() {

0 commit comments

Comments
 (0)