import { firestore } from 'firebase-admin' ; import { GitHubContributorInput , GitHubContributorMapper , GitHubEventInput , GitHubEventMapper , GitHubIssueInput , GitHubIssueMapper , GitHubMilestoneInput , GitHubMilestoneMapper , GitHubPullRequestInput , GitHubPullRequestMapper , GitHubPullRequestModel , GitHubReleaseInput , GitHubReleaseMapper , GitHubRepositoryInput , GitHubRepositoryMapper , GitHubRepositoryModel , GitHubRepositoryWebhookModel , } from '../mappers/github/index.mapper' ; import { FirebaseAdmin } from './../client/firebase-admin' ; import { GitHubClient } from './../client/github' ; import { Logger } from './../client/logger' ; import { getWebhook } from './create-git-webhook-repository' ; import { getPullRequestStatus } from './pull-request' ; export interface RepositoryInfoInput { token : string ; repository : { uid : string ; id : number ; fullName : string ; } ; } export interface PullRequestStatusInput { token : string ; repository : { fullName : string ; ref : string ; } ; } export const getRepositoryInfo = async ( token , repository ) => { let data ; let mappedData ; try { data = await Promise . all ( [ GitHubClient < GitHubRepositoryInput > ( `/repos/${ repository . fullName }` , token ) , GitHubClient < GitHubPullRequestInput [ ] > ( `/repos/${ repository . fullName }/pulls?state=open` , token ) , GitHubClient < GitHubEventInput [ ] > ( `/repos/${ repository . fullName }/events` , token ) , GitHubClient < GitHubReleaseInput [ ] > ( `/repos/${ repository . fullName }/releases` , token ) , GitHubClient < GitHubIssueInput [ ] > ( `/repos/${ repository . fullName }/issues` , token ) , GitHubClient < GitHubContributorInput [ ] > ( `/repos/${ repository . fullName }/stats/contributors` , token ) , GitHubClient < GitHubMilestoneInput [ ] > ( `/repos/${ repository . fullName }/milestones` , token ) , ] ) ; mappedData = { ... GitHubRepositoryMapper . import ( data [ 0 ] , 'all' ) , pullRequests : data [ 1 ] ? data [ 1 ] . map ( ( pullrequest ) => GitHubPullRequestMapper . import ( pullrequest ) ) : [ ] , events : data [ 2 ] ? data [ 2 ] . map ( ( event ) => GitHubEventMapper . import ( event ) ) : [ ] , releases : data [ 3 ] ? GitHubReleaseMapper . sortReleaseList ( data [ 3 ] . map ( ( release ) => GitHubReleaseMapper . import ( release ) ) ) : [ ] , issues : Array . isArray ( data [ 4 ] ) ? data [ 4 ] . map ( ( issue ) => GitHubIssueMapper . import ( issue ) ) : [ ] , contributors : Array . isArray ( data [ 5 ] ) ? data [ 5 ] . map ( ( contributor ) => GitHubContributorMapper . import ( contributor ) ) : [ ] , milestones : Array . isArray ( data [ 6 ] ) ? data [ 6 ] . map ( ( milestone ) => GitHubMilestoneMapper . import ( milestone ) ) : [ ] , updatedAt : firestore . Timestamp . fromDate ( new Date ( ) ) , } ; } catch ( error ) { Logger . error ( error , [ `Repository Path: ${ repository . fullName }` ] ) ; throw new Error ( error ) ; } try { mappedData . webhook = await getWebhook ( repository . fullName , token ) ; } catch ( error ) { Logger . error ( error , [ 'Webhook failed' , `Repository Path: ${ repository . fullName }` ] ) ; } Logger . info ( { repository : repository . fullName , imported : { pullRequests : mappedData && mappedData . pullRequests . length || 0 , events : mappedData && mappedData . events . length || 0 , releases : mappedData && mappedData . releases . length || 0 , issues : mappedData && mappedData . issues . length || 0 , milestones : mappedData && mappedData . milestones . length || 0 , webhook : mappedData && mappedData . webhook , updatedAt : mappedData && mappedData . updatedAt , } , } ) ; mappedData . uid = repository . uid ; const promises = [ ] ; mappedData . pullRequests . forEach ( ( pullrequest ) => { promises . push ( GitHubClient < GitHubPullRequestInput [ ] > ( `/repos/${ repository . fullName }/pulls/${ pullrequest . id }` , token ) ) ; } ) ; const pullRequestData = await Promise . all ( promises ) ; mappedData . pullRequests = pullRequestData ? pullRequestData . map ( ( pullrequest ) => GitHubPullRequestMapper . import ( pullrequest ) ) : [ ] ; await FirebaseAdmin . firestore ( ) . collection ( 'repositories' ) . doc ( mappedData . uid ) . set ( mappedData , { merge : true } ) ; const buildTimePromises = [ ] ; mappedData . pullRequests . map ( ( pullRequest ) => { const ref = pullRequest . statusesUrl . split ( '/' ) . pop ( ) ; buildTimePromises . push ( getPullRequestStatus ( token , repository . fullName , ref , mappedData . uid , pullRequest . uid ) ) ; } ) ; await Promise . all ( buildTimePromises ) ; return mappedData ; } ;