All files / modules/72-triggers/pages/triggers/views TriggersList.tsx

90.57% Statements 48/53
71.43% Branches 35/49
70% Functions 7/10
90.57% Lines 48/53

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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283              5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x   5x 5x 5x 5x         5x         5x   5x 5x   5x             5x 6x 5x   5x               5x 5x             5x                   5x 5x 5x                                     5x                         5x   5x 4x         5x 4x     4x       5x 1x                           5x                               5x                                                                                                                           5x           5x                                     1x                       8x                 2x                                  
/*
 * 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, { useState } from 'react'
import { Button, ButtonVariation, TextInput } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { useModalHook } from '@harness/use-modal'
import { useParams, useHistory } from 'react-router-dom'
import { useStrings } from 'framework/strings'
import { Page } from '@common/exports'
import routes from '@common/RouteDefinitions'
import { useGetTriggerListForTarget } from 'services/pipeline-ng'
import { useGetListOfBranchesWithStatus } from 'services/cd-ng'
import { useQueryParams } from '@common/hooks'
import { AddDrawer } from '@common/components'
import { DrawerContext } from '@common/components/AddDrawer/AddDrawer'
import type { GitQueryParams, PipelineType } from '@common/interfaces/RouteInterfaces'
import { usePermission } from '@rbac/hooks/usePermission'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import {
  manifestTypeIcons,
  manifestTypeLabels,
  ManifestDataType
} from '@pipeline/components/ManifestSelection/Manifesthelper'
import {
  ArtifactIconByType,
  ArtifactTitleIdByType,
  ENABLED_ARTIFACT_TYPES
} from '@pipeline/components/ArtifactsSelection/ArtifactHelper'
import { TriggersListSection, GoToEditWizardInterface } from './TriggersListSection'
 
import { TriggerTypes } from '../utils/TriggersWizardPageUtils'
import { getCategoryItems, ItemInterface, TriggerDataInterface } from '../utils/TriggersListUtils'
 
import css from './TriggersList.module.scss'
 
interface TriggersListPropsInterface {
  onNewTriggerClick: (val: TriggerDataInterface) => void
  isPipelineInvalid?: boolean
}
 
export default function TriggersList(props: TriggersListPropsInterface & GitQueryParams): JSX.Element {
  const { onNewTriggerClick, isPipelineInvalid } = props
  const { branch, repoIdentifier } = useQueryParams<GitQueryParams>()
 
  const { projectIdentifier, orgIdentifier, accountId, pipelineIdentifier, module } = useParams<
    PipelineType<{
      projectIdentifier: string
      orgIdentifier: string
      accountId: string
      pipelineIdentifier: string
    }>
  >()
  const [searchParam, setSearchParam] = useState('')
  const { getString } = useStrings()
 
  const {
    data: triggerListResponse,
    error,
    refetch,
    loading
  } = useGetTriggerListForTarget({
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier,
      targetIdentifier: pipelineIdentifier,
      searchTerm: searchParam
    },
    debounce: 300
  })
  const triggerList = triggerListResponse?.data?.content || undefined
  const history = useHistory()
  const [isEditable] = usePermission(
    {
      resourceScope: {
        projectIdentifier: projectIdentifier,
        orgIdentifier: orgIdentifier,
        accountIdentifier: accountId
      },
      resource: {
        resourceType: ResourceType.PIPELINE,
        resourceIdentifier: pipelineIdentifier
      },
      permissions: [PermissionIdentifier.EDIT_PIPELINE],
      options: {
        skipCache: true
      }
    },
    [projectIdentifier, orgIdentifier, accountId, pipelineIdentifier]
  )
 
  const { data: branchesWithStatusData, refetch: getDefaultBranchName } = useGetListOfBranchesWithStatus({
    queryParams: {
      accountIdentifier: accountId,
      orgIdentifier,
      projectIdentifier,
      yamlGitConfigIdentifier: repoIdentifier,
      page: 0,
      size: 20
    },
    lazy: true
  })
 
  // should be disabled when project is git sync enabled & the branch in the URL is not the default branch name
  const [incompatibleGitSyncBranch, setIncompatibleGitSyncBranch] = React.useState(false)
 
  React.useEffect(() => {
    Iif (repoIdentifier) {
      getDefaultBranchName()
    }
  }, [repoIdentifier])
 
  React.useEffect(() => {
    Iif (branchesWithStatusData?.data?.defaultBranch?.branchName !== branch) {
      setIncompatibleGitSyncBranch(true)
    } else {
      setIncompatibleGitSyncBranch(false)
    }
  }, [branchesWithStatusData])
 
  const goToEditWizard = ({ triggerIdentifier, triggerType }: GoToEditWizardInterface): void => {
    history.push(
      routes.toTriggersWizardPage({
        accountId,
        orgIdentifier,
        projectIdentifier,
        pipelineIdentifier,
        triggerIdentifier,
        triggerType,
        module,
        repoIdentifier,
        branch
      })
    )
  }
  const goToDetails = ({ triggerIdentifier }: GoToEditWizardInterface): void => {
    /* istanbul ignore next */
    history.push(
      routes.toTriggersDetailPage({
        accountId,
        orgIdentifier,
        projectIdentifier,
        pipelineIdentifier,
        triggerIdentifier,
        module,
        repoIdentifier,
        branch
      })
    )
  }
 
  const [openDrawer, hideDrawer] = useModalHook(() => {
    /* istanbul ignore next */
    const onSelect = (val: ItemInterface): void => {
      if (val?.categoryValue) {
        hideDrawer()
        onNewTriggerClick({
          triggerType: val.categoryValue,
          sourceRepo: (val.categoryValue === TriggerTypes.WEBHOOK && val.value) || undefined,
          artifactType: (val.categoryValue === TriggerTypes.ARTIFACT && val.value) || undefined,
          manifestType: (val.categoryValue === TriggerTypes.MANIFEST && val.value) || undefined
        })
      }
    }
 
    const categoryItems = getCategoryItems(getString)
    /* istanbul ignore next */
    categoryItems.categories.splice(
      1,
      0,
      {
        categoryLabel: getString('pipeline.artifactTriggerConfigPanel.artifact'),
        categoryValue: 'Artifact',
        items: [
          {
            itemLabel: getString(ArtifactTitleIdByType[ENABLED_ARTIFACT_TYPES.Gcr]),
            value: ENABLED_ARTIFACT_TYPES.Gcr,
            iconName: ArtifactIconByType.Gcr
          },
          {
            itemLabel: getString(ArtifactTitleIdByType[ENABLED_ARTIFACT_TYPES.Ecr]),
            value: ENABLED_ARTIFACT_TYPES.Ecr,
            iconName: ArtifactIconByType.Ecr
          },
          {
            itemLabel: getString(ArtifactTitleIdByType[ENABLED_ARTIFACT_TYPES.DockerRegistry]),
            value: ENABLED_ARTIFACT_TYPES.DockerRegistry,
            iconName: ArtifactIconByType.DockerRegistry
          }
        ]
      },
      {
        categoryLabel: getString('manifestsText'),
        categoryValue: 'Manifest',
        items: [
          {
            itemLabel: getString(manifestTypeLabels.HelmChart),
            value: ManifestDataType.HelmChart,
            iconName: manifestTypeIcons.HelmChart
          }
        ]
      }
    )
 
    return (
      <AddDrawer
        addDrawerMap={categoryItems}
        onSelect={onSelect}
        onClose={hideDrawer}
        drawerContext={DrawerContext.STUDIO}
      />
    )
  })
  const buttonProps = incompatibleGitSyncBranch
    ? {
        tooltip: getString('triggers.tooltip.defaultGitSyncBranchOnly')
      }
    : {}
 
  return (
    <>
      <Page.SubHeader>
        <Button
          disabled={!isEditable || incompatibleGitSyncBranch || isPipelineInvalid}
          tooltip={isPipelineInvalid ? getString('pipeline.cannotAddTriggerInvalidPipeline') : ''}
          text={getString('triggers.newTrigger')}
          variation={ButtonVariation.PRIMARY}
          onClick={openDrawer}
          {...buttonProps}
        ></Button>
        <TextInput
          leftIcon="thinner-search"
          leftIconProps={{ name: 'thinner-search', size: 14, color: Color.GREY_700 }}
          placeholder={getString('search')}
          data-name="search"
          wrapperClassName={css.searchWrapper}
          value={searchParam}
          onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
            setSearchParam(e.target.value.trim())
          }}
        />
      </Page.SubHeader>
 
      <Page.Body
        loading={loading}
        error={error?.message}
        retryOnError={() => refetch()}
        noData={
          !searchParam
            ? {
                when: () => (Array.isArray(triggerList) && triggerList.length === 0) || incompatibleGitSyncBranch,
                icon: 'yaml-builder-trigger',
                message: getString('triggers.aboutTriggers'),
                buttonText: getString('triggers.addNewTrigger'),
                onClick: openDrawer,
                buttonDisabled: !isEditable || incompatibleGitSyncBranch || isPipelineInvalid,
                buttonDisabledTooltip: isPipelineInvalid ? getString('pipeline.cannotAddTriggerInvalidPipeline') : ''
              }
            : {
                when: () => Array.isArray(triggerList) && triggerList.length === 0,
                icon: 'yaml-builder-trigger',
                message: getString('triggers.noTriggersFound')
              }
        }
      >
        <TriggersListSection
          data={triggerList}
          refetchTriggerList={refetch}
          goToEditWizard={goToEditWizard}
          goToDetails={goToDetails}
          isPipelineInvalid={isPipelineInvalid}
        />
      </Page.Body>
    </>
  )
}