All files / modules/70-pipeline/pages/execution/ExecutionLandingPage/ExecutionTabs ExecutionTabs.tsx

87.32% Statements 62/71
82.42% Branches 75/91
100% Functions 3/3
87.32% Lines 62/71

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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249              2x 2x 2x 2x   2x 2x 2x       2x 2x 2x 2x 2x   2x                   2x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x   21x   21x 21x 21x 21x   21x       21x     2x   2x             21x 9x     9x 3x   6x     6x     6x     6x     6x     6x     6x     6x     6x     6x     6x     6x       6x     21x                               21x 21x                             21x                               21x 2x                         2x                                 21x 7x                             21x                                       21x                                
/*
 * 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 { HarnessDocTooltip, Icon, Tabs } from '@wings-software/uicore'
import { Switch } from '@blueprintjs/core'
import { NavLink, useParams, useLocation, matchPath } from 'react-router-dom'
 
import routes from '@common/RouteDefinitions'
import { useQueryParams, useUpdateQueryParams } from '@common/hooks'
import { accountPathProps, executionPathProps, pipelineModuleParams } from '@common/utils/routeUtils'
import type { ExecutionPathProps, PipelineType } from '@common/interfaces/RouteInterfaces'
import type { CIWebhookInfoDTO } from 'services/ci'
import type { ExecutionQueryParams } from '@pipeline/utils/executionUtils'
import { useExecutionContext } from '@pipeline/context/ExecutionContext'
import { useStrings } from 'framework/strings'
import { useFeatureFlag } from '@common/hooks/useFeatureFlag'
import { FeatureFlag } from '@common/featureFlags'
import css from './ExecutionTabs.module.scss'
 
const TAB_ID_MAP = {
  PIPELINE: 'pipeline_view',
  INPUTS: 'inputs_view',
  ARTIFACTS: 'artifacts_view',
  COMMITS: 'commits_view',
  TESTS: 'tests_view',
  POLICY_EVALUATIONS: 'policy_evaluations',
  STO_SECURITY: 'sto_security'
}
 
export default function ExecutionTabs(props: React.PropsWithChildren<unknown>): React.ReactElement {
  const [selectedTabId, setSelectedTabId] = React.useState('')
  const { children } = props
  const { getString } = useStrings()
  const { pipelineExecutionDetail, isPipelineInvalid } = useExecutionContext()
  const params = useParams<PipelineType<ExecutionPathProps>>()
  const location = useLocation()
  const { view } = useQueryParams<ExecutionQueryParams>()
  const { updateQueryParams } = useUpdateQueryParams<ExecutionQueryParams>()
  const opaBasedGovernanceEnabled = useFeatureFlag(FeatureFlag.OPA_PIPELINE_GOVERNANCE)
  const stoCDPipelineSecurityEnabled = useFeatureFlag(FeatureFlag.STO_CD_PIPELINE_SECURITY)
  const stoCIPipelineSecurityEnabled = useFeatureFlag(FeatureFlag.STO_CI_PIPELINE_SECURITY)
 
  const routeParams = { ...accountPathProps, ...executionPathProps, ...pipelineModuleParams }
  // const isGraphView = !view || view === 'graph'
  const isLogView = view === 'log'
  const isCD = params.module === 'cd'
  const isCI = params.module === 'ci'
  const isCIInPipeline = pipelineExecutionDetail?.pipelineExecutionSummary?.moduleInfo?.ci
 
  const ciData = pipelineExecutionDetail?.pipelineExecutionSummary?.moduleInfo?.ci
    ?.ciExecutionInfoDTO as CIWebhookInfoDTO
  // NOTE: hide commits tab if there are no commits
  // by default we are showing Commits tab > 'isEmpty(pipelineExecutionDetail)'
  const ciShowCommitsTab = !!ciData?.branch?.commits?.length || !!ciData?.pullRequest?.commits?.length
 
  function handleLogViewChange(e: React.ChangeEvent<HTMLInputElement>): void {
    const { checked } = e.target as HTMLInputElement
 
    updateQueryParams({
      view: checked ? 'log' : 'graph',
      filterAnomalous: 'false',
      type: getString('pipeline.verification.analysisTab.metrics')
    })
  }
 
  React.useEffect(() => {
    const isPipeLineView = !!matchPath(location.pathname, {
      path: routes.toExecutionPipelineView(routeParams)
    })
    if (isPipeLineView) {
      return setSelectedTabId(TAB_ID_MAP.PIPELINE)
    }
    const isInputsView = !!matchPath(location.pathname, {
      path: routes.toExecutionInputsView(routeParams)
    })
    Iif (isInputsView) {
      return setSelectedTabId(TAB_ID_MAP.INPUTS)
    }
    const isArtifactsView = !!matchPath(location.pathname, {
      path: routes.toExecutionArtifactsView(routeParams)
    })
    Iif (isArtifactsView) {
      return setSelectedTabId(TAB_ID_MAP.ARTIFACTS)
    }
    const isCommitsView = !!matchPath(location.pathname, {
      path: routes.toExecutionCommitsView(routeParams)
    })
    Iif (isCommitsView) {
      return setSelectedTabId(TAB_ID_MAP.COMMITS)
    }
    const isTestsView = !!matchPath(location.pathname, {
      path: routes.toExecutionTestsView(routeParams)
    })
    Iif (isTestsView) {
      return setSelectedTabId(TAB_ID_MAP.TESTS)
    }
    const isPolicyEvaluationsView = !!matchPath(location.pathname, {
      path: routes.toExecutionPolicyEvaluationsView(routeParams)
    })
    Iif (isPolicyEvaluationsView) {
      return setSelectedTabId(TAB_ID_MAP.POLICY_EVALUATIONS)
    }
    const isSecurityView = !!matchPath(location.pathname, {
      path: routes.toExecutionSecurityView(routeParams)
    })
    Iif (isSecurityView) {
      return setSelectedTabId(TAB_ID_MAP.STO_SECURITY)
    }
    // Defaults to Pipelines Tab
    return setSelectedTabId(TAB_ID_MAP.PIPELINE)
  }, [location.pathname])
 
  const tabList = [
    {
      id: TAB_ID_MAP.PIPELINE,
      title: (
        <NavLink
          to={routes.toExecutionPipelineView(params) + location.search}
          className={css.tabLink}
          activeClassName={css.activeLink}
        >
          <Icon name="alignment-vertical-center" size={16} />
          <span>{getString('common.pipeline')}</span>
        </NavLink>
      )
    }
  ]
 
  Eif (!isPipelineInvalid) {
    tabList.push({
      id: TAB_ID_MAP.INPUTS,
      title: (
        <NavLink
          to={routes.toExecutionInputsView(params) + location.search}
          className={css.tabLink}
          activeClassName={css.activeLink}
        >
          <Icon name="manually-entered-data" size={16} />
          <span>{getString('inputs')}</span>
        </NavLink>
      )
    })
  }
 
  Iif (opaBasedGovernanceEnabled) {
    tabList.push({
      id: TAB_ID_MAP.POLICY_EVALUATIONS,
      title: (
        <NavLink
          to={routes.toExecutionPolicyEvaluationsView(params) + location.search}
          className={css.tabLink}
          activeClassName={css.activeLink}
        >
          <Icon name="governance" size={16} />
          <span>{getString('pipeline.policyEvaluations.title')}</span>
        </NavLink>
      )
    })
  }
 
  if (isCI) {
    tabList.push({
      id: TAB_ID_MAP.ARTIFACTS,
      title: (
        <NavLink
          to={routes.toExecutionArtifactsView(params) + location.search}
          className={css.tabLink}
          activeClassName={css.activeLink}
        >
          <Icon name="add-to-artifact" size={16} />
          <span>{getString('artifacts')}</span>
        </NavLink>
      )
    })
    Iif (ciShowCommitsTab) {
      tabList.push({
        id: TAB_ID_MAP.COMMITS,
        title: (
          <NavLink
            to={routes.toExecutionCommitsView(params) + location.search}
            className={css.tabLink}
            activeClassName={css.activeLink}
          >
            <Icon name="git-commit" size={16} />
            <span>{getString('commits')}</span>
          </NavLink>
        )
      })
    }
  }
 
  if (isCI || isCIInPipeline) {
    tabList.push({
      id: TAB_ID_MAP.TESTS,
      title: (
        <NavLink
          to={routes.toExecutionTestsView(params) + location.search}
          className={css.tabLink}
          activeClassName={css.activeLink}
        >
          <Icon name="lab-test" size={16} />
          <span>{getString('tests')}</span>
        </NavLink>
      )
    })
  }
 
  Iif (
    (isCD && stoCDPipelineSecurityEnabled) ||
    (isCI && stoCIPipelineSecurityEnabled) ||
    localStorage.STO_PIPELINE_SECURITY_ENABLED
  ) {
    tabList.push({
      id: TAB_ID_MAP.STO_SECURITY,
      title: (
        <NavLink
          to={routes.toExecutionSecurityView(params) + location.search}
          className={css.tabLink}
          activeClassName={css.activeLink}
        >
          <Icon name="sto-grey" size={16} />
          <span>{getString('pipeline.security.title')}</span>
        </NavLink>
      )
    })
  }
 
  return (
    <div className={css.main}>
      <div>
        <Tabs id="execution-tabs" selectedTabId={selectedTabId} renderAllTabPanels={false} tabList={tabList} />
      </div>
      <div className={css.children}>{children}</div>
      {selectedTabId === TAB_ID_MAP.PIPELINE ? (
        <div className={css.viewToggle}>
          <span data-tooltip-id="consoleViewToggle">{getString('consoleView')}</span>
          <Switch checked={isLogView} name="console-view-toggle" onChange={handleLogViewChange} />
          <HarnessDocTooltip tooltipId="consoleViewToggle" useStandAlone={true} />
        </div>
      ) : null}
    </div>
  )
}