From d95f0b5d6bc53aee939bfd0c0acdbea6ee627e38 Mon Sep 17 00:00:00 2001 From: "lin.zhou" Date: Fri, 15 Feb 2019 14:33:15 +0800 Subject: [PATCH] feat: add support for Windows --- change-merged | 14 +++++++++++++- change-merged.bat | 1 + comment-added | 12 ++++++++++++ comment-added.bat | 1 + patchset-created | 12 ++++++++++++ patchset-created.bat | 1 + ref-update | 10 ++++++++++ ref-update.bat | 2 ++ trac_update.py | 29 +++++++++++++++++++---------- 9 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 change-merged.bat create mode 100644 comment-added.bat create mode 100644 patchset-created.bat create mode 100644 ref-update.bat diff --git a/change-merged b/change-merged index 4c81752..c1f334c 100755 --- a/change-merged +++ b/change-merged @@ -36,15 +36,27 @@ parser.add_option("-b", "--branch", dest="branch_name", parser.add_option("-t", "--topic", dest="topic", help="Gerrit topic of Changeset") parser.add_option("-s", "--submitter", dest="submitter", - help="Gerrit Patchset id") + help="Gerrit Submitter") +parser.add_option("--submitter-username", dest="submitter_username", + help="Gerrit Submitter User Name") parser.add_option("-c", "--commit", dest="commit", help="GIT Commit SHA ID") parser.add_option("--change-owner", dest="change_owner", help="Gerrit Change Owner") +parser.add_option("--change-owner-username", dest="change_owner_username", + help="Gerrit Change Owner User Name") parser.add_option("--newrev", dest="new_rev", help="Gerrit New Rev") options, args = parser.parse_args() +try: + idx = sys.argv.index('--topic') +except ValueError: + pass +else: + if sys.argv[idx+1].startswith('--'): + sys.argv.insert(idx+1, '') + options, args = parser.parse_args() # run trac update with hook-name and options trac_update = TracGerritTicket(hook_name=sys.argv[0], diff --git a/change-merged.bat b/change-merged.bat new file mode 100644 index 0000000..2487016 --- /dev/null +++ b/change-merged.bat @@ -0,0 +1 @@ +python D:\gerrit\site\hooks\change-merged %* \ No newline at end of file diff --git a/comment-added b/comment-added index 1a12957..c13a07e 100755 --- a/comment-added +++ b/comment-added @@ -39,6 +39,8 @@ parser.add_option("-t", "--topic", dest="topic", help="Gerrit topic of Changeset") parser.add_option("-a", "--author", dest="author", help="Gerrit Comment Author", default=None) +parser.add_option("--author-username", dest="author_username", + help="Gerrit Comment Author User Name", default=None) parser.add_option("-c", "--commit", dest="commit", help="GIT Commit SHA ID") parser.add_option("-m", "--comment", dest="comment", @@ -47,6 +49,8 @@ parser.add_option("--Code-Review", dest="review", help="Gerrit Review", default=None) parser.add_option("--change-owner", dest="change_owner", help="Gerrit Change Owner", default=None) +parser.add_option("--change-owner-username", dest="change_owner_username", + help="Gerrit Change Owner User Name") parser.add_option("--Verified", dest="verified", help="Gerrit Verified", default=None) parser.add_option("--Verified-oldValue", dest="verified_oldValue", @@ -56,6 +60,14 @@ parser.add_option("--Code-Review-oldValue", dest="review_oldValue", options, args = parser.parse_args() +try: + idx = sys.argv.index('--topic') +except ValueError: + pass +else: + if sys.argv[idx+1].startswith('--'): + sys.argv.insert(idx+1, '') + options, args = parser.parse_args() # run trac update with hook-name and options trac_update = TracGerritTicket(hook_name=sys.argv[0], diff --git a/comment-added.bat b/comment-added.bat new file mode 100644 index 0000000..2fb7f55 --- /dev/null +++ b/comment-added.bat @@ -0,0 +1 @@ +python D:\gerrit\site\hooks\comment-added %* \ No newline at end of file diff --git a/patchset-created b/patchset-created index fb0aebf..c79a8cd 100755 --- a/patchset-created +++ b/patchset-created @@ -35,6 +35,8 @@ parser.add_option("-b", "--branch", dest="branch_name", help="Gerrit Branch Name") parser.add_option("-l", "--uploader", dest="uploader", help="Gerrit Uploader", default=None) +parser.add_option("--uploader-username", dest="uploader_username", + help="Gerrit Uploader User Name", default=None) parser.add_option("-c", "--commit", dest="commit", help="GIT Commit SHA ID") parser.add_option("-u", "--change-url", dest="change_url", @@ -47,8 +49,18 @@ parser.add_option("-k", "--kind", dest="kind", help="Gerrit Change Kind") parser.add_option("--change-owner", dest="change_owner", help="Gerrit Change Owner") +parser.add_option("--change-owner-username", dest="change_owner_username", + help="Gerrit Change Owner User Name") options, args = parser.parse_args() +try: + idx = sys.argv.index('--topic') +except ValueError: + pass +else: + if sys.argv[idx+1].startswith('--'): + sys.argv.insert(idx+1, '') + options, args = parser.parse_args() # run trac update with hook-name and options trac_update = TracGerritTicket(hook_name=sys.argv[0], diff --git a/patchset-created.bat b/patchset-created.bat new file mode 100644 index 0000000..c1b963e --- /dev/null +++ b/patchset-created.bat @@ -0,0 +1 @@ +python D:\gerrit\site\hooks\patchset-created %* \ No newline at end of file diff --git a/ref-update b/ref-update index 4bd94c8..d8451c1 100755 --- a/ref-update +++ b/ref-update @@ -31,12 +31,22 @@ parser.add_option("-r", "--refname", dest="refname", help="Git ref Name") parser.add_option("-u", "--uploader", dest="uploader", help="Gerrit Uploader") +parser.add_option("--uploader-username", dest="uploader_username", + help="Gerrit Uploader User Name", default=None) parser.add_option("-o", "--oldrev", dest="oldrev", help="GIT old rev") parser.add_option("-n", "--newrev", dest="newrev", help="GIT new rev") options, args = parser.parse_args() +try: + idx = sys.argv.index('--topic') +except ValueError: + pass +else: + if sys.argv[idx+1].startswith('--'): + sys.argv.insert(idx+1, '') + options, args = parser.parse_args() trac_update = TracGerritTicket(hook_name=sys.argv[0], options=options) diff --git a/ref-update.bat b/ref-update.bat new file mode 100644 index 0000000..2c594c7 --- /dev/null +++ b/ref-update.bat @@ -0,0 +1,2 @@ +exit 0 +python D:\gerrit\site\hooks\ref-update %* \ No newline at end of file diff --git a/trac_update.py b/trac_update.py index c1284a0..8589137 100644 --- a/trac_update.py +++ b/trac_update.py @@ -30,8 +30,10 @@ from subprocess import Popen, PIPE, call from datetime import datetime +from trac.env import open_environment + ## regex to find #TICKETNUMBER -TICKET_RE = re.compile('#([0-9]+)') +TICKET_RE = re.compile('(?:#|issue|bug|ticket|<)\s*([0-9]+)') ## remove substring from end of string # thanks to http://stackoverflow.com/a/3663505/1922402 @@ -208,15 +210,16 @@ def __init__(self, hook_name, options, config_path=None, debug=False): self.trac_env = self.config.get_env_for_repo(self.repo_name) if not self.trac_env: + print("no trac environment setting, exit") sys.exit(0) - if self.trac_env.startswith("http"): + if self.trac_env.startswith("http"): self.trac_over_rpc = True else: self.trac_over_rpc = False self.env = open_environment(self.trac_env) - self.hook_name = hook_name + self.hook_name = rchop(hook_name, ".py") self.debug = debug self.commit_msg = "" @@ -333,12 +336,14 @@ def trac_merge_success(self): msg = "!Repo/Branch: %s/%s\n" \ "[%s Gerrit Patchset merged]\n\n" \ "%s\n\n" \ - "merged by %s" \ + "merged by %s\n\n" \ + "Commit-Id: %s" \ % (self.repo_name, self.options.branch_name, self.options.change_url, self.commit_msg, - self.options.submitter) + self.options.submitter, + self.options.commit) return msg def trac_new_review(self): @@ -356,7 +361,7 @@ def trac_new_review(self): else: comment = self.options.comment.split('\n') change_url_line = "[%s Comment]\n\n" % self.options.change_url - comment_line = "Comment zu %s\n\n%s" % (comment[0], + comment_line = "Comment to %s\n\n%s" % (comment[0], '\n>'.join(comment[1:])) msg = "%s" \ "%s" \ @@ -371,13 +376,17 @@ def trac_new_patchset(self): correct_change_url = "{0}/c/{1}".format(part_one, part_two) msg = "!Repo/Branch: %s/%s\n" \ "[%s/%s Gerrit Patchset %s]\n\n" \ - "%s" \ + "Author: %s\n" \ + "%s\n\n" \ + "uploaded by %s\n\n" \ % (self.repo_name, self.options.branch_name, correct_change_url, self.options.patchset, self.options.patchset, - self.commit_msg) + self.options.change_owner, + self.commit_msg, + self.options.uploader) return msg @@ -387,7 +396,7 @@ def handle_trac(self): self.options.is_draft == 'true'): return - if self.trac_over_rpc: + if self.trac_over_rpc: import xmlrpclib else: if not (os.path.exists(self.trac_env) and @@ -473,7 +482,7 @@ def handle_trac(self): sys.stderr.write('Unexpected error while handling Trac ' \ 'ticket ID %s: %s (RPC)' \ % (ticket_id, e)) - + else: try: db = self.env.get_db_cnx()