All files / modules/70-pipeline/factories/FeatureRestrictionBannersFactory FeatureRestrictionBannersFactory.tsx

85% Statements 34/40
52% Branches 26/50
100% Functions 5/5
85% Lines 34/40

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              1x   1x   1x 1x 1x           1x 1x             1x                                                                                                                                                 1x         15x 15x                   1x 9x 9x 8x 8x     8x   15x 15x   15x 26x 26x                                         26x                   24x 24x 24x             24x 7x 7x     26x       8x 7x     7x         1x    
/*
 * 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 type { Module } from '@common/interfaces/RouteInterfaces'
import { useFeatures } from '@common/hooks/useFeatures'
import type { CheckFeaturesReturn } from 'framework/featureStore/featureStoreUtil'
import { FeatureIdentifier } from 'framework/featureStore/FeatureIdentifier'
import FeatureWarningCommonBanner from '@common/components/FeatureWarning/FeatureWarningCommonBanner'
import {
  FeatureWarningTheme,
  RedirectButton,
  DisplayBanner,
  getDismissBannerKey
} from '@common/components/FeatureWarning/FeatureWarningCommonBannerUtils'
import { useStrings } from 'framework/strings'
import {
  getBannerDependencyMet,
  getQualifiedEnforcedBanner,
  ModuleToFeatureMapValue
} from './FeatureRestrictionBannersFactoryUtils'
 
// If multiple limits within a FeatureIdentifier, show higher limit first in the array
const ModuleToFeatureMap: Record<string, Record<string, ModuleToFeatureMapValue[]>> = {
  cd: {
    SERVICES: [
      {
        limit: 5,
        bannerKey: getDismissBannerKey({ featureIdentifier: FeatureIdentifier.SERVICES, limit: 5 }),
        limitCrossedMessage: 'pipeline.featureRestriction.serviceLimitExceeded',
        theme: FeatureWarningTheme.UPGRADE_REQUIRED
      }
    ]
  },
  ci: {
    MAX_TOTAL_BUILDS: [
      {
        limit: 2250,
        bannerKey: getDismissBannerKey({ featureIdentifier: FeatureIdentifier.MAX_TOTAL_BUILDS, limit: 2250 }),
        limitCrossedMessage: 'pipeline.featureRestriction.maxTotalBuilds90PercentLimit',
        theme: FeatureWarningTheme.INFO,
        redirectButtons: [RedirectButton.VIEW_USAGE_LINK, RedirectButton.EXPLORE_PLANS]
      }
    ],
    MAX_BUILDS_PER_MONTH: [
      {
        limit: 100,
        bannerKey: getDismissBannerKey({ featureIdentifier: FeatureIdentifier.MAX_BUILDS_PER_MONTH, limit: 100 }),
        limitCrossedMessage: 'pipeline.featureRestriction.maxBuildsPerMonth100PercentLimit',
        theme: FeatureWarningTheme.UPGRADE_REQUIRED,
        redirectButtons: [RedirectButton.VIEW_USAGE_LINK, RedirectButton.EXPLORE_PLANS],
        dependency: {
          MAX_TOTAL_BUILDS: { enabled: false }
        }
      },
      {
        limit: 0,
        bannerKey: getDismissBannerKey({ featureIdentifier: FeatureIdentifier.MAX_BUILDS_PER_MONTH, limit: 0 }),
        limitCrossedMessage: 'pipeline.featureRestriction.numMonthlyBuilds',
        theme: FeatureWarningTheme.INFO,
        redirectButtons: [RedirectButton.VIEW_USAGE_LINK, RedirectButton.EXPLORE_PLANS],
        dependency: {
          MAX_TOTAL_BUILDS: { enabled: false }
        }
      }
    ],
    ACTIVE_COMMITTERS: [
      {
        limitPercent: 100,
        bannerKey: getDismissBannerKey({ featureIdentifier: FeatureIdentifier.ACTIVE_COMMITTERS, limitPercent: 100 }),
        limitCrossedMessage: 'pipeline.featureRestriction.subscriptionExceededLimit',
        theme: FeatureWarningTheme.OVERUSE,
        redirectButtons: [RedirectButton.MANAGE_SUBSCRIPTION],
        dependency: {
          MAX_TOTAL_BUILDS: { enabled: true },
          MAX_BUILDS_PER_MONTH: { enabled: true }
        }
      },
      {
        limitPercent: 90,
        bannerKey: getDismissBannerKey({ featureIdentifier: FeatureIdentifier.ACTIVE_COMMITTERS, limitPercent: 90 }),
        limitCrossedMessage: 'pipeline.featureRestriction.subscription90PercentLimit',
        theme: FeatureWarningTheme.INFO,
        redirectButtons: [RedirectButton.MANAGE_SUBSCRIPTION],
        dependency: {
          MAX_TOTAL_BUILDS: { enabled: true },
          MAX_BUILDS_PER_MONTH: { enabled: true }
        }
      }
    ]
  },
  cf: {},
  cv: {},
  ce: {}
}
 
const getFeatureRestrictionDetailsForModule = (
  module: Module,
  featureIdentifier: FeatureIdentifier
): ModuleToFeatureMapValue[] | undefined => {
  // return the above map value for the supplied module 'key'
  const fromMap = ModuleToFeatureMap[module]
  return fromMap && fromMap[featureIdentifier]
}
 
interface FeatureRestrictionBannersProps {
  module: Module
  featureNames?: FeatureIdentifier[] // order will determine which banner will appear
  getCustomMessageString?: (features: CheckFeaturesReturn) => string
}
 
// Show this banner if limit usage is breached for the feature
export function FeatureRestrictionBanners(props: FeatureRestrictionBannersProps): JSX.Element | null {
  const { getString } = useStrings()
  const { module, featureNames = [] } = props
  const banners: DisplayBanner[] = []
  const { features } = useFeatures({ featuresRequest: { featureNames } })
 
  // only 1 banner will be shown for this module
  featureNames.some(featureName => {
    // Get the above map details
    const featureRestrictionModuleDetails = getFeatureRestrictionDetailsForModule(module, featureName)
    const { featureDetail } = features.get(featureName) || {}
    // check for the first message that would appear
    return featureRestrictionModuleDetails?.some((uiDisplayBanner: ModuleToFeatureMapValue) => {
      let bannerAdded = false
      Iif (
        featureDetail?.enabled === false &&
        featureDetail?.moduleType === module.toUpperCase() &&
        uiDisplayBanner.theme === FeatureWarningTheme.UPGRADE_REQUIRED
      ) {
        // when feature is not allowed (typically free account) and upgrade banner should be shown
        // moduleType necessary since sometimes enabled===false and no moduleType exists
        const dependency = uiDisplayBanner?.dependency
        const dependencyMet = getBannerDependencyMet({ features, featureName, dependency })
        const messageString = uiDisplayBanner?.limitCrossedMessage && getString(uiDisplayBanner.limitCrossedMessage)
        if (dependencyMet && messageString) {
          banners.push({
            featureName,
            isFeatureRestrictionAllowedForModule: featureDetail.enabled,
            theme: uiDisplayBanner.theme,
            redirectButtons: uiDisplayBanner.redirectButtons,
            bannerKey: uiDisplayBanner.bannerKey,
            messageString
          })
          bannerAdded = true
        }
      } else if (featureDetail?.enabled) {
        /*
          Show the banner if
          1. Feature enforcement enabled
          2. If dependency exists and matches
          
          getQualifiedEnforcedBanner
          3. Usage limit | percent uiDisplayBanner is breached
          4. Message is present in the above map value
        */
        const dependency = uiDisplayBanner?.dependency
        const dependencyMet = getBannerDependencyMet({ features, dependency })
        const addBanner = getQualifiedEnforcedBanner({
          dependencyMet,
          featureDetail,
          uiDisplayBanner,
          getString,
          featureName
        })
        if (addBanner) {
          banners.push(addBanner)
          bannerAdded = true
        }
      }
      return bannerAdded
    })
  })
  // Awaiting PM/UX for multiple banners or aggregated single banner
  if (banners.length) {
    return (
      <>
        {banners.map(banner => (
          <FeatureWarningCommonBanner key={banner.messageString} banner={banner} />
        ))}
      </>
    )
  }
  return null
}