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

123 lines
3.3 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
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-01-03 12:48:30 +05:30
url = [drone_url,
"gitlab/#{project.full_path}/commits/#{sha}",
"?branch=#{URI.encode(ref.to_s)}&access_token=#{token}"]
URI.join(*url).to_s
2015-09-25 12:07:36 +05:30
end
2017-08-17 22:00:37 +05:30
def commit_status(sha, ref)
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)
2018-03-26 14:24:53 +05:30
response = Gitlab::HTTP.get(commit_status_path(sha, ref), verify: enable_ssl_verification)
2015-09-25 12:07:36 +05:30
2017-08-17 22:00:37 +05:30
status =
if response.code == 200 && response['status']
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 }
rescue Errno::ECONNREFUSED
{ commit_status: :error }
2015-09-25 12:07:36 +05:30
end
2017-08-17 22:00:37 +05:30
def build_page(sha, ref)
2019-01-03 12:48:30 +05:30
url = [drone_url,
"gitlab/#{project.full_path}/redirect/commits/#{sha}",
"?branch=#{URI.encode(ref.to_s)}"]
URI.join(*url).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
private
def tag_push_valid?(data)
data[:total_commits_count] > 0 && !Gitlab::Git.blank_ref?(data[:after])
end
def push_valid?(data)
2015-12-23 02:04:40 +05:30
opened_merge_requests = project.merge_requests.opened.where(source_project_id: project.id,
2015-09-25 12:07:36 +05:30
source_branch: Gitlab::Git.ref_name(data[:ref]))
2015-12-23 02:04:40 +05:30
opened_merge_requests.empty? && data[:total_commits_count] > 0 &&
2015-09-25 12:07:36 +05:30
!Gitlab::Git.blank_ref?(data[:after])
end
def merge_request_valid?(data)
2017-09-10 17:25:29 +05:30
data[:object_attributes][:state] == 'opened' &&
2018-11-08 19:23:39 +05:30
MergeRequest.state_machines[:merge_status].check_state?(data[:object_attributes][:merge_status])
2015-09-25 12:07:36 +05:30
end
end