2018-08-10 02:45:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-30 20:48:41 -04:00
|
|
|
class PushoverService < Service
|
2018-03-13 18:38:25 -04:00
|
|
|
BASE_URI = 'https://api.pushover.net/1'.freeze
|
2014-09-30 20:48:41 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-12-27 07:44:24 -05:00
|
|
|
def self.to_param
|
2014-09-30 20:48:41 -04:00
|
|
|
'pushover'
|
|
|
|
end
|
|
|
|
|
|
|
|
def fields
|
|
|
|
[
|
2017-05-22 06:07:12 -04:00
|
|
|
{ type: 'text', name: 'api_key', placeholder: 'Your application key', required: true },
|
|
|
|
{ type: 'text', name: 'user_key', placeholder: 'Your user key', required: true },
|
2014-09-30 20:48:41 -04:00
|
|
|
{ type: 'text', name: 'device', placeholder: 'Leave blank for all active devices' },
|
2017-05-22 06:07:12 -04:00
|
|
|
{ type: 'select', name: 'priority', required: true, choices:
|
2014-09-30 20:48:41 -04:00
|
|
|
[
|
|
|
|
['Lowest Priority', -2],
|
|
|
|
['Low Priority', -1],
|
|
|
|
['Normal Priority', 0],
|
|
|
|
['High Priority', 1]
|
|
|
|
],
|
2017-02-22 12:44:44 -05:00
|
|
|
default_choice: 0 },
|
2014-09-30 20:48:41 -04:00
|
|
|
{ type: 'select', name: 'sound', choices:
|
|
|
|
[
|
|
|
|
['Device default sound', nil],
|
|
|
|
['Pushover (default)', 'pushover'],
|
2017-02-22 12:46:57 -05:00
|
|
|
%w(Bike bike),
|
|
|
|
%w(Bugle bugle),
|
2014-09-30 20:48:41 -04:00
|
|
|
['Cash Register', 'cashregister'],
|
2017-02-22 12:46:57 -05:00
|
|
|
%w(Classical classical),
|
|
|
|
%w(Cosmic cosmic),
|
|
|
|
%w(Falling falling),
|
|
|
|
%w(Gamelan gamelan),
|
|
|
|
%w(Incoming incoming),
|
|
|
|
%w(Intermission intermission),
|
|
|
|
%w(Magic magic),
|
|
|
|
%w(Mechanical mechanical),
|
2014-09-30 20:48:41 -04:00
|
|
|
['Piano Bar', 'pianobar'],
|
2017-02-22 12:46:57 -05:00
|
|
|
%w(Siren siren),
|
2014-09-30 20:48:41 -04:00
|
|
|
['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']
|
2017-05-03 07:22:03 -04:00
|
|
|
] }
|
2014-09-30 20:48:41 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2016-12-27 07:44:24 -05:00
|
|
|
def self.supported_events
|
2015-02-28 11:33:18 -05:00
|
|
|
%w(push)
|
|
|
|
end
|
|
|
|
|
2015-02-20 08:49:26 -05:00
|
|
|
def execute(data)
|
2015-02-28 11:33:18 -05:00
|
|
|
return unless supported_events.include?(data[:object_kind])
|
2015-02-19 00:02:57 -05:00
|
|
|
|
2015-03-10 06:51:36 -04:00
|
|
|
ref = Gitlab::Git.ref_name(data[:ref])
|
2015-02-20 08:49:26 -05:00
|
|
|
before = data[:before]
|
|
|
|
after = data[:after]
|
2014-09-30 20:48:41 -04:00
|
|
|
|
2017-02-22 10:10:32 -05:00
|
|
|
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
|
2014-09-30 20:48:41 -04:00
|
|
|
|
2015-02-20 08:49:26 -05:00
|
|
|
if data[:total_commits_count] > 0
|
2018-08-10 02:45:01 -04:00
|
|
|
message = [message, "Total commits count: #{data[:total_commits_count]}"].join("\n")
|
2014-09-30 20:48:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
pushover_data = {
|
|
|
|
token: api_key,
|
|
|
|
user: user_key,
|
|
|
|
device: device,
|
|
|
|
priority: priority,
|
2018-03-05 09:15:26 -05:00
|
|
|
title: "#{project.full_name}",
|
2014-09-30 20:48:41 -04:00
|
|
|
message: message,
|
2016-02-16 07:03:00 -05:00
|
|
|
url: data[:project][:web_url],
|
2018-03-05 09:15:26 -05:00
|
|
|
url_title: "See project #{project.full_name}"
|
2014-09-30 20:48:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
# Sound parameter MUST NOT be sent to API if not selected
|
|
|
|
if sound
|
2017-02-21 18:50:22 -05:00
|
|
|
pushover_data[:sound] = sound
|
2014-09-30 20:48:41 -04:00
|
|
|
end
|
|
|
|
|
2018-03-13 18:38:25 -04:00
|
|
|
Gitlab::HTTP.post('/messages.json', base_uri: BASE_URI, body: pushover_data)
|
2014-09-30 20:48:41 -04:00
|
|
|
end
|
|
|
|
end
|