debian-mirror-gitlab/app/models/project_services/drone_ci_service.rb

101 lines
2.7 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2015-09-25 12:07:36 +05:30
class DroneCiService < CiService
2017-08-17 22:00:37 +05:30
include ReactiveService
2019-09-30 21:07:59 +05:30
include ServicePushDataValidations
2017-08-17 22:00:37 +05:30
prop_accessor :drone_url, :token
boolean_accessor :enable_ssl_verification
2015-12-23 02:04:40 +05:30
2018-11-08 19:23:39 +05:30
validates :drone_url, presence: true, public_url: true, if: :activated?
2015-12-23 02:04:40 +05:30
validates :token, presence: true, if: :activated?
2015-09-25 12:07:36 +05:30
after_save :compose_service_hook, if: :activated?
def compose_service_hook
hook = service_hook || build_service_hook
2015-11-26 14:37:03 +05:30
# If using a service template, project may not be available
2017-08-17 22:00:37 +05:30
hook.url = [drone_url, "/api/hook", "?owner=#{project.namespace.full_path}", "&name=#{project.path}", "&access_token=#{token}"].join if project
2015-12-23 02:04:40 +05:30
hook.enable_ssl_verification = !!enable_ssl_verification
2015-09-25 12:07:36 +05:30
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
2017-08-17 22:00:37 +05:30
def self.supported_events
2015-09-25 12:07:36 +05:30
%w(push merge_request tag_push)
end
def commit_status_path(sha, ref)
2019-02-15 15:39:39 +05:30
Gitlab::Utils.append_path(
drone_url,
"gitlab/#{project.full_path}/commits/#{sha}?branch=#{URI.encode(ref.to_s)}&access_token=#{token}")
2015-09-25 12:07:36 +05:30
end
2017-08-17 22:00:37 +05:30
def commit_status(sha, ref)
2019-09-30 21:07:59 +05:30
with_reactive_cache(sha, ref) { |cached| cached[:commit_status] }
2015-09-25 12:07:36 +05:30
end
2017-08-17 22:00:37 +05:30
def calculate_reactive_cache(sha, ref)
2020-04-22 19:07:51 +05:30
response = Gitlab::HTTP.try_get(commit_status_path(sha, ref),
verify: enable_ssl_verification,
extra_log_info: { project_id: project_id })
2015-09-25 12:07:36 +05:30
2017-08-17 22:00:37 +05:30
status =
2020-04-22 19:07:51 +05:30
if response && response.code == 200 && response['status']
2017-08-17 22:00:37 +05:30
case response['status']
when 'killed'
:canceled
when 'failure', 'error'
# Because drone return error if some test env failed
:failed
else
response["status"]
end
2015-09-25 12:07:36 +05:30
else
2017-08-17 22:00:37 +05:30
:error
2015-09-25 12:07:36 +05:30
end
2017-08-17 22:00:37 +05:30
{ commit_status: status }
2015-09-25 12:07:36 +05:30
end
2017-08-17 22:00:37 +05:30
def build_page(sha, ref)
2019-02-15 15:39:39 +05:30
Gitlab::Utils.append_path(
drone_url,
"gitlab/#{project.full_path}/redirect/commits/#{sha}?branch=#{URI.encode(ref.to_s)}")
2015-09-25 12:07:36 +05:30
end
def title
'Drone CI'
end
def description
'Drone is a Continuous Integration platform built on Docker, written in Go'
end
2017-08-17 22:00:37 +05:30
def self.to_param
2015-09-25 12:07:36 +05:30
'drone_ci'
end
def fields
[
2017-09-10 17:25:29 +05:30
{ type: 'text', name: 'token', placeholder: 'Drone CI project specific token', required: true },
{ type: 'text', name: 'drone_url', placeholder: 'http://drone.example.com', required: true },
2015-09-25 12:07:36 +05:30
{ type: 'checkbox', name: 'enable_ssl_verification', title: "Enable SSL verification" }
]
end
end