2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2017-05-30 04:43:13 -04:00
|
|
|
import _ from 'underscore';
|
2018-06-05 22:17:47 -04:00
|
|
|
import { polyfillSticky } from './lib/utils/sticky';
|
2018-01-30 11:33:23 -05:00
|
|
|
import axios from './lib/utils/axios_utils';
|
2017-12-11 07:28:11 -05:00
|
|
|
import { visitUrl } from './lib/utils/url_utility';
|
2017-08-10 09:02:30 -04:00
|
|
|
import bp from './breakpoints';
|
2017-12-27 09:17:28 -05:00
|
|
|
import { numberToHumanSize } from './lib/utils/number_utils';
|
2017-09-06 12:14:34 -04:00
|
|
|
import { setCiStatusFavicon } from './lib/utils/common_utils';
|
2018-05-29 17:36:11 -04:00
|
|
|
import { isScrolledToBottom, scrollDown } from './lib/utils/scroll_utils';
|
|
|
|
import LogOutputBehaviours from './lib/utils/logoutput_behaviours';
|
2017-04-18 08:27:10 -04:00
|
|
|
|
2018-05-29 17:36:11 -04:00
|
|
|
export default class Job extends LogOutputBehaviours {
|
2017-10-10 07:06:42 -04:00
|
|
|
constructor(options) {
|
2018-05-29 17:36:11 -04:00
|
|
|
super();
|
2017-10-10 07:06:42 -04:00
|
|
|
this.timeout = null;
|
|
|
|
this.state = null;
|
2018-02-02 05:05:37 -05:00
|
|
|
this.fetchingStatusFavicon = false;
|
2017-04-07 07:13:23 -04:00
|
|
|
this.options = options || $('.js-build-options').data();
|
|
|
|
|
2017-12-04 21:18:45 -05:00
|
|
|
this.pagePath = this.options.pagePath;
|
2017-04-07 07:13:23 -04:00
|
|
|
this.buildStatus = this.options.buildStatus;
|
|
|
|
this.state = this.options.logState;
|
|
|
|
this.buildStage = this.options.buildStage;
|
2017-03-11 02:30:44 -05:00
|
|
|
this.$document = $(document);
|
2017-11-09 09:47:05 -05:00
|
|
|
this.$window = $(window);
|
2017-04-18 08:27:10 -04:00
|
|
|
this.logBytes = 0;
|
2017-05-30 04:43:13 -04:00
|
|
|
this.updateDropdown = this.updateDropdown.bind(this);
|
2017-04-07 07:13:23 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
this.$buildTrace = $('#build-trace');
|
|
|
|
this.$buildRefreshAnimation = $('.js-build-refresh');
|
2017-04-07 07:13:23 -04:00
|
|
|
this.$truncatedInfo = $('.js-truncated-info');
|
2017-05-30 04:43:13 -04:00
|
|
|
this.$buildTraceOutput = $('.js-build-output');
|
2017-07-05 07:32:22 -04:00
|
|
|
this.$topBar = $('.js-top-bar');
|
2017-05-30 04:43:13 -04:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
clearTimeout(this.timeout);
|
2017-03-11 02:30:44 -05:00
|
|
|
|
|
|
|
this.initSidebar();
|
|
|
|
this.populateJobs(this.buildStage);
|
|
|
|
this.updateStageDropdownText(this.buildStage);
|
|
|
|
this.sidebarOnResize();
|
|
|
|
|
2017-04-07 07:13:23 -04:00
|
|
|
this.$document
|
|
|
|
.off('click', '.js-sidebar-build-toggle')
|
|
|
|
.on('click', '.js-sidebar-build-toggle', this.sidebarOnClick.bind(this));
|
|
|
|
|
|
|
|
this.$document
|
|
|
|
.off('click', '.stage-item')
|
|
|
|
.on('click', '.stage-item', this.updateDropdown);
|
|
|
|
|
2017-07-05 07:32:22 -04:00
|
|
|
this.scrollThrottled = _.throttle(this.toggleScroll.bind(this), 100);
|
2017-06-09 07:58:52 -04:00
|
|
|
|
2017-11-09 09:47:05 -05:00
|
|
|
this.$window
|
2017-06-09 07:58:52 -04:00
|
|
|
.off('scroll')
|
|
|
|
.on('scroll', () => {
|
2018-05-29 17:36:11 -04:00
|
|
|
if (!isScrolledToBottom()) {
|
2017-07-05 07:32:22 -04:00
|
|
|
this.toggleScrollAnimation(false);
|
2018-05-29 17:36:11 -04:00
|
|
|
} else if (isScrolledToBottom() && !this.isLogComplete) {
|
2017-11-09 09:47:05 -05:00
|
|
|
this.toggleScrollAnimation(true);
|
2017-07-05 07:32:22 -04:00
|
|
|
}
|
|
|
|
this.scrollThrottled();
|
2017-06-09 07:58:52 -04:00
|
|
|
});
|
|
|
|
|
2017-11-09 09:47:05 -05:00
|
|
|
this.$window
|
2017-04-07 07:13:23 -04:00
|
|
|
.off('resize.build')
|
2017-06-02 12:16:54 -04:00
|
|
|
.on('resize.build', _.throttle(this.sidebarOnResize.bind(this), 100));
|
2017-04-07 07:13:23 -04:00
|
|
|
|
2017-07-05 07:32:22 -04:00
|
|
|
this.initAffixTopArea();
|
2017-05-30 04:43:13 -04:00
|
|
|
|
2017-07-05 07:32:22 -04:00
|
|
|
this.getBuildTrace();
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
initAffixTopArea() {
|
2018-06-05 22:17:47 -04:00
|
|
|
polyfillSticky(this.$topBar);
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
2017-07-05 07:32:22 -04:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
scrollToBottom() {
|
2018-05-29 17:36:11 -04:00
|
|
|
scrollDown();
|
2017-06-09 07:58:52 -04:00
|
|
|
this.hasBeenScrolled = true;
|
2017-05-30 04:43:13 -04:00
|
|
|
this.toggleScroll();
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
2017-05-30 04:43:13 -04:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
scrollToTop() {
|
2017-12-21 10:48:24 -05:00
|
|
|
$(document).scrollTop(0);
|
2017-06-09 07:58:52 -04:00
|
|
|
this.hasBeenScrolled = true;
|
2017-05-30 04:43:13 -04:00
|
|
|
this.toggleScroll();
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
2017-05-30 04:43:13 -04:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
toggleScrollAnimation(toggle) {
|
2017-05-30 04:43:13 -04:00
|
|
|
this.$scrollBottomBtn.toggleClass('animate', toggle);
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
2017-05-30 04:43:13 -04:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
initSidebar() {
|
2017-03-11 02:30:44 -05:00
|
|
|
this.$sidebar = $('.js-build-sidebar');
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
getBuildTrace() {
|
2018-01-30 11:33:23 -05:00
|
|
|
return axios.get(`${this.pagePath}/trace.json`, {
|
|
|
|
params: { state: this.state },
|
2017-05-30 04:43:13 -04:00
|
|
|
})
|
2018-01-30 11:33:23 -05:00
|
|
|
.then((res) => {
|
|
|
|
const log = res.data;
|
|
|
|
|
2018-02-02 05:05:37 -05:00
|
|
|
if (!this.fetchingStatusFavicon) {
|
|
|
|
this.fetchingStatusFavicon = true;
|
|
|
|
|
|
|
|
setCiStatusFavicon(`${this.pagePath}/status.json`)
|
|
|
|
.then(() => {
|
|
|
|
this.fetchingStatusFavicon = false;
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
this.fetchingStatusFavicon = false;
|
|
|
|
});
|
|
|
|
}
|
2017-06-29 07:08:54 -04:00
|
|
|
|
2017-04-07 07:13:23 -04:00
|
|
|
if (log.state) {
|
|
|
|
this.state = log.state;
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2017-04-07 07:13:23 -04:00
|
|
|
|
2018-05-29 17:36:11 -04:00
|
|
|
this.isScrollInBottom = isScrolledToBottom();
|
2017-07-05 07:32:22 -04:00
|
|
|
|
2017-04-07 07:13:23 -04:00
|
|
|
if (log.append) {
|
2017-05-30 04:43:13 -04:00
|
|
|
this.$buildTraceOutput.append(log.html);
|
2017-04-18 08:27:10 -04:00
|
|
|
this.logBytes += log.size;
|
2017-04-07 07:13:23 -04:00
|
|
|
} else {
|
2017-05-30 04:43:13 -04:00
|
|
|
this.$buildTraceOutput.html(log.html);
|
2017-04-18 08:27:10 -04:00
|
|
|
this.logBytes = log.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the incremental sum of logBytes we received is less than the total
|
|
|
|
// we need to show a message warning the user about that.
|
|
|
|
if (this.logBytes < log.total) {
|
|
|
|
// size is in bytes, we need to calculate KiB
|
2017-12-27 09:17:28 -05:00
|
|
|
const size = numberToHumanSize(this.logBytes);
|
2017-04-18 08:27:10 -04:00
|
|
|
$('.js-truncated-info-size').html(`${size}`);
|
|
|
|
this.$truncatedInfo.removeClass('hidden');
|
|
|
|
} else {
|
|
|
|
this.$truncatedInfo.addClass('hidden');
|
2017-04-07 07:13:23 -04:00
|
|
|
}
|
2017-11-09 09:47:05 -05:00
|
|
|
this.isLogComplete = log.complete;
|
2017-04-07 07:13:23 -04:00
|
|
|
|
2018-02-07 07:49:27 -05:00
|
|
|
if (log.complete === false) {
|
2017-10-10 07:06:42 -04:00
|
|
|
this.timeout = setTimeout(() => {
|
2017-07-05 07:32:22 -04:00
|
|
|
this.getBuildTrace();
|
2017-04-07 07:13:23 -04:00
|
|
|
}, 4000);
|
|
|
|
} else {
|
2017-03-11 02:30:44 -05:00
|
|
|
this.$buildRefreshAnimation.remove();
|
2017-05-30 04:43:13 -04:00
|
|
|
this.toggleScrollAnimation(false);
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
|
|
|
|
2017-04-07 07:13:23 -04:00
|
|
|
if (log.status !== this.buildStatus) {
|
2017-12-11 07:28:11 -05:00
|
|
|
visitUrl(this.pagePath);
|
2017-04-07 07:13:23 -04:00
|
|
|
}
|
2017-05-30 04:43:13 -04:00
|
|
|
})
|
2018-01-30 11:33:23 -05:00
|
|
|
.catch(() => {
|
2017-04-07 07:13:23 -04:00
|
|
|
this.$buildRefreshAnimation.remove();
|
2017-07-05 07:32:22 -04:00
|
|
|
})
|
|
|
|
.then(() => {
|
2017-11-09 09:47:05 -05:00
|
|
|
if (this.isScrollInBottom) {
|
2018-05-29 17:36:11 -04:00
|
|
|
scrollDown();
|
2017-07-05 07:32:22 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(() => this.toggleScroll());
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
shouldHideSidebarForViewport() {
|
2017-08-10 09:02:30 -04:00
|
|
|
const bootstrapBreakpoint = bp.getBreakpointSize();
|
2018-06-28 17:51:14 -04:00
|
|
|
return bootstrapBreakpoint === 'xs';
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
toggleSidebar(shouldHide) {
|
2017-06-08 08:21:03 -04:00
|
|
|
const shouldShow = typeof shouldHide === 'boolean' ? !shouldHide : undefined;
|
2017-06-12 05:20:19 -04:00
|
|
|
const $toggleButton = $('.js-sidebar-build-toggle-header');
|
2017-04-07 07:13:23 -04:00
|
|
|
|
2017-05-30 04:43:13 -04:00
|
|
|
this.$sidebar
|
|
|
|
.toggleClass('right-sidebar-expanded', shouldShow)
|
2017-03-11 02:30:44 -05:00
|
|
|
.toggleClass('right-sidebar-collapsed', shouldHide);
|
2017-06-12 05:20:19 -04:00
|
|
|
|
2017-07-05 07:32:22 -04:00
|
|
|
this.$topBar
|
2017-06-12 05:20:19 -04:00
|
|
|
.toggleClass('sidebar-expanded', shouldShow)
|
|
|
|
.toggleClass('sidebar-collapsed', shouldHide);
|
|
|
|
|
|
|
|
if (this.$sidebar.hasClass('right-sidebar-expanded')) {
|
|
|
|
$toggleButton.addClass('hidden');
|
|
|
|
} else {
|
|
|
|
$toggleButton.removeClass('hidden');
|
|
|
|
}
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
sidebarOnResize() {
|
2017-03-11 02:30:44 -05:00
|
|
|
this.toggleSidebar(this.shouldHideSidebarForViewport());
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
sidebarOnClick() {
|
2017-03-11 02:30:44 -05:00
|
|
|
if (this.shouldHideSidebarForViewport()) this.toggleSidebar();
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
2018-01-11 14:02:30 -05:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
populateJobs(stage) {
|
2017-03-11 02:30:44 -05:00
|
|
|
$('.build-job').hide();
|
2017-04-07 07:13:23 -04:00
|
|
|
$(`.build-job[data-stage="${stage}"]`).show();
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
updateStageDropdownText(stage) {
|
2017-03-11 02:30:44 -05:00
|
|
|
$('.stage-selection').text(stage);
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
|
2017-10-10 07:06:42 -04:00
|
|
|
updateDropdown(e) {
|
2017-03-11 02:30:44 -05:00
|
|
|
e.preventDefault();
|
2017-04-07 07:13:23 -04:00
|
|
|
const stage = e.currentTarget.text;
|
2017-03-11 02:30:44 -05:00
|
|
|
this.updateStageDropdownText(stage);
|
|
|
|
this.populateJobs(stage);
|
2017-10-10 07:06:42 -04:00
|
|
|
}
|
|
|
|
}
|