import GithubAPI from "../config/github-api" ; import { NextFunction , Request , RequestHandler , Response } from "express" ; import { App } from "@octokit/app" ; import Logger from "bunyan" ; export default ( octokitApp ) : RequestHandler => async ( req , res , next ) : Promise < void > => { res . locals . github = GithubAPI ( ) ; res . locals . client = GithubAPI ( { auth : octokitApp . getSignedJsonWebToken ( ) } ) ; res . locals . isAdmin = isAdmin ( res , req . log ) ; next ( ) ; } ; export const isAdmin = ( res , logger ) => async ( args ) : Promise < boolean > => { const { org , username , type } = args ; if ( type === "User" ) { return org === username ; } try { const { data : { role } } = await res . locals . github . orgs . getMembership ( { org , username } ) ; logger . info ( `isAdmin: User ${ username } has ${ role } role for org ${ org }` ) ; return role === "admin" ; } catch ( err ) { logger . warn ( { err , org , username } , `could not determine admin status of user ${ username } in org ${ org }` ) ; return false ; } } ;