import { AppPluginBase } from '../../basic/AppPluginBase' ; import { Application } from 'egg' ; import { InstallationType } from './types' ; import { HostingPlatformInitEvent } from '../../basic/HostingPlatform/event' ; import { IClient } from './IClient' ; export class AppInstallationManager extends AppPluginBase < Config > { private clientMap : Map < number , InstallationInfo > ; constructor ( config , app ) { super ( config , app ) ; this . clientMap = new Map < number , InstallationInfo > ( ) ; } public async onReady ( ) { this . app . event . subscribeAll ( HostingPlatformInitEvent , async ( e ) => { this . clientMap . set ( e . id , { name : e . config . name , type : e . type , } ) ; } ) ; } public async onStart ( ) { } public async onClose ( ) { } public async getClient ( installationId , name ) { const type = this . clientMap . get ( installationId ) ?. type ; if ( type ) { switch ( type ) { case 'github' : return this . app . github . getClient ( installationId , name ) ; case 'gitlab' : return this . app . gitlab . getClient ( installationId , name ) ; case 'gitee' : return this . app . gitee . getClient ( installationId , name ) ; default : break ; } } return undefined ; } public getInstallationType ( installationId ) { const installationInfo = this . clientMap . get ( installationId ) ; if ( installationInfo ) return installationInfo . type ; return undefined ; } public getInstallationInfoByName ( installationName ) { let id = - 1 ; let type ; this . clientMap . forEach ( ( value , key ) => { if ( value . name === installationName ) { id = key ; type = value . type ; } } ) ; return { id , type } ; } public async getHostingClientByInstallationName ( installationName , fullName ) { let client ; this . clientMap . forEach ( async ( hostingPlatform , installationId ) => { if ( hostingPlatform . name === installationName ) { switch ( hostingPlatform . type ) { case 'github' : client = this . app . github . getClient ( installationId , fullName ) ; break ; case 'gitlab' : client = this . app . gitlab . getClient ( installationId , fullName ) ; break ; case 'gitee' : client = this . app . gitee . getClient ( installationId , fullName ) ; break ; default : break ; } } } ) ; return client ; } } interface Config { configs : Array < { type : InstallationType ; config : any ; } > ; } export interface InstallationInfo { name : string ; type : InstallationType ; }