1+ import bodyParser from "body-parser" ;
12import { exec } from "child_process" ;
23import { Request , Response } from "express" ;
4+ import updateDotenv from "update-dotenv" ;
35import { Application } from "../application" ;
46import { ManifestCreation } from "../manifest-creation" ;
57
@@ -27,12 +29,7 @@ export const setupAppFactory = (
2729 printWelcomeMessage ( app , host , port ) ;
2830
2931 route . get ( "/probot" , async ( req , res ) => {
30- const protocols = req . headers [ "x-forwarded-proto" ] || req . protocol ;
31- const protocol =
32- typeof protocols === "string" ? protocols . split ( "," ) [ 0 ] : protocols [ 0 ] ;
33- const host = req . headers [ "x-forwarded-host" ] || req . get ( "host" ) ;
34- const baseUrl = `${ protocol } ://${ host } ` ;
35-
32+ const baseUrl = getBaseUrl ( req ) ;
3633 const pkg = setup . pkg ;
3734 const manifest = setup . getManifest ( pkg , baseUrl ) ;
3835 const createAppUrl = setup . createAppUrl ;
@@ -56,6 +53,26 @@ export const setupAppFactory = (
5653 res . redirect ( `${ response } /installations/new` ) ;
5754 } ) ;
5855
56+ route . get ( "/probot/import" , async ( _req , res ) => {
57+ const { WEBHOOK_PROXY_URL , GHE_HOST } = process . env ;
58+ const GH_HOST = `https://${ GHE_HOST ?? "github.com" } ` ;
59+ res . render ( "import.hbs" , { WEBHOOK_PROXY_URL , GH_HOST } ) ;
60+ } ) ;
61+
62+ route . post ( "/probot/import" , bodyParser . json ( ) , async ( req , res ) => {
63+ const { appId, pem, webhook_secret } = req . body ;
64+ if ( ! appId || ! pem || ! webhook_secret ) {
65+ res . status ( 400 ) . send ( "appId and/or pem and/or webhook_secret missing" ) ;
66+ return ;
67+ }
68+ updateDotenv ( {
69+ APP_ID : appId ,
70+ PRIVATE_KEY : `"${ pem } "` ,
71+ WEBHOOK_SECRET : webhook_secret ,
72+ } ) ;
73+ res . end ( ) ;
74+ } ) ;
75+
5976 route . get ( "/probot/success" , async ( req , res ) => {
6077 res . render ( "success.hbs" ) ;
6178 } ) ;
@@ -87,3 +104,12 @@ function printWelcomeMessage(
87104 app . log . info ( line ) ;
88105 } ) ;
89106}
107+
108+ function getBaseUrl ( req : Request ) : string {
109+ const protocols = req . headers [ "x-forwarded-proto" ] || req . protocol ;
110+ const protocol =
111+ typeof protocols === "string" ? protocols . split ( "," ) [ 0 ] : protocols [ 0 ] ;
112+ const host = req . headers [ "x-forwarded-host" ] || req . get ( "host" ) ;
113+ const baseUrl = `${ protocol } ://${ host } ` ;
114+ return baseUrl ;
115+ }
0 commit comments