1
- const fetch = require ( 'node-fetch' )
2
1
const User = require ( '../Models/User' )
3
2
const Project = require ( '../Models/Project' )
4
3
const gitHub = require ( '../../github/githubApi' )
5
- const { request } = require ( "@octokit/request" ) ;
6
- const { Octokit } = require ( 'octokit' ) ;
7
4
const authLink = 'https://github.com/login/oauth/authorize?client_id=7864fe4bf9aed444e764&scope=repo'
8
5
const TEMP_currentproject = '627380b04dd41fde76c9bf66' // TODO: use URL instead
9
6
10
7
async function page ( req , res ) {
11
- if ( req . session . user )
12
- {
8
+ if ( req . session . user ) {
13
9
if ( req . query . code ) await handleAuth ( req , res )
14
10
const project = await Project . findById ( TEMP_currentproject )
15
- if ( project . categories . development . services && project . categories . development . services . github )
16
- {
11
+ if ( project . categories . development . services && project . categories . development . services . github ) {
17
12
const github = project . categories . development . services . github
18
13
res . render ( 'project/githubtemp' , {
19
14
githubName : github . name ,
@@ -23,47 +18,45 @@ async function page (req, res) {
23
18
return
24
19
}
25
20
}
26
- res . render ( 'project/githubtemp' , { AuthLink : authLink } )
21
+ res . render ( 'project/githubtemp' , { AuthLink : authLink } )
27
22
}
28
- async function webHookReceiver ( req ) {
23
+ async function webHookReceiver ( req ) {
29
24
try {
30
25
// TODO: implement security for github webhook
31
26
const push = JSON . parse ( req . body . read )
32
27
console . log ( push )
33
- const project = await Project . findOne ( { services : { github : push . repository_id } } )
28
+ const project = await Project . findOne ( { services : { github : push . repository_id } } )
34
29
if ( project ) {
35
- project . categories . development . services . github . hookMessages . push ( { event : push . event , action : push . action } )
36
- if ( project . categories . development . services . github . hookMessages . length > 10 )
37
- project . categories . development . services . github . hookMessages . shift ( )
30
+ project . categories . development . services . github . hookMessages . push ( { event : push . event , action : push . action } )
31
+ if ( project . categories . development . services . github . hookMessages . length > 10 ) { project . categories . development . services . github . hookMessages . shift ( ) }
38
32
project . markModified ( 'categories.development.services' )
39
33
project . save ( )
40
34
} else console . log ( "Couldn't find a project with " + push . repository_id )
41
- }
42
- catch ( e ) {
43
- console . log ( "Webhook error: " + e )
35
+ } catch ( e ) {
36
+ console . log ( 'Webhook error: ' + e )
44
37
}
45
38
}
46
- async function handleAuth ( req , res ) {
39
+ async function handleAuth ( req , res ) {
47
40
try {
48
41
const userAuth = await gitHub . auth ( {
49
- type : " oauth-user" ,
42
+ type : ' oauth-user' ,
50
43
code : req . query . code ,
51
- state : req . query . state ,
52
- } ) ;
44
+ state : req . query . state
45
+ } )
53
46
console . log ( userAuth )
54
47
await putUserInDB ( userAuth , req . session . user . username )
55
48
await gitHub . setupProject ( userAuth , req . session . user , TEMP_currentproject )
56
49
// TODO: Handle case where project with that name exists
57
- }
58
- catch ( e ) {
59
- console . log ( "Failed discord auth: " + e )
50
+ } catch ( e ) {
51
+ console . log ( 'Failed discord auth: ' + e )
60
52
}
61
53
}
62
54
63
- async function putUserInDB ( auth , username ) {
55
+ async function putUserInDB ( auth , username ) {
64
56
const user = await User . findOne ( { username : username } )
65
- user . services = { ...user . services ,
66
- github : { clientId : auth . clientId , clientSecret : auth . clientSecret , token : auth . token }
57
+ user . services = {
58
+ ...user . services ,
59
+ github : { clientId : auth . clientId , clientSecret : auth . clientSecret , token : auth . token }
67
60
}
68
61
await user . save ( )
69
62
console . log ( `Sucessfully linked ${ username } with github account ${ auth . clientId } ` )
0 commit comments