All files / modules/70-pipeline/pages/pipeline-details PipelineDetails.tsx

88.41% Statements 61/69
62.11% Branches 59/95
87.5% Functions 7/8
88.41% Lines 61/69

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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268              2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x 2x               2x   8x 8x 8x 8x 8x         8x                       8x                       8x 8x   8x 4x 3x       8x 4x 1x   3x       8x 4x                   4x     4x         4x     4x         4x     4x         4x     4x         8x 4x     8x           8x 4x 4x       8x 8x 8x                 8x                   8x       8x       8x                                                                                                                                                                                      
/*
 * 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 { isEmpty } from 'lodash-es'
import { Heading, Layout, TabNavigation } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { matchPath, useLocation, useParams, useRouteMatch } from 'react-router-dom'
import { Page } from '@common/exports'
import routes from '@common/RouteDefinitions'
import { useGlobalEventListener, useQueryParams } from '@common/hooks'
import { useGetPipelineSummary } from 'services/pipeline-ng'
import { useGetListOfBranchesWithStatus } from 'services/cd-ng'
import { NavigatedToPage } from '@common/constants/TrackingConstants'
import { useTelemetry } from '@common/hooks/useTelemetry'
import { useStrings, String } from 'framework/strings'
import { useAppStore } from 'framework/AppStore/AppStoreContext'
import { GitSyncStoreProvider } from 'framework/GitRepoStore/GitSyncStoreContext'
import type { GitQueryParams, PipelinePathProps, PipelineType } from '@common/interfaces/RouteInterfaces'
import { DefaultNewPipelineId } from '@pipeline/components/PipelineStudio/PipelineContext/PipelineActions'
import GitPopover from '@pipeline/components/GitPopover/GitPopover'
import GenericErrorHandler from '@common/pages/GenericErrorHandler/GenericErrorHandler'
import { NGBreadcrumbs } from '@common/components/NGBreadcrumbs/NGBreadcrumbs'
import NoEntityFound from '../utils/NoEntityFound/NoEntityFound'
import css from './PipelineDetails.module.scss'
// add custom event to the global scope
declare global {
  interface WindowEventMap {
    RENAME_PIPELINE: CustomEvent<string>
  }
}
 
export default function PipelineDetails({ children }: React.PropsWithChildren<unknown>): React.ReactElement {
  const { orgIdentifier, projectIdentifier, pipelineIdentifier, accountId, module } =
    useParams<PipelineType<PipelinePathProps>>()
  const { isGitSyncEnabled } = useAppStore()
  const location = useLocation()
  const { trackEvent } = useTelemetry()
  const { branch, repoIdentifier } = useQueryParams<GitQueryParams>()
  const {
    data: pipeline,
    refetch,
    error
  } = useGetPipelineSummary({
    pipelineIdentifier,
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier,
      branch,
      repoIdentifier
    },
    lazy: true
  })
 
  const { data: branchesWithStatusData, refetch: getDefaultBranchName } = useGetListOfBranchesWithStatus({
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier,
      yamlGitConfigIdentifier: repoIdentifier,
      page: 0,
      size: 20
    },
    lazy: true
  })
 
  const [pipelineName, setPipelineName] = React.useState('')
  const [triggerTabDisabled, setTriggerTabDisabled] = React.useState(false)
 
  React.useEffect(() => {
    if (repoIdentifier) {
      getDefaultBranchName()
    }
  }, [repoIdentifier])
 
  React.useEffect(() => {
    if (branch && branchesWithStatusData?.data?.defaultBranch?.branchName !== branch) {
      setTriggerTabDisabled(true)
    } else {
      setTriggerTabDisabled(false)
    }
  }, [branchesWithStatusData])
 
  React.useEffect(() => {
    const routeParams = {
      orgIdentifier,
      projectIdentifier,
      pipelineIdentifier,
      accountId,
      module,
      repoIdentifier,
      branch
    }
    // Pipeline View
    const isPipeLineStudioView = !!matchPath(location.pathname, {
      path: routes.toPipelineStudio(routeParams)
    })
    Iif (isPipeLineStudioView) {
      return trackEvent(NavigatedToPage.PipelineStudio, {})
    }
 
    // Inout View
    const isInputSetsView = !!matchPath(location.pathname, {
      path: routes.toInputSetList(routeParams)
    })
    Iif (isInputSetsView) {
      return trackEvent(NavigatedToPage.PipelineInputSet, {})
    }
 
    // Triggers View
    const isTriggersView = !!matchPath(location.pathname, {
      path: routes.toTriggersPage(routeParams)
    })
    Iif (isTriggersView) {
      return trackEvent(NavigatedToPage.PipelineTriggers, {})
    }
 
    // Execution History View
    const isExecutionHistoryView = !!matchPath(location.pathname, {
      path: routes.toPipelineDeploymentList(routeParams)
    })
    Iif (isExecutionHistoryView) {
      return trackEvent(NavigatedToPage.PipelineExecutionHistory, {})
    }
  }, [location.pathname])
 
  React.useEffect(() => {
    setPipelineName(pipeline?.data?.name || '')
  }, [pipeline?.data?.name])
 
  useGlobalEventListener('RENAME_PIPELINE', event => {
    if (event.detail) {
      setPipelineName(event.detail)
    }
  })
 
  React.useEffect(() => {
    Eif (pipelineIdentifier !== DefaultNewPipelineId) {
      refetch()
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [pipelineIdentifier])
  const { getString } = useStrings()
  const getBreadCrumbs = React.useCallback(
    () => [
      {
        url: routes.toPipelines({ orgIdentifier, projectIdentifier, accountId, module }),
        label: getString('pipelineBreadcrumb')
      }
    ],
    [accountId, getString, module, orgIdentifier, projectIdentifier]
  )
 
  const { isExact: isPipelineStudioRoute } = useRouteMatch(
    routes.toPipelineStudio({
      orgIdentifier,
      projectIdentifier,
      pipelineIdentifier,
      accountId,
      module
    })
  ) || { isExact: false }
 
  Iif (error?.data && !isGitSyncEnabled) {
    return <GenericErrorHandler errStatusCode={error?.status} errorMessage={(error?.data as Error)?.message} />
  }
 
  Iif (error?.data && isEmpty(pipeline) && isGitSyncEnabled) {
    return <NoEntityFound identifier={pipelineIdentifier} entityType={'pipeline'} />
  }
 
  return (
    <div className={css.wrapper}>
      <GitSyncStoreProvider>
        <Page.Header
          className={isPipelineStudioRoute ? css.rightMargin : ''}
          testId={isPipelineStudioRoute ? 'pipeline-studio' : 'not-pipeline-studio'}
          size={isPipelineStudioRoute ? 'small' : 'standard'}
          title={
            <Layout.Vertical>
              <Layout.Horizontal>
                <NGBreadcrumbs links={getBreadCrumbs()} />
              </Layout.Horizontal>
              {isPipelineStudioRoute && (
                <String tagName="div" className={css.pipelineStudioTitle} stringID="pipelineStudio" />
              )}
              {!isPipelineStudioRoute && (
                <Layout.Horizontal spacing="xsmall" flex={{ justifyContent: 'left', alignItems: 'center' }}>
                  <Heading level={2} color={Color.GREY_800} font={{ weight: 'bold' }}>
                    {pipelineName}
                  </Heading>
                  {repoIdentifier && (
                    <GitPopover data={{ repoIdentifier, branch }} iconProps={{ margin: { left: 'small' } }} />
                  )}
                </Layout.Horizontal>
              )}
            </Layout.Vertical>
          }
          toolbar={
            <TabNavigation
              size={'small'}
              links={[
                {
                  label: getString('pipelineStudio'),
                  to: routes.toPipelineStudio({
                    orgIdentifier,
                    projectIdentifier,
                    pipelineIdentifier,
                    accountId,
                    module,
                    repoIdentifier,
                    branch
                  })
                },
                {
                  label: getString('inputSetsText'),
                  to: routes.toInputSetList({
                    orgIdentifier,
                    projectIdentifier,
                    pipelineIdentifier,
                    accountId,
                    module,
                    repoIdentifier,
                    branch
                  }),
                  disabled: pipelineIdentifier === DefaultNewPipelineId
                },
                {
                  label: getString('common.triggersLabel'),
                  to: routes.toTriggersPage({
                    orgIdentifier,
                    projectIdentifier,
                    pipelineIdentifier,
                    accountId,
                    module,
                    repoIdentifier,
                    branch
                  }),
                  disabled: pipelineIdentifier === DefaultNewPipelineId || triggerTabDisabled
                },
                {
                  label: getString('executionHeaderText'),
                  to: routes.toPipelineDeploymentList({
                    orgIdentifier,
                    projectIdentifier,
                    pipelineIdentifier,
                    accountId,
                    module,
                    repoIdentifier,
                    branch
                  }),
                  disabled: pipelineIdentifier === DefaultNewPipelineId
                }
              ]}
            />
          }
        />
      </GitSyncStoreProvider>
      <Page.Body className={isPipelineStudioRoute ? css.rightMargin : ''}>{children}</Page.Body>
    </div>
  )
}