gitlab-org--gitlab-foss/app/assets/javascripts/merge_request_widget.js.coffee

141 lines
4.2 KiB
CoffeeScript
Raw Normal View History

class @MergeRequestWidget
# 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
#
constructor: (@opts) ->
2016-03-21 05:54:13 -04:00
$('#modal_merge_info').modal(show: false)
@firstCICheck = true
@readyForCICheck = false
@cancel = false
2016-03-21 05:52:38 -04:00
clearInterval @fetchBuildStatusInterval
@clearEventListeners()
@addEventListeners()
@getCIStatus(false)
2016-03-21 05:52:38 -04:00
@pollCIStatus()
notifyPermissions()
clearEventListeners: ->
$(document).off 'page:change.merge_request'
cancelPolling: ->
@cancel = true
addEventListeners: ->
allowedPages = ['show', 'commits', 'builds', 'changes']
$(document).on 'page:change.merge_request', =>
page = $('body').data('page').split(':').last()
if allowedPages.indexOf(page) < 0
clearInterval @fetchBuildStatusInterval
@cancelPolling()
@clearEventListeners()
2016-04-06 21:46:12 -04:00
mergeInProgress: (deleteSourceBranch = false)->
$.ajax
type: 'GET'
url: $('.merge-request').data('url')
success: (data) =>
if data.state == "merged"
urlSuffix = if deleteSourceBranch then '?delete_source=true' else ''
window.location.href = window.location.pathname + urlSuffix
else if data.merge_error
$('.mr-widget-body').html("<h4>" + data.merge_error + "</h4>")
else
callback = -> merge_request_widget.mergeInProgress(deleteSourceBranch)
setTimeout(callback, 2000)
dataType: 'json'
getMergeStatus: ->
$.get @opts.merge_check_url, (data) ->
$('.mr-state-widget').replaceWith(data)
ciLabelForStatus: (status) ->
2016-04-06 12:04:22 -04:00
if status is 'success'
'passed'
else
status
2016-03-21 05:52:38 -04:00
pollCIStatus: ->
@fetchBuildStatusInterval = setInterval ( =>
return if not @readyForCICheck
2016-03-21 05:52:38 -04:00
@getCIStatus(true)
@readyForCICheck = false
2016-04-06 21:46:12 -04:00
), 10000
2016-03-21 05:52:38 -04:00
getCIStatus: (showNotification) ->
_this = @
$('.ci-widget-fetching').show()
$.getJSON @opts.ci_status_url, (data) =>
return if @cancel
2016-03-21 05:52:38 -04:00
@readyForCICheck = true
2016-05-04 14:45:12 -04:00
if data.status is ''
return
2016-04-06 21:46:12 -04:00
if @firstCICheck || data.status isnt @opts.ci_status and data.status?
2016-05-04 14:45:12 -04:00
@opts.ci_status = data.status
2016-03-21 05:52:38 -04:00
@showCIStatus data.status
if data.coverage
@showCICoverage data.coverage
2016-05-04 14:45:12 -04:00
# The first check should only update the UI, a notification
# should only be displayed on status changes
if showNotification and not @firstCICheck
2016-04-06 12:04:22 -04:00
status = @ciLabelForStatus(data.status)
if status is "preparing"
title = @opts.ci_title.preparing
status = status.charAt(0).toUpperCase() + status.slice(1);
message = @opts.ci_message.preparing.replace('{{status}}', status)
else
title = @opts.ci_title.normal
message = @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(
2016-04-06 12:04:22 -04:00
title,
message,
@opts.gitlab_icon,
->
@close()
Turbolinks.visit _this.opts.builds_path
)
@firstCICheck = false
2016-03-21 05:52:38 -04:00
showCIStatus: (state) ->
return if not state?
$('.ci_widget').hide()
2015-07-30 07:58:47 -04:00
allowed_states = ["failed", "canceled", "running", "pending", "success", "skipped", "not_found"]
if state in allowed_states
$('.ci_widget.ci-' + state).show()
switch state
when "failed", "canceled", "not_found"
@setMergeButtonClass('btn-danger')
when "running"
@setMergeButtonClass('btn-warning')
when "success"
@setMergeButtonClass('btn-create')
else
$('.ci_widget.ci-error').show()
@setMergeButtonClass('btn-danger')
showCICoverage: (coverage) ->
text = 'Coverage ' + coverage + '%'
$('.ci_widget:visible .ci-coverage').text(text)
setMergeButtonClass: (css_class) ->
$('.js-merge-button,.accept-action .dropdown-toggle')
.removeClass('btn-danger btn-warning btn-create')
.addClass(css_class)