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

Unified Diff: sitescripts/hg/bin/update_issues.py

Issue 29336790: Issue 3674 - Add a hg hook that references commits in issues (Closed)
Patch Set: Created Feb. 22, 2016, 10:40 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« .sitescripts.example ('K') | « .sitescripts.example ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sitescripts/hg/bin/update_issues.py
===================================================================
new file mode 100644
--- /dev/null
+++ b/sitescripts/hg/bin/update_issues.py
@@ -0,0 +1,80 @@
+#!/usr/bin/env python
+
+# This file is part of Adblock Plus <https://adblockplus.org/>,
+# Copyright (C) 2006-2016 Eyeo GmbH
+#
+# Adblock Plus is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3 as
+# published by the Free Software Foundation.
+#
+# Adblock Plus is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+
+"""
+This module implements a changegroup hook that inspects all commit messages and
+checks if issues from the Adblock Plus issue tracker are being referenced. If
+there are, it updates them with the respective changeset URLs.
+"""
+
+import posixpath
+import re
+import xmlrpclib
+
+from sitescripts.utils import get_config
+
+def _generate_changeset_url(repository_name, changeset_id):
+ template = get_config().get("hg", "changeset_url_template")
+ return template.format(repo=repository_name, id=changeset_id[:12])
+
+def _generate_comments(repository_name, changes_by_issue):
+ comments = {}
+ for issue_id, changes in changes_by_issue.iteritems():
+ multiple_changes = len(changes) > 1
+ if multiple_changes:
+ comment = "Some commits referencing this issue have landed:\n"
+ else:
+ comment = "A commit referencing this issue has landed:\n"
+ for change in changes:
+ if multiple_changes:
+ comment += "- "
+ comment += _generate_changeset_url(repository_name, change.hex())
+ comment += "\n"
+ comments[issue_id] = comment
Wladimir Palant 2016/02/22 11:32:33 I'd rather have this logic in a template. This sho
Felix Dahlke 2016/02/22 20:09:49 Done.
+ return comments
+
+def _post_comment(issue_id, comment):
+ issue_id = int(issue_id)
+ url = get_config().get("hg", "trac_xmlrpc_url")
+ server = xmlrpclib.ServerProxy(url)
+ attributes = server.ticket.get(issue_id)[3]
+ server.ticket.update(issue_id, comment,
+ {"_ts": attributes["_ts"], "action": "leave"})
Wladimir Palant 2016/02/22 12:02:51 The ridiculous part about this is: there is a race
Wladimir Palant 2016/02/22 12:06:01 Sorry about so many mails, I've only noticed some
Felix Dahlke 2016/02/22 20:09:49 Done.
Felix Dahlke 2016/02/22 20:09:50 That seems to be quite the hassle for what I think
+
+def hook(ui, repo, node, **kwargs):
+ first_change = repo[node]
+ issue_number_regex = re.compile(r"Issue (\d+) - ")
Wladimir Palant 2016/02/22 11:32:33 This is too restrictive, see https://hg.adblockplu
Felix Dahlke 2016/02/22 20:09:49 Done.
+ changes_by_issue = {}
+ for revision in xrange(first_change.rev(), len(repo)):
+ change = repo[revision]
+ description = change.description()
+ match = issue_number_regex.search(description)
Wladimir Palant 2016/02/22 11:32:33 What if there multiple issues referenced in the co
Felix Dahlke 2016/02/22 20:09:49 Never seen that before, but since you've shown a r
+ if match:
+ issue_number = match.group(1)
+ changes_by_issue.setdefault(issue_number, []).append(change)
+
+ repository_name = posixpath.split(repo.url())[1]
+ comments = _generate_comments(repository_name, changes_by_issue)
+
+ issue_url_template = get_config().get("hg", "issue_url_template")
+ for issue_id, comment in comments.iteritems():
+ try:
+ _post_comment(issue_id, comment)
+ ui.status("updating %s\n" % issue_url_template.format(id=issue_id))
+ except:
+ ui.warn("failed to update %s - does it exist?\n" % \
Wladimir Palant 2016/02/22 11:32:33 This backslash is unnecessary.
Felix Dahlke 2016/02/22 20:09:49 Done, also changed the message a bit.
+ issue_url_template.format(id=issue_id))
« .sitescripts.example ('K') | « .sitescripts.example ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld