debian-mirror-gitlab/lib/event_filter.rb

66 lines
1.3 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
2018-12-05 23:21:45 +05:30
class EventFilter
2020-07-28 23:09:34 +05:30
include Gitlab::Utils::StrongMemoize
2018-12-05 23:21:45 +05:30
attr_accessor :filter
ALL = 'all'
PUSH = 'push'
MERGED = 'merged'
ISSUE = 'issue'
COMMENTS = 'comments'
TEAM = 'team'
2020-04-22 19:07:51 +05:30
WIKI = 'wiki'
2020-07-28 23:09:34 +05:30
DESIGNS = 'designs'
2018-12-05 23:21:45 +05:30
def initialize(filter)
# Split using comma to maintain backward compatibility Ex/ "filter1,filter2"
filter = filter.to_s.split(',')[0].to_s
2019-12-21 20:55:43 +05:30
@filter = filters.include?(filter) ? filter : ALL
2014-09-02 18:07:02 +05:30
end
2018-12-05 23:21:45 +05:30
def active?(key)
filter == key.to_s
2014-09-02 18:07:02 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2015-04-26 12:48:37 +05:30
def apply_filter(events)
2018-12-05 23:21:45 +05:30
case filter
when PUSH
2020-06-23 00:09:42 +05:30
events.pushed_action
2018-12-05 23:21:45 +05:30
when MERGED
2020-06-23 00:09:42 +05:30
events.merged_action
2018-12-05 23:21:45 +05:30
when COMMENTS
2020-06-23 00:09:42 +05:30
events.commented_action
2018-12-05 23:21:45 +05:30
when TEAM
2020-06-23 00:09:42 +05:30
events.where(action: [:joined, :left, :expired])
2018-12-05 23:21:45 +05:30
when ISSUE
2020-06-23 00:09:42 +05:30
events.where(action: [:created, :updated, :closed, :reopened], target_type: 'Issue')
2020-04-22 19:07:51 +05:30
when WIKI
wiki_events(events)
2020-07-28 23:09:34 +05:30
when DESIGNS
design_events(events)
2017-08-17 22:00:37 +05:30
else
2018-12-05 23:21:45 +05:30
events
2017-08-17 22:00:37 +05:30
end
2014-09-02 18:07:02 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2019-12-21 20:55:43 +05:30
private
2020-04-22 19:07:51 +05:30
def wiki_events(events)
events.for_wiki_page
end
2020-07-28 23:09:34 +05:30
def design_events(events)
events.for_design
end
2019-12-21 20:55:43 +05:30
def filters
2020-07-28 23:09:34 +05:30
[ALL, PUSH, MERGED, ISSUE, COMMENTS, TEAM, WIKI, DESIGNS]
2019-12-21 20:55:43 +05:30
end
2014-09-02 18:07:02 +05:30
end
2019-12-21 20:55:43 +05:30
2021-06-08 01:23:25 +05:30
EventFilter.prepend_mod_with('EventFilter')