2018-01-18 14:34:32 +00:00
// Description:
// Script that handles the installation of the GitHub app
//
// Dependencies:
// github: "^13.1.0"
// hubot-github-webhook-listener: "^0.9.1"
//
// Author:
// PombeirP
module . exports = function ( robot ) {
2018-01-19 21:59:32 +00:00
const gitHubContext = require ( './github-context.js' ) ( ) ;
2018-01-18 14:34:32 +00:00
return robot . on ( "github-repo-event" , async function ( repo _event ) {
const githubPayload = repo _event . payload ;
robot . logger . debug ( ` Received ${ repo _event . eventType } / ${ githubPayload . action } event from GitHub ` ) ;
switch ( repo _event . eventType ) {
case "integration_installation" :
// Make sure we don't listen to our own messages
2018-01-18 17:54:54 +00:00
if ( gitHubContext . equalsRobotName ( robot , githubPayload . sender . login ) ) { return ; }
2018-01-18 14:34:32 +00:00
var { action } = githubPayload ;
switch ( action ) {
case "created" :
// App was installed on an organization
robot . logger . info ( ` Initializing installation for app with ID ${ githubPayload . installation . app _id } and installation ID ${ githubPayload . installation . id } ` ) ;
robot . brain . set ( 'github-app_install-payload' , JSON . stringify ( githubPayload ) ) ;
robot . brain . set ( 'github-app_id' , githubPayload . installation . app _id ) ;
robot . brain . set ( 'github-app_repositories' , githubPayload . repositories . map ( ( x ) => x . full _name ) . join ( ) ) ;
2018-01-18 17:54:54 +00:00
gitHubContext . initialize ( robot , githubPayload . installation . app _id ) ;
2018-01-18 14:34:32 +00:00
var perms = githubPayload . installation . permissions ;
if ( perms . repository _projects !== 'write' ) { robot . logger . error ( formatPermMessage ( 'repository_projects' , 'write' ) ) ; }
if ( perms . metadata !== 'read' ) { robot . logger . error ( formatPermMessage ( 'metadata' , 'read' ) ) ; }
if ( perms . issues !== 'read' ) { robot . logger . error ( formatPermMessage ( 'issues' , 'read' ) ) ; }
if ( perms . pull _requests !== 'write' ) { robot . logger . error ( formatPermMessage ( 'pull_requests' , 'write' ) ) ; }
if ( ! githubPayload . installation . events . includes ( 'pull_request' ) ) {
robot . logger . error ( "Please enable 'pull_request' events in the app configuration on github.com" ) ;
}
2018-01-18 17:54:54 +00:00
await createAccessToken ( robot , gitHubContext . api ( ) , githubPayload . installation . id ) ;
2018-01-18 14:34:32 +00:00
break ;
case "deleted" :
// App was uninstalled from an organization
robot . logger . info ( ` Removing installation for app with ID ${ githubPayload . installation . app _id } and installation ID ${ githubPayload . installation . id } ` ) ;
robot . brain . set ( 'github-app_id' , null ) ;
robot . brain . set ( 'github-app_install-payload' , null ) ;
robot . brain . set ( 'github-app_repositories' , null ) ;
robot . brain . set ( 'github-token' , null ) ;
process . env . HUBOT _GITHUB _TOKEN = null ;
break ;
}
break ;
}
} ) ;
} ;
async function createAccessToken ( robot , github , id ) {
try {
response = await github . apps . createInstallationToken ( { installation _id : id } ) ;
robot . brain . set ( 'github-token' , response . data . token ) ;
// TODO: Set Redis expiration date to value from response.data.expires_at
process . env . HUBOT _GITHUB _TOKEN = response . data . token ;
github . authenticate ( {
type : 'token' ,
token : response . data . token
} ) ;
2018-01-19 23:27:29 +00:00
} catch ( err ) {
2018-01-18 14:34:32 +00:00
robot . logger . error ( ` Couldn't create installation token: ${ err } ` , id ) ;
}
}
var formatPermMessage = ( permName , perm ) => ` Please enable ' ${ permName } ' ${ perm } permission in the app configuration on github.com ` ;