import chalk from 'chalk' ; import { setPipelineEnd } from './set-pipeline-end' ; import { Jobs } from '../jobs/jobs' ; import { computePipelineStatus } from './compute-pipeline-status' ; import { Logger } from '../../commons/logger/logger' ; import { setPipelineStatus } from './set-pipeline-status' ; const logger = new Logger ( 'metroline.server:updatePipelineStatus' ) ; export async function updatePipelineStatus ( pipelineId ) { const jobs = await Jobs ( ) . find ( { pipelineId } ) . toArray ( ) ; const status = computePipelineStatus ( jobs ) ; const onlyHasSucceededCloneJob = jobs . length === 1 && status === 'success' ; if ( status !== 'running' && status !== 'created' && ! onlyHasSucceededCloneJob ) { const jobStatuses = jobs . map ( j => `${ chalk . blue ( j . name ) }:${ chalk . bold . blue ( j . status ) }` ) . join ( ',' ) ; logger . debug ( `Setting end of pipeline ${ chalk . blue ( pipelineId ) } with status ${ chalk . blue ( status ) }; job statuses are ${ jobStatuses }` ) ; setPipelineEnd ( pipelineId , new Date ( ) ) ; } if ( onlyHasSucceededCloneJob ) { logger . debug ( `Skipping status update of pipeline ${ pipelineId } to avoid bad transition` ) ; return ; } await setPipelineStatus ( pipelineId , status ) ; }