gitlab-org--gitlab-foss/app/models/project_services/pushover_service.rb

105 lines
3 KiB
Ruby
Raw Normal View History

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
def self.to_param
'pushover'
end
def fields
[
{ type: 'text', name: 'api_key', placeholder: 'Your application key', required: true },
{ type: 'text', name: 'user_key', placeholder: 'Your user key', required: true },
{ type: 'text', name: 'device', placeholder: 'Leave blank for all active devices' },
{ type: 'select', name: 'priority', required: true, choices:
[
['Lowest Priority', -2],
['Low Priority', -1],
['Normal Priority', 0],
['High Priority', 1]
],
2017-02-22 12:44:44 -05:00
default_choice: 0 },
{ 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),
['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),
['Piano Bar', 'pianobar'],
2017-02-22 12:46:57 -05:00
%w(Siren siren),
['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']
] }
]
end
def self.supported_events
%w(push)
end
2015-02-20 08:49:26 -05:00
def execute(data)
return unless supported_events.include?(data[:object_kind])
ref = Gitlab::Git.ref_name(data[:ref])
2015-02-20 08:49:26 -05:00
before = data[:before]
after = data[:after]
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
2015-02-20 08:49:26 -05:00
if data[:total_commits_count] > 0
message << "\nTotal commits count: #{data[:total_commits_count]}"
end
pushover_data = {
token: api_key,
user: user_key,
device: device,
priority: priority,
title: "#{project.name_with_namespace}",
message: message,
url: data[:project][:web_url],
url_title: "See project #{project.name_with_namespace}"
}
# 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
end
PushoverService.post('/messages.json', body: pushover_data)
end
end