import { HostingClientBase } from '../../basic/HostingPlatform/HostingClientBase' ; import { Gitlab } from 'gitlab' ; import { Application } from 'egg' ; import { CheckRun , CreatePullRequestOption , RepoDir , RepoFile } from '../../basic/DataTypes' ; import { getAll } from './data/getAll' ; import { GitlabGraphqlClient } from './client/GitlabGraphqlClient' ; import { GitLabConfig } from './GitLabConfig' ; import { HostingBase } from '../../basic/HostingPlatform/HostingBase' ; export class GitLabClient extends HostingClientBase < GitLabConfig , Gitlab > { private id : number ; private gitlabGraphqlClient : GitlabGraphqlClient ; constructor ( name , hostId , app , id , client , gitlabGraphqlClient , hostBase ) { super ( name , hostId , app , hostBase ) ; this . id = id ; this . rawClient = client ; this . gitlabGraphqlClient = gitlabGraphqlClient ; } protected async updateData ( ) { this . repoDataService . setRepoData ( await getAll ( this . gitlabGraphqlClient , this . fullName ) ) ; } public async getFileContent ( path , ref ? ) { const res = await this . rawClient . RepositoryFiles . show ( this . id , path , ref ?? 'master' ) ; if ( res . content && res . encoding === 'base64' ) { res . content = Buffer . from ( res . content , 'base64' ) . toString ( 'ascii' ) ; delete res . encoding ; } return res ; } public async getDirectoryContent ( ) { throw new Error ( 'Method not implemented.' ) ; } public async addIssue ( title , body , labels ? ) { await this . rawClient . Issues . create ( this . id , { title , description : body , labels : labels ? labels . join ( ',' ) : undefined , } ) ; } public async listLabels ( ) { const res = await this . rawClient . Labels . all ( this . id ) as any [ ] ; if ( ! res ) return [ ] ; return res . map ( r => { return { name : r . name , description : r . description , color : r . color , } ; } ) ; } public async addLabels ( number , labels ) { await this . rawClient . Issues . edit ( this . id , number , { labels : labels . join ( ',' ) , } ) ; } public async removeLabel ( number , label ) { this . logger . info ( number , label ) ; await this . rawClient . Issues . edit ( this . id , number , { labels : [ ] , } ) ; } public async updateIssue ( number , update ) { let state_event ; if ( update . state === 'open' ) state_event = 'reopen' ; if ( update . state === 'closed' ) state_event = 'close' ; await this . rawClient . Issues . edit ( this . id , number , { title : update . title , description : update . body , state_event , } ) ; } public async updatePull ( number , update ) { let state_event ; if ( update . state === 'open' ) state_event = 'reopen' ; if ( update . state === 'closed' ) state_event = 'close' ; await this . rawClient . MergeRequests . edit ( this . id , number , { title : update . title , description : update . body , state_event , } ) ; } public async addIssueComment ( number , body ) { await this . rawClient . IssueNotes . create ( this . id , number , body ) ; } public async updateIssueComment ( comment_id , body ) { this . logger . info ( comment_id , body ) ; await this . rawClient . Labels . all ( this . id ) ; } public async updateLabels ( labels ) { await Promise . all ( labels . map ( label => { return this . rawClient . Labels . edit ( this . id , label . current_name , { color : this . parseColor ( label . color ) , description : label . description , } ) ; } ) ) ; } public async createLabels ( labels ) { await Promise . all ( labels . map ( async ( label ) => { return this . rawClient . Labels . create ( this . id , label . name , this . parseColor ( label . color ) , { description : label . description , } ) ; } ) ) ; } public async merge ( num ) { await this . rawClient . MergeRequests . accept ( this . id , num , { squash : true , } ) ; } public async assign ( num , login ) { try { const user = await this . rawClient . Users . search ( login ) as any ; if ( user && Array . isArray ( user ) && user . length > 0 ) { const userId = user [ 0 ] . id ; await this . rawClient . Issues . edit ( this . id , num , { assignee_ids : [ userId ] , } ) ; } } catch ( e ) { this . logger . error ( e ) ; } } private parseColor < T > ( color ) { if ( ! color ) return color ; if ( ! ( color as any ) . startsWith ( '#' ) ) { color = `#${ color }` as any ; } return ( color as any ) . toUpperCase ( ) ; } public createCheckRun ( check ) { this . logger . info ( check ) ; return new Promise ( ( ) => { } ) ; } public async newBranch ( newBranchName , baseBranchName , cb ? ) { this . logger . info ( newBranchName , baseBranchName ) ; await this . rawClient . Branches . create ( this . id , newBranchName , baseBranchName ) ; if ( cb ) cb ( ) ; } public async createOrUpdateFile ( filePath , content , commitMessgae , branchName , cb ? ) { this . logger . info ( filePath , content , commitMessgae , branchName ) ; await this . rawClient . RepositoryFiles . create ( this . id , filePath , branchName , content , commitMessgae ) ; if ( cb ) cb ( ) ; } public async newPullRequest ( option ) { this . logger . info ( 'New pull request' , option ) ; this . rawClient . MergeRequests . create ( this . id , option . head , option . base , option . title , { description : option . body , allow_collaboration : option . allowModify , } ) ; return new Promise ( ( ) => { } ) ; } }