@@ -19,7 +19,12 @@ const dbHelper = require('../common/db-helper');
1919const User = require ( '../models' ) . User ;
2020const GithubUserMapping = require ( '../models' ) . GithubUserMapping ;
2121const OwnerUserTeam = require ( '../models' ) . OwnerUserTeam ;
22+ const Organisation = require ( '../models' ) . Organisation ;
2223const errors = require ( '../common/errors' ) ;
24+ const superagent = require ( 'superagent' ) ;
25+ const superagentPromise = require ( 'superagent-promise' ) ;
26+
27+ const request = superagentPromise ( superagent , Promise ) ;
2328
2429/**
2530 * Ensure the owner user is in database.
@@ -229,6 +234,89 @@ addTeamMember.schema = Joi.object().keys({
229234 accessLevel : Joi . string ( ) . required ( ) ,
230235} ) ;
231236
237+ /**
238+ * Add organisation member.
239+ * @param {String } organisation the organisation name
240+ * @param {String } normalUserToken the normal user token
241+ * @returns {Promise } the promise result
242+ */
243+ async function addOrganisationMember ( organisation , normalUserToken ) {
244+ let state ;
245+ try {
246+ const dbOrganisation = await dbHelper . queryOneOrganisation ( Organisation , organisation ) ;
247+ if ( ! dbOrganisation ) {
248+ console . log ( `Personal access token not found for organisation ${ organisation } .` ) ; /* eslint-disable-line no-console */
249+ return { } ;
250+ }
251+ const githubNormalUser = new GitHub ( {
252+ token : normalUserToken ,
253+ } ) ;
254+ const normalUser = await githubNormalUser . getUser ( ) . getProfile ( ) ;
255+ const username = normalUser . data . login ;
256+ const base64PAT = Buffer . from ( `${ dbOrganisation . owner } :${ dbOrganisation . personalAccessToken } ` ) . toString ( 'base64' ) ;
257+ const result = await request
258+ . put ( `https://api.github.com/orgs/${ organisation } /memberships/${ username } ` )
259+ . send ( { role : 'member' } )
260+ . set ( 'User-Agent' , 'superagent' )
261+ . set ( 'Accept' , 'application/vnd.github.v3+json' )
262+ . set ( 'Authorization' , `Basic ${ base64PAT } ` )
263+ . end ( ) ;
264+ state = _ . get ( result , 'body.state' ) ;
265+ } catch ( err ) {
266+ // if error is already exists discard
267+ if ( _ . chain ( err ) . get ( 'body.errors' ) . countBy ( {
268+ code : 'already_exists' ,
269+ } ) . get ( 'true' )
270+ . isUndefined ( )
271+ . value ( ) ) {
272+ throw helper . convertGitHubError ( err , 'Failed to add organisation member' ) ;
273+ }
274+ }
275+ // return its state
276+ return { state} ;
277+ }
278+
279+ addOrganisationMember . schema = Joi . object ( ) . keys ( {
280+ organisation : Joi . string ( ) . required ( ) ,
281+ normalUserToken : Joi . string ( ) . required ( )
282+ } ) ;
283+
284+ /**
285+ * Accept organisation invitation by member.
286+ * @param {String } organisation the organisation name
287+ * @param {String } normalUserToken the normal user token
288+ * @returns {Promise } the promise result
289+ */
290+ async function acceptOrganisationInvitation ( organisation , normalUserToken ) {
291+ let state ;
292+ try {
293+ const result = await request
294+ . patch ( `https://api.github.com/user/memberships/orgs/${ organisation } ` )
295+ . send ( { state : 'active' } )
296+ . set ( 'User-Agent' , 'superagent' )
297+ . set ( 'Accept' , 'application/vnd.github.v3+json' )
298+ . set ( 'Authorization' , `token ${ normalUserToken } ` )
299+ . end ( ) ;
300+ state = _ . get ( result , 'body.state' ) ;
301+ } catch ( err ) {
302+ // if error is already exists discard
303+ if ( _ . chain ( err ) . get ( 'body.errors' ) . countBy ( {
304+ code : 'already_exists' ,
305+ } ) . get ( 'true' )
306+ . isUndefined ( )
307+ . value ( ) ) {
308+ throw helper . convertGitHubError ( err , 'Failed to accept organisation invitation' ) ;
309+ }
310+ }
311+ // return its state
312+ return { state} ;
313+ }
314+
315+ acceptOrganisationInvitation . schema = Joi . object ( ) . keys ( {
316+ organisation : Joi . string ( ) . required ( ) ,
317+ normalUserToken : Joi . string ( ) . required ( )
318+ } ) ;
319+
232320/**
233321 * Gets the user id by username
234322 * @param {string } username the username
@@ -320,6 +408,8 @@ module.exports = {
320408 getUserIdByUsername,
321409 getTeamDetails,
322410 deleteUserFromGithubTeam,
411+ addOrganisationMember,
412+ acceptOrganisationInvitation
323413} ;
324414
325415helper . buildService ( module . exports ) ;
0 commit comments