diff options
author | Kirilll Zaitsev <kirik910@gmail.com> | 2015-08-27 02:58:49 +0300 |
---|---|---|
committer | Kirilll Zaitsev <kirik910@gmail.com> | 2015-09-02 05:52:16 +0300 |
commit | 263abda3fd7ddfb826cd17ae88fb47d0e1d67fae (patch) | |
tree | 14a7085fc1998bbb0d30b7d1e5dca71f6b599193 /app/models | |
parent | 308c6428aef2a46b0370a24d85a97b0e133283a8 (diff) |
Drone CI service
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/project.rb | 2 | ||||
-rw-r--r-- | app/models/project_services/ci_service.rb | 37 | ||||
-rw-r--r-- | app/models/project_services/drone_ci_service.rb | 170 | ||||
-rw-r--r-- | app/models/service.rb | 1 |
4 files changed, 206 insertions, 4 deletions
diff --git a/app/models/project.rb b/app/models/project.rb index 69f9af91c51..8e33a4b2f0f 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -73,6 +73,7 @@ class Project < ActiveRecord::Base has_many :services has_one :gitlab_ci_service, dependent: :destroy has_one :campfire_service, dependent: :destroy + has_one :drone_ci_service, dependent: :destroy has_one :emails_on_push_service, dependent: :destroy has_one :irker_service, dependent: :destroy has_one :pivotaltracker_service, dependent: :destroy @@ -613,6 +614,7 @@ class Project < ActiveRecord::Base name: name, ssh_url: ssh_url_to_repo, http_url: http_url_to_repo, + web_url: web_url, namespace: namespace.name, visibility_level: visibility_level } diff --git a/app/models/project_services/ci_service.rb b/app/models/project_services/ci_service.rb index 803402c83ee..88186113c68 100644 --- a/app/models/project_services/ci_service.rb +++ b/app/models/project_services/ci_service.rb @@ -25,12 +25,24 @@ class CiService < Service def category :ci end - + + def valid_token?(token) + self.respond_to?(:token) && self.token.present? && self.token == token + end + def supported_events %w(push) end - # Return complete url to build page + def merge_request_page(iid, sha, ref) + commit_page(sha, ref) + end + + def commit_page(sha, ref) + build_page(sha, ref) + end + + # Return complete url to merge_request page # # Ex. # http://jenkins.example.com:8888/job/test1/scm/bySHA1/12d65c @@ -45,10 +57,27 @@ class CiService < Service # # # Ex. - # @service.commit_status('13be4ac') + # @service.merge_request_status(9, '13be4ac', 'dev') + # # => 'success' + # + # @service.merge_request_status(10, '2abe4ac', 'dev) + # # => 'running' + # + # + def merge_request_status(iid, sha, ref) + commit_status(sha, ref) + 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') + # @service.commit_status('2abe4ac', 'dev') # # => 'running' # # diff --git a/app/models/project_services/drone_ci_service.rb b/app/models/project_services/drone_ci_service.rb new file mode 100644 index 00000000000..39f732c8113 --- /dev/null +++ b/app/models/project_services/drone_ci_service.rb @@ -0,0 +1,170 @@ +class DroneCiService < CiService + + prop_accessor :drone_url, :token, :enable_ssl_verification + validates :drone_url, + presence: true, + format: { with: /\A#{URI.regexp(%w(http https))}\z/, message: "should be a valid url" }, if: :activated? + validates :token, + presence: true, + format: { with: /\A([A-Za-z0-9]+)\z/ }, if: :activated? + + after_save :compose_service_hook, if: :activated? + + def compose_service_hook + hook = service_hook || build_service_hook + hook.url = [drone_url, "/api/hook", "?owner=#{project.namespace.path}", "&name=#{project.path}", "&access_token=#{token}"].join + hook.enable_ssl_verification = enable_ssl_verification + 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 + + def supported_events + %w(push merge_request tag_push) + end + + def merge_request_status_path(iid, sha = nil, ref = nil) + url = [drone_url, + "gitlab/#{project.namespace.path}/#{project.path}/pulls/#{iid}", + "?access_token=#{token}"] + + URI.join(*url).to_s + end + + def commit_status_path(sha, ref) + url = [drone_url, + "gitlab/#{project.namespace.path}/#{project.path}/commits/#{sha}", + "?branch=#{URI::encode(ref.to_s)}&access_token=#{token}"] + + URI.join(*url).to_s + end + + def merge_request_status(iid, sha, ref) + response = HTTParty.get(merge_request_status_path(iid), verify: enable_ssl_verification) + + if response.code == 200 and 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 + rescue Errno::ECONNREFUSED + :error + end + + def commit_status(sha, ref) + response = HTTParty.get(commit_status_path(sha, ref), verify: enable_ssl_verification) + + if response.code == 200 and 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 + rescue Errno::ECONNREFUSED + :error + end + + def merge_request_page(iid, sha, ref) + url = [drone_url, + "gitlab/#{project.namespace.path}/#{project.path}/redirect/pulls/#{iid}"] + + URI.join(*url).to_s + end + + def commit_page(sha, ref) + url = [drone_url, + "gitlab/#{project.namespace.path}/#{project.path}/redirect/commits/#{sha}", + "?branch=#{URI::encode(ref.to_s)}"] + + URI.join(*url).to_s + end + + def commit_coverage(sha, ref) + nil + end + + def build_page(sha, ref) + commit_page(sha, ref) + end + + def builds_path + url = [drone_url, "#{project.namespace.path}/#{project.path}"] + + URI.join(*url).to_s + end + + def status_img_path + url = [drone_url, + "api/badges/#{project.namespace.path}/#{project.path}/status.svg", + "?branch=#{URI::encode(project.default_branch)}"] + + 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 + + def to_param + 'drone_ci' + end + + def fields + [ + { type: 'text', name: 'token', placeholder: 'Drone CI project specific token' }, + { type: 'text', name: 'drone_url', placeholder: 'http://drone.example.com' }, + { 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) + opened_merge_requests = project.merge_requests.opened.where(source_project_id: project.id, + source_branch: Gitlab::Git.ref_name(data[:ref])) + + opened_merge_requests.empty? && data[:total_commits_count] > 0 && + !Gitlab::Git.blank_ref?(data[:after]) + end + + def merge_request_valid?(data) + ['opened', 'reopened'].include?(data[:object_attributes][:state]) && + data[:object_attributes][:merge_status] == 'unchecked' + end +end diff --git a/app/models/service.rb b/app/models/service.rb index dcef2866c3b..60fcc9d2857 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -135,6 +135,7 @@ class Service < ActiveRecord::Base buildkite campfire custom_issue_tracker + drone_ci emails_on_push external_wiki flowdock |