All files / ee/app/assets/javascripts/escalation_policies/components escalation_policy.vue

9.52% Statements 2/21
0% Branches 0/11
6.25% Functions 1/16
10.52% Lines 2/19

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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264                                                    2x                         2x                                                                                                                                                                                                                                                                                                                                                                                                                                                                
<script>
import {
  GlModalDirective,
  GlTooltipDirective,
  GlButton,
  GlButtonGroup,
  GlCard,
  GlSprintf,
  GlIcon,
  GlCollapse,
  GlToken,
  GlAvatar,
} from '@gitlab/ui';
import { s__, __, sprintf } from '~/locale';
import {
  ACTIONS,
  ALERT_STATUSES,
  EMAIL_ONCALL_SCHEDULE_USER,
  deleteEscalationPolicyModalId,
  editEscalationPolicyModalId,
  EMAIL_USER,
} from '../constants';
import { getParticipantsWithTokenStyles, getEscalationUserIndex } from '../utils';
import EditEscalationPolicyModal from './add_edit_escalation_policy_modal.vue';
import DeleteEscalationPolicyModal from './delete_escalation_policy_modal.vue';
 
export const i18n = {
  editPolicy: s__('EscalationPolicies|Edit escalation policy'),
  deletePolicy: s__('EscalationPolicies|Delete escalation policy'),
  escalationRuleCondition: s__(
    'EscalationPolicies|%{clockIcon} IF alert is not %{alertStatus} in %{minutes}',
  ),
  escalationRuleAction: s__(
    'EscalationPolicies|%{notificationIcon} THEN %{doAction} %{forScheduleOrUser}',
  ),
  minutes: s__('EscalationPolicies|mins'),
  noRules: s__('EscalationPolicies|This policy has no escalation rules.'),
};
 
const isRuleValid = ({ status, elapsedTimeMinutes, oncallSchedule, user }) =>
  Object.keys(ALERT_STATUSES).includes(status) &&
  typeof elapsedTimeMinutes === 'number' &&
  (typeof oncallSchedule?.name === 'string' || typeof user?.username === 'string');
 
export default {
  i18n,
  ACTIONS,
  ALERT_STATUSES,
  EMAIL_ONCALL_SCHEDULE_USER,
  components: {
    GlButton,
    GlButtonGroup,
    GlCard,
    GlSprintf,
    GlIcon,
    GlCollapse,
    GlToken,
    GlAvatar,
    DeleteEscalationPolicyModal,
    EditEscalationPolicyModal,
  },
  directives: {
    GlModal: GlModalDirective,
    GlTooltip: GlTooltipDirective,
  },
  props: {
    policy: {
      type: Object,
      required: true,
      validator: ({ name, rules }) => {
        return typeof name === 'string' && Array.isArray(rules) && rules.every(isRuleValid);
      },
    },
    index: {
      type: Number,
      required: true,
    },
  },
  data() {
    return {
      isPolicyVisible: this.index === 0,
      bodyClass: '',
    };
  },
  computed: {
    policyVisibleChevronIcon() {
      return this.isPolicyVisible ? 'chevron-lg-down' : 'chevron-lg-right';
    },
    policyVisibleChevronIconLabel() {
      return this.isPolicyVisible ? __('Collapse') : __('Expand');
    },
    editPolicyModalId() {
      return `${editEscalationPolicyModalId}-${this.policy.id}`;
    },
    deletePolicyModalId() {
      return `${deleteEscalationPolicyModalId}-${this.policy.id}`;
    },
    mappedParticipants() {
      return getParticipantsWithTokenStyles(this.policy.rules);
    },
  },
  methods: {
    hasEscalationSchedule(rule) {
      return rule.oncallSchedule?.iid;
    },
    hasEscalationUser(rule) {
      return rule.user?.username;
    },
    getBackgroundStyle(rule) {
      const userIndex = getEscalationUserIndex(this.mappedParticipants, rule.user.username);
      return this.mappedParticipants[userIndex].style;
    },
    getTextClass(rule) {
      const userIndex = getEscalationUserIndex(this.mappedParticipants, rule.user.username);
      return this.mappedParticipants[userIndex].class;
    },
    getActionName(rule) {
      return (this.hasEscalationSchedule(rule)
        ? ACTIONS[EMAIL_ONCALL_SCHEDULE_USER]
        : ACTIONS[EMAIL_USER]
      ).toLowerCase();
    },
    getArrowLength(index) {
      // each next rule arrow's length will be +4% of the container width
      // the first arrow's length is 4% and the 10th is 40%
      const length = (index + 1) * 4;
      return `${length}%`;
    },
    getActionTooltip(rule) {
      return sprintf(i18n.escalationRuleAction, {
        notificationIcon: '',
        doAction: this.getActionName(rule),
        forScheduleOrUser: this.hasEscalationSchedule(rule)
          ? rule.oncallSchedule.name
          : rule.user.name,
      });
    },
  },
};
</script>
 
