BigW Consortium Gitlab

merge_request_widget.js.coffee 2.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
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
  #   current_status         - String, current automerge status
  #   ci_enable              - Boolean, whether a CI service is enabled
  #   url_to_ci_check        - String, URL to use to check CI status
  #
  constructor: (@opts) ->
    modal = $('#modal_merge_info').modal(show: false)

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

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

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

  getCiStatus: ->
    if @opts.ci_enable
      $.get @opts.url_to_ci_check, (data) =>
        this.showCiState data.status
        if data.coverage
          this.showCiCoverage data.coverage
      , 'json'

  showCiState: (state) ->
    $('.ci_widget').hide()
43
    allowed_states = ["failed", "canceled", "running", "pending", "success", "skipped", "not_found"]
44 45 46
    if state in allowed_states
      $('.ci_widget.ci-' + state).show()
      switch state
47
        when "failed", "canceled", "not_found"
48 49 50 51 52 53 54 55
          @setMergeButtonClass('btn-danger')
        when "running", "pending"
          @setMergeButtonClass('btn-warning')
    else
      $('.ci_widget.ci-error').show()
      @setMergeButtonClass('btn-danger')

  showCiCoverage: (coverage) ->
56 57
    text = 'Coverage ' + coverage + '%'
    $('.ci_widget:visible .ci-coverage').text(text)
58 59 60

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