Add new API to update files by gerrit module

The new API here could support users to update files to remote
repo without loading the source code to the bot.

BUG=1207955
TEST=train

Change-Id: I2970171bf5b58db0db41bab5a433fc6721b7b2aa
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3042407
Commit-Queue: Xinan Lin <linxinan@chromium.org>
Reviewed-by: Michael Moss <mmoss@chromium.org>
Reviewed-by: Andrii Shyshkalov <tandrii@google.com>
This commit is contained in:
Xinan Lin
2021-07-28 01:15:22 +00:00
committed by LUCI CQ
parent 1bd4ffa295
commit 493faf1621
6 changed files with 266 additions and 19 deletions

View File

@@ -249,3 +249,85 @@ class GerritApi(recipe_api.RecipeApi):
args,
step_test_data=step_test_data,
).json.output
def update_files(self,
host,
project,
branch,
new_contents_by_file_path,
commit_msg,
submit=False):
"""Update a set of files by creating and submitting a Gerrit CL.
Args:
* host: URL of Gerrit host to name.
* project: Gerrit project name, e.g. chromium/src.
* branch: The branch to land the change, e.g. main
* new_contents_by_file_path: Dict of the new contents with file path as
the key.
* commit_msg: Description to add to the CL.
* submit: Should land this CL instantly.
Returns:
Integer change number.
"""
assert len(new_contents_by_file_path
) > 0, 'The dict of file paths should not be empty.'
step_result = self('create change at (%s %s)' % (project, branch), [
'createchange',
'--host',
host,
'--project',
project,
'--branch',
branch,
'--subject',
commit_msg,
'--json_file',
self.m.json.output(),
])
change = int(step_result.json.output.get('_number'))
step_result.presentation.links['change %d' %
change] = '%s/#/q/%d' % (host, change)
with self.m.step.nest('update contents in CL %d' % change):
for path, content in new_contents_by_file_path.items():
_file = self.m.path.mkstemp()
self.m.file.write_raw('store the new content for %s' % path, _file,
content)
self('edit file %s' % path, [
'changeedit',
'--host',
host,
'--change',
change,
'--path',
path,
'--file',
_file,
])
self('publish edit', [
'publishchangeedit',
'--host',
host,
'--change',
change,
])
if submit:
self('set Bot-Commit+1 for change %d' % change, [
'setbotcommit',
'--host',
host,
'--change',
change,
])
self('submit change %d' % change, [
'submitchange',
'--host',
host,
'--change',
change,
])
return change