11import fs from "fs" ;
22import { storage } from "./storage.js" ;
3- import { queryStringToJson } from "./helpers.js" ;
3+ import {
4+ PROJECT_ROOT_PATH ,
5+ afterCLA ,
6+ queryStringToJson ,
7+ parseUrlQueryParams ,
8+ } from "./helpers.js" ;
9+ import { resolve } from "path" ;
410
511export const routes = {
612 home ( req , res ) {
7- fs . readFile ( "./home.html" , function ( err , data ) {
13+ const htmlPath = resolve ( PROJECT_ROOT_PATH , "views" , "home.html" ) ;
14+ fs . readFile ( htmlPath , function ( err , data ) {
815 if ( err ) {
16+ console . error ( err ) ;
917 res . writeHead ( 404 ) ;
1018 res . write ( "Errors: File not found" ) ;
1119 return res . end ( ) ;
@@ -16,8 +24,9 @@ export const routes = {
1624 } ) ;
1725 } ,
1826
19- form ( req , res ) {
20- fs . readFile ( "./form.html" , function ( err , data ) {
27+ cla ( req , res ) {
28+ const htmlPath = resolve ( PROJECT_ROOT_PATH , "views" , "cla.html" ) ;
29+ fs . readFile ( htmlPath , function ( err , data ) {
2130 if ( err ) {
2231 res . writeHead ( 404 ) ;
2332 res . write ( "Errors: File not found" ) ;
@@ -29,23 +38,36 @@ export const routes = {
2938 } ) ;
3039 } ,
3140
32- submitForm ( req , res ) {
41+ submitCla ( req , res , octokit ) {
3342 let body = "" ;
3443 req . on ( "data" , ( chunk ) => {
3544 body += chunk . toString ( ) ; // convert Buffer to string
3645 } ) ;
3746
38- req . on ( "end" , ( ) => {
39- console . log ( body ) ;
47+ req . on ( "end" , async ( ) => {
4048 let bodyJson = queryStringToJson ( body ) ;
4149 const ip = req . headers [ "x-forwarded-for" ] || req . connection . remoteAddress ;
4250 bodyJson . ip = ip ;
51+ if ( ! bodyJson . referrer && req . headers [ "referer" ] ) {
52+ bodyJson . referrer = req . headers [ "referer" ] ;
53+ }
4354 const serverTimestamp = new Date ( ) . toISOString ( ) ;
4455 bodyJson . serverTimestamp = serverTimestamp ;
4556 storage . save ( bodyJson ) ;
57+ await afterCLA ( octokit , bodyJson ) ;
58+ // Referrer has information about which PR this CLA flow started from
59+ const { org, repo, prNumber } = parseUrlQueryParams ( bodyJson . referrer ) ;
60+ if ( org && repo && prNumber ) {
61+ // Redirect To PR
62+ const prLink = `https://github.com/${ org } /${ repo } /pull/${ prNumber } ` ;
63+ res . writeHead ( 302 , {
64+ Location : prLink ,
65+ } ) ;
66+ return res . end ( ) ;
67+ }
4668 res . writeHead ( 200 , { "Content-Type" : "text/html; charset=utf-8" } ) ;
4769 res . write (
48- "<h1>Form data saved ☑️</h1>\n<pre>" +
70+ "<h1>CLA Signed successfully ☑️</h1>\n<pre>" +
4971 JSON . stringify ( bodyJson , null , 2 ) +
5072 "</pre>" ,
5173 ) ;
0 commit comments