import { Subscription } from "../models" ; import { getRepositorySummary } from "./jobs" ; import enhanceOctokit from "../config/enhance-octokit" ; import { Application } from "probot" ; import { Repositories , SyncStatus } from "../models/subscription" ; import { LoggerWithTarget } from "probot/lib/wrap-logger" ; import sqsQueues from "../sqs/queues" ; export const DISCOVERY_LOGGER_NAME = "sync.discovery" ; export const discovery = ( app ) => async ( job , logger ) => { const startTime = new Date ( ) ; const { jiraHost , installationId } = job . data ; const github = await app . auth ( installationId ) ; enhanceOctokit ( github ) ; try { const repositories = await github . paginate ( github . apps . listRepos . endpoint . merge ( { per_page : 100 } ) , ( res ) => res . data . repositories ) ; logger . info ( { job } , `${ repositories . length } Repositories found` ) ; const subscription = await Subscription . getSingleInstallation ( jiraHost , installationId ) ; if ( ! subscription ) { logger . info ( { jiraHost , installationId } , "Subscription has been removed, ignoring job." ) ; return ; } if ( repositories . length === 0 ) { await subscription . update ( { syncStatus : SyncStatus . COMPLETE } ) ; return ; } const repos = repositories . reduce ( ( obj , repo ) => { obj [ repo . id ] = { repository : getRepositorySummary ( repo ) } ; return obj ; } , { } ) ; await subscription . updateSyncState ( { numberOfSyncedRepos : 0 , repos } ) ; await sqsQueues . backfill . sendMessage ( { installationId , jiraHost , startTime : startTime . toISOString ( ) } , 0 , logger ) ; } catch ( err ) { logger . error ( { job , err } , "Discovery error" ) ; } } ;