@@ -20,7 +20,7 @@ const GithubService = require('../services/GithubService');
2020const UserService = require ( '../services/UserService' ) ;
2121const OwnerUserTeam = require ( '../models' ) . OwnerUserTeam ;
2222const UserTeamMapping = require ( '../models' ) . UserTeamMapping ;
23- const UserMapping = require ( '../models' ) . UserMapping ;
23+ const GithubUserMapping = require ( '../models' ) . GithubUserMapping ;
2424const constants = require ( '../common/constants' ) ;
2525
2626const request = superagentPromise ( superagent , Promise ) ;
@@ -127,6 +127,8 @@ async function addUserToTeam(req, res) {
127127 config . GITHUB_CLIENT_ID
128128 } &redirect_uri=${
129129 encodeURIComponent ( callbackUri )
130+ } &scope=${
131+ encodeURIComponent ( 'admin:org' )
130132 } &state=${ identifier } `) ;
131133}
132134
@@ -156,22 +158,33 @@ async function addUserToTeamCallback(req, res) {
156158 throw new errors . UnauthorizedError ( 'Github authorization failed.' , result . body . error_description ) ;
157159 }
158160 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+
159174 // add user to team
160175 console . log ( `adding ${ token } to ${ team . teamId } with ${ team . ownerToken } ` ) ; /* eslint-disable-line no-console */
161176 const githubUser = await GithubService . addTeamMember ( team . teamId , team . ownerToken , token , team . accessLevel ) ;
162177 // 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 ) ;
167179
168180 if ( mapping ) {
169- await dbHelper . update ( UserMapping , mapping . id , {
181+ await dbHelper . update ( GithubUserMapping , mapping . id , {
170182 githubUsername : githubUser . username ,
171183 githubUserId : githubUser . id ,
172184 } ) ;
173185 } 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 , {
175188 id : helper . generateIdentifier ( ) ,
176189 topcoderUsername : req . session . tcUsername ,
177190 githubUsername : githubUser . username ,
0 commit comments