import { GiteeApp } from '../../../app/plugin/gitee/gitee-app' ; import { GiteeConfig } from '../../../app/plugin/gitee/gitee-config' ; import { Application } from 'egg' ; import { GiteeClient } from '../../../app/plugin/gitee/gitee-client' ; import { GiteeRawClient } from '../../../app/plugin/gitee/gitee-raw-client/gitee-raw-client' ; import { HostingBase } from '../../../app/basic/HostingPlatform/HostingBase' ; import { HostingClientBase } from '../../../app/basic/HostingPlatform/HostingClientBase' ; import { MockApplication } from 'egg-mock' ; import { HostingPlatformInitEvent } from '../../../app/basic/HostingPlatform/event' ; import { waitFor } from '../../Util' ; import { waitUntil } from '../../../app/basic/Utils' ; import { readFileSync } from 'fs' ; import { join } from 'path' ; import { Repo , RepoFile } from '../../../app/basic/DataTypes' ; /**
 * Mock Gitee App
 * Only replace some methods that need to connect to Internet
 */ export class MockGiteeApp extends GiteeApp { mockRepoData : Repo ; constructor ( id , config , app , repoData ) { super ( id , config , app ) ; this . mockRepoData = repoData ; } public async getInstalledRepos ( ) { const ret = [ ] ; ret . push ( { fullName : this . mockRepoData . owner + '/' + this . mockRepoData . name , payload : 0 , } ) ; return ret ; } public async addRepo ( name , _payload ) { const giteeClient = new MockGiteeClient ( name , this . id , this . app , this , this . mockRepoData ) ; this . clientMap . set ( name , async ( ) => giteeClient ) ; } } export class MockGiteeClient extends GiteeClient { testResult : any [ ] = [ ] ; mockRepoData : any ; constructor ( name , hostId , app , hostBase , mockRepoData ) { super ( name , hostId , app , { } as any , hostBase ) ; this . mockRepoData = mockRepoData ; } public async updateData ( ) { this . testResult . push ( [ 'updateData' ] ) ; this . repoDataService . setRepoData ( this . mockRepoData ) ; } public async getFileContent ( path ) { this . testResult . push ( [ 'getFileContent' , path ] ) ; return ; } public async listIssues ( ) { this . testResult . push ( [ 'listIssues' ] ) ; return ; } public async addIssueComment ( number , body ) { this . testResult . push ( [ 'addIssueComment' , number , body ] ) ; return ; } public async addIssue ( title , body , labels ? ) { this . testResult . push ( [ 'addIssue' , title , body , labels ] ) ; return ; } public async updateIssue ( number , update ) { this . testResult . push ( [ 'updateIssue' , number , update ] ) ; return ; } public async assign ( number , login ) { this . testResult . push ( [ 'assign' , number , login ] ) ; return ; } public async updatePull ( number , update ) { this . testResult . push ( [ 'updatePull' , number , update ] ) ; return ; } public async listLabels ( ) { this . testResult . push ( [ 'listLabels' ] ) ; return [ ] ; } public async addLabels ( number , labels ) { this . testResult . push ( [ 'addLabels' , number , labels ] ) ; return ; } public async updateLabels ( labels ) { this . testResult . push ( [ 'updateLabels' , labels ] ) ; return ; } public async createLabels ( labels ) { this . testResult . push ( [ 'createLabels' , labels ] ) ; return ; } public async createCheckRun ( ) { this . testResult . push ( [ 'createCheckRun' ] ) ; return ; } public async merge ( number ) { this . testResult . push ( [ 'merge' , number ] ) ; return ; } } export async function initWebhooks ( app , custom = { } ) { /**
     * Notice: The webhook payload's owner and repo data, have to keep in line
     * with that in custom.repoData. Or the test won't work correctly
     * VERY IMPORTANT!!!
     */ if ( ! custom . giteeConfig ) { custom . giteeConfig = JSON . parse ( readFileSync ( join ( __dirname , './data/global-config.json' ) ) . toString ( ) ) . installation . client . configs [ 0 ] ; } if ( ! custom . repoData ) { custom . repoData = JSON . parse ( readFileSync ( join ( __dirname , './data/repo-data.json' ) ) . toString ( ) ) ; } ( app . gitee as any ) . getNewHostingPlatform = async function ( id , config ) : Promise < GiteeApp > { return new MockGiteeApp ( id , config , this . app , custom . repoData ) ; } ; ( app as any ) . agent . event . publish ( 'all' , HostingPlatformInitEvent , { id : 0 , ... custom . giteeConfig , } ) ; let giteeClient ; let retryTime = 0 ; while ( ! giteeClient ) { if ( retryTime >= 10 ) { throw new Error ( 'init webhook timeout' ) ; } await waitFor ( 100 ) ; giteeClient = await app . gitee . getClient ( 0 , ( custom . repoData as any ) . owner + '/' + ( custom . repoData as any ) . name ) ; retryTime ++ ; } const testResult = ( giteeClient as any ) . testResult ; await waitFor ( 500 ) ; await waitUntil ( ( ) => testResult . length !== 0 , { interval : 100 } ) ; await waitFor ( 200 ) ; testResult . length = 0 ; return testResult ; } export async function sendToWebhooks ( app , headers , payload ) { await ( app as MockApplication ) . httpRequest ( ) . post ( '/installation/0/' ) . set ( 'X-Gitee-Event' , headers [ 'X-Gitee-Event' ] ) . set ( 'X-Gitee-Token' , headers [ 'X-Gitee-Token' ] ) . send ( payload ) ; }