All files / modules/70-pipeline/components/RunPipelineModal VisualView.tsx

95.74% Statements 45/47
89.09% Branches 98/110
100% Functions 9/9
95.74% Lines 45/47

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              31x 31x 31x 31x 31x   31x 31x     31x 31x   31x 31x   31x                                                   31x                                     189x 188x   188x 188x 5x 183x                       59x       188x 106x     188x 124x     188x 124x     188x 7x     188x   188x             1x 1x 1x 1x   1x     1x                                                         6x                                                                                                         98x 98x   98x                       98x 78x 78x   78x                               20x    
/*
 * 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, { Dispatch, FormEvent, SetStateAction } from 'react'
import cx from 'classnames'
import { FormikForm, Layout, PageSpinner, Text } from '@harness/uicore'
import { parse } from 'yaml'
import { defaultTo } from 'lodash-es'
 
import { useStrings } from 'framework/strings'
import { GitSyncStoreProvider } from 'framework/GitRepoStore/GitSyncStoreContext'
import type { ResponseInputSetTemplateWithReplacedExpressionsResponse } from 'services/pipeline-ng'
import type { PipelineInfoConfig } from 'services/cd-ng'
import SelectExistingInputsOrProvideNew from './SelectExistingOrProvide'
import { InputSetSelector } from '../InputSetSelector/InputSetSelector'
import type { InputSetValue } from '../InputSetSelector/utils'
import { PipelineInputSetForm } from '../PipelineInputSetForm/PipelineInputSetForm'
import { StepViewType } from '../AbstractSteps/Step'
 
import css from './RunPipelineForm.module.scss'
 
export type ExistingProvide = 'existing' | 'provide'
 
export interface VisualViewProps {
  executionView?: boolean
  existingProvide: ExistingProvide
  setExistingProvide: Dispatch<SetStateAction<ExistingProvide>>
  setRunClicked: Dispatch<SetStateAction<boolean>>
  selectedInputSets?: InputSetValue[]
  pipelineIdentifier: string
  executionInputSetTemplateYaml?: string
  template: ResponseInputSetTemplateWithReplacedExpressionsResponse | null
  pipeline?: PipelineInfoConfig
  resolvedPipeline?: PipelineInfoConfig
  currentPipeline?: {
    pipeline?: PipelineInfoConfig
  }
  getTemplateError: any
  submitForm(): void
  hasInputSets: boolean
  setSelectedInputSets: Dispatch<SetStateAction<InputSetValue[] | undefined>>
  loading?: boolean
  loadingMergeInputSetUpdate: boolean
}
 
export default function VisualView(props: VisualViewProps): React.ReactElement {
  const {
    executionView,
    existingProvide,
    setExistingProvide,
    selectedInputSets,
    pipelineIdentifier,
    executionInputSetTemplateYaml,
    template,
    pipeline,
    currentPipeline,
    getTemplateError,
    resolvedPipeline,
    setRunClicked,
    submitForm,
    hasInputSets,
    setSelectedInputSets,
    loading,
    loadingMergeInputSetUpdate
  } = props
  const { getString } = useStrings()
 
  const checkIfRuntimeInputsNotPresent = (): string | undefined => {
    if (executionView && !executionInputSetTemplateYaml) {
      return getString('pipeline.inputSets.noRuntimeInputsWhileExecution')
    } else if (
      !executionView &&
      resolvedPipeline &&
      currentPipeline &&
      !template?.data?.inputSetTemplateYaml &&
      !getTemplateError
    ) {
      /*
      We don't have any runtime inputs required for running this pipeline
        - if API doesn't fail and
        - the inputSetTemplateYaml is not present
      */
      return getString('runPipelineForm.noRuntimeInput')
    }
  }
 
  const showInputSetSelector = (): boolean => {
    return !!(pipeline && currentPipeline && template?.data?.inputSetTemplateYaml && existingProvide === 'existing')
  }
 
  const showPipelineInputSetForm = (): boolean => {
    return !!(existingProvide === 'provide' || selectedInputSets?.length || executionView)
  }
 
  const showVoidPipelineInputSetForm = (): boolean => {
    return !!(existingProvide === 'existing' && selectedInputSets?.length)
  }
 
  const onExistingProvideRadioChange = (ev: FormEvent<HTMLInputElement>): void => {
    setExistingProvide((ev.target as HTMLInputElement).value as ExistingProvide)
  }
 
  const noRuntimeInputs = checkIfRuntimeInputsNotPresent()
 
  return (
    <div
      className={cx(executionView ? css.runModalFormContentExecutionView : css.runModalFormContent, {
        [css.noRuntimeInput]: (template as any)?.data?.replacedExpressions?.length > 0 && noRuntimeInputs
      })}
      data-testid="runPipelineVisualView"
      onKeyDown={ev => {
        Eif (ev.key === 'Enter') {
          ev.preventDefault()
          ev.stopPropagation()
          setRunClicked(true)
 
          Iif ((!selectedInputSets || selectedInputSets.length === 0) && existingProvide === 'existing') {
            setExistingProvide('provide')
          } else {
            submitForm()
          }
        }
      }}
    >
      <FormikForm>
        {noRuntimeInputs ? (
          <Layout.Horizontal padding="medium" margin="medium">
            <Text>{noRuntimeInputs}</Text>
          </Layout.Horizontal>
        ) : (
          <>
            {hasInputSets ? (
              <>
                {executionView ? null : (
                  <Layout.Vertical
                    className={css.pipelineHeader}
                    padding={{ top: 'xlarge', left: 'xlarge', right: 'xlarge' }}
                  >
                    <SelectExistingInputsOrProvideNew
                      existingProvide={existingProvide}
                      onExistingProvideRadioChange={onExistingProvideRadioChange}
                    />
 
                    {showInputSetSelector() ? (
                      <GitSyncStoreProvider>
                        <InputSetSelector
                          pipelineIdentifier={pipelineIdentifier}
                          onChange={inputsets => {
                            setSelectedInputSets(inputsets)
                          }}
                          value={selectedInputSets}
                        />
                      </GitSyncStoreProvider>
                    ) : null}
                  </Layout.Vertical>
                )}
              </>
            ) : null}
            {showPipelineInputSetForm() ? (
              <PipelineInputSetFormWrapper
                executionView={executionView}
                loading={loading}
                existingProvide={existingProvide}
                currentPipeline={currentPipeline}
                executionInputSetTemplateYaml={executionInputSetTemplateYaml}
                template={template}
                resolvedPipeline={resolvedPipeline}
                loadingMergeInputSetUpdate={loadingMergeInputSetUpdate}
              />
            ) : null}
            {showVoidPipelineInputSetForm() ? <div className={css.noPipelineInputSetForm} /> : null}
          </>
        )}
      </FormikForm>
    </div>
  )
}
 
