-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost-receive-hipchat
77 lines (66 loc) · 2.43 KB
/
post-receive-hipchat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#! /usr/bin/env ruby
require 'rubygems'
require 'hipchat-api'
require 'yaml'
require 'time'
config = YAML::load(File.open(File.join(File.dirname(__FILE__), 'config.yml')))
# Exit if pushing to repo listed for exclusion in config file
repository = config['repository'] ||= File.basename(Dir.getwd, ".git")
exit if config['exclude_repos'].include?(repository)
message = ""
branches = []
tags = []
# Loop through STDIN, sort into branches and tags
STDIN.read.each do |line|
oldrev, newrev, ref = line.split(' ')
parsed_ref = ref.split('/')
if parsed_ref[1] == "heads"
revhash = { :oldrev => oldrev, :newrev => newrev, :branch => parsed_ref[2] }
branches.push revhash
elsif parsed_ref[1] == "tags"
taghash = { :tag => parsed_ref[2], :rev => newrev }
tags.push taghash
else
# ignore
end
end
chat = HipChat::API.new(config['hipchat_token'])
url = "#{config['gitweb_url']}/#{repository}.git/commit/" unless config['use_url'] == false
GIT = `which git`.strip
# Loop through ref/heads/* and print commit messages
if branches.any?
message += "<strong>#{repository}</strong> has been pushed commits:<br>"
branches.each do |b|
message += "To branch <strong>#{b[:branch]}</strong>:<br>"
# Generate array of all new commits
commit_log = `#{GIT} log --branches=#{b[:branch]} #{b[:oldrev]}..#{b[:newrev]} --reverse`
commits = commit_log.split("\n\ncommit ")
commits.each do |com|
revision = com[/([a-f0-9]{40})/]
commit_author = `#{GIT} show --pretty=format:"%an" #{revision} | sed q`.chomp
commit_msg = `#{GIT} show --pretty=format:"%s" #{revision} | sed q`.chomp
if config['use_url']
rev_info = "<a href=\"#{url + revision}\">#{revision[0,6]}</a>"
else
rev_info = revision[0,6]
end
message += " - (#{rev_info}) #{commit_author}: #{commit_msg}<br>"
end
end
end
# Loop through ref/tags/* and print tag names & annotations
if tags.any?
message += "<strong>#{repository}</strong> has been pushed tags:<br>"
tags.each do |t|
message += "Tag <strong>#{t[:tag]}</strong>:<br>"
tag_msg = `#{GIT} show #{t[:tag]} | sed -n 5p`.chomp
if config['use_url']
rev_info = "<a href=\"#{url + t[:rev]}\">#{t[:rev][0,6]}</a>"
else
rev_info = t[:rev][0,6]
end
message += " - (#{rev_info}) #{tag_msg}<br>"
end
end
# Speak!
chat.rooms_message(config['hipchat_room_id'], config['hipchat_name'], message, notify = 0)