All files / app/assets/javascripts/vue_merge_request_widget/components/approvals approvals_summary.vue

90.32% Statements 28/31
81.25% Branches 13/16
100% Functions 17/17
93.33% Lines 28/30

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                      6x                                                 29x           29x     25x     5x     29x           29x 4x                           25x 1x             24x     24x 1x     23x 9x     14x 14x           29x     24x 10x   14x         10x     10x         1x   1x         25x         2x     1x                                                                    
<script>
import { GlLink, GlPopover } from '@gitlab/ui';
import { toNounSeriesText } from '~/lib/utils/grammar';
import { n__, sprintf } from '~/locale';
import {
  APPROVED_BY_YOU_AND_OTHERS,
  APPROVED_BY_YOU,
  APPROVED_BY_OTHERS,
} from '~/vue_merge_request_widget/components/approvals/messages';
import UserAvatarList from '~/vue_shared/components/user_avatar/user_avatar_list.vue';
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
import { getApprovalRuleNamesLeft } from 'ee_else_ce/vue_merge_request_widget/mappers';
 
export default {
  components: {
    GlLink,
    GlPopover,
    UserAvatarList,
  },
  props: {
    multipleApprovalRulesAvailable: {
      type: Boolean,
      required: false,
      default: false,
    },
    approvalState: {
      type: Object,
      required: true,
    },
    disableCommittersApproval: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  data() {
    return {
      isUserAvatarListExpanded: false,
    };
  },
  computed: {
    approvers() {
      return this.approvalState.approvedBy?.nodes || [];
    },
    approved() {
      return this.approvalState.approved || this.approvalState.approvedBy?.nodes.length > 0;
    },
    approvalsLeft() {
      return this.approvalState.approvalsLeft || 0;
    },
    rulesLeft() {
      return getApprovalRuleNamesLeft(
        this.multipleApprovalRulesAvailable,
        (this.approvalState.approvalState?.rules || []).filter((r) => !r.approved),
      );
    },
    approvalLeftMessage() {
      if (this.rulesLeft.length) {
        return sprintf(
          n__(
            'Requires %{count} approval from %{names}.',
            'Requires %{count} approvals from %{names}.',
            this.approvalsLeft,
          ),
          {
            names: toNounSeriesText(this.rulesLeft),
            count: this.approvalsLeft,
          },
          false,
        );
      }
 
      if (!this.approved) {
        return n__(
          'Requires %d approval from eligible users.',
          'Requires %d approvals from eligible users.',
          this.approvalsLeft,
        );
      }
 
      return '';
    },
    message() {
      if (this.approvedByMe && this.approvedByOthers) {
        return APPROVED_BY_YOU_AND_OTHERS;
      }
 
      if (this.approvedByMe) {
        return APPROVED_BY_YOU;
      }
 
      if (this.approved) {
        return APPROVED_BY_OTHERS;
      }
 
      return '';
    },
    hasApprovers() {
      return Boolean(this.approvers.length);
    },
    approvedByMe() {
      if (!this.currentUserId) {
        return false;
      }
      return this.approvers.some(
        (approver) => getIdFromGraphQLId(approver.id) === this.currentUserId,
      );
    },
    approvedByOthers() {
      Iif (!this.currentUserId) {
        return false;
      }
      return this.approvers.some(
        (approver) => getIdFromGraphQLId(approver.id) !== this.currentUserId,
      );
    },
    currentUserHasCommitted() {
      Iif (!this.currentUserId) return false;
 
      return this.approvalState.committers?.nodes?.some(
        (user) => getIdFromGraphQLId(user.id) === this.currentUserId,
      );
    },
    currentUserId() {
      return gon.current_user_id;
    },
  },
  methods: {
    onUserAvatarListExpanded() {
      this.isUserAvatarListExpanded = true;
    },
    onUserAvatarListCollapsed() {
      this.isUserAvatarListExpanded = false;
    },
  },
};
</script>
 
<template>
  <div
    class="gl-display-flex gl-flex-wrap gl-align-items-center gl-gap-2"
    data-testid="approvals-summary-content"
  >
    <span class="gl-font-weight-bold">{{ approvalLeftMessage }}</span>
    <template v-if="hasApprovers">
      <span v-if="approvalLeftMessage">{{ message }}</span>
      <span v-else class="gl-font-weight-bold">{{ message }}</span>
      <user-avatar-list
        class="gl-display-inline-block"
        :class="{ 'gl-pt-1': isUserAvatarListExpanded }"
        :img-size="24"
        :items="approvers"
        @expanded="onUserAvatarListExpanded"
        @collapsed="onUserAvatarListCollapsed"
      />
    </template>
    <template v-if="disableCommittersApproval && currentUserHasCommitted">
      <gl-link id="cant-approve-popover" data-testid="commit-cant-approve" class="gl-cursor-help">{{
        __("Why can't I approve?")
      }}</gl-link>
      <gl-popover target="cant-approve-popover">
        {{ __("You can't approve because you added one or more commits to this merge request.") }}
      </gl-popover>
    </template>
  </div>
</template>