debian-mirror-gitlab/lib/event_filter.rb

67 lines
1.4 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
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'
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)
2020-04-22 19:07:51 +05:30
events = apply_feature_flags(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)
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 apply_feature_flags(events)
return events.not_wiki_page unless Feature.enabled?(:wiki_events)
events
end
def wiki_events(events)
return events unless Feature.enabled?(:wiki_events)
events.for_wiki_page
end
2019-12-21 20:55:43 +05:30
def filters
2020-04-22 19:07:51 +05:30
[ALL, PUSH, MERGED, ISSUE, COMMENTS, TEAM, WIKI]
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
EventFilter.prepend_if_ee('EE::EventFilter')