BigW Consortium Gitlab

drone_ci_service.rb 3.24 KB
Newer Older
Kirilll Zaitsev committed
1
class DroneCiService < CiService
2 3
  include ReactiveService

4 5
  prop_accessor :drone_url, :token
  boolean_accessor :enable_ssl_verification
6 7 8

  validates :drone_url, presence: true, url: true, if: :activated?
  validates :token, presence: true, if: :activated?
Kirilll Zaitsev committed
9 10 11 12 13

  after_save :compose_service_hook, if: :activated?

  def compose_service_hook
    hook = service_hook || build_service_hook
14
    # If using a service template, project may not be available
15
    hook.url = [drone_url, "/api/hook", "?owner=#{project.namespace.full_path}", "&name=#{project.path}", "&access_token=#{token}"].join if project
16
    hook.enable_ssl_verification = !!enable_ssl_verification
Kirilll Zaitsev committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    hook.save
  end

  def execute(data)
    case data[:object_kind]
    when 'push'
      service_hook.execute(data) if push_valid?(data)
    when 'merge_request'
      service_hook.execute(data) if merge_request_valid?(data)
    when 'tag_push'
      service_hook.execute(data) if tag_push_valid?(data)
    end
  end

  def allow_target_ci?
    true
  end

35
  def self.supported_events
Kirilll Zaitsev committed
36 37 38 39
    %w(push merge_request tag_push)
  end

  def commit_status_path(sha, ref)
40
    url = [drone_url,
41
           "gitlab/#{project.full_path}/commits/#{sha}",
42
           "?branch=#{URI.encode(ref.to_s)}&access_token=#{token}"]
Kirilll Zaitsev committed
43 44 45 46

    URI.join(*url).to_s
  end

47 48
  def commit_status(sha, ref)
    with_reactive_cache(sha, ref) {|cached| cached[:commit_status] }
Kirilll Zaitsev committed
49 50
  end

51
  def calculate_reactive_cache(sha, ref)
Kirilll Zaitsev committed
52 53
    response = HTTParty.get(commit_status_path(sha, ref), verify: enable_ssl_verification)

54
    status =
Douwe Maan committed
55
      if response.code == 200 && response['status']
56 57 58 59 60 61 62 63 64
        case response['status']
        when 'killed'
          :canceled
        when 'failure', 'error'
          # Because drone return error if some test env failed
          :failed
        else
          response["status"]
        end
Kirilll Zaitsev committed
65
      else
66
        :error
Kirilll Zaitsev committed
67 68
      end

69 70 71
    { commit_status: status }
  rescue Errno::ECONNREFUSED
    { commit_status: :error }
Kirilll Zaitsev committed
72 73
  end

74
  def build_page(sha, ref)
75
    url = [drone_url,
76
           "gitlab/#{project.full_path}/redirect/commits/#{sha}",
77
           "?branch=#{URI.encode(ref.to_s)}"]
Kirilll Zaitsev committed
78 79 80 81 82 83 84 85 86 87 88 89

    URI.join(*url).to_s
  end

  def title
    'Drone CI'
  end

  def description
    'Drone is a Continuous Integration platform built on Docker, written in Go'
  end

90
  def self.to_param
Kirilll Zaitsev committed
91 92 93 94 95
    'drone_ci'
  end

  def fields
    [
96 97
      { type: 'text', name: 'token', placeholder: 'Drone CI project specific token', required: true },
      { type: 'text', name: 'drone_url', placeholder: 'http://drone.example.com', required: true },
Kirilll Zaitsev committed
98 99 100 101 102 103 104 105 106 107 108
      { type: 'checkbox', name: 'enable_ssl_verification', title: "Enable SSL verification" }
    ]
  end

  private

  def tag_push_valid?(data)
    data[:total_commits_count] > 0 && !Gitlab::Git.blank_ref?(data[:after])
  end

  def push_valid?(data)
109
    opened_merge_requests = project.merge_requests.opened.where(source_project_id: project.id,
Kirilll Zaitsev committed
110 111
                                                                source_branch: Gitlab::Git.ref_name(data[:ref]))

112
    opened_merge_requests.empty? && data[:total_commits_count] > 0 &&
Kirilll Zaitsev committed
113 114 115 116
      !Gitlab::Git.blank_ref?(data[:after])
  end

  def merge_request_valid?(data)
117
    data[:object_attributes][:state] == 'opened' &&
Kirilll Zaitsev committed
118 119 120
      data[:object_attributes][:merge_status] == 'unchecked'
  end
end