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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/slack_notification_generator/main.rb', line 8
def self.run
unless ENV['SLACK_HOOK']
puts 'you must define the "SLACK_HOOK" environment variable'
exit 1
end
env =
case ENV['CI_BRANCH']
when 'master'
'Production'
when 'develop'
'Staging'
when /^release/
'Release'
else
'Development'
end
changelog = []
this_tag = 'HEAD'
prev_tag = `git describe --abbrev=0 --tags`.strip
if ARGV.select { |arg| arg == "HEAD" }.empty?
this_tag = prev_tag
prev_tag = `git describe --abbrev=0 --tags #{this_tag}^`.strip
end
server = ARGV.select { |arg| arg != "HEAD" }.first
commits = []
commit = nil
blob = `git log --merges #{this_tag} ^#{prev_tag}`
blob.split("\n").map(&:strip).each do |line|
case line
when /^commit ([0-9a-f]+)$/
commits << commit unless commit.nil?
commit = { hash: $1[0..6] }
when /^Merge: ([a-f0-9]+) ([a-f0-9]+)/
commit[:range] = ($1..$2)
when /^Author: ([a-zA-Z]+)/
when /^Date: +(.*)$/
commit[:date] = Date.parse($1)
when /^Merge pull request #([0-9]*) /
commit[:pull] = $1
else
commit[:message] ||= line unless line.empty?
end
end
commits << commit unless commit.nil?
pull_requests = []
commit_filter = {}
commits.each do |commit|
next unless commit[:pull]
blob = `git log --no-merges #{commit[:range].last} ^#{commit[:range].first}`
commit[:authors] = []
commit[:issues] = []
commit[:times] = []
(blob).each do |child|
commit_filter[child[:hash]] = true
commit[:authors] << child[:author]
commit[:issues] << child[:issue] if child[:issue]
commit[:times] << child[:time] if child[:time]
end
commit[:authors].sort!.uniq!
commit[:issues].sort!.uniq!
pull_requests << commit
end
pull_requests.map! { |commit| format(commit) }
blob = `git log --no-merges #{this_tag} ^#{prev_tag}`
other_commits = []
(blob).each do |commit|
next if commit_filter[commit[:hash]] || commit[:skip]
other_commits << commit
end
other_commits.map! { |commit| format(commit) }
attachments = []
add_attachment(attachments, "Pull Requests", pull_requests)
add_attachment(attachments, "Other Commits", other_commits)
return if attachments.length == 0
version = ""
if this_tag != 'HEAD'
version = " v#{this_tag}"
end
message = "*#{[ENV['SLACK_NAME'], env].compact.join(' ')} Release#{version}*"
if server
name = server.gsub(/^http.*\/\//, "").gsub(/\/$/, '')
message << " (<#{server}|#{name}>)"
end
payload =
{
username: ENV['SLACK_USER'] || 'Notification',
icon_emoji: ENV['SLACK_ICON'] || ':bell:',
channel: ENV['SLACK_CHAN'] || '#general',
text: message,
attachments: attachments
}
`curl -X POST --data-urlencode 'payload=#{payload.to_json.gsub("'", "'\"'\"'")}' #{ENV['SLACK_HOOK']}`
end
|