2018-08-10 02:45:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-05-22 10:59:59 -04:00
|
|
|
class CampfireService < Service
|
2014-09-07 20:54:18 -04:00
|
|
|
prop_accessor :token, :subdomain, :room
|
2013-05-22 10:59:59 -04:00
|
|
|
validates :token, presence: true, if: :activated?
|
|
|
|
|
|
|
|
def title
|
|
|
|
'Campfire'
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
'Simple web-based real-time group chat'
|
|
|
|
end
|
|
|
|
|
2016-12-27 07:44:24 -05:00
|
|
|
def self.to_param
|
2013-05-22 10:59:59 -04:00
|
|
|
'campfire'
|
|
|
|
end
|
|
|
|
|
|
|
|
def fields
|
|
|
|
[
|
2017-05-22 06:07:12 -04:00
|
|
|
{ type: 'text', name: 'token', placeholder: '', required: true },
|
2013-05-22 10:59:59 -04:00
|
|
|
{ type: 'text', name: 'subdomain', placeholder: '' },
|
|
|
|
{ type: 'text', name: 'room', placeholder: '' }
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2016-12-27 07:44:24 -05:00
|
|
|
def self.supported_events
|
2015-02-28 11:33:18 -05:00
|
|
|
%w(push)
|
|
|
|
end
|
|
|
|
|
2015-02-20 08:49:26 -05:00
|
|
|
def execute(data)
|
2015-02-28 11:33:18 -05:00
|
|
|
return unless supported_events.include?(data[:object_kind])
|
2015-02-19 00:02:57 -05:00
|
|
|
|
2015-02-20 08:49:26 -05:00
|
|
|
message = build_message(data)
|
2016-08-10 06:25:01 -04:00
|
|
|
speak(self.room, message, auth)
|
2013-05-22 10:59:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-08-10 06:25:01 -04:00
|
|
|
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 }
|
2019-02-08 07:19:53 -05:00
|
|
|
return unless room
|
2016-08-10 06:25:01 -04:00
|
|
|
|
|
|
|
path = "/room/#{room["id"]}/speak.json"
|
|
|
|
body = {
|
|
|
|
body: {
|
|
|
|
message: {
|
|
|
|
type: 'TextMessage',
|
|
|
|
body: message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-13 18:38:25 -04:00
|
|
|
res = Gitlab::HTTP.post(path, base_uri: base_uri, **auth.merge(body))
|
2016-08-10 06:25:01 -04:00
|
|
|
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-13 18:38:25 -04:00
|
|
|
res = Gitlab::HTTP.get("/rooms.json", base_uri: base_uri, **auth)
|
2016-08-10 06:25:01 -04:00
|
|
|
res.code == 200 ? res["rooms"] : []
|
2013-05-22 10:59:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_message(push)
|
2015-03-10 06:51:36 -04:00
|
|
|
ref = Gitlab::Git.ref_name(push[:ref])
|
2013-05-22 10:59:59 -04:00
|
|
|
before = push[:before]
|
|
|
|
after = push[:after]
|
|
|
|
|
2018-08-10 02:45:01 -04:00
|
|
|
message = []
|
2018-03-05 09:15:26 -05:00
|
|
|
message << "[#{project.full_name}] "
|
2013-05-22 10:59:59 -04:00
|
|
|
message << "#{push[:user_name]} "
|
|
|
|
|
2015-03-10 06:51:36 -04:00
|
|
|
if Gitlab::Git.blank_ref?(before)
|
2013-05-22 10:59:59 -04:00
|
|
|
message << "pushed new branch #{ref} \n"
|
2015-03-10 06:51:36 -04:00
|
|
|
elsif Gitlab::Git.blank_ref?(after)
|
2013-05-22 10:59:59 -04:00
|
|
|
message << "removed branch #{ref} \n"
|
|
|
|
else
|
|
|
|
message << "pushed #{push[:total_commits_count]} commits to #{ref}. "
|
|
|
|
message << "#{project.web_url}/compare/#{before}...#{after}"
|
|
|
|
end
|
|
|
|
|
2018-08-10 02:45:01 -04:00
|
|
|
message.join
|
2013-05-22 10:59:59 -04:00
|
|
|
end
|
|
|
|
end
|