Skip to content

Commit 6e0b9de

Browse files
committed
Add ability to add collaborator
1 parent 22b889c commit 6e0b9de

File tree

6 files changed

+36
-9
lines changed

6 files changed

+36
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ node_modules/
99
.DS_Store
1010
npm-debug.log
1111
sauce.json
12+
package-lock.json

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ lib/
55
.nyc_output/
66
.DS_Store
77
sauce.json
8+
package-lock.json

lib/Repository.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,13 +436,27 @@ class Repository extends Requestable {
436436
* List the users who are collaborators on the repository. The currently authenticated user must have
437437
* push access to use this method
438438
* @see https://developer.github.com/v3/repos/collaborators/#list-collaborators
439-
* @param {Requestable.callback} cb - will receive the list of collaborators
439+
* @param {Requestable.callback} cb - will receive the fetched data
440440
* @return {Promise} - the promise for the http request
441441
*/
442442
getCollaborators(cb) {
443443
return this._request('GET', `/repos/${this.__fullname}/collaborators`, null, cb);
444444
}
445445

446+
/**
447+
* Adds user as a collaborator on the repository. The currently authenticated user must have admin access for the
448+
* repo to use this method
449+
* @see https://developer.github.com/enterprise/2.10/v3/repos/collaborators/#add-user-as-a-collaborator
450+
* @param {string} username - the user to add as a collaborator
451+
* @param {Object} [options] - collaborator permissions, only applicable on repos in an org
452+
* @param {Object} [options.permission=push] - can be one of: `pull`, `push`, or `admin`
453+
* @param {Requestable.callback} cb - will receive the information about the newly added contributor
454+
* @return {Promise} - the promise for the http request
455+
*/
456+
addCollaborator(username, options, cb) {
457+
return this._request('PUT', `/repos/${this.__fullname}/collaborators/${username}`, options, cb);
458+
}
459+
446460
/**
447461
* Check if a user is a collaborator on the repository
448462
* @see https://developer.github.com/v3/repos/collaborators/#check-if-a-user-is-a-collaborator
@@ -451,7 +465,7 @@ class Repository extends Requestable {
451465
* @return {Promise} - the promise for the http request {Boolean} [description]
452466
*/
453467
isCollaborator(username, cb) {
454-
return this._request('GET', `/repos/${this.__fullname}/collaborators/${username}`, null, cb);
468+
return this._request204or404(`/repos/${this.__fullname}/collaborators/${username}`, null, cb);
455469
}
456470

457471
/**

test/fixtures/alt-user.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"USERNAME": "mtscout6-test"
3+
}

test/repository.spec.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import expect from 'must';
33
import Github from '../lib/GitHub';
44
import wait from './helpers/wait';
55
import testUser from './fixtures/user.json';
6+
import altUser from './fixtures/alt-user.json';
67
import loadImage from './fixtures/imageBlob';
78
import {assertSuccessful, assertFailure} from './helpers/callbacks';
89
import getTestRepoName from './helpers/getTestRepoName';
@@ -262,7 +263,7 @@ describe('Repository', function() {
262263
});
263264
});
264265

265-
describe('creating/modifiying', function() {
266+
describe.only('creating/modifiying', function() {
266267
const fileName = 'test.md';
267268

268269
const initialText = 'This is a test.';
@@ -343,8 +344,18 @@ describe('Repository', function() {
343344
}));
344345
});
345346

346-
it('should test whether user is collaborator', function(done) {
347-
remoteRepo.isCollaborator(testUser.USERNAME, assertSuccessful(done));
347+
it('should add repo collaborator', async function() {
348+
expect(await remoteRepo.isCollaborator(altUser.USERNAME)).to.be.false;
349+
await remoteRepo.addCollaborator(altUser.USERNAME);
350+
expect(await remoteRepo.isCollaborator(altUser.USERNAME)).to.be.true;
351+
});
352+
353+
it('should test whether user is collaborator', async function() {
354+
expect(await remoteRepo.isCollaborator(testUser.USERNAME)).to.be.true;
355+
});
356+
357+
it('should test whether user is not collaborator', async function() {
358+
expect(await remoteRepo.isCollaborator(altUser.USERNAME)).to.be.false;
348359
});
349360

350361
it('should write to repo', function(done) {

test/team.spec.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import Github from '../lib/GitHub';
44
import testUser from './fixtures/user.json';
55
import {assertFailure} from './helpers/callbacks';
66
import getTestRepoName from './helpers/getTestRepoName';
7-
8-
const altUser = {
9-
USERNAME: 'mtscout6-test',
10-
};
7+
import altUser from './fixtures/alt-user.json';
118

129
function createTestTeam() {
1310
const name = getTestRepoName();

0 commit comments

Comments
 (0)