summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_merge_request_widget/components/states/mr_widget_conflicts.vue
blob: 2335e2984e4f0426162e32cff68b9dda0d1264a6 (plain)
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
<script>
import { GlButton, GlModalDirective, GlSkeletonLoader } from '@gitlab/ui';
import $ from 'jquery';
import { escape } from 'lodash';
import { s__, sprintf } from '~/locale';
import { mouseenter, debouncedMouseleave, togglePopover } from '~/shared/popover';
import glFeatureFlagMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import mergeRequestQueryVariablesMixin from '../../mixins/merge_request_query_variables';
import userPermissionsQuery from '../../queries/permissions.query.graphql';
import conflictsStateQuery from '../../queries/states/conflicts.query.graphql';
import StatusIcon from '../mr_widget_status_icon.vue';

export default {
  name: 'MRWidgetConflicts',
  components: {
    GlSkeletonLoader,
    StatusIcon,
    GlButton,
  },
  directives: {
    GlModalDirective,
  },
  mixins: [glFeatureFlagMixin(), mergeRequestQueryVariablesMixin],
  apollo: {
    userPermissions: {
      query: userPermissionsQuery,
      skip() {
        return !this.glFeatures.mergeRequestWidgetGraphql;
      },
      variables() {
        return this.mergeRequestQueryVariables;
      },
      update: (data) => data.project.mergeRequest.userPermissions,
    },
    stateData: {
      query: conflictsStateQuery,
      skip() {
        return !this.glFeatures.mergeRequestWidgetGraphql;
      },
      variables() {
        return this.mergeRequestQueryVariables;
      },
      update: (data) => data.project.mergeRequest,
    },
  },
  props: {
    /* TODO: This is providing all store and service down when it
      only needs a few props */
    mr: {
      type: Object,
      required: true,
      default: () => ({}),
    },
  },
  data() {
    return {
      userPermissions: {},
      stateData: {},
    };
  },
  computed: {
    isLoading() {
      return (
        this.glFeatures.mergeRequestWidgetGraphql &&
        this.$apollo.queries.userPermissions.loading &&
        this.$apollo.queries.stateData.loading
      );
    },
    canPushToSourceBranch() {
      if (this.glFeatures.mergeRequestWidgetGraphql) {
        return this.userPermissions.pushToSourceBranch;
      }

      return this.mr.canPushToSourceBranch;
    },
    canMerge() {
      if (this.glFeatures.mergeRequestWidgetGraphql) {
        return this.userPermissions.canMerge;
      }

      return this.mr.canMerge;
    },
    shouldBeRebased() {
      if (this.glFeatures.mergeRequestWidgetGraphql) {
        return this.stateData.shouldBeRebased;
      }

      return this.mr.shouldBeRebased;
    },
    sourceBranchProtected() {
      if (this.glFeatures.mergeRequestWidgetGraphql) {
        return this.stateData.sourceBranchProtected;
      }

      return this.mr.sourceBranchProtected;
    },
    popoverTitle() {
      return s__(
        'mrWidget|This feature merges changes from the target branch to the source branch. You cannot use this feature since the source branch is protected.',
      );
    },
    showResolveButton() {
      return this.mr.conflictResolutionPath && this.canPushToSourceBranch;
    },
    showPopover() {
      return this.showResolveButton && this.sourceBranchProtected;
    },
  },
  watch: {
    showPopover: {
      handler(newVal) {
        if (newVal) {
          this.$nextTick(this.initPopover);
        }
      },
      immediate: true,
    },
  },
  methods: {
    initPopover() {
      const $el = $(this.$refs.popover);

      $el
        .popover({
          html: true,
          trigger: 'focus',
          container: 'body',
          placement: 'top',
          template:
            '<div class="popover" role="tooltip"><div class="arrow"></div><p class="popover-header"></p><div class="popover-body"></div></div>',
          title: s__(
            'mrWidget|This feature merges changes from the target branch to the source branch. You cannot use this feature since the source branch is protected.',
          ),
          content: sprintf(
            s__('mrWidget|%{link_start}Learn more about resolving conflicts%{link_end}'),
            {
              link_start: `<a href="${escape(
                this.mr.conflictsDocsPath,
              )}" target="_blank" rel="noopener noreferrer">`,
              link_end: '</a>',
            },
            false,
          ),
        })
        .on('mouseenter', mouseenter)
        .on('mouseleave', debouncedMouseleave(300))
        .on('show.bs.popover', () => {
          window.addEventListener('scroll', togglePopover.bind($el, false), { once: true });
        });
    },
  },
};
</script>
<template>
  <div class="mr-widget-body media">
    <status-icon :show-disabled-button="true" status="warning" />

    <div v-if="isLoading" class="gl-ml-4 gl-w-full mr-conflict-loader">
      <gl-skeleton-loader :width="334" :height="30">
        <rect x="0" y="7" width="150" height="16" rx="4" />
        <rect x="158" y="7" width="84" height="16" rx="4" />
        <rect x="250" y="7" width="84" height="16" rx="4" />
      </gl-skeleton-loader>
    </div>
    <div v-else class="media-body space-children">
      <span v-if="shouldBeRebased" class="bold">
        {{
          s__(`mrWidget|Fast-forward merge is not possible.
  To merge this request, first rebase locally.`)
        }}
      </span>
      <template v-else>
        <span class="bold">
          {{ s__('mrWidget|There are merge conflicts') }}<span v-if="!canMerge">.</span>
          <span v-if="!canMerge">
            {{
              s__(`mrWidget|Resolve these conflicts or ask someone
              with write access to this repository to merge it locally`)
            }}
          </span>
        </span>
        <span v-if="showResolveButton" ref="popover">
          <gl-button
            :href="!sourceBranchProtected && mr.conflictResolutionPath"
            :disabled="sourceBranchProtected"
            class="js-resolve-conflicts-button"
          >
            {{ s__('mrWidget|Resolve conflicts') }}
          </gl-button>
        </span>
        <gl-button
          v-if="canMerge"
          v-gl-modal-directive="'modal-merge-info'"
          class="js-merge-locally-button"
        >
          {{ s__('mrWidget|Merge locally') }}
        </gl-button>
      </template>
    </div>
  </div>
</template>