Class: Integrations::DroneCi

Inherits:
Integration show all
Includes:
Base::Ci, EnableSslVerification, HasAvatar, HasWebHook, PushDataValidations, ReactivelyCached
Defined in:
app/models/integrations/drone_ci.rb

Constant Summary collapse

DRONE_SAAS_HOSTNAME =
'cloud.drone.io'

Constants included from Base::Integration

Base::Integration::BASE_ATTRIBUTES, Base::Integration::BASE_CLASSES, Base::Integration::DEV_INTEGRATION_NAMES, Base::Integration::ENCRYPTED_PROPERTIES_MAX_SIZE, Base::Integration::INSTANCE_LEVEL_ONLY_INTEGRATION_NAMES, Base::Integration::INTEGRATION_NAMES, Base::Integration::PROJECT_AND_GROUP_LEVEL_ONLY_INTEGRATION_NAMES, Base::Integration::PROJECT_LEVEL_ONLY_INTEGRATION_NAMES, Base::Integration::SECTION_TYPE_CONFIGURATION, Base::Integration::SECTION_TYPE_CONNECTION, Base::Integration::SECTION_TYPE_TRIGGER, Base::Integration::SNOWPLOW_EVENT_ACTION, Base::Integration::SNOWPLOW_EVENT_LABEL, Base::Integration::UnknownType

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::MAX_RESET_PERIOD

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PushDataValidations

#merge_request_valid?, #push_valid?, #tag_push_valid?

Methods included from HasAvatar

#avatar_url

Methods included from HasWebHook

#execute_web_hook!, #hook_ssl_verification

Methods included from Base::Ci

#valid_token?

Methods included from EnableSslVerification

#fields, #initialize_properties

Methods included from Base::Integration

#activate!, #activate_disabled_reason, #activated?, #after_build_from_integration, #api_field_names, #async_execute, #attributes, #attribution_notice, #category, #chat?, #ci?, #configurable_events, #deactivate!, #default_test_event, #description, #dup, #editable?, #event_channel_names, #event_names, #fields, #form_fields, #group_level?, #help, #inheritable?, #initialize_properties, #instance_level?, #json_fields, #manual_activation?, #operating?, #organization_id_from_parent, #parent, #project_level?, #reencrypt_properties, #reset_updated_properties, #secret_fields, #sections, #supported_events, #supports_data_fields?, #test, #testable?, #title, #to_database_hash, #to_param, #toggle!, #updated_properties, #validate_encrypted_properties_size_limit

Methods included from Gitlab::Utils::Override

#extended, extensions, #included, #method_added, #override, #prepended, #queue_verification, verify!

Methods inherited from ApplicationRecord

===, cached_column_list, #create_or_load_association, current_transaction, declarative_enum, default_select_columns, delete_all_returning, #deleted_from_database?, id_in, id_not_in, iid_in, nullable_column?, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from Organizations::Sharding

#sharding_organization

Methods included from ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Class Method Details

.attribution_noticeObject



126
127
128
# File 'app/models/integrations/drone_ci.rb', line 126

def self.attribution_notice
  'Drone CI icon and logo by Harness Inc. are licensed under CC NC-ND 4.0.'
end

.descriptionObject



94
95
96
# File 'app/models/integrations/drone_ci.rb', line 94

def self.description
  s_('ProjectService|Run CI/CD pipelines with Drone.')
end

.helpObject



98
99
100
# File 'app/models/integrations/drone_ci.rb', line 98

def self.help
  s_('ProjectService|Run CI/CD pipelines with Drone.')
end

.supported_eventsObject



45
46
47
# File 'app/models/integrations/drone_ci.rb', line 45

def self.supported_events
  %w[push merge_request tag_push]
end

.titleObject



90
91
92
# File 'app/models/integrations/drone_ci.rb', line 90

def self.title
  'Drone'
end

.to_paramObject



102
103
104
# File 'app/models/integrations/drone_ci.rb', line 102

def self.to_param
  'drone_ci'
end

Instance Method Details

#build_page(sha, ref) ⇒ Object



84
85
86
87
88
# File 'app/models/integrations/drone_ci.rb', line 84

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

#calculate_reactive_cache(sha, ref) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/integrations/drone_ci.rb', line 59

def calculate_reactive_cache(sha, ref)
  response = Clients::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



55
56
57
# File 'app/models/integrations/drone_ci.rb', line 55

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

#commit_status_path(sha, ref) ⇒ Object



49
50
51
52
53
# File 'app/models/integrations/drone_ci.rb', line 49

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

#enable_ssl_verificationObject



121
122
123
124
# File 'app/models/integrations/drone_ci.rb', line 121

def enable_ssl_verification
  original_value = Gitlab::Utils.to_boolean(properties['enable_ssl_verification'])
  original_value.nil? ? (new_record? || url_is_saas?) : original_value
end

#execute(data) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/integrations/drone_ci.rb', line 32

def execute(data)
  return unless project

  case data[:object_kind]
  when 'push'
    execute_web_hook!(data) if push_valid?(data)
  when 'merge_request'
    execute_web_hook!(data) if merge_request_valid?(data)
  when 'tag_push'
    execute_web_hook!(data) if tag_push_valid?(data)
  end
end

#hook_urlObject



107
108
109
# File 'app/models/integrations/drone_ci.rb', line 107

def hook_url
  [drone_url, "/hook", "?owner=#{project.namespace.full_path}", "&name=#{project.path}", "&access_token={token}"].join
end

#update_web_hook!Object



116
117
118
119
# File 'app/models/integrations/drone_ci.rb', line 116

def update_web_hook!
  # If using a service template, project may not be available
  super if project
end

#url_variablesObject



111
112
113
# File 'app/models/integrations/drone_ci.rb', line 111

def url_variables
  { 'token' => token }
end