2018-09-06 10:07:33 +00:00
|
|
|
# frozen_string_literal: true
|
2012-11-05 18:12:26 +00:00
|
|
|
|
2018-09-06 10:07:33 +00:00
|
|
|
class EventFilter
|
|
|
|
attr_accessor :filter
|
|
|
|
|
|
|
|
ALL = 'all'
|
|
|
|
PUSH = 'push'
|
|
|
|
MERGED = 'merged'
|
|
|
|
ISSUE = 'issue'
|
|
|
|
COMMENTS = 'comments'
|
|
|
|
TEAM = 'team'
|
2020-03-24 12:09:42 +00:00
|
|
|
WIKI = 'wiki'
|
2018-09-06 10:07:33 +00:00
|
|
|
|
|
|
|
def initialize(filter)
|
|
|
|
# Split using comma to maintain backward compatibility Ex/ "filter1,filter2"
|
|
|
|
filter = filter.to_s.split(',')[0].to_s
|
2019-10-11 06:06:27 +00:00
|
|
|
@filter = filters.include?(filter) ? filter : ALL
|
2012-11-05 18:12:26 +00:00
|
|
|
end
|
|
|
|
|
2018-09-06 10:07:33 +00:00
|
|
|
def active?(key)
|
|
|
|
filter == key.to_s
|
2012-11-05 18:12:26 +00:00
|
|
|
end
|
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2014-09-25 22:07:40 +00:00
|
|
|
def apply_filter(events)
|
2020-03-24 12:09:42 +00:00
|
|
|
events = apply_feature_flags(events)
|
|
|
|
|
2018-09-06 10:07:33 +00:00
|
|
|
case filter
|
|
|
|
when PUSH
|
2016-11-22 17:11:34 +00:00
|
|
|
events.where(action: Event::PUSHED)
|
2018-09-06 10:07:33 +00:00
|
|
|
when MERGED
|
2016-11-22 17:11:34 +00:00
|
|
|
events.where(action: Event::MERGED)
|
2018-09-06 10:07:33 +00:00
|
|
|
when COMMENTS
|
2016-11-22 17:11:34 +00:00
|
|
|
events.where(action: Event::COMMENTED)
|
2018-09-06 10:07:33 +00:00
|
|
|
when TEAM
|
2016-11-22 17:11:34 +00:00
|
|
|
events.where(action: [Event::JOINED, Event::LEFT, Event::EXPIRED])
|
2018-09-06 10:07:33 +00:00
|
|
|
when ISSUE
|
2019-03-06 15:22:45 +00:00
|
|
|
events.where(action: [Event::CREATED, Event::UPDATED, Event::CLOSED, Event::REOPENED], target_type: 'Issue')
|
2020-03-24 12:09:42 +00:00
|
|
|
when WIKI
|
|
|
|
wiki_events(events)
|
2012-11-05 18:12:26 +00:00
|
|
|
else
|
2018-09-06 10:07:33 +00:00
|
|
|
events
|
2016-11-22 17:11:34 +00:00
|
|
|
end
|
2012-11-05 18:12:26 +00:00
|
|
|
end
|
2018-09-06 10:07:33 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2019-10-11 06:06:27 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-03-24 12:09:42 +00:00
|
|
|
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-10-11 06:06:27 +00:00
|
|
|
def filters
|
2020-03-24 12:09:42 +00:00
|
|
|
[ALL, PUSH, MERGED, ISSUE, COMMENTS, TEAM, WIKI]
|
2019-10-11 06:06:27 +00:00
|
|
|
end
|
2012-11-05 18:12:26 +00:00
|
|
|
end
|
2019-10-11 06:06:27 +00:00
|
|
|
|
|
|
|
EventFilter.prepend_if_ee('EE::EventFilter')
|