summaryrefslogtreecommitdiff
path: root/lib/event_filter.rb
blob: 24fdcd6fbb102b4f88729caf87b65d2399cfa774 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# frozen_string_literal: true

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
  end

  def active?(key)
    filter == key.to_s
  end

  # rubocop: disable CodeReuse/ActiveRecord
  def apply_filter(events)
    case filter
    when PUSH
      events.where(action: Event::PUSHED)
    when MERGED
      events.where(action: Event::MERGED)
    when COMMENTS
      events.where(action: Event::COMMENTED)
    when TEAM
      events.where(action: [Event::JOINED, Event::LEFT, Event::EXPIRED])
    when ISSUE
      events.where(action: [Event::CREATED, Event::UPDATED, Event::CLOSED, Event::REOPENED])
    else
      events
    end
  end
  # rubocop: enable CodeReuse/ActiveRecord
end