Class: SlackNotificationGenerator
- Inherits:
-
Object
- Object
- SlackNotificationGenerator
- Defined in:
- lib/slack_notification_generator/main.rb
Class Method Summary collapse
- .add_attachment(store, title, data) ⇒ Object
- .extract_commits(blob) ⇒ Object
- .format(commit) ⇒ Object
- .run ⇒ Object
Class Method Details
.add_attachment(store, title, data) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/slack_notification_generator/main.rb', line 128 def self.(store, title, data) return if data.nil? || data.length == 0 data.map! do |line| line.split("\n").map(&:strip).join("\n ") end store << { title: title, text: data.map { |l| "• #{l}" }.join("\n") } end |
.extract_commits(blob) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/slack_notification_generator/main.rb', line 102 def self.extract_commits(blob) commits = [] commit = nil 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], skip: false } when /^Author: ([a-zA-Z]+)/ commit[:author] = $1.downcase when /^Date: +(.*)$/ commit[:date] = Date.parse($1) when /^([A-Z]+-[0-9]+)/ commit[:issue] = $1 if line =~ /#time +([0-9][^\s]*)/ commit[:time] = $1 end else commit[:message] ||= line unless line.empty? commit[:skip] = true if line =~ /\[skip ci\]/ end end commits << commit unless commit.nil? commits end |
.format(commit) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/slack_notification_generator/main.rb', line 140 def self.format(commit) commit[:authors] ||= [commit[:author]].compact commit[:issues] ||= [commit[:issue]].compact commit[:times] ||= [commit[:time]].compact link = if commit[:pull] if ENV['SLACK_REPO'] "<#{ENV['SLACK_REPO']}/pull/#{commit[:pull]}|##{commit[:pull]}>" else "##{commit[:pull]}" end else if ENV['SLACK_REPO'] "<#{ENV['SLACK_REPO']}/commit/#{commit[:hash]}|##{commit[:hash]}>" else "##{commit[:hash]}" end end issues = if commit[:issues].empty? nil else commit[:issues].map do |issue| if ENV['SLACK_JIRA'] "<#{ENV['SLACK_JIRA']}/browse/#{issue}|#{issue}>" else issue end end.join(', ') end time = if commit[:times].empty? nil else seconds = commit[:times].map do |data| ChronicDuration.parse(data) end.reduce(:+) ChronicDuration.output(seconds) end extras = [issues, commit[:authors].join(', '), time].compact.join(', ') if commit[:message].length > 50 commit[:message] = commit[:message][0, 50] + "..." end " \#{link} \#{commit[:message]} [\#{extras}]\n eos\nend\n" |
.run ⇒ Object
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 |
# 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 unless ARGV[0] == "HEAD" this_tag = prev_tag prev_tag = `git describe --abbrev=0 --tags #{this_tag}^`.strip end 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] = [] extract_commits(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 = [] extract_commits(blob).each do |commit| next if commit_filter[commit[:hash]] || commit[:skip] other_commits << commit end other_commits.map! { |commit| format(commit) } = [] (, "Pull Requests", pull_requests) (, "Other Commits", other_commits) exit if .length == 0 payload = { username: ENV['SLACK_USER'] || 'Notification', icon_emoji: ENV['SLACK_ICON'] || ':bell:', channel: ENV['SLACK_CHAN'] || '#general', text: "*#{[ENV['SLACK_NAME'], env].compact.join(' ')} Release*", attachments: } `curl -X POST --data-urlencode 'payload=#{payload.to_json}' #{ENV['SLACK_HOOK']}` end |