BigW Consortium Gitlab

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

3
class BuildkiteService < CiService
4 5
  ENDPOINT = "https://buildkite.com"

6
  prop_accessor :project_url, :token, :enable_ssl_verification
Keith Pitt committed
7

8
  validates :project_url, presence: true, url: true, if: :activated?
Keith Pitt committed
9 10 11 12 13
  validates :token, presence: true, if: :activated?

  after_save :compose_service_hook, if: :activated?

  def webhook_url
14
    "#{buildkite_endpoint('webhook')}/deliver/#{webhook_token}"
Keith Pitt committed
15 16 17 18 19
  end

  def compose_service_hook
    hook = service_hook || build_service_hook
    hook.url = webhook_url
20
    hook.enable_ssl_verification = !!enable_ssl_verification
Keith Pitt committed
21 22 23
    hook.save
  end

24 25 26 27
  def supported_events
    %w(push)
  end

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

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

Valery Sizov committed
34
  def commit_status(sha, ref)
Keith Pitt committed
35 36 37 38 39 40 41 42 43 44
    response = HTTParty.get(commit_status_path(sha), verify: false)

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

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

Valery Sizov committed
48
  def build_page(sha, ref)
Keith Pitt committed
49 50 51 52
    "#{project_url}/builds?commit=#{sha}"
  end

  def title
53
    'Buildkite'
Keith Pitt committed
54 55 56 57 58 59 60
  end

  def description
    'Continuous integration and deployments'
  end

  def to_param
61
    'buildkite'
Keith Pitt committed
62 63 64 65 66 67
  end

  def fields
    [
      { type: 'text',
        name: 'token',
68
        placeholder: 'Buildkite project GitLab token' },
Keith Pitt committed
69 70 71

      { type: 'text',
        name: 'project_url',
72
        placeholder: "#{ENDPOINT}/example/project" },
73

74 75 76
      { type: 'checkbox',
        name: 'enable_ssl_verification',
        title: "Enable SSL verification" }
Keith Pitt committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    ]
  end

  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

98
  def buildkite_endpoint(subdomain = nil)
Keith Pitt committed
99
    if subdomain.present?
100
      uri = Addressable::URI.parse(ENDPOINT)
Keith Pitt committed
101 102 103 104 105 106 107 108
      new_endpoint = "#{uri.scheme || 'http'}://#{subdomain}.#{uri.host}"

      if uri.port.present?
        "#{new_endpoint}:#{uri.port}"
      else
        new_endpoint
      end
    else
109
      ENDPOINT
Keith Pitt committed
110 111 112
    end
  end
end