Class: PushoverService

Inherits:
Service show all
Defined in:
app/models/project_services/pushover_service.rb

Constant Summary collapse

BASE_URI =
'https://api.pushover.net/1'

Constants inherited from Service

Service::DEV_SERVICE_NAMES, Service::PROJECT_SPECIFIC_SERVICE_NAMES, Service::SERVICE_NAMES

Instance Attribute Summary

Attributes included from Importable

#imported, #importing

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Service

#activated?, #api_field_names, #async_execute, available_services_names, available_services_types, boolean_accessor, build_from_integration, #can_test?, #category, #configurable_event_actions, #configurable_events, create_from_active_default_integrations, default_integration, #default_test_event, default_test_event, dev_services_names, #editable?, #event_channel_names, event_description, #event_field, #event_names, event_names, #external_issue_tracker?, #external_wiki?, find_or_create_templates, find_or_initialize_all_non_project_specific, find_or_initialize_non_project_specific_integration, #global_fields, #help, inherited_descendants_from_self_or_ancestors_from, #initialize_properties, instance_exists_for?, #json_fields, #operating?, project_specific_services_names, prop_accessor, #reset_updated_properties, services_names, #show_active_box?, supported_event_actions, #supported_events, #supports_data_fields?, #test, #to_data_fields_hash, #to_param, #to_service_hash, #updated_properties

Methods included from ProjectServicesLoggable

#build_message, #log_error, #log_info, #logger

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, where_exists, with_fast_statement_timeout, without_order

Class Method Details

.supported_eventsObject


63
64
65
# File 'app/models/project_services/pushover_service.rb', line 63

def self.supported_events
  %w(push)
end

.to_paramObject


17
18
19
# File 'app/models/project_services/pushover_service.rb', line 17

def self.to_param
  'pushover'
end

Instance Method Details

#descriptionObject


13
14
15
# File 'app/models/project_services/pushover_service.rb', line 13

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

#execute(data) ⇒ Object


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/project_services/pushover_service.rb', line 67

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

  ref = Gitlab::Git.ref_name(data[:ref])
  before = data[:before]
  after = data[:after]

  message =
    if Gitlab::Git.blank_ref?(before)
      s_("PushoverService|%{user_name} pushed new branch \"%{ref}\".") % { user_name: data[:user_name], ref: ref }
    elsif Gitlab::Git.blank_ref?(after)
      s_("PushoverService|%{user_name} deleted branch \"%{ref}\".") % { user_name: data[:user_name], ref: ref }
    else
      s_("PushoverService|%{user_name} push to branch \"%{ref}\".") % { user_name: data[:user_name], ref: ref }
    end

  if data[:total_commits_count] > 0
    message = [message, s_("PushoverService|Total commits count: %{total_commits_count}") % { total_commits_count: data[:total_commits_count] }].join("\n")
  end

  pushover_data = {
    token: api_key,
    user: user_key,
    device: device,
    priority: priority,
    title: "#{project.full_name}",
    message: message,
    url: data[:project][:web_url],
    url_title: s_("PushoverService|See project %{project_full_name}") % { project_full_name: project.full_name }
  }

  # Sound parameter MUST NOT be sent to API if not selected
  if sound
    pushover_data[:sound] = sound
  end

  Gitlab::HTTP.post('/messages.json', base_uri: BASE_URI, body: pushover_data)
end

#fieldsObject


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/project_services/pushover_service.rb', line 21

def fields
  [
    { type: 'text', name: 'api_key', placeholder: s_('PushoverService|Your application key'), required: true },
    { type: 'text', name: 'user_key', placeholder: s_('PushoverService|Your user key'), required: true },
    { type: 'text', name: 'device', placeholder: s_('PushoverService|Leave blank for all active devices') },
    { type: 'select', name: 'priority', required: true, choices:
      [
        [s_('PushoverService|Lowest Priority'), -2],
        [s_('PushoverService|Low Priority'), -1],
        [s_('PushoverService|Normal Priority'), 0],
        [s_('PushoverService|High Priority'), 1]
      ],
      default_choice: 0 },
    { type: 'select', name: 'sound', choices:
      [
        ['Device default sound', nil],
        ['Pushover (default)', 'pushover'],
        %w(Bike bike),
        %w(Bugle bugle),
        ['Cash Register', 'cashregister'],
        %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'],
        %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

#titleObject


9
10
11
# File 'app/models/project_services/pushover_service.rb', line 9

def title
  'Pushover'
end