All files / modules/72-triggers/pages/triggers/utils WebhookTriggerConfigPanelUtils.tsx

83.58% Statements 56/67
75.71% Branches 53/70
81.82% Functions 9/11
81.36% Lines 48/59

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              5x 5x 5x 5x 5x   5x 5x 5x 5x   5x             5x             72x 3x 69x 17x 52x 52x         5x                                                 5x                                 76x                     3x   3x 1x 1x     3x 3x 3x     3x                                                                                       2x 2x 1x   1x                                                     5x   27x 9x 18x   18x 9x   9x     5x                     11x 27x       11x     5x                     7x         25x 7x 7x        
/*
 * Copyright 2022 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, { SetStateAction, Dispatch } from 'react'
import cx from 'classnames'
import { FormInput, SelectOption, Text, Container } from '@wings-software/uicore'
import { FontVariation, Color } from '@harness/design-system'
import { isEmpty, isUndefined } from 'lodash-es'
import type { StringKeys, UseStringsReturn } from 'framework/strings'
import { GitSourceProviders } from './TriggersListUtils'
import { ConnectorSection } from '../views/ConnectorSection'
import { eventTypes } from './TriggersWizardPageUtils'
import css from '../views/WebhookTriggerConfigPanel.module.scss'
 
export const autoAbortPreviousExecutionsTypes = [
  eventTypes.PUSH,
  eventTypes.PULL_REQUEST,
  eventTypes.ISSUE_COMMENT,
  eventTypes.MERGE_REQUEST
]
 
export const getAutoAbortDescription = ({
  event,
  getString
}: {
  event: string
  getString: (key: StringKeys) => string
}): string => {
  if (event === eventTypes.PUSH) {
    return getString('triggers.triggerConfigurationPanel.autoAbortPush')
  } else if (event === eventTypes.PULL_REQUEST || event === eventTypes.MERGE_REQUEST) {
    return getString('triggers.triggerConfigurationPanel.autoAbortPR')
  } else Eif (event === eventTypes.ISSUE_COMMENT) {
    return getString('triggers.triggerConfigurationPanel.autoAbortIssueComment')
  }
  return ''
}
 
export const handleSourceRepoChange = ({ e, formikProps }: { e: SelectOption; formikProps: any }): void => {
  if (e.value === GitSourceProviders.CUSTOM.value) {
    formikProps.setValues({
      ...formikProps.values,
      sourceRepo: e.value,
      connectorRef: undefined,
      repoName: '',
      actions: undefined,
      anyAction: false,
      secretToken: undefined
    })
  } else {
    formikProps.setValues({
      ...formikProps.values,
      sourceRepo: e.value,
      connectorRef: undefined,
      repoName: '',
      actions: undefined,
      anyAction: false,
      secretToken: undefined,
      headerConditions: undefined
    })
  }
}
 
export const renderNonCustomEventFields = ({
  sourceRepo,
  formikProps,
  event,
  eventOptions,
  getString,
  actionsOptions,
  actions
}: {
  sourceRepo?: string
  formikProps: any
  event?: string
  eventOptions: SelectOption[]
  getString: UseStringsReturn['getString']
  actionsOptions: SelectOption[]
  actions: SelectOption[]
}): JSX.Element => {
  return (
    <>
      {sourceRepo && <ConnectorSection formikProps={formikProps} />}
      <FormInput.Select
        className={cx(!event && css.bottomMarginZero)}
        key={event}
        label={getString('triggers.triggerConfigurationPanel.event')}
        name="event"
        placeholder={getString('triggers.triggerConfigurationPanel.eventPlaceholder')}
        items={eventOptions}
        onChange={e => {
          const additionalValues: any = {}
 
          if (e.value === eventTypes.PUSH) {
            additionalValues.sourceBranchOperator = undefined
            additionalValues.sourceBranchValue = undefined
          }
 
          Eif (e.value !== eventTypes.TAG) {
            additionalValues.tagConditionOperator = undefined
            additionalValues.tagConditionValue = undefined
          }
 
          formikProps.setValues({
            ...formikProps.values,
            event: e.value,
            actions: e.value === eventTypes.PUSH ? [] : undefined,
            anyAction: false,
            ...additionalValues
          })
        }}
      />
      {event && (
        <>
          {actionsOptions?.length !== 0 && (
            <div className={css.actionsContainer}>
              <div>
                <Text
                  font={{ variation: FontVariation.FORM_INPUT_TEXT, weight: 'semi-bold' }}
                  color={Color.GREY_600}
                  style={{ marginBottom: 'var(--spacing-xsmall)' }}
                >
                  {getString('triggers.triggerConfigurationPanel.actions')}
                </Text>
                <FormInput.MultiSelect
                  className={css.multiSelect}
                  name="actions"
                  items={actionsOptions}
                  // yaml design: empty array means selecting all
                  disabled={Array.isArray(actions) && isEmpty(actions)}
                  onChange={e => {
                    if (!e || (Array.isArray(e) && isEmpty(e))) {
                      formikProps.setFieldValue('actions', undefined)
                    } else {
                      formikProps.setFieldValue('actions', e)
                    }
                  }}
                />
              </div>
              <Container className={css.anyActionContainer}>
                <FormInput.CheckBox
                  name="anyAction"
                  key={Date.now()}
                  label={getString('triggers.triggerConfigurationPanel.anyActions')}
                  defaultChecked={(Array.isArray(actions) && actions.length === 0) || false}
                  className={css.checkboxAlignment}
                  onClick={(e: React.FormEvent<HTMLInputElement>) => {
                    formikProps.setFieldTouched('actions', true)
                    if (e.currentTarget?.checked) {
                      formikProps.setFieldValue('actions', [])
                    } else {
                      formikProps.setFieldValue('actions', undefined)
                    }
                  }}
                />
              </Container>
            </div>
          )}
          {autoAbortPreviousExecutionsTypes.includes(event) && (
            <>
              <FormInput.CheckBox
                style={{ position: 'relative', left: '0' }}
                name="autoAbortPreviousExecutions"
                label="Auto-abort Previous Execution"
                className={css.checkboxAlignment}
              />
              <Text className={css.autoAbortDescription}>{getAutoAbortDescription({ event, getString })}</Text>
            </>
          )}
        </>
      )}
    </>
  )
}
interface ActionsOptionsMapInterface {
  [key: string]: string[]
}
 
export const getEventLabelMap = (event: string): string => {
  // add space between camelcase-separated words
  if (event === eventTypes.PULL_REQUEST) {
    return 'Pull Request'
  } else Iif (event === eventTypes.MERGE_REQUEST) {
    return 'Merge Request'
  } else if (event === eventTypes.ISSUE_COMMENT) {
    return 'Issue Comment'
  }
  return event
}
 
export const getEventAndActions = ({
  data,
  sourceRepo
}: {
  data: {
    [key: string]: {
      [key: string]: string[]
    }
  }
  sourceRepo: string
}): { eventOptions: SelectOption[]; actionsOptionsMap: ActionsOptionsMapInterface } => {
  const filteredData = data?.[sourceRepo] || {}
  const eventOptions = Object.keys(filteredData).map(event => ({
    label: getEventLabelMap(event),
    value: event
  }))
  return { eventOptions, actionsOptionsMap: filteredData }
}
 
export const clearEventsAndActions = ({
  newActionsOptionsMap,
  formikProps,
  setActionsOptions,
  event
}: {
  newActionsOptionsMap: ActionsOptionsMapInterface
  formikProps: any
  setActionsOptions: Dispatch<SetStateAction<SelectOption[]>>
  event: string
}): void => {
  Iif (isUndefined(newActionsOptionsMap[event])) {
    formikProps.setFieldValue('event', undefined)
    return
  }
 
  const newActionsOptions = newActionsOptionsMap[event]?.map((val: string) => ({ label: val, value: val }))
  setActionsOptions(newActionsOptions)
  Iif (newActionsOptions?.length === 0) {
    formikProps.setFieldValue('actions', [])
  }
}