From c1c45f8a853111bcdb24e2de7a6e8781bf2e9c06 Mon Sep 17 00:00:00 2001 From: Leszek Swirski Date: Thu, 9 Jun 2022 16:21:07 +0000 Subject: [PATCH] [gerrit] Add more gerrit API wrappers Add helpers to gerrit_util for: * Setting the commit message on a ChangeEdit, * Creating a cherry-pick, * Getting the contents of a file in a change. Bug: v8:12849 Change-Id: I1a5de3054a366d480def32942fd42be179ffb749 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3683383 Auto-Submit: Leszek Swirski Reviewed-by: Aravind Vasudevan Commit-Queue: Aravind Vasudevan --- gerrit_util.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/gerrit_util.py b/gerrit_util.py index d4ce8953c..fea3862b3 100644 --- a/gerrit_util.py +++ b/gerrit_util.py @@ -782,6 +782,14 @@ def ChangeEdit(host, change, path, data): return ReadHttpJsonResponse(conn, accept_statuses=(204, 409)) +def SetChangeEditMessage(host, change, message): + """Sets the commit message of a change edit.""" + path = 'changes/%s/edit:message' % change + body = {'message': message} + conn = CreateHttpConn(host, path, reqtype='PUT', body=body) + return ReadHttpJsonResponse(conn, accept_statuses=(204, 409)) + + def HasPendingChangeEdit(host, change): conn = CreateHttpConn(host, 'changes/%s/edit' % change) try: @@ -801,6 +809,28 @@ def DeletePendingChangeEdit(host, change): ReadHttpResponse(conn, accept_statuses=[204, 404]) +def CherryPick(host, change, destination, revision='current'): + """Create a cherry-pick commit from the given change, onto the given + destination. + """ + path = 'changes/%s/revisions/%s/cherrypick' % (change, revision) + body = {'destination': destination} + conn = CreateHttpConn(host, path, reqtype='POST', body=body) + return ReadHttpJsonResponse(conn) + + +def GetFileContents(host, change, path): + """Get the contents of a file with the given path in the given revision. + + Returns: + A bytes object with the file's contents. + """ + path = 'changes/%s/revisions/current/files/%s/content' % ( + change, urllib.parse.quote(path, '')) + conn = CreateHttpConn(host, path, reqtype='GET') + return base64.b64decode(ReadHttpResponse(conn).read()) + + def SetCommitMessage(host, change, description, notify='ALL'): """Updates a commit message.""" assert notify in ('ALL', 'NONE')