gitlab-org--gitlab-foss/app/models/project_services/campfire_service.rb

88 lines
2.2 KiB
Ruby
Raw Normal View History

2013-05-22 10:59:59 -04:00
# == Schema Information
#
# Table name: services
#
2015-03-04 17:14:00 -05:00
# id :integer not null, primary key
# type :string(255)
# title :string(255)
# project_id :integer
# created_at :datetime
# updated_at :datetime
# active :boolean default(FALSE), not null
# properties :text
# template :boolean default(FALSE)
# push_events :boolean default(TRUE)
# issues_events :boolean default(TRUE)
# merge_requests_events :boolean default(TRUE)
# tag_push_events :boolean default(TRUE)
# note_events :boolean default(TRUE), not null
2016-01-05 21:30:59 -05:00
# build_events :boolean default(FALSE), not null
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
def to_param
'campfire'
end
def fields
[
{ type: 'text', name: 'token', placeholder: '' },
{ type: 'text', name: 'subdomain', placeholder: '' },
{ type: 'text', name: 'room', placeholder: '' }
]
end
def supported_events
%w(push)
end
2015-02-20 08:49:26 -05:00
def execute(data)
return unless supported_events.include?(data[:object_kind])
2013-05-22 10:59:59 -04:00
room = gate.find_room_by_name(self.room)
return true unless room
2015-02-20 08:49:26 -05:00
message = build_message(data)
2013-05-22 10:59:59 -04:00
room.speak(message)
end
private
def gate
@gate ||= Tinder::Campfire.new(subdomain, token: token)
end
def build_message(push)
ref = Gitlab::Git.ref_name(push[:ref])
2013-05-22 10:59:59 -04:00
before = push[:before]
after = push[:after]
message = ""
message << "[#{project.name_with_namespace}] "
message << "#{push[:user_name]} "
if Gitlab::Git.blank_ref?(before)
2013-05-22 10:59:59 -04:00
message << "pushed new branch #{ref} \n"
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
message
end
end