/* * Copyright 2023 Harness, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import React, { useMemo } from 'react' import { Falsy, Match, Render, Truthy } from 'react-jsx-match' import { Container, Text, useToggle, Button, ButtonVariation, ButtonSize, Utils, TableV2, Layout, Avatar, stringSubstitute } from '@harnessio/uicore' import cx from 'classnames' import { Color, FontVariation } from '@harnessio/design-system' import type { CellProps, Column } from 'react-table' import type { GitInfoProps } from 'utils/GitUtils' import { useStrings } from 'framework/strings' import { ExecutionState, ExecutionStatus } from 'components/ExecutionStatus/ExecutionStatus' import { useShowRequestError } from 'hooks/useShowRequestError' import type { TypesCodeOwnerEvaluation, TypesCodeOwnerEvaluationEntry } from 'services/code' import type { PRChecksDecisionResult } from 'hooks/usePRChecksDecision' import { findChangeReqDecisions, findWaitingDecisions } from 'utils/Utils' import css from './CodeOwnersOverview.module.scss' interface ChecksOverviewProps extends Pick { prChecksDecisionResult: PRChecksDecisionResult codeOwners?: TypesCodeOwnerEvaluation } enum CodeOwnerReqDecision { CHANGEREQ = 'changereq', APPROVED = 'approved', WAIT_FOR_APPROVAL = '' } export function CodeOwnersOverview({ codeOwners, repoMetadata, pullRequestMetadata, prChecksDecisionResult }: ChecksOverviewProps) { const { getString } = useStrings() const [isExpanded, toggleExpanded] = useToggle(false) const { error } = prChecksDecisionResult useShowRequestError(error) const changeReqEntries = findChangeReqDecisions(codeOwners?.evaluation_entries, CodeOwnerReqDecision.CHANGEREQ) const waitingEntries = findWaitingDecisions(codeOwners?.evaluation_entries) const approvalEntries = findChangeReqDecisions(codeOwners?.evaluation_entries, CodeOwnerReqDecision.APPROVED) const checkEntries = ( // eslint-disable-next-line @typescript-eslint/no-explicit-any changeReqArr: any[], // eslint-disable-next-line @typescript-eslint/no-explicit-any waitingEntriesArr: any[], // eslint-disable-next-line @typescript-eslint/no-explicit-any approvalEntriesArr: any[] ): { borderColor: string; message: string; overallStatus: ExecutionState } => { if (changeReqArr.length !== 0) { return { borderColor: 'red800', overallStatus: ExecutionState.FAILURE, message: stringSubstitute(getString('codeOwner.changesRequested'), { count: changeReqArr.length }) as string } } else if (waitingEntriesArr.length !== 0) { return { borderColor: 'orange800', message: stringSubstitute(getString('codeOwner.waitToApprove'), { count: waitingEntriesArr.length }) as string, overallStatus: ExecutionState.PENDING } } return { borderColor: 'green800', message: stringSubstitute(getString('codeOwner.approvalCompleted'), { count: approvalEntriesArr.length, total: codeOwners?.evaluation_entries?.length }) as string, overallStatus: ExecutionState.SUCCESS } } const { borderColor, message, overallStatus } = checkEntries(changeReqEntries, waitingEntries, approvalEntries) return codeOwners?.evaluation_entries?.length ? ( {codeOwners && ( )} {getString('codeOwner.title')} {message}