@@ -4,6 +4,11 @@ require("dotenv").config();
44const axios = require ( "axios" ) ;
55const { saveUser } = require ( "../../database/controllers/user.controller.js" ) ;
66const { getUserInfo, getUserRepositories } = require ( "./APICalls.js" ) ;
7+ const {
8+ encrypt,
9+ decrypt,
10+ convertToMarkdown,
11+ } = require ( "../../helpers/crypto.js" ) ;
712
813// instantiate Github App for event handling (webhooks)
914const githubApp = new App ( {
@@ -21,17 +26,28 @@ const githubApp = new App({
2126 * @param {* } res Response to send back to the caller
2227 */
2328const githubAuth = ( req , res ) => {
29+ const { type } = req . body ;
2430 if ( ! process . env . GITHUB_AUTH_CLIENT_ID ) {
2531 res . status ( 500 ) . send ( "GitHub provider is not configured" ) ;
2632 return ;
2733 }
2834
29- const scopes = [ "user" , "repo" ] ;
30- const url = `https://github.com/login/oauth/authorize?client_id=${
31- process . env . GITHUB_AUTH_CLIENT_ID
32- } &scope=${ scopes . join ( "," ) } `;
35+ if ( type === "event-badging" ) {
36+ const scopes = [ "repo" ] ;
37+ const encryptedFormData = encrypt ( JSON . stringify ( req . body ) ) ;
38+ const url = `https://github.com/login/oauth/authorize?client_id=${
39+ process . env . GITHUB_AUTH_CLIENT_ID
40+ } &scope=${ scopes . join ( "," ) } &state=${ encryptedFormData } `;
3341
34- res . redirect ( url ) ;
42+ res . send ( { authorizationLink : url } ) ;
43+ } else {
44+ const scopes = [ "user" , "repo" ] ;
45+ const url = `https://github.com/login/oauth/authorize?client_id=${
46+ process . env . GITHUB_AUTH_CLIENT_ID
47+ } &scope=${ scopes . join ( "," ) } `;
48+
49+ res . redirect ( url ) ;
50+ }
3551} ;
3652
3753/**
@@ -72,6 +88,17 @@ const requestAccessToken = async (code) => {
7288const handleOAuthCallback = async ( req , res ) => {
7389 const code = req . body . code ?? req . query . code ;
7490
91+ let issueTitle ;
92+ let markdown ;
93+
94+ if ( req . query . state ) {
95+ const encryptedState = req . query . state ;
96+ const formData = decrypt ( encryptedState ) ;
97+ const parsedFormData = JSON . parse ( formData ) ;
98+ issueTitle = parsedFormData . title ;
99+ markdown = convertToMarkdown ( parsedFormData . body ) ;
100+ }
101+
75102 const { access_token : accessToken , errors : accessTokenErrors } =
76103 await requestAccessToken ( code ) ;
77104 if ( accessTokenErrors . length > 0 ) {
@@ -81,6 +108,18 @@ const handleOAuthCallback = async (req, res) => {
81108
82109 const octokit = new Octokit ( { auth : `${ accessToken } ` } ) ;
83110
111+ if ( issueTitle && markdown ) {
112+ const { data : issue } = await octokit . rest . issues . create ( {
113+ owner : "badging" ,
114+ repo : "event-diversity-and-inclusion" ,
115+ title : issueTitle ,
116+ body : markdown ,
117+ } ) ;
118+
119+ res . redirect ( issue . html_url ) ;
120+ return ;
121+ }
122+
84123 // Authenticated user details
85124 const { user_info : userInfo , errors : userInfoErrors } = await getUserInfo (
86125 octokit
0 commit comments