Read about our upcoming Code of Conduct on this issue

pushover_service.rb 3.44 KB
Newer Older
1
2
# frozen_string_literal: true

3
class PushoverService < Service
4
  BASE_URI = 'https://api.pushover.net/1'.freeze
5
6
7
8
9
10
11
12
13

  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
14
    s_('PushoverService|Pushover makes it easy to get real-time notifications on your Android device, iPhone, iPad, and Desktop.')
15
16
  end

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

  def fields
    [
23
24
25
      { 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') },
26
      { type: 'select', name: 'priority', required: true, choices:
27
        [
28
29
30
31
          [s_('PushoverService|Lowest Priority'), -2],
          [s_('PushoverService|Low Priority'), -1],
          [s_('PushoverService|Normal Priority'), 0],
          [s_('PushoverService|High Priority'), 1]
32
        ],
33
        default_choice: 0 },
34
35
36
37
      { type: 'select', name: 'sound', choices:
        [
          ['Device default sound', nil],
          ['Pushover (default)', 'pushover'],
Douwe Maan's avatar
Douwe Maan committed
38
39
          %w(Bike bike),
          %w(Bugle bugle),
40
          ['Cash Register', 'cashregister'],
Douwe Maan's avatar
Douwe Maan committed
41
42
43
44
45
46
47
48
          %w(Classical classical),
          %w(Cosmic cosmic),
          %w(Falling falling),
          %w(Gamelan gamelan),
          %w(Incoming incoming),
          %w(Intermission intermission),
          %w(Magic magic),
          %w(Mechanical mechanical),
49
          ['Piano Bar', 'pianobar'],
Douwe Maan's avatar
Douwe Maan committed
50
          %w(Siren siren),
51
52
53
54
55
56
57
58
          ['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']
59
        ] }
60
61
62
    ]
  end

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

Douwe Maan's avatar
Douwe Maan committed
67
  def execute(data)
68
    return unless supported_events.include?(data[:object_kind])
69

70
    ref = Gitlab::Git.ref_name(data[:ref])
Douwe Maan's avatar
Douwe Maan committed
71
72
    before = data[:before]
    after = data[:after]
73

Douwe Maan's avatar
Douwe Maan committed
74
75
    message =
      if Gitlab::Git.blank_ref?(before)
76
        s_("PushoverService|%{user_name} pushed new branch \"%{ref}\".") % { user_name: data[:user_name], ref: ref }
Douwe Maan's avatar
Douwe Maan committed
77
      elsif Gitlab::Git.blank_ref?(after)
78
        s_("PushoverService|%{user_name} deleted branch \"%{ref}\".") % { user_name: data[:user_name], ref: ref }
Douwe Maan's avatar
Douwe Maan committed
79
      else
80
        s_("PushoverService|%{user_name} push to branch \"%{ref}\".") % { user_name: data[:user_name], ref: ref }
Douwe Maan's avatar
Douwe Maan committed
81
      end
82

Douwe Maan's avatar
Douwe Maan committed
83
    if data[:total_commits_count] > 0
84
      message = [message, s_("PushoverService|Total commits count: %{total_commits_count}") % { total_commits_count: data[:total_commits_count] }].join("\n")
85
86
87
88
89
90
91
    end

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

    # Sound parameter MUST NOT be sent to API if not selected
    if sound
Douwe Maan's avatar
Douwe Maan committed
100
      pushover_data[:sound] = sound
101
102
    end

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