<template>
  <div>
    <gl-card
      class="gl-mt-5"
      :class="{ 'gl-border-bottom-0': !isPolicyVisible }"
      :body-class="bodyClass"
      :header-class="{ 'gl-py-3': true, 'gl-rounded-base': !isPolicyVisible }"
    >
      <template #header>
        <div class="gl-display-flex gl-align-items-center">
          <gl-button
            v-gl-tooltip
            class="gl-mr-2 gl-p-0!"
            :title="policyVisibleChevronIconLabel"
            :aria-label="policyVisibleChevronIconLabel"
            category="tertiary"
            @click="isPolicyVisible = !isPolicyVisible"
          >
            <gl-icon :size="12" :name="policyVisibleChevronIcon" />
          </gl-button>
 
          <h3 class="gl-font-weight-bold gl-font-lg gl-m-0">{{ policy.name }}</h3>
          <gl-button-group class="gl-ml-auto">
            <gl-button
              v-gl-modal="editPolicyModalId"
              v-gl-tooltip
              :title="$options.i18n.editPolicy"
              icon="pencil"
              :aria-label="$options.i18n.editPolicy"
            />
            <gl-button
              v-gl-modal="deletePolicyModalId"
              v-gl-tooltip
              :title="$options.i18n.deletePolicy"
              :aria-label="$options.i18n.deletePolicy"
              icon="remove"
            />
          </gl-button-group>
        </div>
      </template>
      <gl-collapse
        :visible="isPolicyVisible"
        @hidden="bodyClass = 'gl-p-0'"
        @show="bodyClass = 'gl-p-5'"
      >
        <p v-if="policy.description" class="gl-text-gray-500 gl-mb-5">
          {{ policy.description }}
        </p>
        <div class="gl-border-solid gl-border-1 gl-border-gray-100 gl-rounded-base gl-p-5">
          <div v-if="!policy.rules.length" class="gl-text-red-500">
            <gl-icon name="status_warning" class="gl-mr-3" /> {{ $options.i18n.noRules }}
          </div>
          <template v-else>
            <div
              v-for="(rule, ruleIndex) in policy.rules"
              :key="rule.id"
              :class="{ 'gl-mb-5': ruleIndex !== policy.rules.length - 1 }"
              class="gl-display-flex gl-align-items-center escalation-rule-row"
            >
              <span class="rule-condition gl-md-w-full">
                <gl-sprintf :message="$options.i18n.escalationRuleCondition">
                  <template #clockIcon>
                    <gl-icon name="clock" class="gl-mr-3" />
                  </template>
                  <template #alertStatus>
                    {{ $options.ALERT_STATUSES[rule.status].toLowerCase() }}
                  </template>
                  <template #minutes>
                    <span class="gl-font-weight-bold">
                      {{ rule.elapsedTimeMinutes }}&nbsp;{{ $options.i18n.minutes }}
                    </span>
                  </template>
                </gl-sprintf>
              </span>
 
              <span
                class="right-arrow gl-relative gl-min-w-7 gl-bg-gray-900 gl-display-none gl-lg-display-block gl-flex-shrink-0 gl-mx-5"
                :style="{ width: getArrowLength(ruleIndex) }"
              >
                <i class="right-arrow-head gl-absolute gl-p-1 gl-border-solid"></i>
              </span>
 
              <span class="gl-display-flex gl-align-items-center gl-min-w-0">
                <span v-gl-tooltip class="gl-text-truncate" :title="getActionTooltip(rule)">
                  <gl-sprintf :message="$options.i18n.escalationRuleAction">
                    <template #notificationIcon>
                      <gl-icon name="notifications" class="gl-mr-3" />
                    </template>
                    <template #doAction>
                      {{ getActionName(rule) }}
                    </template>
                    <template #forScheduleOrUser>
                      <span v-if="hasEscalationSchedule(rule)" class="gl-font-weight-bold">
                        {{ rule.oncallSchedule.name }}
                      </span>
                      <gl-token
                        v-else-if="hasEscalationUser(rule)"
                        view-only
                        :style="getBackgroundStyle(rule)"
                        :class="getTextClass(rule)"
                      >
                        <gl-avatar :src="rule.user.avatarUrl" :size="16" />
                        {{ rule.user.name }}
                      </gl-token>
                    </template>
                  </gl-sprintf>
                </span>
              </span>
            </div>
          </template>
        </div>
      </gl-collapse>
    </gl-card>
 
    <delete-escalation-policy-modal :escalation-policy="policy" :modal-id="deletePolicyModalId" />
    <edit-escalation-policy-modal
      :escalation-policy="policy"
      :modal-id="editPolicyModalId"
      is-edit-mode
    />
  </div>
</template>