All files / modules/70-pipeline/utils statusHelpers.ts

100% Statements 58/58
93.02% Branches 40/43
100% Functions 24/24
100% Lines 56/56

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              131x                               131x                                               131x   131x 4476x   4476x     131x 719x 719x               131x 60x     131x 448x 448x     131x 412x     131x 415x 415x     131x 141x     131x 197x         131x 197x     131x 185x     131x 219x     131x 313x     131x 347x     131x 358x 358x     131x 175x     131x 211x     131x 212x     131x 236x     131x 64x     131x 302x                 131x 184x                   131x 17x         131x 74x                         131x 75x              
/*
 * 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 { camelCase } from 'lodash-es'
import type { ExecutionSummaryInfo } from 'services/pipeline-ng'
 
export type ExecutionStatus = Exclude<
  Required<ExecutionSummaryInfo>['lastExecutionStatus'],
  'NOT_STARTED' | 'INTERVENTION_WAITING' | 'APPROVAL_WAITING' | 'APPROVAL_REJECTED' | 'WAITING'
>
 
/**
 * Statues are to be grouped as follows:
 * Running -> Running, AsyncWaiting, TaskWaiting, TimedWaiting
 * Failed -> Failed, Errored
 * Aborted -> Discontinuing, Aborted,
 * Success -> Success, IgnoreFailed
 */
 
export const ExecutionStatusEnum: Readonly<Record<ExecutionStatus, ExecutionStatus>> = {
  Aborted: 'Aborted',
  Expired: 'Expired',
  Failed: 'Failed',
  NotStarted: 'NotStarted',
  Paused: 'Paused',
  Queued: 'Queued',
  Running: 'Running',
  Success: 'Success',
  Suspended: 'Suspended',
  ResourceWaiting: 'ResourceWaiting',
  AsyncWaiting: 'AsyncWaiting',
  Skipped: 'Skipped',
  TaskWaiting: 'TaskWaiting',
  TimedWaiting: 'TimedWaiting',
  Errored: 'Errored',
  IgnoreFailed: 'IgnoreFailed',
  Discontinuing: 'Discontinuing',
  ApprovalRejected: 'ApprovalRejected',
  InterventionWaiting: 'InterventionWaiting',
  ApprovalWaiting: 'ApprovalWaiting',
  Pausing: 'Pausing'
}
 
export const EXECUTION_STATUS: readonly ExecutionStatus[] = Object.keys(ExecutionStatusEnum) as ExecutionStatus[]
 
const changeCase = (status?: string): string => {
  const temp = camelCase(status)
 
  return temp.charAt(0).toUpperCase() + temp.slice(1)
}
 
export function isExecutionRunning(status?: string): boolean {
  const st = changeCase(status)
  return (
    st === ExecutionStatusEnum.Running ||
    st === ExecutionStatusEnum.AsyncWaiting ||
    st === ExecutionStatusEnum.TimedWaiting ||
    st === ExecutionStatusEnum.TaskWaiting
  )
}
 
export function isExecutionIgnoreFailed(status?: string): boolean {
  return changeCase(status) === ExecutionStatusEnum.IgnoreFailed
}
 
export function isExecutionFailed(status?: string): boolean {
  const st = changeCase(status)
  return st === ExecutionStatusEnum.Failed || st === ExecutionStatusEnum.Errored
}
 
export function isExecutionExpired(status?: string): boolean {
  return changeCase(status) === ExecutionStatusEnum.Expired
}
 
export function isExecutionAborted(status?: string): boolean {
  const st = changeCase(status)
  return st === ExecutionStatusEnum.Aborted || st === ExecutionStatusEnum.Discontinuing
}
 
export function isExecutionQueued(status?: string): boolean {
  return changeCase(status) === ExecutionStatusEnum.Queued
}
 
export function isExecutionWaiting(status?: string): boolean {
  return (
    isExecutionOnlyWaiting(status) || isExecutionWaitingForApproval(status) || isExecutionWaitingForIntervention(status)
  )
}
 
export function isExecutionOnlyWaiting(status?: string): boolean {
  return changeCase(status) === ExecutionStatusEnum.ResourceWaiting
}
 
export function isExecutionWaitingForApproval(status?: string): boolean {
  return changeCase(status) === ExecutionStatusEnum.ApprovalWaiting
}
 
export function isExecutionWaitingForIntervention(status?: string): boolean {
  return changeCase(status) === ExecutionStatusEnum.InterventionWaiting
}
 
export function isExecutionPaused(status?: string): boolean {
  return changeCase(status) === ExecutionStatusEnum.Paused
}
 
export function isExecutionNotStarted(status?: string): boolean {
  return changeCase(status) === ExecutionStatusEnum.NotStarted
}
 
export function isExecutionSuccess(status?: string): boolean {
  const st = changeCase(status)
  return st === ExecutionStatusEnum.Success || st === ExecutionStatusEnum.IgnoreFailed
}
 
export function isExecutionSuspended(status?: string): boolean {
  return changeCase(status) === ExecutionStatusEnum.Suspended
}
 
export function isExecutionPausing(status?: string): boolean {
  return changeCase(status) === ExecutionStatusEnum.Pausing
}
 
export function isExecutionApprovalRejected(status?: string): boolean {
  return changeCase(status) === ExecutionStatusEnum.ApprovalRejected
}
 
export function isExecutionComplete(status?: string): boolean {
  return isExecutionSuccess(status) || isExecutionCompletedWithBadState(status)
}
 
export function isExecutionSkipped(status?: string): boolean {
  return changeCase(status) === ExecutionStatusEnum.Skipped
}
 
export function isExecutionCompletedWithBadState(status?: string): boolean {
  return (
    isExecutionAborted(status) ||
    isExecutionExpired(status) ||
    isExecutionFailed(status) ||
    isExecutionSuspended(status) ||
    isExecutionApprovalRejected(status)
  )
}
 
export function isExecutionActive(status?: string): boolean {
  return (
    isExecutionPaused(status) ||
    isExecutionPausing(status) ||
    isExecutionRunning(status) ||
    isExecutionNotStarted(status) ||
    isExecutionWaiting(status) ||
    isExecutionQueued(status)
  )
}
 
export function isExecutionRunningLike(status?: string): boolean {
  return (
    isExecutionRunning(status) || isExecutionPaused(status) || isExecutionPausing(status) || isExecutionWaiting(status)
  )
}
 
export const isExecutionFinishedAnyhow = (status?: string): boolean => {
  return (
    isExecutionFailed(status) ||
    isExecutionAborted(status) ||
    isExecutionExpired(status) ||
    isExecutionSuspended(status) ||
    isExecutionApprovalRejected(status) ||
    isExecutionSuccess(status) ||
    isExecutionComplete(status) ||
    isExecutionSkipped(status) ||
    isExecutionCompletedWithBadState(status)
  )
}
 
export const isRetryPipelineAllowed = (status?: string): boolean => {
  return (
    isExecutionFailed(status) ||
    isExecutionExpired(status) ||
    isExecutionApprovalRejected(status) ||
    isExecutionAborted(status)
  )
}