2015-08-26 19:58:49 -04:00
|
|
|
class DroneCiService < CiService
|
2017-01-12 17:31:02 -05:00
|
|
|
include ReactiveService
|
|
|
|
|
2016-12-05 09:40:53 -05:00
|
|
|
prop_accessor :drone_url, :token
|
|
|
|
boolean_accessor :enable_ssl_verification
|
2015-12-01 18:45:36 -05:00
|
|
|
|
|
|
|
validates :drone_url, presence: true, url: true, if: :activated?
|
|
|
|
validates :token, presence: true, if: :activated?
|
2015-08-26 19:58:49 -04:00
|
|
|
|
|
|
|
after_save :compose_service_hook, if: :activated?
|
|
|
|
|
|
|
|
def compose_service_hook
|
|
|
|
hook = service_hook || build_service_hook
|
2015-11-20 11:13:25 -05:00
|
|
|
# If using a service template, project may not be available
|
2017-02-14 09:03:24 -05:00
|
|
|
hook.url = [drone_url, "/api/hook", "?owner=#{project.namespace.full_path}", "&name=#{project.path}", "&access_token=#{token}"].join if project
|
2015-12-03 03:33:38 -05:00
|
|
|
hook.enable_ssl_verification = !!enable_ssl_verification
|
2015-08-26 19:58:49 -04:00
|
|
|
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
|
|
|
|
|
2016-12-27 07:44:24 -05:00
|
|
|
def self.supported_events
|
2015-08-26 19:58:49 -04:00
|
|
|
%w(push merge_request tag_push)
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_status_path(sha, ref)
|
2015-12-01 18:45:36 -05:00
|
|
|
url = [drone_url,
|
2017-02-14 09:03:24 -05:00
|
|
|
"gitlab/#{project.full_path}/commits/#{sha}",
|
2017-02-21 19:51:36 -05:00
|
|
|
"?branch=#{URI.encode(ref.to_s)}&access_token=#{token}"]
|
2015-08-26 19:58:49 -04:00
|
|
|
|
|
|
|
URI.join(*url).to_s
|
|
|
|
end
|
|
|
|
|
2017-01-12 17:31:02 -05:00
|
|
|
def commit_status(sha, ref)
|
|
|
|
with_reactive_cache(sha, ref) {|cached| cached[:commit_status] }
|
2015-08-26 19:58:49 -04:00
|
|
|
end
|
|
|
|
|
2017-01-12 17:31:02 -05:00
|
|
|
def calculate_reactive_cache(sha, ref)
|
2015-08-26 19:58:49 -04:00
|
|
|
response = HTTParty.get(commit_status_path(sha, ref), verify: enable_ssl_verification)
|
|
|
|
|
2017-01-12 17:31:02 -05:00
|
|
|
status =
|
2017-02-21 17:31:14 -05:00
|
|
|
if response.code == 200 && response['status']
|
2017-01-12 17:31:02 -05:00
|
|
|
case response['status']
|
|
|
|
when 'killed'
|
|
|
|
:canceled
|
|
|
|
when 'failure', 'error'
|
|
|
|
# Because drone return error if some test env failed
|
|
|
|
:failed
|
|
|
|
else
|
|
|
|
response["status"]
|
|
|
|
end
|
2015-08-26 19:58:49 -04:00
|
|
|
else
|
2017-01-12 17:31:02 -05:00
|
|
|
:error
|
2015-08-26 19:58:49 -04:00
|
|
|
end
|
|
|
|
|
2017-01-12 17:31:02 -05:00
|
|
|
{ commit_status: status }
|
|
|
|
rescue Errno::ECONNREFUSED
|
|
|
|
{ commit_status: :error }
|
2015-08-26 19:58:49 -04:00
|
|
|
end
|
|
|
|
|
2017-01-12 17:31:02 -05:00
|
|
|
def build_page(sha, ref)
|
2015-12-01 18:45:36 -05:00
|
|
|
url = [drone_url,
|
2017-02-14 09:03:24 -05:00
|
|
|
"gitlab/#{project.full_path}/redirect/commits/#{sha}",
|
2017-02-21 19:51:36 -05:00
|
|
|
"?branch=#{URI.encode(ref.to_s)}"]
|
2015-08-26 19:58:49 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-12-27 07:44:24 -05:00
|
|
|
def self.to_param
|
2015-08-26 19:58:49 -04:00
|
|
|
'drone_ci'
|
|
|
|
end
|
|
|
|
|
|
|
|
def fields
|
|
|
|
[
|
2017-05-22 06:07:12 -04:00
|
|
|
{ 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-08-26 19:58:49 -04:00
|
|
|
{ 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-01 18:45:36 -05:00
|
|
|
opened_merge_requests = project.merge_requests.opened.where(source_project_id: project.id,
|
2015-08-26 19:58:49 -04:00
|
|
|
source_branch: Gitlab::Git.ref_name(data[:ref]))
|
|
|
|
|
2015-12-01 18:45:36 -05:00
|
|
|
opened_merge_requests.empty? && data[:total_commits_count] > 0 &&
|
2015-08-26 19:58:49 -04:00
|
|
|
!Gitlab::Git.blank_ref?(data[:after])
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_request_valid?(data)
|
2017-02-22 12:46:57 -05:00
|
|
|
%w(opened reopened).include?(data[:object_attributes][:state]) &&
|
2015-08-26 19:58:49 -04:00
|
|
|
data[:object_attributes][:merge_status] == 'unchecked'
|
|
|
|
end
|
|
|
|
end
|