BigW Consortium Gitlab

pushover_service.rb 2.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class PushoverService < Service
  include HTTParty
  base_uri 'https://api.pushover.net/1'

  prop_accessor :api_key, :user_key, :device, :priority, :sound
  validates :api_key, :user_key, :priority, presence: true, if: :activated?

  def title
    'Pushover'
  end

  def description
    'Pushover makes it easy to get real-time notifications on your Android device, iPhone, iPad, and Desktop.'
  end

16
  def self.to_param
17 18 19 20 21
    'pushover'
  end

  def fields
    [
22 23
      { type: 'text', name: 'api_key', placeholder: 'Your application key', required: true },
      { type: 'text', name: 'user_key', placeholder: 'Your user key', required: true },
24
      { type: 'text', name: 'device', placeholder: 'Leave blank for all active devices' },
25
      { type: 'select', name: 'priority', required: true, choices:
26 27 28 29 30 31
        [
          ['Lowest Priority', -2],
          ['Low Priority', -1],
          ['Normal Priority', 0],
          ['High Priority', 1]
        ],
32
        default_choice: 0 },
33 34 35 36
      { type: 'select', name: 'sound', choices:
        [
          ['Device default sound', nil],
          ['Pushover (default)', 'pushover'],
Douwe Maan committed
37 38
          %w(Bike bike),
          %w(Bugle bugle),
39
          ['Cash Register', 'cashregister'],
Douwe Maan committed
40 41 42 43 44 45 46 47
          %w(Classical classical),
          %w(Cosmic cosmic),
          %w(Falling falling),
          %w(Gamelan gamelan),
          %w(Incoming incoming),
          %w(Intermission intermission),
          %w(Magic magic),
          %w(Mechanical mechanical),
48
          ['Piano Bar', 'pianobar'],
Douwe Maan committed
49
          %w(Siren siren),
50 51 52 53 54 55 56 57
          ['Space Alarm', 'spacealarm'],
          ['Tug Boat', 'tugboat'],
          ['Alien Alarm (long)', 'alien'],
          ['Climb (long)', 'climb'],
          ['Persistent (long)', 'persistent'],
          ['Pushover Echo (long)', 'echo'],
          ['Up Down (long)', 'updown'],
          ['None (silent)', 'none']
58
        ] }
59 60 61
    ]
  end

62
  def self.supported_events
63 64 65
    %w(push)
  end

66
  def execute(data)
67
    return unless supported_events.include?(data[:object_kind])
68

69
    ref = Gitlab::Git.ref_name(data[:ref])
70 71
    before = data[:before]
    after = data[:after]
72

Douwe Maan committed
73 74 75 76 77 78 79 80
    message =
      if Gitlab::Git.blank_ref?(before)
        "#{data[:user_name]} pushed new branch \"#{ref}\"."
      elsif Gitlab::Git.blank_ref?(after)
        "#{data[:user_name]} deleted branch \"#{ref}\"."
      else
        "#{data[:user_name]} push to branch \"#{ref}\"."
      end
81

82 83
    if data[:total_commits_count] > 0
      message << "\nTotal commits count: #{data[:total_commits_count]}"
84 85 86 87 88 89 90 91 92
    end

    pushover_data = {
      token: api_key,
      user: user_key,
      device: device,
      priority: priority,
      title: "#{project.name_with_namespace}",
      message: message,
93
      url: data[:project][:web_url],
94 95 96 97 98
      url_title: "See project #{project.name_with_namespace}"
    }

    # Sound parameter MUST NOT be sent to API if not selected
    if sound
99
      pushover_data[:sound] = sound
100 101 102 103 104
    end

    PushoverService.post('/messages.json', body: pushover_data)
  end
end