Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2015-03-05 12:54:28 +0300
committerStan Hu <stanhu@gmail.com>2015-03-05 19:23:38 +0300
commitf12ec5f4e80bee7690a2ec72cd65e72e62b21a18 (patch)
tree98efa29d4abb9ac6853f6f318c02759c0b451260 /app/models/project_services/hipchat_service.rb
parent6296f65d6560e7b04231cdbd53e3b5769397a9a7 (diff)
Add merge and issue event notification for HipChat
Diffstat (limited to 'app/models/project_services/hipchat_service.rb')
-rw-r--r--app/models/project_services/hipchat_service.rb87
1 files changed, 82 insertions, 5 deletions
diff --git a/app/models/project_services/hipchat_service.rb b/app/models/project_services/hipchat_service.rb
index 4fb80a98d24..9d094eaf146 100644
--- a/app/models/project_services/hipchat_service.rb
+++ b/app/models/project_services/hipchat_service.rb
@@ -45,7 +45,7 @@ class HipchatService < Service
end
def supported_events
- %w(push)
+ %w(push issue merge_request)
end
def execute(data)
@@ -62,7 +62,21 @@ class HipchatService < Service
@gate ||= HipChat::Client.new(token, options)
end
- def create_message(push)
+ def create_message(data)
+ object_kind = data[:object_kind]
+
+ message = \
+ case object_kind
+ when "push"
+ create_push_message(data)
+ when "issue"
+ create_issue_message(data) unless is_update?(data)
+ when "merge_request"
+ create_merge_request_message(data) unless is_update?(data)
+ end
+ end
+
+ def create_push_message(push)
ref = push[:ref].gsub("refs/heads/", "")
before = push[:before]
after = push[:after]
@@ -71,9 +85,9 @@ class HipchatService < Service
message << "#{push[:user_name]} "
if before.include?('000000')
message << "pushed new branch <a href=\""\
- "#{project.web_url}/commits/#{URI.escape(ref)}\">#{ref}</a>"\
- " to <a href=\"#{project.web_url}\">"\
- "#{project.name_with_namespace.gsub!(/\s/, "")}</a>\n"
+ "#{project_url}/commits/#{URI.escape(ref)}\">#{ref}</a>"\
+ " to <a href=\"#{project_url}\">"\
+ "#{project_url}</a>\n"
elsif after.include?('000000')
message << "removed branch #{ref} from <a href=\"#{project.web_url}\">#{project.name_with_namespace.gsub!(/\s/,'')}</a> \n"
else
@@ -93,4 +107,67 @@ class HipchatService < Service
message
end
+
+ def create_issue_message(data)
+ username = data[:user][:username]
+
+ obj_attr = data[:object_attributes]
+ obj_attr = HashWithIndifferentAccess.new(obj_attr)
+ title = obj_attr[:title]
+ state = obj_attr[:state]
+ issue_iid = obj_attr[:iid]
+ issue_url = obj_attr[:url]
+ description = obj_attr[:description]
+
+ issue_link = "<a href=\"#{issue_url}\">##{issue_iid}</a>"
+ message = "#{username} #{state} issue #{issue_link} in #{project_link}: <b>#{title}</b>"
+
+ if description
+ description = description.truncate(200, separator: ' ', omission: '...')
+ message << "<pre>#{description}</pre>"
+ end
+
+ message
+ end
+
+ def create_merge_request_message(data)
+ username = data[:user][:username]
+
+ obj_attr = data[:object_attributes]
+ obj_attr = HashWithIndifferentAccess.new(obj_attr)
+ merge_request_id = obj_attr[:iid]
+ source_branch = obj_attr[:source_branch]
+ target_branch = obj_attr[:target_branch]
+ state = obj_attr[:state]
+ description = obj_attr[:description]
+ title = obj_attr[:title]
+
+ merge_request_url = "#{project_url}/merge_requests/#{merge_request_id}"
+ merge_request_link = "<a href=\"#{merge_request_url}\">##{merge_request_id}</a>"
+ message = "#{username} #{state} merge request #{merge_request_link} in " \
+ "#{project_link}: <b>#{title}</b>"
+
+ if description
+ description = description.truncate(200, separator: ' ', omission: '...')
+ message << "<pre>#{description}</pre>"
+ end
+
+ message
+ end
+
+ def project_name
+ project.name_with_namespace.gsub(/\s/, '')
+ end
+
+ def project_url
+ project.web_url
+ end
+
+ def project_link
+ "<a href=\"#{project_url}\">#{project_name}</a>"
+ end
+
+ def is_update?(data)
+ data[:object_attributes][:action] == 'update'
+ end
end