Class: DroneCiService

Inherits:
CiService show all
Defined in:
app/models/project_services/drone_ci_service.rb

Overview

Schema Information

Table name: services

id                    :integer          not null, primary key
type                  :string
title                 :string
project_id            :integer
created_at            :datetime         not null
updated_at            :datetime         not null
active                :boolean          not null
properties            :text
template              :boolean          default(FALSE)
push_events           :boolean          default(TRUE)
issues_events         :boolean          default(TRUE)
merge_requests_events :boolean          default(TRUE)
tag_push_events       :boolean          default(TRUE)
note_events           :boolean          default(TRUE), not null
build_events          :boolean          default(FALSE), not null
category              :string           default("common"), not null
default               :boolean          default(FALSE)
wiki_page_events      :boolean          default(TRUE)

Instance Method Summary collapse

Methods inherited from CiService

#valid_token?

Methods inherited from Service

#activated?, #async_execute, available_services_names, boolean_accessor, #can_test?, #category, create_from_template, #help, #initialize_properties, #issue_tracker?, prop_accessor, #reset_updated_properties, #template?, #test, #updated_properties

Instance Method Details

#allow_target_ci?Boolean

Returns:

  • (Boolean)

53
54
55
# File 'app/models/project_services/drone_ci_service.rb', line 53

def allow_target_ci?
  true
end

#build_page(sha, ref) ⇒ Object


136
137
138
# File 'app/models/project_services/drone_ci_service.rb', line 136

def build_page(sha, ref)
  commit_page(sha, ref)
end

#commit_coverage(sha, ref) ⇒ Object


132
133
134
# File 'app/models/project_services/drone_ci_service.rb', line 132

def commit_coverage(sha, ref)
  nil
end

#commit_page(sha, ref) ⇒ Object


124
125
126
127
128
129
130
# File 'app/models/project_services/drone_ci_service.rb', line 124

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

#commit_status(sha, ref) ⇒ Object


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/project_services/drone_ci_service.rb', line 97

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

#commit_status_path(sha, ref) ⇒ Object


69
70
71
72
73
74
75
# File 'app/models/project_services/drone_ci_service.rb', line 69

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

#compose_service_hookObject


34
35
36
37
38
39
40
# File 'app/models/project_services/drone_ci_service.rb', line 34

def compose_service_hook
  hook = service_hook || build_service_hook
  # If using a service template, project may not be available
  hook.url = [drone_url, "/api/hook", "?owner=#{project.namespace.path}", "&name=#{project.path}", "&access_token=#{token}"].join if project
  hook.enable_ssl_verification = !!enable_ssl_verification
  hook.save
end

#descriptionObject


144
145
146
# File 'app/models/project_services/drone_ci_service.rb', line 144

def description
  'Drone is a Continuous Integration platform built on Docker, written in Go'
end

#execute(data) ⇒ Object


42
43
44
45
46
47
48
49
50
51
# File 'app/models/project_services/drone_ci_service.rb', line 42

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

#fieldsObject


152
153
154
155
156
157
158
# File 'app/models/project_services/drone_ci_service.rb', line 152

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

#merge_request_page(iid, sha, ref) ⇒ Object


117
118
119
120
121
122
# File 'app/models/project_services/drone_ci_service.rb', line 117

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

#merge_request_status(iid, sha, ref) ⇒ Object


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/project_services/drone_ci_service.rb', line 77

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

#merge_request_status_path(iid, sha = nil, ref = nil) ⇒ Object


61
62
63
64
65
66
67
# File 'app/models/project_services/drone_ci_service.rb', line 61

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

#supported_eventsObject


57
58
59
# File 'app/models/project_services/drone_ci_service.rb', line 57

def supported_events
  %w(push merge_request tag_push)
end

#titleObject


140
141
142
# File 'app/models/project_services/drone_ci_service.rb', line 140

def title
  'Drone CI'
end

#to_paramObject


148
149
150
# File 'app/models/project_services/drone_ci_service.rb', line 148

def to_param
  'drone_ci'
end