BigW Consortium Gitlab

buildkite_service.rb 2.38 KB
Newer Older
1 2
require "addressable/uri"

3
class BuildkiteService < CiService
4 5
  include ReactiveService

6
  ENDPOINT = "https://buildkite.com".freeze
7

8 9
  prop_accessor :project_url, :token
  boolean_accessor :enable_ssl_verification
Keith Pitt committed
10

11
  validates :project_url, presence: true, url: true, if: :activated?
Keith Pitt committed
12 13 14 15 16
  validates :token, presence: true, if: :activated?

  after_save :compose_service_hook, if: :activated?

  def webhook_url
17
    "#{buildkite_endpoint('webhook')}/deliver/#{webhook_token}"
Keith Pitt committed
18 19 20 21 22
  end

  def compose_service_hook
    hook = service_hook || build_service_hook
    hook.url = webhook_url
23
    hook.enable_ssl_verification = !!enable_ssl_verification
Keith Pitt committed
24 25 26 27
    hook.save
  end

  def execute(data)
28
    return unless supported_events.include?(data[:object_kind])
29

Keith Pitt committed
30 31 32
    service_hook.execute(data)
  end

Valery Sizov committed
33
  def commit_status(sha, ref)
34
    with_reactive_cache(sha, ref) {|cached| cached[:commit_status] }
Keith Pitt committed
35 36 37
  end

  def commit_status_path(sha)
38
    "#{buildkite_endpoint('gitlab')}/status/#{status_token}.json?commit=#{sha}"
Keith Pitt committed
39 40
  end

Valery Sizov committed
41
  def build_page(sha, ref)
Keith Pitt committed
42 43 44 45
    "#{project_url}/builds?commit=#{sha}"
  end

  def title
46
    'Buildkite'
Keith Pitt committed
47 48 49 50 51 52
  end

  def description
    'Continuous integration and deployments'
  end

53
  def self.to_param
54
    'buildkite'
Keith Pitt committed
55 56 57 58 59 60
  end

  def fields
    [
      { type: 'text',
        name: 'token',
61
        placeholder: 'Buildkite project GitLab token', required: true },
Keith Pitt committed
62 63 64

      { type: 'text',
        name: 'project_url',
65
        placeholder: "#{ENDPOINT}/example/project", required: true },
66

67 68 69
      { type: 'checkbox',
        name: 'enable_ssl_verification',
        title: "Enable SSL verification" }
Keith Pitt committed
70 71 72
    ]
  end

73 74 75 76 77 78 79 80 81 82 83 84 85
  def calculate_reactive_cache(sha, ref)
    response = HTTParty.get(commit_status_path(sha), verify: false)

    status =
      if response.code == 200 && response['status']
        response['status']
      else
        :error
      end

    { commit_status: status }
  end

Keith Pitt committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  private

  def webhook_token
    token_parts.first
  end

  def status_token
    token_parts.second
  end

  def token_parts
    if token.present?
      token.split(':')
    else
      []
    end
  end

104
  def buildkite_endpoint(subdomain = nil)
Keith Pitt committed
105
    if subdomain.present?
106
      uri = Addressable::URI.parse(ENDPOINT)
Keith Pitt committed
107 108 109 110 111 112 113 114
      new_endpoint = "#{uri.scheme || 'http'}://#{subdomain}.#{uri.host}"

      if uri.port.present?
        "#{new_endpoint}:#{uri.port}"
      else
        new_endpoint
      end
    else
115
      ENDPOINT
Keith Pitt committed
116 117 118
    end
  end
end