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
|
|
|
|
|
|
|
validates :drone_url, presence: true, url: true, if: :activated?
|
|
|
|
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)
|
2015-12-23 02:04:40 +05:30
|
|
|
url = [drone_url,
|
2017-08-17 22:00:37 +05:30
|
|
|
"gitlab/#{project.full_path}/commits/#{sha}",
|
|
|
|
"?branch=#{URI.encode(ref.to_s)}&access_token=#{token}"]
|
2015-09-25 12:07:36 +05:30
|
|
|
|
|
|
|
URI.join(*url).to_s
|
|
|
|
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)
|
2015-09-25 12:07:36 +05:30
|
|
|
response = HTTParty.get(commit_status_path(sha, ref), verify: enable_ssl_verification)
|
|
|
|
|
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)
|
2015-12-23 02:04:40 +05:30
|
|
|
url = [drone_url,
|
2017-08-17 22:00:37 +05:30
|
|
|
"gitlab/#{project.full_path}/redirect/commits/#{sha}",
|
|
|
|
"?branch=#{URI.encode(ref.to_s)}"]
|
2015-09-25 12:07:36 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
|
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' &&
|
2015-09-25 12:07:36 +05:30
|
|
|
data[:object_attributes][:merge_status] == 'unchecked'
|
|
|
|
end
|
|
|
|
end
|