-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
That introduce select field type for services options.
- Loading branch information
1 parent
4e96c84
commit 2678398
Showing
9 changed files
with
223 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# == Schema Information | ||
# | ||
# Table name: services | ||
# | ||
# id :integer not null, primary key | ||
# type :string(255) | ||
# title :string(255) | ||
# project_id :integer not null | ||
# created_at :datetime | ||
# updated_at :datetime | ||
# active :boolean default(FALSE), not null | ||
# properties :text | ||
# | ||
|
||
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 to_param | ||
'pushover' | ||
end | ||
|
||
def fields | ||
[ | ||
{ type: 'text', name: 'api_key', placeholder: 'Your application key' }, | ||
{ type: 'text', name: 'user_key', placeholder: 'Your user key' }, | ||
{ type: 'text', name: 'device', placeholder: 'Leave blank for all active devices' }, | ||
{ type: 'select', name: 'priority', choices: | ||
[ | ||
['Lowest Priority', -2], | ||
['Low Priority', -1], | ||
['Normal Priority', 0], | ||
['High Priority', 1] | ||
], | ||
default_choice: 0 | ||
}, | ||
{ type: 'select', name: 'sound', choices: | ||
[ | ||
['Device default sound', nil], | ||
['Pushover (default)', 'pushover'], | ||
['Bike', 'bike'], | ||
['Bugle', 'bugle'], | ||
['Cash Register', 'cashregister'], | ||
['Classical', 'classical'], | ||
['Cosmic', 'cosmic'], | ||
['Falling', 'falling'], | ||
['Gamelan', 'gamelan'], | ||
['Incoming', 'incoming'], | ||
['Intermission', 'intermission'], | ||
['Magic', 'magic'], | ||
['Mechanical', 'mechanical'], | ||
['Piano Bar', 'pianobar'], | ||
['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 execute(push_data) | ||
ref = push_data[:ref].gsub('refs/heads/', '') | ||
before = push_data[:before] | ||
after = push_data[:after] | ||
|
||
if before =~ /000000/ | ||
message = "#{push_data[:user_name]} pushed new branch \"#{ref}\"." | ||
elsif after =~ /000000/ | ||
message = "#{push_data[:user_name]} deleted branch \"#{ref}\"." | ||
else | ||
message = "#{push_data[:user_name]} push to branch \"#{ref}\"." | ||
end | ||
|
||
if push_data[:total_commits_count] > 0 | ||
message << "\nTotal commits count: #{push_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: push_data[:repository][:homepage], | ||
url_title: "See project #{project.name_with_namespace}" | ||
} | ||
|
||
# Sound parameter MUST NOT be sent to API if not selected | ||
if sound | ||
pushover_data.merge!(sound: sound) | ||
end | ||
|
||
PushoverService.post('/messages.json', body: pushover_data) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# == Schema Information | ||
# | ||
# Table name: services | ||
# | ||
# id :integer not null, primary key | ||
# type :string(255) | ||
# title :string(255) | ||
# project_id :integer not null | ||
# created_at :datetime | ||
# updated_at :datetime | ||
# active :boolean default(FALSE), not null | ||
# properties :text | ||
# | ||
|
||
require 'spec_helper' | ||
|
||
describe PushoverService do | ||
describe 'Associations' do | ||
it { should belong_to :project } | ||
it { should have_one :service_hook } | ||
end | ||
|
||
describe 'Validations' do | ||
context 'active' do | ||
before do | ||
subject.active = true | ||
end | ||
|
||
it { should validate_presence_of :api_key } | ||
it { should validate_presence_of :user_key } | ||
it { should validate_presence_of :priority } | ||
end | ||
end | ||
|
||
describe 'Execute' do | ||
let(:pushover) { PushoverService.new } | ||
let(:user) { create(:user) } | ||
let(:project) { create(:project) } | ||
let(:sample_data) { GitPushService.new.sample_data(project, user) } | ||
|
||
let(:api_key) { 'verySecret' } | ||
let(:user_key) { 'verySecret' } | ||
let(:device) { 'myDevice' } | ||
let(:priority) { 0 } | ||
let(:sound) { 'bike' } | ||
let(:api_url) { 'https://api.pushover.net/1/messages.json' } | ||
|
||
before do | ||
pushover.stub( | ||
project: project, | ||
project_id: project.id, | ||
service_hook: true, | ||
api_key: api_key, | ||
user_key: user_key, | ||
device: device, | ||
priority: priority, | ||
sound: sound | ||
) | ||
|
||
WebMock.stub_request(:post, api_url) | ||
end | ||
|
||
it 'should call Pushover API' do | ||
pushover.execute(sample_data) | ||
|
||
WebMock.should have_requested(:post, api_url).once | ||
end | ||
end | ||
end |