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

67 lines
1.4 KiB
Ruby
Raw Normal View History

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
def to_param
'campfire'
end
def fields
[
{ type: 'text', name: 'token', placeholder: '' },
{ type: 'text', name: 'subdomain', placeholder: '' },
{ type: 'text', name: 'room', placeholder: '' }
]
end
2015-04-26 12:48:37 +05:30
def supported_events
%w(push)
end
def execute(data)
return unless supported_events.include?(data[:object_kind])
2014-09-02 18:07:02 +05:30
room = gate.find_room_by_name(self.room)
return true unless room
2015-04-26 12:48:37 +05:30
message = build_message(data)
2014-09-02 18:07:02 +05:30
room.speak(message)
end
private
def gate
@gate ||= Tinder::Campfire.new(subdomain, token: token)
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]
message = ""
message << "[#{project.name_with_namespace}] "
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
message
end
end