Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
upload: Add support for setting patchset description
Browse files Browse the repository at this point in the history
Bug: 308467447
Change-Id: I7abcbc98131b826120fc9ab85d5b889f90db4b0a
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/355968
Tested-by: Sergiy Belozorov <[email protected]>
Reviewed-by: Mike Frysinger <[email protected]>
Commit-Queue: Sergiy Belozorov <[email protected]>
  • Loading branch information
rryk authored and LUCI committed Mar 4, 2024
1 parent 5554572 commit 96edb9b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
32 changes: 32 additions & 0 deletions project.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import re
import shutil
import stat
import string
import subprocess
import sys
import tarfile
Expand Down Expand Up @@ -266,6 +267,7 @@ def UploadForReview(
dest_branch=None,
validate_certs=True,
push_options=None,
patchset_description=None,
):
self.project.UploadForReview(
branch=self.name,
Expand All @@ -281,6 +283,7 @@ def UploadForReview(
dest_branch=dest_branch,
validate_certs=validate_certs,
push_options=push_options,
patchset_description=patchset_description,
)

def GetPublishedRefs(self):
Expand Down Expand Up @@ -1089,6 +1092,7 @@ def UploadForReview(
dest_branch=None,
validate_certs=True,
push_options=None,
patchset_description=None,
):
"""Uploads the named branch for code review."""
if branch is None:
Expand Down Expand Up @@ -1171,6 +1175,10 @@ def UploadForReview(
opts += ["wip"]
if ready:
opts += ["ready"]
if patchset_description:
opts += [
f"m={self._encode_patchset_description(patchset_description)}"
]
if opts:
ref_spec = ref_spec + "%" + ",".join(opts)
cmd.append(ref_spec)
Expand All @@ -1183,6 +1191,30 @@ def UploadForReview(
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):
"""Extract the given tar on its current location
Expand Down
6 changes: 6 additions & 0 deletions subcmds/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ def _Options(self, p):
default=[],
help="add a label when uploading",
)
p.add_option(
"--pd",
"--patchset-description",
help="description for patchset",
)
p.add_option(
"--re",
"--reviewers",
Expand Down Expand Up @@ -655,6 +660,7 @@ def _ExpandCommaList(value):
dest_branch=destination,
validate_certs=opt.validate_certs,
push_options=opt.push_options,
patchset_description=opt.patchset_description,
)

branch.uploaded = True
Expand Down
10 changes: 10 additions & 0 deletions tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ def test_smoke(self):
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):
"""TestCase for stub repo client checkouts.
Expand Down

0 comments on commit 96edb9b

Please sign in to comment.