# HG changeset patch # User Atul Varma # Date 1290537770 18000 # Node ID 8d0d0339d878f9707f55706ea5f7de8f8824b306 # Parent cf3583d9bf07088a370fcb2968dd9b9f952dc45c Added 'pullreq' command for use w/ github pull requests. diff -r cf3583d9bf07 -r 8d0d0339d878 bzpatch.py --- a/bzpatch.py Wed Jul 14 17:22:25 2010 -0700 +++ b/bzpatch.py Tue Nov 23 13:42:50 2010 -0500 @@ -6,6 +6,21 @@ import bugzilla +PULL_REQ_TEMPLATE = """ + + +Bugzilla Code Review +

You can review this patch at {URL}, +or wait 5 seconds to be redirected there automatically.

""" + +def make_pull_req_html(url): + """ + >>> make_pull_req_html('http://foo.com/pull/1') + '\\n\\n\\nBugzilla Code Review\\n

You can review this patch at http://foo.com/pull/1,\\nor wait 5 seconds to be redirected there automatically.

' + """ + + return PULL_REQ_TEMPLATE.replace("{URL}", url) + def strip_patch_header(patch): """ >>> strip_patch_header('#HG blarg\\n\\ndiff --git\\n') @@ -111,14 +126,33 @@ is_patch=True) return full_patch +def post_pullreq(bzapi, bug, url): + """ + >>> bzapi = MockBugzillaApi({'username': 'avarma@mozilla.com'}) + >>> bug = bugzilla.Bug(TEST_BUG, bzapi) + >>> post_pullreq(bzapi, bug, 'http://foo.com/pull/1') + Called bzapi.request( + 'POST', + '/bug/558680/attachment', + body={'is_obsolete': False, 'flags': [], 'description': 'Pointer to pull request', 'content_type': 'text/html', 'encoding': 'base64', 'file_name': 'bug-558680-pullreq.html', 'is_patch': False, 'data': 'PCFET0NUWVBFIGh0bWw+CjxtZXRhIGNoYXJzZXQ9InV0Zi04Ij4KPG1ldGEgaHR0cC1lcXVpdj0icmVmcmVzaCIgY29udGVudD0iNTtodHRwOi8vZm9vLmNvbS9wdWxsLzEiPgo8dGl0bGU+QnVnemlsbGEgQ29kZSBSZXZpZXc8L3RpdGxlPgo8cD5Zb3UgY2FuIHJldmlldyB0aGlzIHBhdGNoIGF0IDxhIGhyZWY9Imh0dHA6Ly9mb28uY29tL3B1bGwvMSI+aHR0cDovL2Zvby5jb20vcHVsbC8xPC9hPiwKb3Igd2FpdCA1IHNlY29uZHMgdG8gYmUgcmVkaXJlY3RlZCB0aGVyZSBhdXRvbWF0aWNhbGx5LjwvcD4=', 'is_private': False, 'size': 287}) + """ + + bzapi.attachments.post(bug_id=bug.id, + contents=make_pull_req_html(url), + filename="bug-%d-pullreq.html" % bug.id, + description="Pointer to pull request", + content_type="text/html") + if __name__ == '__main__': if len(sys.argv) < 3: - print "usage: %s [desc]" % sys.argv[0] + print "usage: %s [desc] [url]" % ( + sys.argv[0] + ) sys.exit(1) cmd = sys.argv[1] - if cmd not in ['get', 'post']: + if cmd not in ['get', 'post', 'pullreq']: print "unrecognized command: %s" % cmd sys.exit(1) @@ -128,17 +162,25 @@ print "not a valid bug id: %s" % sys.argv[2] sys.exit(1) - if cmd == 'post' and len(sys.argv) < 4: - print "patch description required." - sys.exit(1) + if len(sys.argv) < 4: + if cmd == 'post': + print "patch description required." + sys.exit(1) + elif cmd == 'pullreq': + print "pull request URL required." + sys.exit(1) bzapi = bugzilla.BugzillaApi() bug = bzapi.bugs.get(bug_id) if cmd == 'get': sys.stdout.write(get_patch(bug)) - else: + elif cmd == 'post': post_patch(bzapi=bzapi, bug=bug, patch=sys.stdin.read(), description=sys.argv[3]) + elif cmd == 'pullreq': + post_pullreq(bzapi=bzapi, + bug=bug, + url=sys.argv[3])