/[adm]/puppet/deployment/mgagit/templates/git-post-receive-hook
ViewVC logotype

Contents of /puppet/deployment/mgagit/templates/git-post-receive-hook

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3418 - (show annotations) (download)
Mon Feb 10 10:33:27 2014 UTC (10 years, 2 months ago) by colin
File size: 7305 byte(s)
mgagit: Use a 5 second timeout to notify alamut.

This keeps the stall time on a push to something sensible should there be
a problem.
1 #!/usr/bin/python
2
3 import sys
4 import os
5 import re
6
7 LIBDIR = '<%= @gitolite_commonhooksdir %>'
8 sys.path.insert(0, LIBDIR)
9
10 import git_multimail
11
12 import xmlrpclib
13 from cookielib import LWPCookieJar
14 from bugz.bugzilla import BugzillaProxy
15
16 import urllib2
17
18 # When editing this list, remember to edit the same list in
19 # modules/cgit/templates/filter.commit-links.sh
20 BUG_REFS = {
21 'Mageia': { 're': re.compile('mga#([0-9]+)'), 'replace': 'https://bugs.mageia.org/show_bug.cgi?id=%s' },
22 'Red Hat': { 're': re.compile('rhbz#([0-9]+)'), 'replace': 'https://bugzilla.redhat.com/show_bug.cgi?id=%s' },
23 'Free Desktop': { 're': re.compile('fdo#([0-9]+)'), 'replace': 'https://bugs.freedesktop.org/show_bug.cgi?id=%s' },
24 'KDE': { 're': re.compile('(?:bko|kde)#([0-9]+)'), 'replace': 'https://bugs.kde.org/show_bug.cgi?id=%s' },
25 'GNOME': { 're': re.compile('(?:bgo|gnome)#([0-9]+)'), 'replace': 'https://bugzilla.gnome.org/show_bug.cgi?id=%s' },
26 'Launchpad': { 're': re.compile('lp#([0-9]+)'), 'replace': 'https://launchpad.net/bugs/%s' },
27 }
28
29 COMMIT_RE = re.compile('^commit ([a-f0-9]{40})')
30 COMMIT_REPLACE = 'http://gitweb.mageia.org/%s/commit/?id=%s'
31
32 MAGEIA_BUGZILLA_URL = 'https://bugs.mageia.org/xmlrpc.cgi'
33 MAGEIA_BUGZILLA_PASSWORD_FILE = '.gitzilla-password'
34 MAGEIA_BUGZILLA_COOKIE_FILE = '.gitzilla-cookie'
35
36
37 git_multimail.FOOTER_TEMPLATE = """\
38
39 -- \n\
40 Mageia Git Monkeys.
41 """
42 git_multimail.REVISION_FOOTER_TEMPLATE = git_multimail.FOOTER_TEMPLATE
43
44 REPO_NAME_RE = re.compile(r'^/git/(?P<name>.+?)(?:\.git)?$')
45 def repo_shortname():
46 basename = os.path.abspath(git_multimail.get_git_dir())
47 m = REPO_NAME_RE.match(basename)
48 if m:
49 return m.group('name')
50 else:
51 return basename
52
53
54 # Override the Environment class to generate an apporpriate short name which is
55 # used in git links and as an email prefix
56 class LinksEnvironment(git_multimail.Environment):
57 def get_repo_shortname(self):
58 return repo_shortname()
59 git_multimail.Environment = LinksEnvironment
60
61 # Override the Reviesion class to inject gitweb/cgit links and any referenced
62 # bug URLs
63 class LinksRevision(git_multimail.Revision):
64 bz = None
65
66 def bugzilla_init(self):
67 if self.bz is None:
68 cookie_file = os.path.join(os.environ['HOME'], MAGEIA_BUGZILLA_COOKIE_FILE)
69 self.cookiejar = LWPCookieJar(cookie_file)
70 try:
71 self.cookiejar.load()
72 except IOError:
73 pass
74
75 self.bz = BugzillaProxy(MAGEIA_BUGZILLA_URL, cookiejar=self.cookiejar)
76 return self.bz
77
78 def bugzilla_login(self):
79 params = {
80 'login': 'bot',
81 'password': open(os.path.join(os.environ['HOME'], MAGEIA_BUGZILLA_PASSWORD_FILE), 'r').readline().rstrip(),
82 'remember': True
83 }
84 self.bz.User.login(params)
85 self.cookiejar.save()
86 os.chmod(self.cookiejar.filename, 0600)
87
88 def bugzilla_call(self, method, *args):
89 """Attempt to call method with args. Log in if authentication is required.
90 """
91 try:
92 return method(*args)
93 except xmlrpclib.Fault, fault:
94 # Fault code 410 means login required
95 if fault.faultCode == 410:
96 self.bugzilla_login()
97 return method(*args)
98 raise
99
100 def generate_email_body(self, push):
101 """Show this revision."""
102
103 output = git_multimail.read_git_lines(
104 ['log'] + self.environment.commitlogopts + ['-1', self.rev.sha1],
105 keepends=True,
106 )
107 bugs = {}
108 commit = None
109 idx = 0
110 for line in output:
111 idx += 1
112 if line == "---\n":
113 if commit and COMMIT_REPLACE:
114 output.insert(idx, "\n")
115 output.insert(idx, " %s\n" % (COMMIT_REPLACE % (self.environment.get_repo_shortname(), commit)))
116 output.insert(idx, " Commit Link:\n")
117 idx += 3
118 if bugs:
119 output.insert(idx, " Bug links:\n")
120 idx += 1
121 for tracker, bugnos in bugs.items():
122 output.insert(idx, " %s\n" % tracker)
123 idx += 1
124 for bugno in bugnos:
125 output.insert(idx, " %s\n" % (BUG_REFS[tracker]['replace'] % bugno))
126 idx += 1
127 output.insert(idx, "\n")
128 idx += 1
129
130 # Attempt to modify bugzilla
131 if "Mageia" in bugs:
132 try:
133 bz = self.bugzilla_init()
134
135 # Mask email address
136 comment = None
137 # Suppress the "Bug links:" section if only one bug
138 # is referenced
139 if len(bugs) == 1 and len(bugs['Mageia']) == 1:
140 comment = output[0:idx-4]
141 else:
142 comment = output[0:idx]
143 comment[1] = re.sub(r'^(Author: [^@]*)@.*(>)?', r'\1@...>', comment[1])
144 comment = "".join(comment)
145
146 params = {}
147 params['ids'] = bugs['Mageia']
148 params['comment'] = { 'body': comment }
149 self.bugzilla_call(bz.Bug.update, params)
150 print "Updated bugzilla bugs: %s" % ", ".join(bugs['Mageia'])
151 except:
152 print "Unable to post to bugzilla bugs: %s :(" % ", ".join(bugs['Mageia'])
153 pass
154
155 break
156 m = COMMIT_RE.search(line)
157 if m:
158 commit = m.group(1)
159 for tracker in BUG_REFS.keys():
160 foundbugs = BUG_REFS[tracker]['re'].findall(line)
161 if len(foundbugs):
162 if not tracker in bugs:
163 bugs[tracker] = foundbugs
164 else:
165 bugs[tracker] = list(set(bugs[tracker] + foundbugs))
166
167 return output
168
169 git_multimail.Revision = LinksRevision
170
171 if __name__ == '__main__':
172 # Attempt to write a last-updated file for cgit cosmetics
173 try:
174 git_dir = git_multimail.get_git_dir()
175 infowebdir = os.path.join(git_dir, 'info', 'web')
176 if not os.path.exists(infowebdir):
177 os.makedirs(infowebdir)
178 lastupdated = git_multimail.read_git_output(
179 ['for-each-ref', '--sort=-committerdate', "--format=%(committerdate:iso8601)", '--count=1', 'refs/heads'],
180 )
181 modfile = open(os.path.join(infowebdir, 'last-modified'), 'w')
182 modfile.write(lastupdated)
183 modfile.close()
184 except Exception:
185 pass
186
187 try:
188 req = urllib2.Request('http://alamut.mageia.org:8000', repo_shortname() + '.git')
189 req.add_header('Content-Type', 'x-git/repo')
190 fp = urllib2.urlopen(req, timeout=5)
191 if (fp):
192 fp.close()
193 except Exception:
194 pass
195
196 git_multimail.main(sys.argv[1:])

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.30