import { enviroment } from '../environments/environment' ; import { GitHubRepositoryWebhookMapper , GitHubRepositoryWebhookModel , GitHubRepositoryWebhookRequestCreate , GitHubRepositoryWebhookResponse } from '../mappers/github/webhook.mapper' ; import { RepositoryModel } from '../models/index.model' ; import { DocumentData , DocumentReference } from './../client/firebase-admin' ; import { GitHubClientPost } from './../client/github' ; import { Logger } from './../client/logger' ; import { deleteWebhook } from './delete-git-webhook-repository' ; import { findWebhook } from './find-git-webhook-repository' ; export interface CreateGitWebhookRepositoryInput { token : string ; repositoryUid : string ; } export const onCreateGitWebhookRepository = async ( token , repositoryUid ) => { try { const repositorySnapshot = RepositoryModel . getRepositoryReference ( repositoryUid ) ; const repository = ( await repositorySnapshot . get ( ) ) . data ( ) ; const webhook = await getWebhook ( repository . fullName , token ) ; repository . webhook = webhook ; await repositorySnapshot . update ( repository ) ; Logger . info ( webhook ? 'Webhook created' : 'Webhook empty' ) ; return repository ; } catch ( error ) { Logger . error ( error ) ; throw new Error ( error ) ; } } ; export function createWebhook ( repositoryFullName , token ) { const body = { name : 'web' , active : true , events : enviroment . githubWebhook . events , config : { url : enviroment . githubWebhook . url , secret : enviroment . githubWebhook . secret , content_type : enviroment . githubWebhook . content_type , insecure_ssl : enviroment . githubWebhook . insecure_ssl , } , } ; return GitHubClientPost < GitHubRepositoryWebhookResponse > ( `/repos/${ repositoryFullName }/hooks` , token , body ) ; } export async function getWebhook ( repositoryFullName , token ) { const exist = await findWebhook ( repositoryFullName , token ) ; if ( exist ) { let isEqual = exist . events . length === enviroment . githubWebhook . events . length && exist . config . content_type === enviroment . githubWebhook . content_type && exist . config . insecure_ssl === enviroment . githubWebhook . insecure_ssl && ( ( ! exist . config . secret && ! enviroment . githubWebhook . secret ) || ( ! ! exist . config . secret && ! ! enviroment . githubWebhook . secret ) ) ; if ( isEqual ) { for ( const existItem of exist . events ) { if ( enviroment . githubWebhook . events . findIndex ( ( envItem ) => existItem === envItem ) === - 1 ) { isEqual = false ; break ; } } if ( isEqual ) { Logger . info ( 'Webhook is exist' ) ; return GitHubRepositoryWebhookMapper . import ( exist ) ; } } Logger . info ( 'Webhook is deleting' ) ; await deleteWebhook ( repositoryFullName , exist . id , token ) ; } Logger . info ( 'Webhook is creating' ) ; return GitHubRepositoryWebhookMapper . import ( await createWebhook ( repositoryFullName , token ) ) ; }