summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_merge_request_widget/components/states/mr_widget_merge_when_pipeline_succeeds.js
blob: 419d174f3ff5ea90f52c107a4beef374520079fd (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
/* global Flash */

import MRWidgetAuthor from '../../components/mr_widget_author';
import eventHub from '../../event_hub';

export default {
  name: 'MRWidgetMergeWhenPipelineSucceeds',
  props: {
    mr: { type: Object, required: true },
    service: { type: Object, required: true },
  },
  components: {
    'mr-widget-author': MRWidgetAuthor,
  },
  data() {
    return {
      isCancellingAutoMerge: false,
      isRemovingSourceBranch: false,
    };
  },
  computed: {
    canRemoveSourceBranch() {
      const { shouldRemoveSourceBranch, canRemoveSourceBranch,
        mergeUserId, currentUserId } = this.mr;

      return !shouldRemoveSourceBranch && canRemoveSourceBranch && mergeUserId === currentUserId;
    },
  },
  methods: {
    cancelAutomaticMerge() {
      this.isCancellingAutoMerge = true;
      this.service.cancelAutomaticMerge()
        .then(res => res.json())
        .then((res) => {
          eventHub.$emit('UpdateWidgetData', res);
        })
        .catch(() => {
          this.isCancellingAutoMerge = false;
          new Flash('Something went wrong. Please try again.'); // eslint-disable-line
        });
    },
    removeSourceBranch() {
      const options = {
        sha: this.mr.sha,
        merge_when_pipeline_succeeds: true,
        should_remove_source_branch: true,
      };

      this.isRemovingSourceBranch = true;
      this.service.mergeResource.save(options)
        .then(res => res.json())
        .then((res) => {
          if (res.status === 'merge_when_pipeline_succeeds') {
            eventHub.$emit('MRWidgetUpdateRequested');
          }
        })
        .catch(() => {
          this.isRemovingSourceBranch = false;
          new Flash('Something went wrong. Please try again.'); // eslint-disable-line
        });
    },
  },
  template: `
    <div class="mr-widget-body">
      <h4>
        Set by
        <mr-widget-author :author="mr.setToMWPSBy" />
        to be merged automatically when the pipeline succeeds.
        <a
          v-if="mr.canCancelAutomaticMerge"
          @click.prevent="cancelAutomaticMerge"
          :disabled="isCancellingAutoMerge"
          role="button"
          href="#"
          class="btn btn-xs btn-default js-cancel-auto-merge">
          <i
            v-if="isCancellingAutoMerge"
            class="fa fa-spinner fa-spin"
            aria-hidden="true" />
            Cancel automatic merge
        </a>
      </h4>
      <section class="mr-info-list">
        <div class="legend"></div>
        <p>The changes will be merged into
          <a
            :href="mr.targetBranchPath"
            class="label-branch">
            {{mr.targetBranch}}
          </a>.
        </p>
        <p v-if="mr.shouldRemoveSourceBranch">
          The source branch will be removed.
        </p>
        <p
          v-else
          class="with-button">
          The source branch will not be removed.
          <a
            v-if="canRemoveSourceBranch"
            :disabled="isRemovingSourceBranch"
            @click.prevent="removeSourceBranch"
            role="button"
            class="btn btn-xs btn-default js-remove-source-branch"
            href="#">
            <i
            v-if="isRemovingSourceBranch"
            class="fa fa-spinner fa-spin"
            aria-hidden="true" />
            Remove source branch
          </a>
        </p>
      </section>
    </div>
  `,
};