debian-mirror-gitlab/lib/event_filter.rb

43 lines
1 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'
FILTERS = [ALL, PUSH, MERGED, ISSUE, COMMENTS, TEAM].freeze
def initialize(filter)
# Split using comma to maintain backward compatibility Ex/ "filter1,filter2"
filter = filter.to_s.split(',')[0].to_s
@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
2017-08-17 22:00:37 +05:30
events.where(action: Event::PUSHED)
2018-12-05 23:21:45 +05:30
when MERGED
2017-08-17 22:00:37 +05:30
events.where(action: Event::MERGED)
2018-12-05 23:21:45 +05:30
when COMMENTS
2017-08-17 22:00:37 +05:30
events.where(action: Event::COMMENTED)
2018-12-05 23:21:45 +05:30
when TEAM
2017-08-17 22:00:37 +05:30
events.where(action: [Event::JOINED, Event::LEFT, Event::EXPIRED])
2018-12-05 23:21:45 +05:30
when ISSUE
2019-07-07 11:18:12 +05:30
events.where(action: [Event::CREATED, Event::UPDATED, Event::CLOSED, Event::REOPENED], target_type: 'Issue')
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
2014-09-02 18:07:02 +05:30
end