Class: DroneCiService

Inherits:
CiService show all
Includes:
ReactiveService, ServicePushDataValidations
Defined in:
app/models/project_services/drone_ci_service.rb

Constant Summary

Constants inherited from Service

Service::DEV_SERVICE_NAMES, Service::SERVICE_NAMES

Instance Attribute Summary

Attributes included from Importable

#imported, #importing

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ServicePushDataValidations

#merge_request_valid?, #push_valid?, #tag_push_valid?

Methods inherited from CiService

#valid_token?

Methods inherited from Service

#activated?, #api_field_names, #async_execute, available_services_names, available_services_types, boolean_accessor, build_from_integration, #can_test?, #category, #configurable_event_actions, #configurable_events, default_integration, dev_services_names, #editable?, #event_channel_names, event_description, #event_field, #event_names, event_names, find_or_create_templates, find_or_initialize_all, find_or_initialize_integration, #global_fields, #help, #initialize_properties, instance_exists_for?, #issue_tracker?, #json_fields, #operating?, prop_accessor, #reset_updated_properties, services_names, services_types, #show_active_box?, supported_event_actions, #supported_events, #supports_data_fields?, #test, #to_data_fields_hash, #to_param, #to_service_hash, #updated_properties

Methods included from ProjectServicesLoggable

#build_message, #log_error, #log_info, #logger

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.supported_eventsObject


38
39
40
# File 'app/models/project_services/drone_ci_service.rb', line 38

def self.supported_events
  %w(push merge_request tag_push)
end

.to_paramObject


89
90
91
# File 'app/models/project_services/drone_ci_service.rb', line 89

def self.to_param
  'drone_ci'
end

Instance Method Details

#allow_target_ci?Boolean

Returns:

  • (Boolean)

34
35
36
# File 'app/models/project_services/drone_ci_service.rb', line 34

def allow_target_ci?
  true
end

#build_page(sha, ref) ⇒ Object


75
76
77
78
79
# File 'app/models/project_services/drone_ci_service.rb', line 75

def build_page(sha, ref)
  Gitlab::Utils.append_path(
    drone_url,
    "gitlab/#{project.full_path}/redirect/commits/#{sha}?branch=#{URI.encode(ref.to_s)}")
end

#calculate_reactive_cache(sha, ref) ⇒ Object


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/project_services/drone_ci_service.rb', line 52

def calculate_reactive_cache(sha, ref)
  response = Gitlab::HTTP.try_get(commit_status_path(sha, ref),
    verify: enable_ssl_verification,
    extra_log_info: { project_id: project_id })

  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

#commit_status(sha, ref) ⇒ Object


48
49
50
# File 'app/models/project_services/drone_ci_service.rb', line 48

def commit_status(sha, ref)
  with_reactive_cache(sha, ref) { |cached| cached[:commit_status] }
end

#commit_status_path(sha, ref) ⇒ Object


42
43
44
45
46
# File 'app/models/project_services/drone_ci_service.rb', line 42

def commit_status_path(sha, ref)
  Gitlab::Utils.append_path(
    drone_url,
    "gitlab/#{project.full_path}/commits/#{sha}?branch=#{URI.encode(ref.to_s)}&access_token=#{token}")
end

#compose_service_hookObject


15
16
17
18
19
20
21
# File 'app/models/project_services/drone_ci_service.rb', line 15

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.full_path}", "&name=#{project.path}", "&access_token=#{token}"].join if project
  hook.enable_ssl_verification = !!enable_ssl_verification
  hook.save
end

#descriptionObject


85
86
87
# File 'app/models/project_services/drone_ci_service.rb', line 85

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

#execute(data) ⇒ Object


23
24
25
26
27
28
29
30
31
32
# File 'app/models/project_services/drone_ci_service.rb', line 23

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


93
94
95
96
97
98
99
# File 'app/models/project_services/drone_ci_service.rb', line 93

def fields
  [
    { type: 'text', name: 'token', placeholder: 'Drone CI project specific token', required: true },
    { type: 'text', name: 'drone_url', placeholder: 'http://drone.example.com', required: true },
    { type: 'checkbox', name: 'enable_ssl_verification', title: "Enable SSL verification" }
  ]
end

#titleObject


81
82
83
# File 'app/models/project_services/drone_ci_service.rb', line 81

def title
  'Drone CI'
end