2017-01-12 04:27:41 +00:00
|
|
|
/* eslint-disable max-len, no-var, func-names, space-before-function-paren, vars-on-top, comma-dangle, no-return-assign, consistent-return, no-param-reassign, one-var, one-var-declaration-per-line, quotes, prefer-template, no-else-return, prefer-arrow-callback, no-unused-vars, no-underscore-dangle, no-shadow, no-mixed-operators, camelcase, default-case, wrap-iife */
|
2016-12-14 03:01:05 +00:00
|
|
|
/* global notify */
|
|
|
|
/* global notifyPermissions */
|
|
|
|
/* global merge_request_widget */
|
|
|
|
|
2017-03-09 18:39:53 +00:00
|
|
|
import './smart_interval';
|
|
|
|
import MiniPipelineGraph from './mini_pipeline_graph_dropdown';
|
2017-01-07 07:51:35 +00:00
|
|
|
|
2016-12-14 03:01:05 +00:00
|
|
|
((global) => {
|
2017-01-10 22:35:09 +00:00
|
|
|
var indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i += 1) { if (i in this && this[i] === item) return i; } return -1; };
|
2016-07-24 20:45:11 +00:00
|
|
|
|
2016-10-04 14:03:13 +00:00
|
|
|
const DEPLOYMENT_TEMPLATE = `<div class="mr-widget-heading" id="<%- id %>">
|
|
|
|
<div class="ci_widget ci-success">
|
|
|
|
<%= ci_success_icon %>
|
|
|
|
<span>
|
|
|
|
Deployed to
|
2017-03-15 21:21:48 +00:00
|
|
|
<a href="<%- url %>" target="_blank" rel="noopener noreferrer" class="environment">
|
2016-10-04 14:03:13 +00:00
|
|
|
<%- name %>
|
|
|
|
</a>
|
|
|
|
<span class="js-environment-timeago" data-toggle="tooltip" data-placement="top" data-title="<%- deployed_at_formatted %>">
|
|
|
|
<%- deployed_at %>
|
|
|
|
</span>
|
2017-03-15 21:21:48 +00:00
|
|
|
<a class="js-environment-link" href="<%- external_url %>" target="_blank" rel="noopener noreferrer">
|
2016-10-04 14:03:13 +00:00
|
|
|
<i class="fa fa-external-link"></i>
|
|
|
|
View on <%- external_url_formatted %>
|
|
|
|
</a>
|
|
|
|
</span>
|
2016-10-17 21:10:43 +00:00
|
|
|
<span class="stop-env-container js-stop-env-link">
|
2016-10-17 14:13:19 +00:00
|
|
|
<a href="<%- stop_url %>" class="close-evn-link" data-method="post" rel="nofollow" data-confirm="Are you sure you want to stop this environment?">
|
2016-10-17 10:44:08 +00:00
|
|
|
<i class="fa fa-stop-circle-o"/>
|
|
|
|
Stop environment
|
|
|
|
</a>
|
|
|
|
</span>
|
2016-10-04 14:03:13 +00:00
|
|
|
</div>
|
|
|
|
</div>`;
|
|
|
|
|
2016-12-14 03:01:05 +00:00
|
|
|
global.MergeRequestWidget = (function() {
|
2016-07-24 20:45:11 +00:00
|
|
|
function MergeRequestWidget(opts) {
|
2016-07-27 03:32:10 +00:00
|
|
|
// Initialize MergeRequestWidget behavior
|
|
|
|
//
|
|
|
|
// check_enable - Boolean, whether to check automerge status
|
|
|
|
// merge_check_url - String, URL to use to check automerge status
|
|
|
|
// ci_status_url - String, URL to use to check CI status
|
|
|
|
//
|
2016-07-24 20:45:11 +00:00
|
|
|
this.opts = opts;
|
2016-10-04 14:03:13 +00:00
|
|
|
this.$widgetBody = $('.mr-widget-body');
|
2016-07-24 20:45:11 +00:00
|
|
|
$('#modal_merge_info').modal({
|
|
|
|
show: false
|
|
|
|
});
|
|
|
|
this.clearEventListeners();
|
|
|
|
this.addEventListeners();
|
|
|
|
this.getCIStatus(false);
|
2016-10-04 14:03:13 +00:00
|
|
|
this.retrieveSuccessIcon();
|
2016-11-25 18:42:25 +00:00
|
|
|
|
2017-01-14 15:27:50 +00:00
|
|
|
this.initMiniPipelineGraph();
|
|
|
|
|
2016-11-25 18:42:25 +00:00
|
|
|
this.ciStatusInterval = new global.SmartInterval({
|
|
|
|
callback: this.getCIStatus.bind(this, true),
|
|
|
|
startingInterval: 10000,
|
|
|
|
maxInterval: 30000,
|
|
|
|
hiddenInterval: 120000,
|
|
|
|
incrementByFactorOf: 5000,
|
|
|
|
});
|
|
|
|
this.ciEnvironmentStatusInterval = new global.SmartInterval({
|
|
|
|
callback: this.getCIEnvironmentsStatus.bind(this),
|
|
|
|
startingInterval: 30000,
|
|
|
|
maxInterval: 120000,
|
|
|
|
hiddenInterval: 240000,
|
|
|
|
incrementByFactorOf: 15000,
|
|
|
|
immediateExecution: true,
|
|
|
|
});
|
2017-01-14 15:27:50 +00:00
|
|
|
|
2016-07-24 20:45:11 +00:00
|
|
|
notifyPermissions();
|
|
|
|
}
|
|
|
|
|
|
|
|
MergeRequestWidget.prototype.clearEventListeners = function() {
|
2017-01-13 21:54:16 +00:00
|
|
|
return $(document).off('DOMContentLoaded');
|
2016-07-24 20:45:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
MergeRequestWidget.prototype.addEventListeners = function() {
|
|
|
|
var allowedPages;
|
2016-11-24 15:45:34 +00:00
|
|
|
allowedPages = ['show', 'commits', 'pipelines', 'changes'];
|
2017-01-13 21:54:16 +00:00
|
|
|
$(document).on('DOMContentLoaded', (function(_this) {
|
2016-07-24 20:45:11 +00:00
|
|
|
return function() {
|
|
|
|
var page;
|
|
|
|
page = $('body').data('page').split(':').last();
|
2017-02-25 12:43:26 +00:00
|
|
|
if (allowedPages.indexOf(page) === -1) {
|
2016-07-24 20:45:11 +00:00
|
|
|
return _this.clearEventListeners();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})(this));
|
|
|
|
};
|
|
|
|
|
2016-10-04 14:03:13 +00:00
|
|
|
MergeRequestWidget.prototype.retrieveSuccessIcon = function() {
|
2016-12-14 03:01:05 +00:00
|
|
|
const $ciSuccessIcon = $('.js-success-icon');
|
|
|
|
this.$ciSuccessIcon = $ciSuccessIcon.html();
|
|
|
|
$ciSuccessIcon.remove();
|
2017-01-10 22:54:56 +00:00
|
|
|
};
|
2016-10-04 14:03:13 +00:00
|
|
|
|
2016-07-24 20:45:11 +00:00
|
|
|
MergeRequestWidget.prototype.mergeInProgress = function(deleteSourceBranch) {
|
|
|
|
if (deleteSourceBranch == null) {
|
|
|
|
deleteSourceBranch = false;
|
|
|
|
}
|
|
|
|
return $.ajax({
|
|
|
|
type: 'GET',
|
|
|
|
url: $('.merge-request').data('url'),
|
|
|
|
success: (function(_this) {
|
|
|
|
return function(data) {
|
|
|
|
var callback, urlSuffix;
|
|
|
|
if (data.state === "merged") {
|
2016-08-11 03:41:48 +00:00
|
|
|
urlSuffix = deleteSourceBranch ? '?deleted_source_branch=true' : '';
|
2016-07-24 20:45:11 +00:00
|
|
|
return window.location.href = window.location.pathname + urlSuffix;
|
|
|
|
} else if (data.merge_error) {
|
2017-02-14 14:13:35 +00:00
|
|
|
return $('.mr-widget-body').html("<h4>" + data.merge_error + "</h4>");
|
2016-07-24 20:45:11 +00:00
|
|
|
} else {
|
|
|
|
callback = function() {
|
|
|
|
return merge_request_widget.mergeInProgress(deleteSourceBranch);
|
|
|
|
};
|
|
|
|
return setTimeout(callback, 2000);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})(this),
|
|
|
|
dataType: 'json'
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-11-25 18:42:25 +00:00
|
|
|
MergeRequestWidget.prototype.cancelPolling = function () {
|
|
|
|
this.ciStatusInterval.cancel();
|
|
|
|
this.ciEnvironmentStatusInterval.cancel();
|
|
|
|
};
|
|
|
|
|
2016-07-24 20:45:11 +00:00
|
|
|
MergeRequestWidget.prototype.getMergeStatus = function() {
|
2017-02-14 22:08:30 +00:00
|
|
|
return $.get(this.opts.merge_check_url, (data) => {
|
2017-01-22 12:38:53 +00:00
|
|
|
var $html = $(data);
|
2017-02-14 22:08:30 +00:00
|
|
|
this.updateMergeButton(this.status, this.hasCi, $html);
|
2017-01-22 15:08:25 +00:00
|
|
|
$('.mr-widget-body').replaceWith($html.find('.mr-widget-body'));
|
|
|
|
$('.mr-widget-footer').replaceWith($html.find('.mr-widget-footer'));
|
2016-07-24 20:45:11 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
MergeRequestWidget.prototype.ciLabelForStatus = function(status) {
|
|
|
|
switch (status) {
|
|
|
|
case 'success':
|
|
|
|
return 'passed';
|
|
|
|
case 'success_with_warnings':
|
|
|
|
return 'passed with warnings';
|
|
|
|
default:
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
MergeRequestWidget.prototype.getCIStatus = function(showNotification) {
|
|
|
|
var _this;
|
|
|
|
_this = this;
|
|
|
|
$('.ci-widget-fetching').show();
|
|
|
|
return $.getJSON(this.opts.ci_status_url, (function(_this) {
|
|
|
|
return function(data) {
|
|
|
|
var message, status, title;
|
2017-02-14 22:08:30 +00:00
|
|
|
_this.status = data.status;
|
|
|
|
_this.hasCi = data.has_ci;
|
|
|
|
_this.updateMergeButton(_this.status, _this.hasCi);
|
2016-10-04 14:03:13 +00:00
|
|
|
if (data.environments && data.environments.length) _this.renderEnvironments(data.environments);
|
2016-12-28 16:53:05 +00:00
|
|
|
if (data.status !== _this.opts.ci_status ||
|
|
|
|
data.sha !== _this.opts.ci_sha ||
|
|
|
|
data.pipeline !== _this.opts.ci_pipeline) {
|
2016-07-24 20:45:11 +00:00
|
|
|
_this.opts.ci_status = data.status;
|
|
|
|
_this.showCIStatus(data.status);
|
|
|
|
if (data.coverage) {
|
|
|
|
_this.showCICoverage(data.coverage);
|
|
|
|
}
|
2016-12-28 16:53:05 +00:00
|
|
|
if (data.pipeline) {
|
|
|
|
_this.opts.ci_pipeline = data.pipeline;
|
|
|
|
_this.updatePipelineUrls(data.pipeline);
|
|
|
|
}
|
|
|
|
if (data.sha) {
|
|
|
|
_this.opts.ci_sha = data.sha;
|
|
|
|
_this.updateCommitUrls(data.sha);
|
|
|
|
}
|
2017-03-17 11:20:36 +00:00
|
|
|
if (showNotification && data.status) {
|
2016-07-24 20:45:11 +00:00
|
|
|
status = _this.ciLabelForStatus(data.status);
|
|
|
|
if (status === "preparing") {
|
|
|
|
title = _this.opts.ci_title.preparing;
|
|
|
|
status = status.charAt(0).toUpperCase() + status.slice(1);
|
|
|
|
message = _this.opts.ci_message.preparing.replace('{{status}}', status);
|
|
|
|
} else {
|
|
|
|
title = _this.opts.ci_title.normal;
|
|
|
|
message = _this.opts.ci_message.normal.replace('{{status}}', status);
|
|
|
|
}
|
|
|
|
title = title.replace('{{status}}', status);
|
|
|
|
message = message.replace('{{sha}}', data.sha);
|
|
|
|
message = message.replace('{{title}}', data.title);
|
|
|
|
notify(title, message, _this.opts.gitlab_icon, function() {
|
|
|
|
this.close();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})(this));
|
|
|
|
};
|
|
|
|
|
2016-10-07 22:10:06 +00:00
|
|
|
MergeRequestWidget.prototype.getCIEnvironmentsStatus = function() {
|
|
|
|
$.getJSON(this.opts.ci_environments_status_url, (environments) => {
|
|
|
|
if (environments && environments.length) this.renderEnvironments(environments);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-10-04 14:03:13 +00:00
|
|
|
MergeRequestWidget.prototype.renderEnvironments = function(environments) {
|
2017-01-10 22:35:09 +00:00
|
|
|
for (let i = 0; i < environments.length; i += 1) {
|
2016-10-07 22:10:06 +00:00
|
|
|
const environment = environments[i];
|
2017-01-12 04:27:41 +00:00
|
|
|
if ($(`.mr-state-widget #${environment.id}`).length) return;
|
2016-10-07 22:10:06 +00:00
|
|
|
const $template = $(DEPLOYMENT_TEMPLATE);
|
2016-10-13 12:00:06 +00:00
|
|
|
if (!environment.external_url || !environment.external_url_formatted) $('.js-environment-link', $template).remove();
|
2016-11-25 18:42:25 +00:00
|
|
|
|
2016-10-17 10:44:08 +00:00
|
|
|
if (!environment.stop_url) {
|
2016-10-17 21:10:43 +00:00
|
|
|
$('.js-stop-env-link', $template).remove();
|
2016-10-17 10:44:08 +00:00
|
|
|
}
|
2016-11-25 18:42:25 +00:00
|
|
|
|
2016-10-13 12:00:06 +00:00
|
|
|
if (environment.deployed_at && environment.deployed_at_formatted) {
|
2016-11-17 20:41:35 +00:00
|
|
|
environment.deployed_at = gl.utils.getTimeago().format(environment.deployed_at, 'gl_en') + '.';
|
2016-10-07 22:10:06 +00:00
|
|
|
} else {
|
|
|
|
$('.js-environment-timeago', $template).remove();
|
|
|
|
environment.name += '.';
|
|
|
|
}
|
|
|
|
environment.ci_success_icon = this.$ciSuccessIcon;
|
|
|
|
const templateString = _.unescape($template[0].outerHTML);
|
2017-01-10 22:54:56 +00:00
|
|
|
const template = _.template(templateString)(environment);
|
2016-10-07 22:10:06 +00:00
|
|
|
this.$widgetBody.before(template);
|
|
|
|
}
|
2016-12-14 03:01:05 +00:00
|
|
|
};
|
2016-10-04 14:03:13 +00:00
|
|
|
|
2016-07-24 20:45:11 +00:00
|
|
|
MergeRequestWidget.prototype.showCIStatus = function(state) {
|
|
|
|
var allowed_states;
|
|
|
|
if (state == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$('.ci_widget').hide();
|
2017-02-14 22:08:30 +00:00
|
|
|
$('.ci_widget.ci-' + state).show();
|
|
|
|
|
|
|
|
this.initMiniPipelineGraph();
|
|
|
|
};
|
|
|
|
|
|
|
|
MergeRequestWidget.prototype.showCICoverage = function(coverage) {
|
|
|
|
var text = `Coverage ${coverage}%`;
|
|
|
|
return $('.ci_widget:visible .ci-coverage').text(text);
|
|
|
|
};
|
|
|
|
|
|
|
|
MergeRequestWidget.prototype.updateMergeButton = function(state, hasCi, $html) {
|
|
|
|
const allowed_states = ["failed", "canceled", "running", "pending", "success", "success_with_warnings", "skipped", "not_found"];
|
|
|
|
let stateClass = 'btn-danger';
|
|
|
|
if (!hasCi) {
|
|
|
|
stateClass = 'btn-create';
|
|
|
|
} else if (indexOf.call(allowed_states, state) !== -1) {
|
2016-07-24 20:45:11 +00:00
|
|
|
switch (state) {
|
|
|
|
case "failed":
|
|
|
|
case "canceled":
|
|
|
|
case "not_found":
|
2017-02-14 22:08:30 +00:00
|
|
|
stateClass = 'btn-danger';
|
2017-01-14 15:27:50 +00:00
|
|
|
break;
|
2016-07-24 20:45:11 +00:00
|
|
|
case "running":
|
2017-02-14 22:08:30 +00:00
|
|
|
stateClass = 'btn-info';
|
2017-01-14 15:27:50 +00:00
|
|
|
break;
|
2016-07-24 20:45:11 +00:00
|
|
|
case "success":
|
|
|
|
case "success_with_warnings":
|
2017-02-14 22:08:30 +00:00
|
|
|
stateClass = 'btn-create';
|
2016-07-24 20:45:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$('.ci_widget.ci-error').show();
|
2017-02-14 22:08:30 +00:00
|
|
|
stateClass = 'btn-danger';
|
2016-07-24 20:45:11 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 22:08:30 +00:00
|
|
|
this.setMergeButtonClass(stateClass, $html);
|
2016-07-24 20:45:11 +00:00
|
|
|
};
|
|
|
|
|
2017-02-14 22:08:30 +00:00
|
|
|
MergeRequestWidget.prototype.setMergeButtonClass = function(css_class, $html = $('.mr-state-widget')) {
|
|
|
|
return $html.find('.js-merge-button').removeClass('btn-danger btn-info btn-create').addClass(css_class);
|
2016-07-24 20:45:11 +00:00
|
|
|
};
|
|
|
|
|
2016-12-28 16:53:05 +00:00
|
|
|
MergeRequestWidget.prototype.updatePipelineUrls = function(id) {
|
|
|
|
const pipelineUrl = this.opts.pipeline_path;
|
|
|
|
$('.pipeline').text(`#${id}`).attr('href', [pipelineUrl, id].join('/'));
|
|
|
|
};
|
|
|
|
|
|
|
|
MergeRequestWidget.prototype.updateCommitUrls = function(id) {
|
|
|
|
const commitsUrl = this.opts.commits_path;
|
|
|
|
$('.js-commit-link').text(`#${id}`).attr('href', [commitsUrl, id].join('/'));
|
|
|
|
};
|
|
|
|
|
2017-01-14 15:27:50 +00:00
|
|
|
MergeRequestWidget.prototype.initMiniPipelineGraph = function() {
|
2017-03-09 18:26:15 +00:00
|
|
|
new MiniPipelineGraph({
|
2017-03-09 18:51:52 +00:00
|
|
|
container: '.js-pipeline-inline-mr-widget-graph:visible',
|
2017-01-14 15:27:50 +00:00
|
|
|
}).bindEvents();
|
|
|
|
};
|
|
|
|
|
2016-07-24 20:45:11 +00:00
|
|
|
return MergeRequestWidget;
|
|
|
|
})();
|
2016-12-14 03:01:05 +00:00
|
|
|
})(window.gl || (window.gl = {}));
|