2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# For an example companion mocking service, see https://gitlab.com/gitlab-org/gitlab-mock-ci-service
|
|
|
|
class MockCiService < CiService
|
|
|
|
ALLOWED_STATES = %w[failed canceled running pending success success_with_warnings skipped not_found].freeze
|
|
|
|
|
|
|
|
prop_accessor :mock_service_url
|
2018-11-08 19:23:39 +05:30
|
|
|
validates :mock_service_url, presence: true, public_url: true, if: :activated?
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
def title
|
|
|
|
'MockCI'
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
'Mock an external CI'
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.to_param
|
|
|
|
'mock_ci'
|
|
|
|
end
|
|
|
|
|
|
|
|
def fields
|
|
|
|
[
|
|
|
|
{ type: 'text',
|
|
|
|
name: 'mock_service_url',
|
2017-09-10 17:25:29 +05:30
|
|
|
placeholder: 'http://localhost:4004',
|
|
|
|
required: true }
|
2017-08-17 22:00:37 +05:30
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return complete url to build page
|
|
|
|
#
|
|
|
|
# Ex.
|
|
|
|
# http://jenkins.example.com:8888/job/test1/scm/bySHA1/12d65c
|
|
|
|
#
|
|
|
|
def build_page(sha, ref)
|
2019-01-03 12:48:30 +05:30
|
|
|
url = [mock_service_url,
|
|
|
|
"#{project.namespace.path}/#{project.path}/status/#{sha}"]
|
|
|
|
|
|
|
|
URI.join(*url).to_s
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# Return string with build status or :error symbol
|
|
|
|
#
|
|
|
|
# Allowed states: 'success', 'failed', 'running', 'pending', 'skipped'
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# Ex.
|
|
|
|
# @service.commit_status('13be4ac', 'master')
|
|
|
|
# # => 'success'
|
|
|
|
#
|
|
|
|
# @service.commit_status('2abe4ac', 'dev')
|
|
|
|
# # => 'running'
|
|
|
|
#
|
|
|
|
#
|
|
|
|
def commit_status(sha, ref)
|
2018-03-26 14:24:53 +05:30
|
|
|
response = Gitlab::HTTP.get(commit_status_path(sha), verify: false)
|
2017-08-17 22:00:37 +05:30
|
|
|
read_commit_status(response)
|
|
|
|
rescue Errno::ECONNREFUSED
|
|
|
|
:error
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_status_path(sha)
|
2019-01-03 12:48:30 +05:30
|
|
|
url = [mock_service_url,
|
|
|
|
"#{project.namespace.path}/#{project.path}/status/#{sha}.json"]
|
|
|
|
|
|
|
|
URI.join(*url).to_s
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def read_commit_status(response)
|
|
|
|
return :error unless response.code == 200 || response.code == 404
|
|
|
|
|
|
|
|
status = if response.code == 404
|
|
|
|
'pending'
|
|
|
|
else
|
|
|
|
response['status']
|
|
|
|
end
|
|
|
|
|
|
|
|
if status.present? && ALLOWED_STATES.include?(status)
|
|
|
|
status
|
|
|
|
else
|
|
|
|
:error
|
|
|
|
end
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
def can_test?
|
|
|
|
false
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|