Skip to content

Commit

Permalink
add tests for filter_only_collaborators
Browse files Browse the repository at this point in the history
  • Loading branch information
jamoor-moj committed Apr 15, 2024
1 parent e558547 commit 4771f80
Show file tree
Hide file tree
Showing 4 changed files with 274 additions and 5 deletions.
4 changes: 2 additions & 2 deletions dist/index.js

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

4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ async function run() {
reviewers.push(...default_reviewers);
}

// core.info(`Possible Reviewers ${reviewers.join(', ')}, prepare filtering out already requested reviewers or approved reviewers`);
// reviewers = reviewers.filter((reviewer) => !requested_approved_reviewers.includes(reviewer));
core.info(`Possible Reviewers ${reviewers.join(', ')}, prepare filtering out already requested reviewers or approved reviewers`);
reviewers = reviewers.filter((reviewer) => !requested_approved_reviewers.includes(reviewer));

core.info(`Possible New Reviewers ${reviewers.join(', ')}, prepare to filter to only collaborators`);
reviewers = await github.filter_only_collaborators(reviewers);
Expand Down
125 changes: 125 additions & 0 deletions test/github.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,131 @@ describe('github', function() {
});
});

describe('filter_only_collaborators()', function() {
const teamStub = sinon.stub();
const aliasStub = sinon.stub();
const octokit = {
repos: {
checkCollaborator: aliasStub,
},
teams: {
checkPermissionsForRepoInOrg: teamStub,
},
};

let restoreModule;
beforeEach(function() {
restoreModule = rewired_github.__set__('octokit_cache', octokit);
});
afterEach(function() {
teamStub.reset();
aliasStub.reset();
restoreModule();
});

it('remove non collaborators - individual', async function() {
const allCandidates = [ 'bowser', 'peach', 'luigi', 'mario' ];

aliasStub.withArgs({
owner: 'necojackarc',
repo: 'auto-request-review',
username: 'bowser',
}).rejects();
aliasStub.withArgs({
owner: 'necojackarc',
repo: 'auto-request-review',
username: 'peach',
}).resolves({ status: '204' });
aliasStub.withArgs({
owner: 'necojackarc',
repo: 'auto-request-review',
username: 'luigi',
}).rejects();
aliasStub.withArgs({
owner: 'necojackarc',
repo: 'auto-request-review',
username: 'mario',
}).resolves({ status: '204' });

const actual = await rewired_github.filter_only_collaborators(allCandidates);
expect(actual).to.deep.equal([ 'peach', 'mario' ]);
expect(teamStub.called).to.be.false;
expect(aliasStub.callCount).to.be.equal(4);
});

it('remove non collaborators - teams', async function() {
const allCandidates = [ 'team:koopa-troop', 'team:toads', 'team:peach-alliance', 'team:bowser-and-co' ];

teamStub.withArgs({
org: 'necojackarc',
team_slug: 'koopa-troop',
owner: 'necojackarc',
repo: 'auto-request-review',
}).resolves({ status: '204' });
teamStub.withArgs({
org: 'necojackarc',
team_slug: 'toads',
owner: 'necojackarc',
repo: 'auto-request-review',
}).rejects();
teamStub.withArgs({
org: 'necojackarc',
team_slug: 'peach-alliance',
owner: 'necojackarc',
repo: 'auto-request-review',
}).rejects();
teamStub.withArgs({
org: 'necojackarc',
team_slug: 'bowser-and-co',
owner: 'necojackarc',
repo: 'auto-request-review',
}).resolves({ status: '204' });

const actual = await rewired_github.filter_only_collaborators(allCandidates);
expect(actual).to.deep.equal([ 'team:koopa-troop', 'team:bowser-and-co' ]);
expect(teamStub.callCount).to.be.equal(4);
expect(aliasStub.called).to.be.false;
});

it('remove non collaborators - mixed', async function() {
const allCandidates = [ 'peach', 'team:peach-alliance', 'luigi', 'mario', 'team:bowser-and-co' ];

aliasStub.withArgs({
owner: 'necojackarc',
repo: 'auto-request-review',
username: 'peach',
}).resolves({ status: '204' });
aliasStub.withArgs({
owner: 'necojackarc',
repo: 'auto-request-review',
username: 'luigi',
}).rejects();
aliasStub.withArgs({
owner: 'necojackarc',
repo: 'auto-request-review',
username: 'mario',
}).rejects();

teamStub.withArgs({
org: 'necojackarc',
team_slug: 'peach-alliance',
owner: 'necojackarc',
repo: 'auto-request-review',
}).resolves({ status: '204' });
teamStub.withArgs({
org: 'necojackarc',
team_slug: 'bowser-and-co',
owner: 'necojackarc',
repo: 'auto-request-review',
}).rejects();

const actual = await rewired_github.filter_only_collaborators(allCandidates);
expect(actual).to.deep.equal([ 'peach', 'team:peach-alliance' ]);
expect(teamStub.callCount).to.be.equal(2);
expect(aliasStub.callCount).to.be.equal(3);
});
});

describe('assign_reviewers()', function() {
const spy = sinon.spy();
const octokit = {
Expand Down
Loading

0 comments on commit 4771f80

Please sign in to comment.