mr_widget_options.vue 8.0 KB
Newer Older
1 2
<script>

3
import Project from '~/pages/projects/project';
4
import SmartInterval from '~/smart_interval';
5
import createFlash from '../flash';
F
Fatih Acet 已提交
6 7 8 9
import {
  WidgetHeader,
  WidgetMergeHelp,
  WidgetPipeline,
10
  Deployment,
F
Fatih Acet 已提交
11 12 13
  WidgetRelatedLinks,
  MergedState,
  ClosedState,
14
  MergingState,
15
  RebaseState,
16
  WorkInProgressState,
F
Fatih Acet 已提交
17 18 19 20 21 22
  ArchivedState,
  ConflictsState,
  NothingToMergeState,
  MissingBranchState,
  NotAllowedState,
  ReadyToMergeState,
G
George Tsiolis 已提交
23
  ShaMismatchState,
F
Fatih Acet 已提交
24 25 26 27 28 29 30 31 32 33 34 35
  UnresolvedDiscussionsState,
  PipelineBlockedState,
  PipelineFailedState,
  FailedToMerge,
  MergeWhenPipelineSucceedsState,
  AutoMergeFailed,
  CheckingState,
  MRWidgetStore,
  MRWidgetService,
  eventHub,
  stateMaps,
  SquashBeforeMerge,
36
  notify,
37
  SourceBranchRemovalStatus,
F
Fatih Acet 已提交
38
} from './dependencies';
39
import { setFavicon } from '../lib/utils/common_utils';
F
Fatih Acet 已提交
40 41 42 43

