merge_request_widget.js.coffee 2.98 KB
Newer Older
1 2 3 4 5 6 7
class @MergeRequestWidget
  # Initialize MergeRequestWidget behavior
  #
  #   check_enable           - Boolean, whether to check automerge status
  #   url_to_automerge_check - String, URL to use to check automerge status
  #   url_to_ci_check        - String, URL to use to check CI status
  #
8

9
  constructor: (@opts) ->
10
    @first = true
11
    modal = $('#modal_merge_info').modal(show: false)
Jacob Schatz's avatar
Jacob Schatz committed
12
    @getBuildStatus()
13
    @readyForCICheck = true
14
    # clear the build poller
15

16
  mergeInProgress: (deleteSourceBranch = false)->
17 18 19 20
    $.ajax
      type: 'GET'
      url: $('.merge-request').data('url')
      success: (data) =>
21
        if data.state == "merged"
22 23
          urlSuffix = if deleteSourceBranch then '?delete_source=true' else ''

24
          window.location.href = window.location.pathname + urlSuffix
25 26 27
        else if data.merge_error
          $('.mr-widget-body').html("<h4>" + data.merge_error + "</h4>")
        else
28 29
          callback = -> merge_request_widget.mergeInProgress(deleteSourceBranch)
          setTimeout(callback, 2000)
30 31 32 33 34 35
      dataType: 'json'

  getMergeStatus: ->
    $.get @opts.url_to_automerge_check, (data) ->
      $('.mr-state-widget').replaceWith(data)

36 37 38 39 40 41
  ciLabelForStatus: (status) ->
    if status == 'success'
      'passed'
    else
      status

Jacob Schatz's avatar
Jacob Schatz committed
42 43
  getBuildStatus: ->
    urlToCiCheck = @opts.url_to_ci_check
44 45
    _this = @
    @fetchBuildStatusInterval = setInterval (->
46 47
      if not _this.readyForCICheck
        return;
48
      $.getJSON urlToCiCheck, (data) ->
49 50 51 52
        _this.readyForCICheck = true
        if _this.first
          _this.first = false
          _this.opts.current_status = data.status
53 54 55
        if data.status isnt _this.opts.current_status
          notify("Build #{_this.ciLabelForStatus(data.status)}",
            _this.opts.ci_message.replace('{{status}}',
56 57 58
              _this.ciLabelForStatus(data.status)), 
            _this.opts.gitlab_icon)
          setTimeout (->
59
            Turbolinks.visit(location.href)
60 61
            return
          ), 2000
62
          _this.opts.current_status = data.status
Jacob Schatz's avatar
Jacob Schatz committed
63
        return
64
      _this.readyForCICheck = false
65
      return
66

67
    ), 5000
Jacob Schatz's avatar
Jacob Schatz committed
68

69
  getCiStatus: ->
Jacob Schatz's avatar
Jacob Schatz committed
70 71 72 73 74
    $.get @opts.url_to_ci_check, (data) =>
      this.showCiState data.status
      if data.coverage
        this.showCiCoverage data.coverage
    , 'json'
75 76 77

  showCiState: (state) ->
    $('.ci_widget').hide()
78
    allowed_states = ["failed", "canceled", "running", "pending", "success", "skipped", "not_found"]
79 80 81
    if state in allowed_states
      $('.ci_widget.ci-' + state).show()
      switch state
82
        when "failed", "canceled", "not_found"
83 84 85 86 87 88 89 90
          @setMergeButtonClass('btn-danger')
        when "running", "pending"
          @setMergeButtonClass('btn-warning')
    else
      $('.ci_widget.ci-error').show()
      @setMergeButtonClass('btn-danger')

  showCiCoverage: (coverage) ->
91 92
    text = 'Coverage ' + coverage + '%'
    $('.ci_widget:visible .ci-coverage').text(text)
93 94 95

  setMergeButtonClass: (css_class) ->
    $('.accept_merge_request').removeClass("btn-create").addClass(css_class)