-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgithub-hook-email.rb
executable file
·159 lines (119 loc) · 2.78 KB
/
github-hook-email.rb
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
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
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
#! /usr/bin/env ruby
require 'json'
require 'date'
def align(text, indent = ' ')
margin = text[/\A\s+/].size
text.gsub(/^\s{#{margin}}/, indent)
end
def commits
Array($payload['commits'])
end
def shorten(text, limit)
text.slice(0, limit) << '...'
end
def name_with_owner
File.join(owner_name, repository_name)
end
def owner_name
$payload['repository']['owner']['login']
end
def repository_name
$payload['repository']['name']
end
def repo_url
$payload['repository']['url']
end
# Public
def mail_subject
if first_commit
"[#{name_with_owner}] #{first_commit_sha.slice(0, 6)}: #{first_commit_title}"
else
"[#{name_with_owner}]"
end
end
# Public
def mail_body
body = commits.inject(repository_text) do |text, commit|
text << commit_text(commit)
end
body << compare_text unless single_commit?
body
end
def repository_text
align(<<-EOH)
Branch: #{branch_ref}
Home: #{repo_url}
EOH
end
def commit_text(commit)
gitsha = commit['id']
added = commit['added'].map { |f| ['A', f] }
removed = commit['removed'].map { |f| ['R', f] }
modified = commit['modified'].map { |f| ['M', f] }
changed_paths = (added + removed + modified).sort_by { |(char, file)| file }
changed_paths = changed_paths.collect { |entry| entry * ' ' }.join("\n ")
timestamp = Date.parse(commit['timestamp'])
commit_author = "#{commit['author']['name']} <#{commit['author']['email']}>"
text = align(<<-EOH)
Commit: #{gitsha}
#{commit['url']}
Author: #{commit_author}
Date: #{timestamp} (#{timestamp.strftime('%a, %d %b %Y')})
EOH
if changed_paths.size > 0
text << align(<<-EOH)
Changed paths:
#{changed_paths}
EOH
end
text << align(<<-EOH)
Log Message:
-----------
#{commit['message']}
EOH
text
end
def compare_text
"Compare: #{$payload['compare']}"
end
def single_commit?
first_commit == last_commit
end
def branch_ref
$payload['ref']
end
def author_address
"#{author_name} <#{author_email}>"
end
def author
commit = last_commit || {}
commit['author'] || commit['committer'] || $payload['pusher']
end
def author_name
author['name']
end
def author_email
author['email']
end
def last_commit
$payload['commits'].last # assume that the last committer is also the pusher
end
def first_commit_sha
first_commit['id']
end
def first_commit_title(limit = 50)
title_line = first_commit['message'][/\A[^\n]+/] || ''
title_line.length > limit ? shorten(title_line, limit) : title_line
end
def first_commit
$payload['commits'].first
end
# must be a difference with push $payloads
def owner_name
$payload['repository']['owner']['name']
end
## main
$payload = JSON.parse(ARGV[0])
puts mail_subject
puts
puts mail_body