Class: WebHook

Inherits:
ApplicationRecord show all
Includes:
Sortable, WebHooks::AutoDisabling
Defined in:
app/models/hooks/web_hook.rb

Direct Known Subclasses

ProjectHook, ServiceHook, SystemHook

Constant Summary collapse

InterpolationError =
Class.new(StandardError)
SECRET_MASK =
'************'
VARIABLE_REFERENCE_RE =

See app/validators/json_schemas/web_hooks_url_variables.json

/\{([A-Za-z]+[0-9]*(?:[._-][A-Za-z0-9]+)*)\}/

Constants included from WebHooks::AutoDisabling

WebHooks::AutoDisabling::BACKOFF_GROWTH_FACTOR, WebHooks::AutoDisabling::ENABLED_HOOK_TYPES, WebHooks::AutoDisabling::EXCEEDED_FAILURE_THRESHOLD, WebHooks::AutoDisabling::FAILURE_THRESHOLD, WebHooks::AutoDisabling::INITIAL_BACKOFF, WebHooks::AutoDisabling::MAX_BACKOFF, WebHooks::AutoDisabling::MAX_FAILURES

Constants included from Gitlab::Loggable

Gitlab::Loggable::ANONYMOUS

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from ResetOnUnionError

ResetOnUnionError::MAX_RESET_PERIOD

Instance Method Summary collapse

Methods included from WebHooks::AutoDisabling

#alert_status, #backoff!, #enable!, #executable?, #failed!, #next_backoff, #permanently_disabled?, #temporarily_disabled?

Methods included from Gitlab::Loggable

#build_structured_payload

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, 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

Instance Method Details

#allow_local_requests?Boolean

Allow urls pointing localhost and the local network

Returns:

  • (Boolean)


68
69
70
# File 'app/models/hooks/web_hook.rb', line 68

def allow_local_requests?
  Gitlab::CurrentSettings.allow_local_requests_from_web_hooks_and_services?
end

#application_contextObject

Custom attributes to be included in the worker context.



92
93
94
# File 'app/models/hooks/web_hook.rb', line 92

def application_context
  { related_class: type }
end

#async_execute(data, hook_name) ⇒ Object

rubocop: disable CodeReuse/ServiceClass



62
63
64
# File 'app/models/hooks/web_hook.rb', line 62

def async_execute(data, hook_name)
  WebHookService.new(self, data, hook_name).async_execute if executable?
end

#execute(data, hook_name, force: false) ⇒ Object

rubocop: disable CodeReuse/ServiceClass



55
56
57
58
# File 'app/models/hooks/web_hook.rb', line 55

def execute(data, hook_name, force: false)
  # hook.executable? is checked in WebHookService#execute
  WebHookService.new(self, data, hook_name, force: force).execute
end

#help_pathObject



72
73
74
# File 'app/models/hooks/web_hook.rb', line 72

def help_path
  'user/project/integrations/webhooks'
end

#interpolated_url(url = self.url, url_variables = self.url_variables) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'app/models/hooks/web_hook.rb', line 108

def interpolated_url(url = self.url, url_variables = self.url_variables)
  return url unless url.include?('{')

  vars = url_variables
  url.gsub(VARIABLE_REFERENCE_RE) do
    vars.fetch(_1.delete_prefix('{').delete_suffix('}'))
  end
rescue KeyError => e
  raise InterpolationError, "Invalid URL template. Missing key #{e.key}"
end

#masked_tokenObject



123
124
125
# File 'app/models/hooks/web_hook.rb', line 123

def masked_token
  token.present? ? SECRET_MASK : nil
end

#parentObject

Returns the associated Project or Group for the WebHook if one exists. Overridden by inheriting classes.



88
89
# File 'app/models/hooks/web_hook.rb', line 88

def parent
end

#rate_limitInteger

Returns The rate limit for the WebHook. ‘0` for no limit.

Returns:

  • (Integer)

    The rate limit for the WebHook. ‘0` for no limit.



82
83
84
# File 'app/models/hooks/web_hook.rb', line 82

def rate_limit
  rate_limiter.limit
end

#rate_limited?Boolean

Returns Whether or not the WebHook is currently throttled.

Returns:

  • (Boolean)

    Whether or not the WebHook is currently throttled.



77
78
79
# File 'app/models/hooks/web_hook.rb', line 77

def rate_limited?
  rate_limiter.rate_limited?
end

#serializable_hash(options = nil) ⇒ Object

Exclude binary columns by default - they have no sensible JSON encoding



97
98
99
100
101
102
103
# File 'app/models/hooks/web_hook.rb', line 97

def serializable_hash(options = nil)
  options = options.try(:dup) || {}
  options[:except] = Array(options[:except]).dup
  options[:except].concat [:encrypted_url_variables, :encrypted_url_variables_iv]

  super(options)
end

#update_last_failureObject



119
120
121
# File 'app/models/hooks/web_hook.rb', line 119

def update_last_failure
  # Overridden in child classes.
end