upload: Add support for setting patchset description

Bug: 308467447
Change-Id: I7abcbc98131b826120fc9ab85d5b889f90db4b0a
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/355968
Tested-by: Sergiy Belozorov <sergiyb@chromium.org>
Reviewed-by: Mike Frysinger <vapier@google.com>
Commit-Queue: Sergiy Belozorov <sergiyb@chromium.org>
This commit is contained in:
Sergiy Belozorov 2024-03-04 19:48:52 +01:00 committed by LUCI
parent 5554572f02
commit 96edb9b573
3 changed files with 48 additions and 0 deletions

View File

@ -21,6 +21,7 @@ import random
import re import re
import shutil import shutil
import stat import stat
import string
import subprocess import subprocess
import sys import sys
import tarfile import tarfile
@ -266,6 +267,7 @@ class ReviewableBranch:
dest_branch=None, dest_branch=None,
validate_certs=True, validate_certs=True,
push_options=None, push_options=None,
patchset_description=None,
): ):
self.project.UploadForReview( self.project.UploadForReview(
branch=self.name, branch=self.name,
@ -281,6 +283,7 @@ class ReviewableBranch:
dest_branch=dest_branch, dest_branch=dest_branch,
validate_certs=validate_certs, validate_certs=validate_certs,
push_options=push_options, push_options=push_options,
patchset_description=patchset_description,
) )
def GetPublishedRefs(self): def GetPublishedRefs(self):
@ -1089,6 +1092,7 @@ class Project:
dest_branch=None, dest_branch=None,
validate_certs=True, validate_certs=True,
push_options=None, push_options=None,
patchset_description=None,
): ):
"""Uploads the named branch for code review.""" """Uploads the named branch for code review."""
if branch is None: if branch is None:
@ -1171,6 +1175,10 @@ class Project:
opts += ["wip"] opts += ["wip"]
if ready: if ready:
opts += ["ready"] opts += ["ready"]
if patchset_description:
opts += [
f"m={self._encode_patchset_description(patchset_description)}"
]
if opts: if opts:
ref_spec = ref_spec + "%" + ",".join(opts) ref_spec = ref_spec + "%" + ",".join(opts)
cmd.append(ref_spec) cmd.append(ref_spec)
@ -1183,6 +1191,30 @@ class Project:
R_PUB + branch.name, R_HEADS + branch.name, message=msg R_PUB + branch.name, R_HEADS + branch.name, message=msg
) )
@staticmethod
def _encode_patchset_description(original):
"""Applies percent-encoding for strings sent as patchset description.
The encoding used is based on but stricter than URL encoding (Section
2.1 of RFC 3986). The only non-escaped characters are alphanumerics, and
'SPACE' (U+0020) can be represented as 'LOW LINE' (U+005F) or
'PLUS SIGN' (U+002B).
For more information, see the Gerrit docs here:
https://gerrit-review.googlesource.com/Documentation/user-upload.html#patch_set_description
"""
SAFE = {ord(x) for x in string.ascii_letters + string.digits}
def _enc(b):
if b in SAFE:
return chr(b)
elif b == ord(" "):
return "_"
else:
return f"%{b:02x}"
return "".join(_enc(x) for x in original.encode("utf-8"))
def _ExtractArchive(self, tarpath, path=None): def _ExtractArchive(self, tarpath, path=None):
"""Extract the given tar on its current location """Extract the given tar on its current location

View File

@ -244,6 +244,11 @@ Gerrit Code Review: https://www.gerritcodereview.com/
default=[], default=[],
help="add a label when uploading", help="add a label when uploading",
) )
p.add_option(
"--pd",
"--patchset-description",
help="description for patchset",
)
p.add_option( p.add_option(
"--re", "--re",
"--reviewers", "--reviewers",
@ -655,6 +660,7 @@ Gerrit Code Review: https://www.gerritcodereview.com/
dest_branch=destination, dest_branch=destination,
validate_certs=opt.validate_certs, validate_certs=opt.validate_certs,
push_options=opt.push_options, push_options=opt.push_options,
patchset_description=opt.patchset_description,
) )
branch.uploaded = True branch.uploaded = True

View File

@ -107,6 +107,16 @@ class ReviewableBranchTests(unittest.TestCase):
self.assertTrue(rb.date) self.assertTrue(rb.date)
class ProjectTests(unittest.TestCase):
"""Check Project behavior."""
def test_encode_patchset_description(self):
self.assertEqual(
project.Project._encode_patchset_description("abcd00!! +"),
"abcd00%21%21_%2b",
)
class CopyLinkTestCase(unittest.TestCase): class CopyLinkTestCase(unittest.TestCase):
"""TestCase for stub repo client checkouts. """TestCase for stub repo client checkouts.