Class: GitCommitNotifier::CommitHook
- Inherits:
-
Object
- Object
- GitCommitNotifier::CommitHook
- Defined in:
- lib/git_commit_notifier/commit_hook.rb
Overview
Represents Git commit hook handler.
Class Attribute Summary collapse
-
.config ⇒ Hash
readonly
Configuration that read from YAML file.
Class Method Summary collapse
- .add_committer_to_recipient(recipient, committer_email) ⇒ Object
- .email?(email) ⇒ Boolean
- .get_reply_to_address(config, committer_email) ⇒ Object
-
.get_subject(commit_info, template, subject_map) ⇒ String
Gets message subject.
-
.include_branches ⇒ Array(String), NilClass
Gets list of branches from CommitHook.config to include into notifications.
-
.info(message) ⇒ NilClass
Prints informational message to $stdout.
-
.logger ⇒ Object
Gets logger.
-
.merge_commit?(commit_info) ⇒ Boolean
Is merge commit?.
-
.run(config_name, rev1, rev2, ref_name) ⇒ NilClass
Runs comit hook handler using specified arguments.
-
.show_error(message) ⇒ NilClass
Prints error message to $stderr.
Class Attribute Details
.config ⇒ Hash (readonly)
Configuration that read from YAML file.
15 16 17 |
# File 'lib/git_commit_notifier/commit_hook.rb', line 15 def config @config end |
Class Method Details
.add_committer_to_recipient(recipient, committer_email) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/git_commit_notifier/commit_hook.rb', line 47 def add_committer_to_recipient(recipient, committer_email) if email?(committer_email) "#{recipient},#{committer_email}" else recipient end end |
.email?(email) ⇒ Boolean
43 44 45 |
# File 'lib/git_commit_notifier/commit_hook.rb', line 43 def email?(email) !!(email =~ /@/) end |
.get_reply_to_address(config, committer_email) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/git_commit_notifier/commit_hook.rb', line 55 def get_reply_to_address(config, committer_email) reply_to_address = config["from"] if config["reply_to_author"] reply_to_address = committer_email elsif config["reply_to_mailinglist"] reply_to_address = recipient end reply_to_address end |
.get_subject(commit_info, template, subject_map) ⇒ String
Gets message subject.
90 91 92 93 94 95 96 97 98 |
# File 'lib/git_commit_notifier/commit_hook.rb', line 90 def get_subject(commit_info, template, subject_map) template.gsub(/\$\{(\w+)\}/) do |m| res = subject_map[$1.intern] if res.kind_of?(Proc) res = res.call(commit_info) end res end end |
.include_branches ⇒ Array(String), NilClass
Note:
All branches will be notified about if returned list is nil; otherwise only specified branches will be notifified about.
Gets list of branches from config to include into notifications.
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/git_commit_notifier/commit_hook.rb', line 68 def include_branches include_branches = config["include_branches"] unless include_branches.nil? if include_branches.kind_of?(String) && include_branches =~ /\,/ include_branches = include_branches.split(/\s*\,\s*/) end include_branches = Array(include_branches) end include_branches end |
.info(message) ⇒ NilClass
Prints informational message to $stdout.
32 33 34 35 36 |
# File 'lib/git_commit_notifier/commit_hook.rb', line 32 def info() $stdout.puts $stdout.flush nil end |
.logger ⇒ Object
Gets logger.
39 40 41 |
# File 'lib/git_commit_notifier/commit_hook.rb', line 39 def logger @logger ||= Logger.new(config) end |
.merge_commit?(commit_info) ⇒ Boolean
Is merge commit?
81 82 83 |
# File 'lib/git_commit_notifier/commit_hook.rb', line 81 def merge_commit?(commit_info) ! commit_info[:commit_info][:merge].nil? end |
.run(config_name, rev1, rev2, ref_name) ⇒ NilClass
Runs comit hook handler using specified arguments.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/git_commit_notifier/commit_hook.rb', line 107 def run(config_name, rev1, rev2, ref_name) if @start_time.nil? @start_time = Time.new @start_time_offset = 0 end # Load the configuration if File.exists?(config_name) @config = YAML::load_file(config_name) else GitCommitNotifier::CommitHook.info("Unable to find configuration file: #{config_name}") @config = {} end project_path = Git.git_dir repo_name = Git.repo_name prefix = config["emailprefix"] || repo_name branch_name = if ref_name =~ /^refs\/heads\/(.+)$/ $1 else ref_name.split("/").last end slash_branch_name = "/#{branch_name}" slash_branch_name = "" if !config["show_master_branch_name"] && slash_branch_name == '/master' # Identify email recipients recipient = nil recipient = Git.mailing_list_address if config["prefer_git_config_mailinglist"] if config["mailinglist"].kind_of?(Hash) recipient ||= config["mailinglist"][branch_name] || config["mailinglist"]["default"] else recipient ||= config["mailinglist"] end recipient ||= Git.mailing_list_address unless config["prefer_git_config_mailinglist"] # If no recipients specified, bail out gracefully. This is not an error, and might be intentional if recipient.nil? || recipient.length == 0 info("bypassing commit notification; no recipients specified (consider setting git config hooks.mailinglist)") return end # Debug information logger.debug('----') logger.debug("cwd: #{Dir.pwd}") logger.debug("Git Directory: #{project_path}") logger.debug("prefix: #{prefix}") logger.debug("repo_name: #{repo_name}") logger.debug("branch: #{branch_name}") logger.debug("slash_branch: #{slash_branch_name}") logger.debug("ref_name: #{ref_name}") logger.debug("rev1: #{rev1}") logger.debug("rev2: #{rev2}") logger.debug("included branches: #{include_branches.join(', ')}") unless include_branches.nil? unless include_branches.nil? || include_branches.include?(branch_name) info("Supressing mail for branch #{branch_name}...") return nil end # Replacements for subject template # prefix # repo_name # branch_name # slash_branch_name # commit_id (hash) # short_commit_id (first few unique digits of the hash) # description ('git describe' tag) # short_message # commit_number # commit_count # commit_count_phrase (1 commit, 2 commits, etc) # commit_count_phrase2 (2 commits:, 3 commits:, etc, or "" if just one) subject_words = { :prefix => prefix, :repo_name => repo_name, :branch_name => branch_name, :slash_branch_name => slash_branch_name, :commit_id => nil, :short_commit_id => lambda { |commit_info| Git.short_commit_id(commit_info[:commit]) }, :description => lambda { |commit_info| Git.describe(commit_info[:commit]) }, :message => nil, :commit_number => nil, :commit_count => nil, :commit_count_phrase => nil, :commit_count_phrase2 => nil } info("Sending mail...") diff2html = DiffToHtml.new(config) if config["group_email_by_push"] diff2html.diff_between_revisions(rev1, rev2, prefix, ref_name) diffresult = diff2html.result diff2html.clear_result text, html = [], [] result = diffresult.first return if result.nil? || !result[:commit_info] diffresult.each_with_index do |result, i| text << result[:text_content] html << result[:html_content] end # Form the subject from template revised_subject_words = subject_words.merge({ :commit_id => result[:commit_info][:commit], :message => result[:commit_info][:message], :commit_number => 1, :commit_count => diffresult.size, :commit_count_phrase => diffresult.size == 1 ? "1 commit" : "#{diffresult.size} commits", :commit_count_phrase2 => diffresult.size == 1 ? "" : "#{diffresult.size} commits: " }) subject_template = config['subject'] || "[${prefix}${slash_branch_name}] ${commit_count_phrase2}${message}" subject = get_subject(result[:commit_info], subject_template, revised_subject_words) emailer = Emailer.new(config, :project_path => project_path, :recipient => config["send_mail_to_committer"] ? add_committer_to_recipient(recipient, result[:commit_info][:email]) : recipient, :from_address => config["from"] || result[:commit_info][:email], :from_alias => result[:commit_info][:author], :reply_to_address => get_reply_to_address(config, result[:commit_info][:email]), :subject => subject, :commit_date => result[:commit_info][:date], :current_date => Time.new.rfc2822, :offset_date => (@start_time + @start_time_offset).rfc2822, :text_message => text.join("------------------------------------------\n\n"), :html_message => html.join("<hr /><br />"), :old_rev => rev1, :new_rev => rev2, :ref_name => ref_name, :repo_name => repo_name ) emailer.send # WEBHOOK patch unless config['webhook'].nil? webhook = Webhook.new(config, :committer => result[:commit_info][:author], :email => result[:commit_info][:email], :message => result[:commit_info][:message], :subject => subject, :changed => Git.split_status(rev1,rev2), :old_rev => rev1, :new_rev => rev2, :ref_name => ref_name, :repo_name => repo_name ) webhook.send @start_time_offset += 1 end else commit_number = 1 diff2html.diff_between_revisions(rev1, rev2, prefix, ref_name) do |count, result| # Form the subject from template revised_subject_words = subject_words.merge({ :commit_id => result[:commit_info][:commit], :message => result[:commit_info][:message], :commit_number => commit_number, :commit_count => count, :commit_count_phrase => count == 1 ? "1 commit" : "#{count} commits", :commit_count_phrase2 => count == 1 ? "" : "#{count} commits: " }) subject_template = config['subject'] || "[${prefix}${slash_branch_name}][${commit_number}/${commit_count}] ${message}" subject = get_subject(result[:commit_info], subject_template, revised_subject_words) emailer = Emailer.new(config, :project_path => project_path, :recipient => config["send_mail_to_committer"] ? add_committer_to_recipient(recipient, result[:commit_info][:email]) : recipient, :from_address => config["from"] || result[:commit_info][:email], :from_alias => result[:commit_info][:author], :reply_to_address => get_reply_to_address(config, result[:commit_info][:email]), :subject => subject, :commit_date => result[:commit_info][:date], :current_date => Time.new.rfc2822, :offset_date => (@start_time + @start_time_offset).rfc2822, :text_message => result[:text_content], :html_message => result[:html_content], :old_rev => rev1, :new_rev => rev2, :ref_name => ref_name, :repo_name => repo_name, :message_link => result[:commit_link] ) emailer.send # WEBHOOK patch unless config['webhook'].nil? webhook = Webhook.new(config, :committer => result[:commit_info][:author], :email => result[:commit_info][:email], :message => result[:commit_info][:message], :subject => subject, :changed => Git.split_status(rev1,rev2), :old_rev => rev1, :new_rev => rev2, :ref_name => ref_name, :repo_name => repo_name ) webhook.send end commit_number += 1 @start_time_offset += 1 end end nil end |
.show_error(message) ⇒ NilClass
Prints error message to $stderr.
20 21 22 23 24 25 26 27 |
# File 'lib/git_commit_notifier/commit_hook.rb', line 20 def show_error() $stderr.puts "************** GIT NOTIFIER PROBLEM *******************" $stderr.puts "\n" $stderr.puts $stderr.puts "\n" $stderr.puts "************** GIT NOTIFIER PROBLEM *******************" nil end |