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

Delta Between Two Patch Sets: sitescripts/hg/bin/update_issues.py

Issue 29336790: Issue 3674 - Add a hg hook that references commits in issues (Closed)
Left Patch Set: Address comments Created Feb. 22, 2016, 8:04 p.m.
Right Patch Set: Don't use RegexObject.match Created Feb. 23, 2016, 9:39 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « .sitescripts.example ('k') | sitescripts/hg/template/issue_commit_comment.tmpl » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 # This file is part of Adblock Plus <https://adblockplus.org/>, 3 # This file is part of Adblock Plus <https://adblockplus.org/>,
4 # Copyright (C) 2006-2016 Eyeo GmbH 4 # Copyright (C) 2006-2016 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 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 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
(...skipping 12 matching lines...) Expand all
23 """ 23 """
24 24
25 import posixpath 25 import posixpath
26 import re 26 import re
27 import xmlrpclib 27 import xmlrpclib
28 28
29 from sitescripts.utils import get_config, get_template 29 from sitescripts.utils import get_config, get_template
30 30
31 def _generate_comments(repository_name, changes_by_issue): 31 def _generate_comments(repository_name, changes_by_issue):
32 comments = {} 32 comments = {}
33 template = get_template("hg/template/issue_commit_comment.tmpl") 33 template = get_template("hg/template/issue_commit_comment.tmpl",
Wladimir Palant 2016/02/23 10:41:31 I think you need autoescape=False here.
Felix Dahlke 2016/02/23 16:22:59 Done.
34 autoescape=False)
34 for issue_id, changes in changes_by_issue.iteritems(): 35 for issue_id, changes in changes_by_issue.iteritems():
35 comments[issue_id] = template.render({"repository_name": repository_name, 36 comments[issue_id] = template.render({"repository_name": repository_name,
36 "changes": changes}) 37 "changes": changes})
37 return comments 38 return comments
38 39
39 def _post_comment(issue_id, comment): 40 def _post_comment(issue_id, comment):
40 issue_id = int(issue_id) 41 issue_id = int(issue_id)
41 url = get_config().get("hg", "trac_xmlrpc_url") 42 url = get_config().get("hg", "trac_xmlrpc_url")
42 server = xmlrpclib.ServerProxy(url) 43 server = xmlrpclib.ServerProxy(url)
43 attributes = server.ticket.get(issue_id)[3] 44 attributes = server.ticket.get(issue_id)[3]
44 server.ticket.update(issue_id, comment, 45 server.ticket.update(issue_id, comment,
45 {"_ts": attributes["_ts"], "action": "leave"}, True) 46 {"_ts": attributes["_ts"], "action": "leave"}, True)
46 47
47 def hook(ui, repo, node, **kwargs): 48 def hook(ui, repo, node, **kwargs):
48 first_change = repo[node] 49 first_change = repo[node]
49 issue_number_regex = re.compile(r"\bissue\s+(\d+)\b", re.I) 50 issue_number_regex = re.compile(r"\bissue\s+(\d+)\b", re.I)
50 noissue_regex = re.compile(r"noissue\b", re.I) 51 noissue_regex = re.compile(r"^noissue\b", re.I)
Wladimir Palant 2016/02/23 10:41:31 This should be \bnoissue\b (make sure that the wor
Felix Dahlke 2016/02/23 16:22:59 I'm using match() so the message has to start with
Wladimir Palant 2016/02/23 21:33:23 We agreed a while ago that we shouldn't be using m
Felix Dahlke 2016/02/23 21:40:58 Fair enough. Tried to be smart, but this comment t
Wladimir Palant 2016/02/24 10:13:49 Yes, particularly with the regexp and re.match() n
51 changes_by_issue = {} 52 changes_by_issue = {}
52 for revision in xrange(first_change.rev(), len(repo)): 53 for revision in xrange(first_change.rev(), len(repo)):
53 change = repo[revision] 54 change = repo[revision]
54 description = change.description() 55 description = change.description()
55 issue_ids = issue_number_regex.findall(description) 56 issue_ids = issue_number_regex.findall(description)
56 if issue_ids: 57 if issue_ids:
57 for issue_id in issue_ids: 58 for issue_id in issue_ids:
58 changes_by_issue.setdefault(issue_id, []).append(change) 59 changes_by_issue.setdefault(issue_id, []).append(change)
59 elif not noissue_regex.match(description): 60 elif not noissue_regex.search(description):
60 # We should just reject all changes when one of them has an invalid 61 # We should just reject all changes when one of them has an invalid
61 # commit message format, see: https://issues.adblockplus.org/ticket/3679 62 # commit message format, see: https://issues.adblockplus.org/ticket/3679
Wladimir Palant 2016/02/23 10:41:31 Is that a TODO comment? Maybe it should actually s
Felix Dahlke 2016/02/23 16:22:59 Well it's referencing an issue, I normally do just
62 ui.warn("warning: invalid commit message format in changeset %s\n" % 63 ui.warn("warning: invalid commit message format in changeset %s\n" %
63 change) 64 change)
64 65
65 repository_name = posixpath.split(repo.url())[1] 66 repository_name = posixpath.split(repo.url())[1]
66 comments = _generate_comments(repository_name, changes_by_issue) 67 comments = _generate_comments(repository_name, changes_by_issue)
67 68
68 issue_url_template = get_config().get("hg", "issue_url_template") 69 issue_url_template = get_config().get("hg", "issue_url_template")
69 for issue_id, comment in comments.iteritems(): 70 for issue_id, comment in comments.iteritems():
70 try: 71 try:
71 _post_comment(issue_id, comment) 72 _post_comment(issue_id, comment)
72 ui.status("updating %s\n" % issue_url_template.format(id=issue_id)) 73 ui.status("updating %s\n" % issue_url_template.format(id=issue_id))
73 except: 74 except:
74 ui.warn("warning: failed to update %s\n" % 75 ui.warn("warning: failed to update %s\n" %
75 issue_url_template.format(id=issue_id)) 76 issue_url_template.format(id=issue_id))
LEFTRIGHT

Powered by Google App Engine
This is Rietveld