export default {
  el: '#js-vue-mr-widget',
  name: 'MRWidget',
F
Filipa Lacerda 已提交
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
  components: {
    'mr-widget-header': WidgetHeader,
    'mr-widget-merge-help': WidgetMergeHelp,
    'mr-widget-pipeline': WidgetPipeline,
    Deployment,
    'mr-widget-related-links': WidgetRelatedLinks,
    'mr-widget-merged': MergedState,
    'mr-widget-closed': ClosedState,
    'mr-widget-merging': MergingState,
    'mr-widget-failed-to-merge': FailedToMerge,
    'mr-widget-wip': WorkInProgressState,
    'mr-widget-archived': ArchivedState,
    'mr-widget-conflicts': ConflictsState,
    'mr-widget-nothing-to-merge': NothingToMergeState,
    'mr-widget-not-allowed': NotAllowedState,
    'mr-widget-missing-branch': MissingBranchState,
    'mr-widget-ready-to-merge': ReadyToMergeState,
    'sha-mismatch': ShaMismatchState,
    'mr-widget-squash-before-merge': SquashBeforeMerge,
    'mr-widget-checking': CheckingState,
    'mr-widget-unresolved-discussions': UnresolvedDiscussionsState,
    'mr-widget-pipeline-blocked': PipelineBlockedState,
    'mr-widget-pipeline-failed': PipelineFailedState,
    'mr-widget-merge-when-pipeline-succeeds': MergeWhenPipelineSucceedsState,
    'mr-widget-auto-merge-failed': AutoMergeFailed,
    'mr-widget-rebase': RebaseState,
    SourceBranchRemovalStatus,
  },
S
Simon Knox 已提交
72 73 74 75
  props: {
    mrData: {
      type: Object,
      required: false,
F
Filipa Lacerda 已提交
76
      default: null,
S
Simon Knox 已提交
77 78
    },
  },
F
Fatih Acet 已提交
79
  data() {
S
Simon Knox 已提交
80
    const store = new MRWidgetStore(this.mrData || window.gl.mrWidgetData);
F
Fatih Acet 已提交
81 82 83 84 85 86 87 88 89 90 91
    const service = this.createService(store);
    return {
      mr: store,
      service,
    };
  },
  computed: {
    componentName() {
      return stateMaps.stateToComponentMap[this.mr.state];
    },
    shouldRenderMergeHelp() {
92
      return stateMaps.statesToShowHelpWidget.indexOf(this.mr.state) > -1;
F
Fatih Acet 已提交
93 94
    },
    shouldRenderPipelines() {
95
      return this.mr.hasCI;
F
Fatih Acet 已提交
96 97
    },
    shouldRenderRelatedLinks() {
F
Filipa Lacerda 已提交
98
      return !!this.mr.relatedLinks && !this.mr.isNothingToMergeState;
F
Fatih Acet 已提交
99
    },
100
    shouldRenderSourceBranchRemovalStatus() {
101 102
      return !this.mr.canRemoveSourceBranch && this.mr.shouldRemoveSourceBranch &&
        (!this.mr.isNothingToMergeState && !this.mr.isMergedState);
103
    },
F
Fatih Acet 已提交
104
  },
F
Filipa Lacerda 已提交
105 106 107 108 109 110 111
  created() {
    this.initPolling();
    this.bindEventHubListeners();
  },
  mounted() {
    this.handleMounted();
  },
F
Fatih Acet 已提交
112 113 114 115 116 117 118 119 120 121 122
  methods: {
    createService(store) {
      const endpoints = {
        mergePath: store.mergePath,
        mergeCheckPath: store.mergeCheckPath,
        cancelAutoMergePath: store.cancelAutoMergePath,
        removeWIPPath: store.removeWIPPath,
        sourceBranchPath: store.sourceBranchPath,
        ciEnvironmentsStatusPath: store.ciEnvironmentsStatusPath,
        statusPath: store.statusPath,
        mergeActionsContentPath: store.mergeActionsContentPath,
123
        rebasePath: store.rebasePath,
F
Fatih Acet 已提交
124 125 126 127
      };
      return new MRWidgetService(endpoints);
    },
    checkStatus(cb) {
128
      return this.service.checkStatus()
129 130 131 132
        .then(res => res.data)
        .then((data) => {
          this.handleNotification(data);
          this.mr.setData(data);
133
          this.setFaviconHelper();
134

F
Fatih Acet 已提交
135
          if (cb) {
136
            cb.call(null, data);
F
Fatih Acet 已提交
137 138
          }
        })
139
        .catch(() => createFlash('Something went wrong. Please try again.'));
F
Fatih Acet 已提交
140 141
    },
    initPolling() {
142
      this.pollingInterval = new SmartInterval({
F
Fatih Acet 已提交
143 144 145 146 147 148 149 150
        callback: this.checkStatus,
        startingInterval: 10000,
        maxInterval: 30000,
        hiddenInterval: 120000,
        incrementByFactorOf: 5000,
      });
    },
    initDeploymentsPolling() {
151
      this.deploymentsInterval = new SmartInterval({
F
Fatih Acet 已提交
152 153 154 155 156 157 158 159
        callback: this.fetchDeployments,
        startingInterval: 30000,
        maxInterval: 120000,
        hiddenInterval: 240000,
        incrementByFactorOf: 15000,
        immediateExecution: true,
      });
    },
160
    setFaviconHelper() {
F
Fatih Acet 已提交
161
      if (this.mr.ciStatusFaviconPath) {
162
        setFavicon(this.mr.ciStatusFaviconPath);
F
Fatih Acet 已提交
163 164 165
      }
    },
    fetchDeployments() {
166
      return this.service.fetchDeployments()
167 168 169
        .then(res => res.data)
        .then((data) => {
          if (data.length) {
P
Phil Hughes 已提交
170
            this.mr.deployments = data;
F
Fatih Acet 已提交
171 172 173
          }
        })
        .catch(() => {
174
          createFlash('Something went wrong while fetching the environments for this merge request. Please try again.'); // eslint-disable-line
F
Fatih Acet 已提交
175 176 177 178 179
        });
    },
    fetchActionsContent() {
      this.service.fetchMergeActionsContent()
        .then((res) => {
180
          if (res.data) {
F
Fatih Acet 已提交
181
            const el = document.createElement('div');
182
            el.innerHTML = res.data;
F
Fatih Acet 已提交
183
            document.body.appendChild(el);
184
            Project.initRefSwitcher();
F
Fatih Acet 已提交
185 186
          }
        })
187
        .catch(() => createFlash('Something went wrong. Please try again.'));
F
Fatih Acet 已提交
188
    },
189 190
    handleNotification(data) {
      if (data.ci_status === this.mr.ciStatus) return;
191
      if (!data.pipeline) return;
192 193 194 195 196

      const label = data.pipeline.details.status.label;
      const title = `Pipeline ${label}`;
      const message = `Pipeline ${label} for "${data.title}"`;

197
      notify.notifyMe(title, message, this.mr.gitlabLogo);
198
    },
F
Fatih Acet 已提交
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
    resumePolling() {
      this.pollingInterval.resume();
    },
    stopPolling() {
      this.pollingInterval.stopTimer();
    },
    bindEventHubListeners() {
      eventHub.$on('MRWidgetUpdateRequested', (cb) => {
        this.checkStatus(cb);
      });

      // `params` should be an Array contains a Boolean, like `[true]`
      // Passing parameter as Boolean didn't work.
      eventHub.$on('SetBranchRemoveFlag', (params) => {
        this.mr.isRemovingSourceBranch = params[0];
      });

      eventHub.$on('FailedToMerge', (mergeError) => {
        this.mr.state = 'failedToMerge';
        this.mr.mergeError = mergeError;
      });

      eventHub.$on('UpdateWidgetData', (data) => {
        this.mr.setData(data);
      });

      eventHub.$on('FetchActionsContent', () => {
        this.fetchActionsContent();
      });

      eventHub.$on('EnablePolling', () => {
        this.resumePolling();
      });

      eventHub.$on('DisablePolling', () => {
        this.stopPolling();
      });
    },
    handleMounted() {
238
      this.setFaviconHelper();
F
Fatih Acet 已提交
239 240 241
      this.initDeploymentsPolling();
    },
  },
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
};
</script>
<template>
  <div class="mr-state-widget prepend-top-default">
    <mr-widget-header
      :mr="mr"
    />
    <mr-widget-pipeline
      v-if="shouldRenderPipelines"
      :pipeline="mr.pipeline"
      :ci-status="mr.ciStatus"
      :has-ci="mr.hasCI"
    />
    <deployment
      v-for="deployment in mr.deployments"
      :key="deployment.id"
      :deployment="deployment"
    />
    <div class="mr-widget-section">
      <component
        :is="componentName"
        :mr="mr"
        :service="service"
      />

      <section
        v-if="mr.maintainerEditAllowed"
        class="mr-info-list mr-links"
      >
        {{ s__("mrWidget|Allows edits from maintainers") }}
      </section>

      <mr-widget-related-links
        v-if="shouldRenderRelatedLinks"
        :state="mr.state"
        :related-links="mr.relatedLinks"
      />

      <source-branch-removal-status
        v-if="shouldRenderSourceBranchRemovalStatus"
282
      />
F
Fatih Acet 已提交
283
    </div>
284 285 286 287 288 289 290
    <div
      class="mr-widget-footer"
      v-if="shouldRenderMergeHelp"
    >
      <mr-widget-merge-help />
    </div>
  </div>
F
Filipa Lacerda 已提交
291
</template>