@@ -160,6 +160,114 @@ const setupRoutes = (app) => {
160160 app . get ( "/api/login" , login ) ;
161161
162162 //callbacks
163+ app . get ( "/api/github/callback" , async ( req , res ) => {
164+ const code = req . body . code ?? req . query . code ;
165+
166+ let issueTitle ;
167+ let markdown ;
168+
169+ if ( req . query . state ) {
170+ const encryptedState = req . query . state ;
171+ const formData = decrypt ( encryptedState ) ;
172+ const parsedFormData = JSON . parse ( formData ) ;
173+ issueTitle = parsedFormData . title ;
174+ markdown = convertToMarkdown ( parsedFormData . body ) ;
175+ }
176+
177+ const { access_token : accessToken , errors : accessTokenErrors } =
178+ await requestAccessToken ( code ) ;
179+ if ( accessTokenErrors . length > 0 ) {
180+ res . status ( 500 ) . send ( accessTokenErrors . join ( ) ) ;
181+ return ;
182+ }
183+
184+ const octokit = new Octokit ( { auth : `${ accessToken } ` } ) ;
185+
186+ if ( issueTitle && markdown ) {
187+ const { data : issue } = await octokit . rest . issues . create ( {
188+ owner : "badging" ,
189+ repo : "event-diversity-and-inclusion" ,
190+ title : issueTitle ,
191+ body : markdown ,
192+ } ) ;
193+
194+ res . redirect ( issue . html_url ) ;
195+ return ;
196+ }
197+
198+ // Authenticated user details
199+ const { user_info : userInfo , errors : userInfoErrors } = await getUserInfo (
200+ octokit
201+ ) ;
202+ if ( userInfoErrors . length > 0 ) {
203+ res . status ( 500 ) . send ( userInfoErrors . join ( ) ) ;
204+ return ;
205+ }
206+
207+ // Save user to database
208+ const savedUser = await saveUser (
209+ userInfo . login ,
210+ userInfo . name ,
211+ userInfo . email ,
212+ userInfo . id ,
213+ null
214+ ) ;
215+ if ( ! savedUser ) {
216+ res . status ( 500 ) . send ( "Error saving user info" ) ;
217+ return ;
218+ }
219+
220+ // Public repos they maintain, administer, or own
221+ const { repositories, errors : repositoriesErrors } =
222+ await getUserRepositories ( octokit ) ;
223+ if ( repositoriesErrors . length > 0 ) {
224+ res . status ( 500 ) . send ( repositoriesErrors . join ( ) ) ;
225+ return ;
226+ }
227+
228+ if ( process . env . NODE_ENV === "production" ) {
229+ res . status ( 200 ) . json ( {
230+ userId : savedUser . id ,
231+ name : savedUser . name ,
232+ username : savedUser . login ,
233+ email : savedUser . email ,
234+ repos : repositories ,
235+ provider : "github" ,
236+ } ) ;
237+ } else if ( process . env . NODE_ENV === "development" ) {
238+ res . status ( 200 ) . send ( `
239+ <html>
240+ <head>
241+ <title>Repo List</title>
242+ </head>
243+ <body>
244+ <h1>Welcome ${ savedUser . name } </h1>
245+ <h2>Username: ${ savedUser . login } </h2>
246+ <h2>Email: ${ savedUser . email } </h2>
247+ <form action="/api/repos-to-badge" method="post">
248+ <input type="hidden" name="provider" value="github">
249+ <input type="hidden" name="userId" value="${ savedUser . id } ">
250+ <h2>Select Repositories:</h2>
251+ ${ repositories
252+ . map (
253+ ( repo ) => `
254+ <div>
255+ <input type="checkbox" name="repos[]" value="${ repo . id } ">
256+ <label for="${ repo . id } ">${ repo . fullName } </label>
257+ </div>
258+ `
259+ )
260+ . join ( "" ) }
261+ <br>
262+ <input type="submit" value="Submit">
263+ </form>
264+ </body>
265+ </html>
266+ ` ) ;
267+ } else {
268+ res . status ( 500 ) . send ( "Unknown process mode" ) ;
269+ }
270+ } ) ;
163271 githubAuthCallback ( app ) ;
164272 gitlabAuthCallback ( app ) ;
165273 app . get ( "/api/badgedRepos" , badgedRepos ) ;
0 commit comments