export interface PipelineInputSetFormWrapperProps {
  loading?: boolean
  executionView?: boolean
  existingProvide: ExistingProvide
  currentPipeline?: {
    pipeline?: PipelineInfoConfig
  }
  executionInputSetTemplateYaml?: string
  template: ResponseInputSetTemplateWithReplacedExpressionsResponse | null
  resolvedPipeline?: PipelineInfoConfig
  loadingMergeInputSetUpdate: boolean
}
 
function PipelineInputSetFormWrapper(props: PipelineInputSetFormWrapperProps): React.ReactElement | null {
  const {
    loading,
    executionView,
    existingProvide,
    currentPipeline,
    executionInputSetTemplateYaml,
    template,
    resolvedPipeline,
    loadingMergeInputSetUpdate
  } = props
  const { getString } = useStrings()
 
  Iif (loading) {
    return (
      <PageSpinner
        className={css.inputSetsUpdatingSpinner}
        message={
          loadingMergeInputSetUpdate
            ? getString('pipeline.inputSets.applyingInputSets')
            : getString('pipeline.inputSets.applyingInputSet')
        }
      />
    )
  }
  if (currentPipeline?.pipeline && resolvedPipeline && template?.data?.inputSetTemplateYaml) {
    let templateSource = executionView ? executionInputSetTemplateYaml : template.data.inputSetTemplateYaml
    templateSource = defaultTo(templateSource, '')
 
    return (
      <>
        {existingProvide === 'existing' ? <div className={css.divider} /> : null}
        <PipelineInputSetForm
          originalPipeline={resolvedPipeline}
          template={parse(templateSource)?.pipeline}
          readonly={executionView}
          path=""
          viewType={StepViewType.DeploymentForm}
          isRunPipelineForm
          maybeContainerClass={existingProvide === 'provide' ? css.inputSetFormRunPipeline : ''}
        />
      </>
    )
  }
 
  return null
}