Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: sitescripts/hg/bin/update_issues.py

Issue 29336790: Issue 3674 - Add a hg hook that references commits in issues (Closed)
Patch Set: Address comments Created Feb. 23, 2016, 4:22 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « .sitescripts.example ('k') | sitescripts/hg/template/issue_commit_comment.tmpl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python
2
3 # This file is part of Adblock Plus <https://adblockplus.org/>,
4 # Copyright (C) 2006-2016 Eyeo GmbH
5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation.
9 #
10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17
18 """
19 This module implements a changegroup (or pretxnchangegroup) hook that inspects
20 all commit messages and checks if issues from the Adblock Plus issue tracker are
21 being referenced. If there are, it updates them with the respective changeset
22 URLs.
23 """
24
25 import posixpath
26 import re
27 import xmlrpclib
28
29 from sitescripts.utils import get_config, get_template
30
31 def _generate_comments(repository_name, changes_by_issue):
32 comments = {}
33 template = get_template("hg/template/issue_commit_comment.tmpl",
34 autoescape=False)
35 for issue_id, changes in changes_by_issue.iteritems():
36 comments[issue_id] = template.render({"repository_name": repository_name,
37 "changes": changes})
38 return comments
39
40 def _post_comment(issue_id, comment):
41 issue_id = int(issue_id)
42 url = get_config().get("hg", "trac_xmlrpc_url")
43 server = xmlrpclib.ServerProxy(url)
44 attributes = server.ticket.get(issue_id)[3]
45 server.ticket.update(issue_id, comment,
46 {"_ts": attributes["_ts"], "action": "leave"}, True)
47
48 def hook(ui, repo, node, **kwargs):
49 first_change = repo[node]
50 issue_number_regex = re.compile(r"\bissue\s+(\d+)\b", re.I)
51 noissue_regex = re.compile(r"noissue\b", re.I)
52 changes_by_issue = {}
53 for revision in xrange(first_change.rev(), len(repo)):
54 change = repo[revision]
55 description = change.description()
56 issue_ids = issue_number_regex.findall(description)
57 if issue_ids:
58 for issue_id in issue_ids:
59 changes_by_issue.setdefault(issue_id, []).append(change)
60 elif not noissue_regex.match(description):
61 # We should just reject all changes when one of them has an invalid
62 # commit message format, see: https://issues.adblockplus.org/ticket/3679
63 ui.warn("warning: invalid commit message format in changeset %s\n" %
64 change)
65
66 repository_name = posixpath.split(repo.url())[1]
67 comments = _generate_comments(repository_name, changes_by_issue)
68
69 issue_url_template = get_config().get("hg", "issue_url_template")
70 for issue_id, comment in comments.iteritems():
71 try:
72 _post_comment(issue_id, comment)
73 ui.status("updating %s\n" % issue_url_template.format(id=issue_id))
74 except:
75 ui.warn("warning: failed to update %s\n" %
76 issue_url_template.format(id=issue_id))
OLDNEW
« no previous file with comments | « .sitescripts.example ('k') | sitescripts/hg/template/issue_commit_comment.tmpl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld