All files / modules/70-pipeline/pages/execution/ExecutionArtifactsView ExecutionArtifactsView.tsx

96.36% Statements 53/55
80.83% Branches 97/120
86.67% Functions 13/15
95.92% Lines 47/49

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              3x 3x 3x 3x 3x 3x   3x 3x 3x 3x   3x 3x   3x 3x     3x       4x 45x 45x 18x     45x       3x 3x 3x 2x     19x           3x 2x   3x         3x               3x 1x 1x 1x 1x 2x       2x     1x 1x     1x                       3x 1x 1x 1x 1x 1x 1x 1x                                
/*
 * 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 from 'react'
import { get } from 'lodash-es'
import { useParams, useHistory } from 'react-router-dom'
import { Text, Select, Container } from '@wings-software/uicore'
import qs from 'qs'
import { useExecutionContext } from '@pipeline/context/ExecutionContext'
import type { ExecutionNode, PipelineExecutionSummary, ExecutionGraph } from 'services/pipeline-ng'
import { useQueryParams } from '@common/hooks'
import routes from '@common/RouteDefinitions'
import { String } from 'framework/strings'
import ArtifactsComponent from './ArtifactsComponent/ArtifactsComponent'
import type { ArtifactGroup } from './ArtifactsComponent/ArtifactsComponent'
import artifactsEmptyState from './images/artifacts_empty_state.svg'
import css from './ExecutionArtifactsView.module.scss'
 
export const getStageSetupIds: (data: PipelineExecutionSummary) => string[] = data => {
  return Object.keys(data?.layoutNodeMap ?? {})
}
 
export const getStageNodesWithArtifacts: (data: ExecutionGraph, stageIds: string[]) => ExecutionNode[] = (
  data,
  stageIds
) => {
  return Object.values(data?.nodeMap ?? {}).filter(entry => {
    const { setupId = '', outcomes = [] } = entry
    const outcomeWithArtifacts = Array.isArray(outcomes)
      ? outcomes?.some((outcome: any) => outcome.fileArtifacts?.length || outcome.imageArtifacts?.length)
      : outcomes?.integrationStageOutcome?.fileArtifacts?.length ||
        outcomes?.integrationStageOutcome?.imageArtifacts?.length
    return stageIds.includes(setupId) && outcomeWithArtifacts
  })
}
 
export const getArtifactGroups: (stages: ExecutionNode[]) => ArtifactGroup[] = stages => {
  return stages.map(node => {
    const outcomeWithImageArtifacts = Array.isArray(node.outcomes)
      ? node.outcomes?.find(outcome => outcome.imageArtifacts)
      : node.outcomes?.integrationStageOutcome
    const imageArtifacts =
      outcomeWithImageArtifacts?.imageArtifacts?.map((artifact: any) => ({
        image: artifact.imageName,
        tag: artifact.tag,
        type: 'Image',
        url: artifact.url
      })) ?? []
    const outcomeWithFileArtifacts = Array.isArray(node.outcomes)
      ? node.outcomes?.find(outcome => outcome.fileArtifacts)
      : node.outcomes?.integrationStageOutcome
    const fileArtifacts = outcomeWithFileArtifacts?.fileArtifacts // TODO: fix typing once BE type is available
      ?.map((artifact: any) => ({
        type: 'File',
        url: artifact.url
      }))
    return {
      name: node.name!,
      icon: 'pipeline-deploy',
      artifacts: imageArtifacts.concat(fileArtifacts)
    }
  })
}
 
export function StageSelector(props: { layoutNodeMap?: PipelineExecutionSummary['layoutNodeMap'] }) {
  const history = useHistory()
  const params = useParams<any>()
  const query = useQueryParams<any>()
  const setupIds = Object.keys(props?.layoutNodeMap ?? {})
  const options = setupIds.map(value => ({
    value,
    label: props.layoutNodeMap![value].name!
  }))
  const selectedOption = options.find(option => option.value === query.stage)
 
  // Need to have a selected change by default when we are opening a page
  Eif (!selectedOption && options.length > 0) {
    history.push(routes.toExecutionArtifactsView(params) + '?' + qs.stringify({ ...query, stage: options[0].value }))
  }
 
  return (
    <Select
      className={css.stageSelector}
      value={selectedOption}
      items={options}
      onChange={val => {
        history.push(routes.toExecutionArtifactsView(params) + '?' + qs.stringify({ ...query, stage: val.value }))
      }}
    />
  )
}
 
export default function ExecutionArtifactsView(): 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)
  return (
    <div className={css.wrapper}>
      <StageSelector layoutNodeMap={executionSummary?.layoutNodeMap} />
      {artifactGroups.length ? (
        <ArtifactsComponent artifactGroups={artifactGroups} />
      ) : (
        <Container className={css.emptyArtifacts}>
          <img src={artifactsEmptyState} />
          <Text>
            <String stringID="pipeline.artifactTriggerConfigPanel.noArtifacts" />
          </Text>
        </Container>
      )}
    </div>
  )
}