Skip to content

Commit bf71b47

Browse files
authored
Use team keys for authors (#88)
* Use keys for config * Update context stub and add github test * Update with docs response for team members * Updates per PR comments * Run build * Fix lint errors * Try to build again * Response in data
1 parent ba38293 commit bf71b47

File tree

6 files changed

+97
-4
lines changed

6 files changed

+97
-4
lines changed

dist/index.js

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

src/github.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ async function assign_reviewers(reviewers) {
112112
});
113113
}
114114

115+
// https://docs.github.com/en/rest/teams/members?apiVersion=2022-11-28#list-team-members
116+
async function get_team_members(team) {
117+
const context = get_context();
118+
const octokit = get_octokit();
119+
120+
return octokit.teams.listMembersInOrg({
121+
org: context.repo.org,
122+
team_slug: team,
123+
})?.data?.map((member) => member.login);
124+
}
125+
115126
/* Private */
116127

117128
let context_cache;
@@ -158,4 +169,5 @@ module.exports = {
158169
fetch_changed_files,
159170
assign_reviewers,
160171
clear_cache,
172+
get_team_members,
161173
};

src/reviewer.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const core = require('@actions/core');
44
const minimatch = require('minimatch');
55
const sample_size = require('lodash/sampleSize');
6+
const github = require('./github'); // Don't destructure this object to stub with sinon in tests
67

78
function fetch_other_group_members({ author, config }) {
89
const DEFAULT_OPTIONS = {
@@ -77,6 +78,14 @@ function identify_reviewers_by_author({ config, 'author': specified_author }) {
7778
return true;
7879
}
7980

81+
if (author.startsWith('team:')) {
82+
const team = author.replace('team:', '');
83+
const individuals_in_team = github.get_team_members(team);
84+
if (individuals_in_team.includes(specified_author)) {
85+
return true;
86+
}
87+
}
88+
8089
const individuals_in_author_setting = replace_groups_with_individuals({ reviewers: [ author ], config });
8190

8291
if (individuals_in_author_setting.includes(specified_author)) {

test/github.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const {
1414
fetch_changed_files,
1515
assign_reviewers,
1616
clear_cache,
17+
get_team_members,
1718
} = require('../src/github');
1819

1920
describe('github', function() {
@@ -155,4 +156,29 @@ describe('github', function() {
155156
});
156157
});
157158
});
159+
160+
describe('get_team_members()', function() {
161+
const spy = sinon.spy();
162+
const octokit = {
163+
teams: {
164+
listMembersInOrg: spy,
165+
},
166+
};
167+
168+
beforeEach(function() {
169+
github.getOctokit.resetBehavior();
170+
github.getOctokit.returns(octokit);
171+
});
172+
173+
it('gets team members', async function() {
174+
const team = 'koopa-troop';
175+
await get_team_members(team);
176+
177+
expect(spy.calledOnce).to.be.true;
178+
expect(spy.lastCall.args[0]).to.deep.equal({
179+
org: 'necojackarc',
180+
team_slug: 'koopa-troop',
181+
});
182+
});
183+
});
158184
});

test/reviewer.test.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const {
1010
} = require('../src/reviewer');
1111
const { expect } = require('chai');
1212

13+
const github = require('../src/github');
14+
const sinon = require('sinon');
15+
1316
describe('reviewer', function() {
1417
describe('fetch_other_group_members()', function() {
1518

@@ -69,7 +72,6 @@ describe('reviewer', function() {
6972
});
7073

7174
describe('identify_reviewers_by_changed_files()', function() {
72-
7375
const config = {
7476
reviewers: {
7577
groups: {
@@ -136,13 +138,30 @@ describe('reviewer', function() {
136138
designers: [ 'mario', 'princess-peach', 'princess-daisy' ],
137139
},
138140
per_author: {
139-
engineers: [ 'engineers', 'dr-mario' ],
140-
designers: [ 'designers' ],
141-
yoshi: [ 'mario', 'luige' ],
141+
'engineers': [ 'engineers', 'dr-mario' ],
142+
'designers': [ 'designers' ],
143+
'yoshi': [ 'mario', 'luige' ],
144+
'team:koopa-troop': [ 'mario' ],
142145
},
143146
},
144147
};
145148

149+
const stub = sinon.stub(github, 'get_team_members');
150+
stub.withArgs('koopa-troop').returns([
151+
{
152+
login: 'bowser',
153+
id: 1,
154+
},
155+
{
156+
login: 'king-boo',
157+
id: 2,
158+
},
159+
{
160+
login: 'goomboss',
161+
id: 3,
162+
},
163+
]);
164+
146165
it('returns nothing when config does not have a "per-author" key', function() {
147166
const author = 'THIS DOES NOT MATTER';
148167
expect(identify_reviewers_by_author({ config: { reviewers: {} }, author })).to.deep.equal([]);
@@ -167,6 +186,11 @@ describe('reviewer', function() {
167186
const author = 'mario';
168187
expect(identify_reviewers_by_author({ config, author })).to.have.members([ 'dr-mario', 'luigi', 'wario', 'waluigi', 'princess-peach', 'princess-daisy' ]);
169188
});
189+
190+
it('works when gh team slug used for auther', function() {
191+
const author = 'bowser';
192+
expect(identify_reviewers_by_author({ config, author })).to.have.members([ 'mario' ]);
193+
});
170194
});
171195

172196
describe('should_request_review()', function() {

test/stubs/context.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class ContextStub {
2424
return {
2525
owner: 'necojackarc',
2626
repo: 'auto-request-review',
27+
org: 'necojackarc',
2728
};
2829
}
2930
}

0 commit comments

Comments
 (0)