@@ -20,7 +20,7 @@ const GithubService = require('../services/GithubService');
20
20
const UserService = require ( '../services/UserService' ) ;
21
21
const OwnerUserTeam = require ( '../models' ) . OwnerUserTeam ;
22
22
const UserTeamMapping = require ( '../models' ) . UserTeamMapping ;
23
- const UserMapping = require ( '../models' ) . UserMapping ;
23
+ const GithubUserMapping = require ( '../models' ) . GithubUserMapping ;
24
24
const constants = require ( '../common/constants' ) ;
25
25
26
26
const request = superagentPromise ( superagent , Promise ) ;
@@ -127,6 +127,8 @@ async function addUserToTeam(req, res) {
127
127
config . GITHUB_CLIENT_ID
128
128
} &redirect_uri=${
129
129
encodeURIComponent ( callbackUri )
130
+ } &scope=${
131
+ encodeURIComponent ( 'admin:org' )
130
132
} &state=${ identifier } `) ;
131
133
}
132
134
@@ -156,22 +158,33 @@ async function addUserToTeamCallback(req, res) {
156
158
throw new errors . UnauthorizedError ( 'Github authorization failed.' , result . body . error_description ) ;
157
159
}
158
160
const token = result . body . access_token ;
161
+
162
+ // get team details
163
+ const teamDetails = await GithubService . getTeamDetails ( team . ownerToken , team . teamId ) ;
164
+ const organisation = teamDetails . organization . login ;
165
+
166
+ // Add member to organisation
167
+ const addOrganisationResult = await GithubService . addOrganisationMember ( organisation , token ) ;
168
+ console . log ( `Add organisation member, state = ${ addOrganisationResult . state } ` ) ; /* eslint-disable-line no-console */
169
+ if ( addOrganisationResult . state === 'pending' ) {
170
+ const acceptInvitation = await GithubService . acceptOrganisationInvitation ( organisation , token ) ;
171
+ console . log ( `Accept organisation invitation by member, state = ${ acceptInvitation . state } ` ) ; /* eslint-disable-line no-console */
172
+ }
173
+
159
174
// add user to team
160
175
console . log ( `adding ${ token } to ${ team . teamId } with ${ team . ownerToken } ` ) ; /* eslint-disable-line no-console */
161
176
const githubUser = await GithubService . addTeamMember ( team . teamId , team . ownerToken , token , team . accessLevel ) ;
162
177
// associate github username with TC username
163
- const mapping = await dbHelper . queryOneUserMappingByTCUsername ( UserMapping , req . session . tcUsername ) ;
164
-
165
- // get team details
166
- const teamDetails = await GithubService . getTeamDetails ( team . ownerToken , team . teamId ) ;
178
+ const mapping = await dbHelper . queryOneUserMappingByTCUsername ( GithubUserMapping , req . session . tcUsername ) ;
167
179
168
180
if ( mapping ) {
169
- await dbHelper . update ( UserMapping , mapping . id , {
181
+ await dbHelper . update ( GithubUserMapping , mapping . id , {
170
182
githubUsername : githubUser . username ,
171
183
githubUserId : githubUser . id ,
172
184
} ) ;
173
185
} else {
174
- await dbHelper . create ( UserMapping , {
186
+ console . log ( 'User mapping not found. Create new mapping.' ) ; /* eslint-disable-line no-console */
187
+ await dbHelper . create ( GithubUserMapping , {
175
188
id : helper . generateIdentifier ( ) ,
176
189
topcoderUsername : req . session . tcUsername ,
177
190
githubUsername : githubUser . username ,
0 commit comments