All files / modules/72-templates-library/utils templatesUtils.ts

88.46% Statements 23/26
92.86% Branches 13/14
80% Functions 4/5
90.91% Lines 20/22

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              61x   61x       61x 61x 61x 61x 61x 61x 61x 61x 61x     61x               61x       19x             61x         31x                                                                         31x               31x     61x 8x            
/*
 * 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 { isEmpty } from 'lodash-es'
import type { UseStringsReturn } from 'framework/strings'
import { Scope } from '@common/interfaces/SecretsInterface'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import type { TemplateSummaryResponse } from 'services/template-ng'
 
export enum TemplateType {
  Step = 'Step',
  Stage = 'Stage',
  Pipeline = 'Pipeline',
  Service = 'Service',
  Infrastructure = 'Infrastructure',
  StepGroup = 'StepGroup',
  Execution = 'Execution',
  MonitoredService = 'MonitoredService'
}
 
export const AllTemplatesTypes = 'All'
 
export interface TemplateTypeOption {
  label: string
  value: string
  disabled?: boolean
}
 
export const getScopeBasedQueryParams = (
  { accountId, projectIdentifier, orgIdentifier }: ProjectPathProps,
  scope: Scope
) => {
  return {
    accountIdentifier: accountId,
    projectIdentifier: scope === Scope.PROJECT ? projectIdentifier : undefined,
    orgIdentifier: scope === Scope.PROJECT || scope === Scope.ORG ? orgIdentifier : undefined
  }
}
 
export const getAllowedTemplateTypes = (
  getString: UseStringsReturn['getString'],
  module?: string,
  isPipelineTemplateEnabled?: boolean
): TemplateTypeOption[] => {
  const AllowedTemplateTypes = [
    {
      label: getString('step'),
      value: TemplateType.Step,
      disabled: false
    },
    {
      label: getString('templatesLibrary.stageTemplate'),
      value: TemplateType.Stage,
      disabled: false
    },
    {
      label: getString('common.pipeline'),
      value: TemplateType.Pipeline,
      disabled: !isPipelineTemplateEnabled
    },
    {
      label: getString('service'),
      value: TemplateType.Service,
      disabled: true
    },
    {
      label: getString('infrastructureText'),
      value: TemplateType.Infrastructure,
      disabled: true
    },
    {
      label: getString('stepGroup'),
      value: TemplateType.StepGroup,
      disabled: true
    },
    {
      label: getString('executionText'),
      value: TemplateType.Execution,
      disabled: true
    }
  ]
  Iif (module === 'cv') {
    AllowedTemplateTypes.push({
      label: getString('connectors.cdng.monitoredService.label'),
      value: TemplateType.MonitoredService,
      disabled: false
    })
    return AllowedTemplateTypes.sort((a, b) => Number(a.disabled) - Number(b.disabled))
  }
  return AllowedTemplateTypes
}
 
export const getVersionLabelText = (template: TemplateSummaryResponse, getString: UseStringsReturn['getString']) => {
  return isEmpty(template.versionLabel)
    ? getString('templatesLibrary.alwaysUseStableVersion')
    : template.stableTemplate
    ? getString('templatesLibrary.stableVersion', { entity: template.versionLabel })
    : template.versionLabel
}