/[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 3384 - (show annotations) (download)
Sun Dec 22 13:31:11 2013 UTC (10 years, 4 months ago) by colin
File size: 6482 byte(s)
mgagit: Match multiple bug references on a single line
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 CookieJar, LWPCookieJar
14 from bugz.bugzilla import BugzillaProxy
15
16 # When editing this list, remember to edit the same list in
17 # modules/cgit/templates/filter.commit-links.sh
18 BUG_REFS = {
19 'Mageia': { 're': re.compile('mga#([0-9]+)'), 'replace': 'https://bugs.mageia.org/show_bug.cgi?id=%s' },
20 'Red Hat': { 're': re.compile('rhbz#([0-9]+)'), 'replace': 'https://bugzilla.redhat.com/show_bug.cgi?id=%s' },
21 'Free Desktop': { 're': re.compile('fdo#([0-9]+)'), 'replace': 'https://bugs.freedesktop.org/show_bug.cgi?id=%s' },
22 'KDE': { 're': re.compile('(?:bko|kde)#([0-9]+)'), 'replace': 'https://bugs.kde.org/show_bug.cgi?id=%s' },
23 'GNOME': { 're': re.compile('(?:bgo|gnome)#([0-9]+)'), 'replace': 'https://bugzilla.gnome.org/show_bug.cgi?id=%s' },
24 'Launchpad': { 're': re.compile('lp#([0-9]+)'), 'replace': 'https://launchpad.net/bugs/%s' },
25 }
26
27 COMMIT_RE = re.compile('^commit ([a-f0-9]{40})')
28 COMMIT_REPLACE = 'http://gitweb.mageia.org/%s/commit/?id=%s'
29
30 MAGEIA_BUGZILLA_URL = 'https://bugs.mageia.org/xmlrpc.cgi'
31 MAGEIA_BUGZILLA_PASSWORD_FILE = '.gitzilla-password'
32 MAGEIA_BUGZILLA_COOKIE_FILE = '.gitzilla-cookie'
33
34
35 git_multimail.FOOTER_TEMPLATE = """\
36
37 -- \n\
38 Mageia Git Monkeys.
39 """
40 git_multimail.REVISION_FOOTER_TEMPLATE = git_multimail.FOOTER_TEMPLATE
41
42 # Override the Environment class to generate an apporpriate short name which is
43 # used in git links and as an email prefix
44 class LinksEnvironment(git_multimail.Environment):
45 REPO_NAME_RE = re.compile(r'^/git/(?P<name>.+?)(?:\.git)?$')
46
47 def get_repo_shortname(self):
48 """Use the last part of the repo path, with ".git" stripped off if present."""
49
50 basename = os.path.abspath(self.get_repo_path())
51 m = self.REPO_NAME_RE.match(basename)
52 if m:
53 return m.group('name')
54 else:
55 return basename
56
57 git_multimail.Environment = LinksEnvironment
58
59 # Override the Reviesion class to inject gitweb/cgit links and any referenced
60 # bug URLs
61 class LinksRevision(git_multimail.Revision):
62 bz = None
63
64 def bugzilla_init(self):
65 if self.bz is None:
66 cookie_file = os.path.join(os.environ['HOME'], MAGEIA_BUGZILLA_COOKIE_FILE)
67 self.cookiejar = LWPCookieJar(cookie_file)
68 try:
69 self.cookiejar.load()
70 except IOError:
71 pass
72
73 self.bz = BugzillaProxy(MAGEIA_BUGZILLA_URL, cookiejar=self.cookiejar)
74 return self.bz
75
76 def bugzilla_login(self):
77 params = {
78 'login': 'bot',
79 'password': open(os.path.join(os.environ['HOME'], MAGEIA_BUGZILLA_PASSWORD_FILE), 'r').readline().rstrip(),
80 'remember': True
81 }
82 self.bz.User.login(params)
83 self.cookiejar.save()
84 os.chmod(self.cookiejar.filename, 0600)
85
86 def bugzilla_call(self, method, *args):
87 """Attempt to call method with args. Log in if authentication is required.
88 """
89 try:
90 return method(*args)
91 except xmlrpclib.Fault, fault:
92 # Fault code 410 means login required
93 if fault.faultCode == 410:
94 self.bugzilla_login()
95 return method(*args)
96 raise
97
98 def generate_email_body(self, push):
99 """Show this revision."""
100
101 output = git_multimail.read_git_lines(
102 ['log'] + self.environment.commitlogopts + ['-1', self.rev.sha1],
103 keepends=True,
104 )
105 bugs = {}
106 commit = None
107 idx = 0
108 for line in output:
109 idx+=1
110 if line == "---\n":
111 if commit and COMMIT_REPLACE:
112 output.insert(idx, "\n")
113 output.insert(idx, " %s\n" % (COMMIT_REPLACE % (self.environment.get_repo_shortname(), commit)))
114 output.insert(idx, " Commit Link:\n")
115 idx+=3
116 if bugs:
117 output.insert(idx, " Bug links:\n")
118 idx+=1
119 for tracker,bugnos in bugs.items():
120 output.insert(idx, " %s\n" % tracker)
121 idx+=1
122 for bugno in bugnos:
123 output.insert(idx, " %s\n" % (BUG_REFS[tracker]['replace'] % bugno))
124 idx+=1
125 output.insert(idx, "\n")
126 idx+=1
127
128 # Attempt to modify bugzilla
129 if "Mageia" in bugs:
130 try:
131 bz = self.bugzilla_init()
132
133 # Mask email address
134 comment = None
135 # Suppress the "Bug links:" section if only one bug
136 # is referenced
137 if len(bugs) == 1 and len(bugs['Mageia']) == 1:
138 comment = output[0:idx-4]
139 else:
140 comment = output[0:idx]
141 comment[1] = re.sub(r'^(Author: [^@]*)@.*(>)?', r'\1@...>', comment[1])
142 comment = "".join(comment)
143
144 params = {}
145 params['ids'] = bugs['Mageia']
146 params['comment'] = { 'body': comment }
147 self.bugzilla_call(bz.Bug.update, params)
148 print "Updated bugzilla bugs: %s" % ", ".join(bugs['Mageia'])
149 except:
150 print "Unable to post to bugzilla bugs: %s :(" % ", ".join(bugs['Mageia'])
151 pass
152
153 break
154 m = COMMIT_RE.search(line)
155 if m:
156 commit = m.group(1);
157 for tracker in BUG_REFS.keys():
158 foundbugs = BUG_REFS[tracker]['re'].findall(line)
159 if len(foundbugs):
160 if not tracker in bugs:
161 bugs[tracker] = foundbugs
162 else:
163 bugs[tracker] = list(set(bugs[tracker] + foundbugs))
164
165 return output
166
167 git_multimail.Revision = LinksRevision
168
169 if __name__ == '__main__':
170 git_multimail.main(sys.argv[1:])

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.30