Class: WebHookLog

Inherits:
ApplicationRecord show all
Includes:
CreatedAtFilterable, DeleteWithLimit, PartitionedTable, Presentable
Defined in:
app/models/hooks/web_hook_log.rb

Constant Summary collapse

OVERSIZE_REQUEST_DATA =
{ 'oversize' => true }.freeze
MAX_RECENT_DAYS =
7

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Constants included from HasCheckConstraints

HasCheckConstraints::NOT_NULL_CHECK_PATTERN

Constants included from ResetOnColumnErrors

ResetOnColumnErrors::MAX_RESET_PERIOD

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Presentable

#present

Methods inherited from ApplicationRecord

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

Methods included from ResetOnColumnErrors

#reset_on_union_error, #reset_on_unknown_attribute_error

Methods included from Gitlab::SensitiveSerializableHash

#serializable_hash

Instance Attribute Details

#interpolated_urlObject

Returns the value of attribute interpolated_url.



12
13
14
# File 'app/models/hooks/web_hook_log.rb', line 12

def interpolated_url
  @interpolated_url
end

Class Method Details

.delete_batch_for(web_hook, batch_size:) ⇒ Object

Delete a batch of log records. Returns true if there may be more remaining.

Raises:

  • (ArgumentError)


43
44
45
46
47
# File 'app/models/hooks/web_hook_log.rb', line 43

def self.delete_batch_for(web_hook, batch_size:)
  raise ArgumentError, 'batch_size is too small' if batch_size < 1

  where(web_hook: web_hook).limit(batch_size).delete_all == batch_size
end

.recent(number_of_days = 2) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'app/models/hooks/web_hook_log.rb', line 32

def self.recent(number_of_days = 2)
  if number_of_days > MAX_RECENT_DAYS
    raise ArgumentError,
      "`recent` scope can only provide up to #{MAX_RECENT_DAYS} days of log records"
  end

  where(created_at: number_of_days.days.ago.beginning_of_day..Time.zone.now)
    .order(created_at: :desc)
end

Instance Method Details

#idempotency_keyObject



67
68
69
# File 'app/models/hooks/web_hook_log.rb', line 67

def idempotency_key
  self[:request_headers]['Idempotency-Key']
end

#internal_error?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'app/models/hooks/web_hook_log.rb', line 53

def internal_error?
  response_status == WebHookService::InternalErrorResponse::ERROR_MESSAGE
end

#oversize?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'app/models/hooks/web_hook_log.rb', line 57

def oversize?
  request_data == OVERSIZE_REQUEST_DATA
end

#request_headersObject



61
62
63
64
65
# File 'app/models/hooks/web_hook_log.rb', line 61

def request_headers
  return super unless self[:request_headers]['X-Gitlab-Token']

  self[:request_headers].merge('X-Gitlab-Token' => _('[REDACTED]'))
end

#success?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'app/models/hooks/web_hook_log.rb', line 49

def success?
  response_status =~ /^2/
end

#url_current?Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
# File 'app/models/hooks/web_hook_log.rb', line 71

def url_current?
  # URL hash hasn't been set, so we must assume there's no prior value to
  # compare to.
  return true if url_hash.nil?

  Gitlab::CryptoHelper.sha256(web_hook.interpolated_url) == url_hash
end