Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit d8f97d9

Browse files
committed
fix for #31 and #32
1 parent 889d984 commit d8f97d9

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

src/routes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ module.exports = {
9595
get: {
9696
controller: 'TCUserController',
9797
method: 'login',
98+
allowNormalUser: true,
9899
},
99100
},
100101
'/admin/tcuser': {

src/services/GithubService.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111
const GitHub = require('github-api');
1212
const Joi = require('joi');
13+
const _ = require('lodash');
1314
const config = require('../config');
1415
const constants = require('../common/constants');
1516
const helper = require('../common/helper');
@@ -173,22 +174,27 @@ getTeamRegistrationUrl.schema = Joi.object().keys({
173174
* @returns {Promise} the promise result
174175
*/
175176
async function addTeamMember(teamId, ownerUserToken, normalUserToken) {
177+
let username;
178+
let id;
176179
try {
177180
// get normal user name
178181
const githubNormalUser = new GitHub({ token: normalUserToken });
179182
const normalUser = await githubNormalUser.getUser().getProfile();
180-
const username = normalUser.data.login;
181-
const id = normalUser.data.id;
183+
username = normalUser.data.login;
184+
id = normalUser.data.id;
182185

183186
// add normal user to team
184187
const github = new GitHub({ token: ownerUserToken });
185188
const team = github.getTeam(teamId);
186189
await team.addMembership(username);
187-
// return github username
188-
return { username, id };
189190
} catch (err) {
190-
throw helper.convertGitHubError(err, 'Failed to add team member');
191+
// if error is already exists discard
192+
if (_.chain(err).get('body.errors').countBy({ code: 'already_exists' }).get('true').isUndefined().value()) {
193+
throw helper.convertGitHubError(err, 'Failed to add team member');
194+
}
191195
}
196+
// return github username
197+
return {username, id};
192198
}
193199

194200
addTeamMember.schema = Joi.object().keys({

src/services/GitlabService.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
const Joi = require('joi');
1212
const superagent = require('superagent');
1313
const superagentPromise = require('superagent-promise');
14+
const _ = require('lodash');
1415
const config = require('../config');
1516
const constants = require('../common/constants');
1617
const helper = require('../common/helper');
@@ -167,13 +168,16 @@ getGroupRegistrationUrl.schema = Joi.object().keys({
167168
* @returns {Promise} the promise result
168169
*/
169170
async function addGroupMember(groupId, ownerUserToken, normalUserToken) {
171+
let username;
172+
let userId;
170173
try {
171174
// get normal user id
172175
const res = await request
173176
.get(`${config.GITLAB_API_BASE_URL}/api/v4/user`)
174177
.set('Authorization', `Bearer ${normalUserToken}`)
175178
.end();
176-
const userId = res.body.id;
179+
userId = res.body.id;
180+
username = res.body.username;
177181
if (!userId) {
178182
throw new errors.UnauthorizedError('Can not get user id from the normal user access token.');
179183
}
@@ -187,10 +191,13 @@ async function addGroupMember(groupId, ownerUserToken, normalUserToken) {
187191
// return gitlab username
188192
return { username: res.body.username, id: res.body.id };
189193
} catch (err) {
190-
if (err instanceof errors.ApiError) {
191-
throw err;
194+
if (_.get(JSON.parse(err.response.text), 'message') !== 'Member already exists') {
195+
if (err instanceof errors.ApiError) {
196+
throw err;
197+
}
198+
throw helper.convertGitLabError(err, 'Failed to add group member');
192199
}
193-
throw helper.convertGitLabError(err, 'Failed to add group member');
200+
return {username, id: userId};
194201
}
195202
}
196203

0 commit comments

Comments
 (0)