import { Request } from 'express' ; import { ValidationError } from 'joi' ; import { Gitlab } from './gitlab/gitlab' ; import { Gitea } from './gitea/gitea' ; import { Webhook } from '../repos/webhook' ; import { DeployKey } from '../repos/deploy-key' ; import { Commit } from '../../commons/types/commit' ; import { GIT_SERVER_TYPE } from '../env-dependent-constants' ; import { env } from '../env' ; import { PipelineStatus } from '../../commons/types/pipeline' ; import { RepoUserPermissions } from '../repos/repo' ; import { Github } from './github/github' ; import chalk from 'chalk' ; export interface Branch { name : string ; } export interface GitRepo { repoId : string ; name : string ; org : string ; public : boolean ; url : string ; lastUpdate : Date ; permissions : RepoUserPermissions ; } export interface GitUser { id : any ; username : string ; orgs : string [ ] ; } export interface CommitValidationResult { commit ? : Commit ; error ? : ValidationError ; } export interface TokenRefresh { token : string ; refreshToken : string ; tokenExpiresIn : number ; } export interface GitServer { listRepos ( page ) : Promise < GitRepo [ ] > ; listBranches ( repoId ) : Promise < Branch [ ] > ; getLatestCommit ( repoId , branch ) : Promise < Commit > ; addWebhook ( url , repoId ) : Promise < Webhook > ; removeWebhook ( webhook , repoId ) : Promise < void > ; addDeployKey ( key , repoId ) : Promise < DeployKey > ; removeDeployKey ( deployKey , repoId ) : Promise < void > ; setCommitStatus ( repoId , sha , status , url ) : Promise < void > ; parseWebhookPayload ( req ) : Promise < CommitValidationResult > ; verifyWebhookSecret ( req , secret ) : Promise < boolean > ; getUser ( ) : Promise < GitUser > ; repoBelongsToOrg ( repo , allowedOrgs ) : Promise < boolean > ; refreshOAuthToken ( refreshToken ) : Promise < TokenRefresh > ; } export function getGitServer ( token ) { switch ( GIT_SERVER_TYPE ) { case 'gitlab' : return new Gitlab ( token , env . METROLINE_GITLAB_URL ) ; case 'gitea' : return new Gitea ( token , env . METROLINE_GITEA_URL ) ; case 'github' : return new Github ( token , env . METROLINE_GITHUB_URL ) ; default : throw new Error ( `Unsupported git server type ${ chalk . blue ( GIT_SERVER_TYPE ) }` ) ; } }