BigW Consortium Gitlab

pushover_service.rb 2.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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 62 63
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

64 65 66 67
  def supported_events
    %w(push)
  end

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

71
    ref = Gitlab::Git.ref_name(data[:ref])
72 73
    before = data[:before]
    after = data[:after]
74

75
    if Gitlab::Git.blank_ref?(before)
76
      message = "#{data[:user_name]} pushed new branch \"#{ref}\"."
77
    elsif Gitlab::Git.blank_ref?(after)
78
      message = "#{data[:user_name]} deleted branch \"#{ref}\"."
79
    else
80
      message = "#{data[:user_name]} push to branch \"#{ref}\"."
81 82
    end

83 84
    if data[:total_commits_count] > 0
      message << "\nTotal commits count: #{data[:total_commits_count]}"
85 86 87 88 89 90 91 92 93
    end

    pushover_data = {
      token: api_key,
      user: user_key,
      device: device,
      priority: priority,
      title: "#{project.name_with_namespace}",
      message: message,
94
      url: data[:project][:web_url],
95 96 97 98 99 100 101 102 103 104 105
      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