import * as React from 'react'; import { PUSH_BUILD_EVENT_TYPES, PipelineRunLabel, PipelineRunType } from '../consts/pipelinerun'; import { ComponentKind } from '../types'; import { SAMPLE_ANNOTATION, getPACProvision, ComponentBuildState, BUILD_REQUEST_ANNOTATION, BuildRequest, useConfigurationTime, } from '../utils/component-utils'; import { useApplicationPipelineGitHubApp } from './useApplicationPipelineGitHubApp'; import { usePipelineRunsV2 } from './usePipelineRunsV2'; export enum PACState { sample, disabled, configureRequested, unconfigureRequested, error, pending, ready, loading, } const usePACState = (component: ComponentKind) => { const isSample = component.metadata?.annotations?.[SAMPLE_ANNOTATION] === 'true'; const isMigrationRequested = component.metadata?.annotations?.[BUILD_REQUEST_ANNOTATION] === BuildRequest.migratePac; const pacProvision = getPACProvision(component); const isConfigureRequested = component.metadata?.annotations?.[BUILD_REQUEST_ANNOTATION] === BuildRequest.configurePac; const isUnconfigureRequested = component.metadata?.annotations?.[BUILD_REQUEST_ANNOTATION] === BuildRequest.unconfigurePac; const { name: prBotName } = useApplicationPipelineGitHubApp(); const configurationTime = useConfigurationTime(component); const [pipelineBuildRuns, pipelineBuildRunsLoaded, pipelineBuildRunsError] = usePipelineRunsV2( !isSample && pacProvision ? component.metadata.namespace : null, React.useMemo( () => ({ selector: { matchLabels: { [PipelineRunLabel.PIPELINE_TYPE]: PipelineRunType.BUILD, [PipelineRunLabel.APPLICATION]: component.spec.application, [PipelineRunLabel.COMPONENT]: component.metadata.name, }, matchExpressions: [ { key: PipelineRunLabel.COMMIT_EVENT_TYPE_LABEL, operator: 'In', values: PUSH_BUILD_EVENT_TYPES, }, ], }, // this limit is based on the assumption that user merges the PR after the component is created limit: 10, }), [component.metadata.name, component.spec.application], ), ); // filter out runs that were created if the component previously had pac configured const runsForComponent = React.useMemo( () => !pipelineBuildRunsLoaded || pipelineBuildRunsError ? [] : pipelineBuildRuns?.filter((p) => configurationTime ? new Date(p.metadata.creationTimestamp) > new Date(configurationTime) : true, ), [configurationTime, pipelineBuildRuns, pipelineBuildRunsLoaded, pipelineBuildRunsError], ); const prMerged = runsForComponent.find( (r) => !r.metadata?.annotations?.[PipelineRunLabel.COMMIT_USER_LABEL]?.includes(prBotName), ); return isSample ? PACState.sample : isConfigureRequested || isMigrationRequested ? PACState.configureRequested : isUnconfigureRequested ? PACState.unconfigureRequested : pacProvision === ComponentBuildState.enabled ? !pipelineBuildRunsLoaded ? PACState.loading : prMerged ? PACState.ready : PACState.pending : !pacProvision || pacProvision === ComponentBuildState.disabled ? PACState.disabled : PACState.error; }; export default usePACState;