2021-05-25 14:10:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Integrations
|
|
|
|
class DroneCi < BaseCi
|
2021-07-14 14:08:31 -04:00
|
|
|
include HasWebHook
|
2021-11-14 19:10:51 -05:00
|
|
|
include PushDataValidations
|
2021-11-12 13:12:20 -05:00
|
|
|
include ReactivelyCached
|
2022-02-03 19:13:53 -05:00
|
|
|
prepend EnableSslVerification
|
2021-07-14 14:08:31 -04:00
|
|
|
extend Gitlab::Utils::Override
|
2021-05-25 14:10:42 -04:00
|
|
|
|
2022-02-03 19:13:53 -05:00
|
|
|
DRONE_SAAS_HOSTNAME = 'cloud.drone.io'
|
|
|
|
|
2022-05-05 05:08:00 -04:00
|
|
|
field :drone_url,
|
2022-06-16 14:09:35 -04:00
|
|
|
title: -> { s_('ProjectService|Drone server URL') },
|
2022-05-05 05:08:00 -04:00
|
|
|
placeholder: 'http://drone.example.com',
|
|
|
|
required: true
|
|
|
|
|
|
|
|
field :token,
|
|
|
|
type: 'password',
|
2022-06-16 14:09:35 -04:00
|
|
|
help: -> { s_('ProjectService|Token for the Drone project.') },
|
|
|
|
non_empty_password_title: -> { s_('ProjectService|Enter new token') },
|
|
|
|
non_empty_password_help: -> { s_('ProjectService|Leave blank to use your current token.') },
|
2022-05-05 05:08:00 -04:00
|
|
|
required: true
|
2021-05-25 14:10:42 -04:00
|
|
|
|
|
|
|
validates :drone_url, presence: true, public_url: true, if: :activated?
|
|
|
|
validates :token, presence: true, if: :activated?
|
|
|
|
|
|
|
|
def execute(data)
|
2021-07-14 14:08:31 -04:00
|
|
|
return unless project
|
|
|
|
|
2021-05-25 14:10:42 -04:00
|
|
|
case data[:object_kind]
|
|
|
|
when 'push'
|
2021-07-14 14:08:31 -04:00
|
|
|
execute_web_hook!(data) if push_valid?(data)
|
2021-05-25 14:10:42 -04:00
|
|
|
when 'merge_request'
|
2021-07-14 14:08:31 -04:00
|
|
|
execute_web_hook!(data) if merge_request_valid?(data)
|
2021-05-25 14:10:42 -04:00
|
|
|
when 'tag_push'
|
2021-07-14 14:08:31 -04:00
|
|
|
execute_web_hook!(data) if tag_push_valid?(data)
|
2021-05-25 14:10:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def allow_target_ci?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.supported_events
|
|
|
|
%w(push merge_request tag_push)
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_status_path(sha, ref)
|
|
|
|
Gitlab::Utils.append_path(
|
|
|
|
drone_url,
|
|
|
|
"gitlab/#{project.full_path}/commits/#{sha}?branch=#{Addressable::URI.encode_component(ref.to_s)}&access_token=#{token}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_status(sha, ref)
|
|
|
|
with_reactive_cache(sha, ref) { |cached| cached[:commit_status] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def calculate_reactive_cache(sha, ref)
|
2021-07-01 17:08:38 -04:00
|
|
|
response = Gitlab::HTTP.try_get(
|
|
|
|
commit_status_path(sha, ref),
|
2021-05-25 14:10:42 -04:00
|
|
|
verify: enable_ssl_verification,
|
2022-07-05 17:08:45 -04:00
|
|
|
extra_log_info: { project_id: project_id }
|
2021-07-01 17:08:38 -04:00
|
|
|
)
|
2021-05-25 14:10:42 -04:00
|
|
|
|
|
|
|
status =
|
|
|
|
if response && 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
|
|
|
|
else
|
|
|
|
:error
|
|
|
|
end
|
|
|
|
|
|
|
|
{ commit_status: status }
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_page(sha, ref)
|
|
|
|
Gitlab::Utils.append_path(
|
|
|
|
drone_url,
|
|
|
|
"gitlab/#{project.full_path}/redirect/commits/#{sha}?branch=#{Addressable::URI.encode_component(ref.to_s)}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def title
|
|
|
|
'Drone'
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
s_('ProjectService|Run CI/CD pipelines with Drone.')
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.to_param
|
|
|
|
'drone_ci'
|
|
|
|
end
|
|
|
|
|
|
|
|
def help
|
|
|
|
s_('ProjectService|Run CI/CD pipelines with Drone.')
|
|
|
|
end
|
|
|
|
|
2021-07-14 14:08:31 -04:00
|
|
|
override :hook_url
|
|
|
|
def hook_url
|
|
|
|
[drone_url, "/hook", "?owner=#{project.namespace.full_path}", "&name=#{project.path}", "&access_token=#{token}"].join
|
|
|
|
end
|
|
|
|
|
|
|
|
override :update_web_hook!
|
|
|
|
def update_web_hook!
|
|
|
|
# If using a service template, project may not be available
|
|
|
|
super if project
|
|
|
|
end
|
2022-02-03 19:13:53 -05:00
|
|
|
|
|
|
|
def enable_ssl_verification
|
|
|
|
original_value = Gitlab::Utils.to_boolean(properties['enable_ssl_verification'])
|
|
|
|
original_value.nil? ? (new_record? || url_is_saas?) : original_value
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def url_is_saas?
|
|
|
|
parsed_url = Addressable::URI.parse(drone_url)
|
|
|
|
parsed_url&.scheme == 'https' && parsed_url.hostname == DRONE_SAAS_HOSTNAME
|
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
false
|
|
|
|
end
|
2021-05-25 14:10:42 -04:00
|
|
|
end
|
|
|
|
end
|