Skip to content

Commit

Permalink
Use team keys for authors (#88)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hbrysiewicz committed May 23, 2023
1 parent ba38293 commit bf71b47
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 4 deletions.
21 changes: 21 additions & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ async function assign_reviewers(reviewers) {
});
}

// https://docs.github.com/en/rest/teams/members?apiVersion=2022-11-28#list-team-members
async function get_team_members(team) {
const context = get_context();
const octokit = get_octokit();

return octokit.teams.listMembersInOrg({
org: context.repo.org,
team_slug: team,
})?.data?.map((member) => member.login);
}

/* Private */

let context_cache;
Expand Down Expand Up @@ -158,4 +169,5 @@ module.exports = {
fetch_changed_files,
assign_reviewers,
clear_cache,
get_team_members,
};
9 changes: 9 additions & 0 deletions src/reviewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const core = require('@actions/core');
const minimatch = require('minimatch');
const sample_size = require('lodash/sampleSize');
const github = require('./github'); // Don't destructure this object to stub with sinon in tests

function fetch_other_group_members({ author, config }) {
const DEFAULT_OPTIONS = {
Expand Down Expand Up @@ -77,6 +78,14 @@ function identify_reviewers_by_author({ config, 'author': specified_author }) {
return true;
}

if (author.startsWith('team:')) {
const team = author.replace('team:', '');
const individuals_in_team = github.get_team_members(team);
if (individuals_in_team.includes(specified_author)) {
return true;
}
}

const individuals_in_author_setting = replace_groups_with_individuals({ reviewers: [ author ], config });

if (individuals_in_author_setting.includes(specified_author)) {
Expand Down
26 changes: 26 additions & 0 deletions test/github.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
fetch_changed_files,
assign_reviewers,
clear_cache,
get_team_members,
} = require('../src/github');

describe('github', function() {
Expand Down Expand Up @@ -155,4 +156,29 @@ describe('github', function() {
});
});
});

describe('get_team_members()', function() {
const spy = sinon.spy();
const octokit = {
teams: {
listMembersInOrg: spy,
},
};

beforeEach(function() {
github.getOctokit.resetBehavior();
github.getOctokit.returns(octokit);
});

it('gets team members', async function() {
const team = 'koopa-troop';
await get_team_members(team);

expect(spy.calledOnce).to.be.true;
expect(spy.lastCall.args[0]).to.deep.equal({
org: 'necojackarc',
team_slug: 'koopa-troop',
});
});
});
});
32 changes: 28 additions & 4 deletions test/reviewer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const {
} = require('../src/reviewer');
const { expect } = require('chai');

const github = require('../src/github');
const sinon = require('sinon');

describe('reviewer', function() {
describe('fetch_other_group_members()', function() {

Expand Down Expand Up @@ -69,7 +72,6 @@ describe('reviewer', function() {
});

describe('identify_reviewers_by_changed_files()', function() {

const config = {
reviewers: {
groups: {
Expand Down Expand Up @@ -136,13 +138,30 @@ describe('reviewer', function() {
designers: [ 'mario', 'princess-peach', 'princess-daisy' ],
},
per_author: {
engineers: [ 'engineers', 'dr-mario' ],
designers: [ 'designers' ],
yoshi: [ 'mario', 'luige' ],
'engineers': [ 'engineers', 'dr-mario' ],
'designers': [ 'designers' ],
'yoshi': [ 'mario', 'luige' ],
'team:koopa-troop': [ 'mario' ],
},
},
};

const stub = sinon.stub(github, 'get_team_members');
stub.withArgs('koopa-troop').returns([
{
login: 'bowser',
id: 1,
},
{
login: 'king-boo',
id: 2,
},
{
login: 'goomboss',
id: 3,
},
]);

it('returns nothing when config does not have a "per-author" key', function() {
const author = 'THIS DOES NOT MATTER';
expect(identify_reviewers_by_author({ config: { reviewers: {} }, author })).to.deep.equal([]);
Expand All @@ -167,6 +186,11 @@ describe('reviewer', function() {
const author = 'mario';
expect(identify_reviewers_by_author({ config, author })).to.have.members([ 'dr-mario', 'luigi', 'wario', 'waluigi', 'princess-peach', 'princess-daisy' ]);
});

it('works when gh team slug used for auther', function() {
const author = 'bowser';
expect(identify_reviewers_by_author({ config, author })).to.have.members([ 'mario' ]);
});
});

describe('should_request_review()', function() {
Expand Down
1 change: 1 addition & 0 deletions test/stubs/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ContextStub {
return {
owner: 'necojackarc',
repo: 'auto-request-review',
org: 'necojackarc',
};
}
}
Expand Down

0 comments on commit bf71b47

Please sign in to comment.