diff options
author | Douwe Maan <douwe@gitlab.com> | 2017-02-27 16:43:05 +0000 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2017-02-27 16:43:05 +0000 |
commit | 23659c52daf42a343a005924615cf428b8a2d7a2 (patch) | |
tree | 68d346a7e8b1f7f4cb12fc6262f1a8432c4eff68 /app/models/project_services | |
parent | 0599ced165b45ef10051c1e74d093846b6298103 (diff) | |
parent | 1c85d86d19d6cb0cb0da82ea062be3ccd351ffef (diff) | |
download | gitlab-ce-23659c52daf42a343a005924615cf428b8a2d7a2.tar.gz |
Merge branch 'mock-ci-service' into 'master'
Add Mock CI service/integration
See merge request !9250
Diffstat (limited to 'app/models/project_services')
-rw-r--r-- | app/models/project_services/mock_ci_service.rb | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/app/models/project_services/mock_ci_service.rb b/app/models/project_services/mock_ci_service.rb new file mode 100644 index 00000000000..a8d581a1f67 --- /dev/null +++ b/app/models/project_services/mock_ci_service.rb @@ -0,0 +1,82 @@ +# 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 + validates :mock_service_url, presence: true, url: true, if: :activated? + + 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', + placeholder: 'http://localhost:4004' }, + ] + end + + # Return complete url to build page + # + # Ex. + # http://jenkins.example.com:8888/job/test1/scm/bySHA1/12d65c + # + def build_page(sha, ref) + url = [mock_service_url, + "#{project.namespace.path}/#{project.path}/status/#{sha}"] + + URI.join(*url).to_s + 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) + response = HTTParty.get(commit_status_path(sha), verify: false) + read_commit_status(response) + rescue Errno::ECONNREFUSED + :error + end + + def commit_status_path(sha) + url = [mock_service_url, + "#{project.namespace.path}/#{project.path}/status/#{sha}.json"] + + URI.join(*url).to_s + 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 +end |