debian-mirror-gitlab/app/models/project_services/campfire_service.rb

103 lines
2.5 KiB
Ruby
Raw Normal View History

2018-11-20 20:47:30 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
class CampfireService < Service
2015-04-26 12:48:37 +05:30
prop_accessor :token, :subdomain, :room
2014-09-02 18:07:02 +05:30
validates :token, presence: true, if: :activated?
def title
'Campfire'
end
def description
'Simple web-based real-time group chat'
end
2017-08-17 22:00:37 +05:30
def self.to_param
2014-09-02 18:07:02 +05:30
'campfire'
end
def fields
[
2017-09-10 17:25:29 +05:30
{ type: 'text', name: 'token', placeholder: '', required: true },
2014-09-02 18:07:02 +05:30
{ type: 'text', name: 'subdomain', placeholder: '' },
{ type: 'text', name: 'room', placeholder: '' }
]
end
2017-08-17 22:00:37 +05:30
def self.supported_events
2015-04-26 12:48:37 +05:30
%w(push)
end
def execute(data)
return unless supported_events.include?(data[:object_kind])
message = build_message(data)
2016-09-13 17:45:13 +05:30
speak(self.room, message, auth)
2014-09-02 18:07:02 +05:30
end
private
2016-09-13 17:45:13 +05:30
def base_uri
@base_uri ||= "https://#{subdomain}.campfirenow.com"
end
def auth
# use a dummy password, as explained in the Campfire API doc:
# https://github.com/basecamp/campfire-api#authentication
@auth ||= {
basic_auth: {
username: token,
password: 'X'
}
}
end
# Post a message into a room, returns the message Hash in case of success.
# Returns nil otherwise.
# https://github.com/basecamp/campfire-api/blob/master/sections/messages.md#create-message
def speak(room_name, message, auth)
room = rooms(auth).find { |r| r["name"] == room_name }
return nil unless room
path = "/room/#{room["id"]}/speak.json"
body = {
body: {
message: {
type: 'TextMessage',
body: message
}
}
}
2018-03-26 14:24:53 +05:30
res = Gitlab::HTTP.post(path, base_uri: base_uri, **auth.merge(body))
2016-09-13 17:45:13 +05:30
res.code == 201 ? res : nil
end
# Returns a list of rooms, or [].
# https://github.com/basecamp/campfire-api/blob/master/sections/rooms.md#get-rooms
def rooms(auth)
2018-03-26 14:24:53 +05:30
res = Gitlab::HTTP.get("/rooms.json", base_uri: base_uri, **auth)
2016-09-13 17:45:13 +05:30
res.code == 200 ? res["rooms"] : []
2014-09-02 18:07:02 +05:30
end
def build_message(push)
2015-04-26 12:48:37 +05:30
ref = Gitlab::Git.ref_name(push[:ref])
2014-09-02 18:07:02 +05:30
before = push[:before]
after = push[:after]
2018-11-20 20:47:30 +05:30
message = []
2018-03-27 19:54:05 +05:30
message << "[#{project.full_name}] "
2014-09-02 18:07:02 +05:30
message << "#{push[:user_name]} "
2015-04-26 12:48:37 +05:30
if Gitlab::Git.blank_ref?(before)
2014-09-02 18:07:02 +05:30
message << "pushed new branch #{ref} \n"
2015-04-26 12:48:37 +05:30
elsif Gitlab::Git.blank_ref?(after)
2014-09-02 18:07:02 +05:30
message << "removed branch #{ref} \n"
else
message << "pushed #{push[:total_commits_count]} commits to #{ref}. "
message << "#{project.web_url}/compare/#{before}...#{after}"
end
2018-11-20 20:47:30 +05:30
message.join
2014-09-02 18:07:02 +05:30
end
end