Skip to content
Snippets Groups Projects
Commit e45cb9d2 authored by Phawin Khongkhasawan's avatar Phawin Khongkhasawan
Browse files

Apply 1 suggestion(s) to 1 file(s)


Co-authored-by: default avatarJames Nutt <jnutt@gitlab.com>
parent 47e5e0c4
No related branches found
No related tags found
No related merge requests found
This commit is part of merge request !151048. Comments created here will be created in the context of that merge request.
......@@ -7,7 +7,7 @@ class WebHookLogsFinder
def initialize(hook, current_user, params = {})
@hook = hook
@current_user = current_user
@params = { recent_only: true }.merge(params)
@params = params
end
def execute
......@@ -15,8 +15,7 @@ def execute
logs = @hook.web_hook_logs
logs = by_status_code(logs)
params[:recent_only] ? logs.recent(7) : logs
logs.recent(WebHookLog::MAX_RECENT_DAYS)
end
private
......@@ -33,6 +32,12 @@ def by_status_code(logs)
filters.map { |code| logs.by_status_code(code) }.reduce(:or)
end
def by_date_range(logs)
return logs unless params[:start_date] && params[:end_date]
logs.by_date_range(params[:start_date], params[:end_date])
end
def string_filter_to_code(status_string)
case status_string
when 'successful'
......@@ -47,3 +52,5 @@ def string_filter_to_code(status_string)
end
end
end
WebHooks::WebHookLogsFinder.prepend_mod
......@@ -7,6 +7,7 @@ class WebHookLog < ApplicationRecord
include PartitionedTable
OVERSIZE_REQUEST_DATA = { 'oversize' => true }.freeze
MAX_RECENT_DAYS = 7
attr_accessor :interpolated_url
......@@ -29,7 +30,10 @@ class WebHookLog < ApplicationRecord
scope :by_status_code, ->(status_code) { where(response_status: status_code) }
def self.recent(number_of_days = 2)
raise ArgumentError, '`recent` scope can only provide up to 7 days of log records' if number_of_days > 7
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)
......
......@@ -69,9 +69,10 @@ def event_names
it_behaves_like 'test web-hook endpoint'
it_behaves_like 'POST webhook API endpoints with a branch filter', '/projects/:id'
it_behaves_like 'PUT webhook API endpoints with a branch filter', '/projects/:id'
it_behaves_like 'web-hook API endpoints with branch-filter', '/projects/:id'
it_behaves_like 'get web-hook event endpoint' do
let(:unauthorized_user) { user3 }
end
it_behaves_like 'web-hook API endpoints with branch-filter', '/projects/:id'
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment