All files / modules/75-ci/components/CIStageDetails CIStageDetails.tsx

100% Statements 32/32
72.83% Branches 67/92
100% Functions 7/7
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167              1x 1x 1x 1x   1x   1x 1x 1x             1x   1x 1x   1x 1x 1x 1x 1x 1x   1x 1x   1x   1x           1x       1x 1x                                   1x                   1x 1x 1x       1x               2x                                 13x                                                                                                                
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import React, { useEffect, useMemo } from 'react'
import { useParams } from 'react-router-dom'
import { get } from 'lodash-es'
import { Container, Button, Text, Layout } from '@wings-software/uicore'
import type { PipelineExecutionSummary } from 'services/pipeline-ng'
import { useReportSummary, useGetToken } from 'services/ti-service'
import type { StageDetailProps } from '@pipeline/factories/ExecutionFactory/types'
import { useExecutionContext } from '@pipeline/context/ExecutionContext'
import { String as StrTemplate } from 'framework/strings'
import {
  getStageSetupIds,
  getStageNodesWithArtifacts,
  getArtifactGroups
} from '@pipeline/pages/execution/ExecutionArtifactsView/ExecutionArtifactsView'
import type { Artifact } from '@pipeline/pages/execution/ExecutionArtifactsView/ArtifactsComponent/ArtifactsComponent'
 
import css from './CIStageDetails.module.scss'
 
export function CIStageDetails(props: StageDetailProps): React.ReactElement {
  const context = useExecutionContext()
 
  const executionSummary = get(context, 'pipelineExecutionDetail.pipelineExecutionSummary')
  const executionGraph = get(context, 'pipelineExecutionDetail.executionGraph')
  const stageSetupIds = getStageSetupIds(executionSummary as PipelineExecutionSummary)
  const stageNodes = getStageNodesWithArtifacts(executionGraph as any, stageSetupIds)
  const artifactGroups = getArtifactGroups(
    stageNodes.filter(({ identifier }) => identifier === props.stage.nodeIdentifier)
  )
  const artifacts: Artifact[] = []
  artifactGroups.forEach(artifactGroup => artifacts.push(...artifactGroup.artifacts))
 
  const status = (context?.pipelineExecutionDetail?.pipelineExecutionSummary?.status || '').toUpperCase()
 
  const { accountId, orgIdentifier, projectIdentifier } = useParams<{
    projectIdentifier: string
    orgIdentifier: string
    accountId: string
  }>()
 
  const { data: serviceToken } = useGetToken({
    queryParams: { accountId }
  })
 
  const queryParams = useMemo(
    () => ({
      accountId,
      orgId: orgIdentifier,
      projectId: projectIdentifier,
      pipelineId: context?.pipelineExecutionDetail?.pipelineExecutionSummary?.pipelineIdentifier || '',
      buildId: String(context?.pipelineExecutionDetail?.pipelineExecutionSummary?.runSequence || ''),
      stageId: props.stage.nodeIdentifier as string
    }),
    [
      accountId,
      orgIdentifier,
      projectIdentifier,
      context?.pipelineExecutionDetail?.pipelineExecutionSummary?.pipelineIdentifier,
      context?.pipelineExecutionDetail?.pipelineExecutionSummary?.runSequence,
      props.stage.nodeIdentifier
    ]
  )
 
  const { data: reportSummary, refetch: fetchReportSummary } = useReportSummary({
    queryParams: { ...queryParams, report: 'junit' as const },
    lazy: true,
    requestOptions: {
      headers: {
        'X-Harness-Token': serviceToken || ''
      }
    }
  })
 
  useEffect(() => {
    Eif (status && serviceToken) {
      fetchReportSummary()
    }
  }, [status, serviceToken])
 
  return (
    <div className={css.container}>
      <div className={css.main}>
        {artifacts && artifacts.length > 0 && (
          <div>
            <StrTemplate className={css.title} tagName="div" stringID="artifactOrArtifacts" />
            <ul className={css.values}>
              {artifacts.slice(0, 2).map(({ type, image, tag, url }, index) => (
                <li key={url}>
                  <Button
                    className={css.artifact}
                    href={url}
                    target="_blank"
                    rel="noreferrer"
                    tooltip={<Container padding="small">{type === 'Image' ? `${image}:${tag}` : url}</Container>}
                    noStyling
                  >
                    {type === 'Image' ? `${image}:${tag}` : url}
                  </Button>
                  {index === 1 && artifacts.length > 2 && (
                    <Text
                      className={css.artifact}
                      tooltip={
                        <Container padding="small">
                          {artifacts.slice(2).map(artifact => (
                            <React.Fragment key={artifact.url}>
                              <Button
                                className={css.artifact}
                                href={artifact.url}
                                target="_blank"
                                rel="noreferrer"
                                tooltip={
                                  <Container padding="small">
                                    {artifact.type === 'Image' ? `${artifact.image}:${artifact.tag}` : artifact.url}
                                  </Container>
                                }
                                noStyling
                              >
                                {artifact.type === 'Image' ? `${artifact.image}:${artifact.tag}` : artifact.url}
                              </Button>
                              <br />
                            </React.Fragment>
                          ))}
                        </Container>
                      }
                      style={{ width: 'auto' }}
                    >
                      {`+${artifacts.length - 2}`}
                    </Text>
                  )}
                </li>
              ))}
            </ul>
          </div>
        )}
        {!!reportSummary?.total_tests && (
          <div>
            <StrTemplate className={css.title} tagName="div" stringID="ci.testSummary" />
            <Layout.Horizontal spacing="small" style={{ marginBottom: 'var(--spacing-1)' }}>
              <div className={css.testSummaryItem}>
                <StrTemplate tagName="div" stringID="total" />:<span>{reportSummary.total_tests}</span>
              </div>
              <div className={css.testSummaryItem}>
                <StrTemplate tagName="div" stringID="pipeline.testsReports.skipped" />:
                <span>{reportSummary.skipped_tests}</span>
              </div>
            </Layout.Horizontal>
            <Layout.Horizontal spacing="small">
              <div className={css.testSummaryItem}>
                <StrTemplate tagName="div" stringID="ci.successful" />:<span>{reportSummary.successful_tests}</span>
              </div>
              <div className={css.testSummaryItem}>
                <StrTemplate tagName="div" stringID="failed" />:<span>{reportSummary.failed_tests}</span>
              </div>
            </Layout.Horizontal>
          </div>
        )}
      </div>
    </div>
  )
}