| # Copyright 2022 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| """Recipe for auto-submitting eligible changes on release branches. See |
| go/change-control-in-gerrit.""" |
| |
| from google.protobuf import json_format |
| |
| from PB.go.chromium.org.luci.buildbucket.proto import common as common_pb2 |
| from PB.recipe_engine.result import RawResult |
| from PB.recipes.fuchsia.release.auto_submit import InputProperties |
| from PB.recipes.fuchsia.release.cherry_pick import ( |
| InputProperties as CherryPickProperties, |
| ) |
| |
| from RECIPE_MODULES.fuchsia.utils import pluralize |
| |
| DEPS = [ |
| "fuchsia/gerrit_auto_submit", |
| "fuchsia/utils", |
| "recipe_engine/buildbucket", |
| "recipe_engine/properties", |
| "recipe_engine/step", |
| ] |
| |
| PROPERTIES = InputProperties |
| |
| |
| def RunSteps(api, props): |
| changes = [] |
| for gerrit_host in props.gerrit_hosts: |
| changes += submit_eligible( |
| api, |
| gerrit_host, |
| props.dry_run, |
| props.submission_builder, |
| ) |
| return RawResult( |
| summary_markdown=f"cherry-picked {pluralize('change', changes)}", |
| status=common_pb2.SUCCESS, |
| ) |
| |
| |
| def submit_eligible(api, gerrit_host, dry_run, submission_builder): |
| with api.step.nest("get eligible"): |
| changes = api.gerrit_auto_submit.get_eligible_changes( |
| gerrit_host, |
| "Release-Approval", |
| max_attempts=1, |
| ) |
| # For now, handle submission sequentially to avoid races. This could be |
| # parallelized across branches in the future. |
| for change in changes: |
| with api.step.nest(f"submit change {change['_number']}"): |
| cherry_pick_properties = CherryPickProperties( |
| changes=[ |
| CherryPickProperties.ChangeInfo( |
| revision=change["current_revision"], |
| change_id=str(change["_number"]), |
| change_url=f"https://{gerrit_host}/c/{change['project']}/+/{change['_number']}", |
| ), |
| ], |
| target_project=change["project"], |
| target_remote=f"https://{gerrit_host.replace('-review', '')}/{change['project']}", |
| target_branch=change["branch"], |
| # TODO(fxbug.dev/105915): Rename cherry_pick.proto dryrun |
| # property to dry_run for consistency. |
| dryrun=dry_run, |
| ) |
| request = api.buildbucket.schedule_request( |
| submission_builder, |
| properties=json_format.MessageToDict( |
| cherry_pick_properties, preserving_proto_field_name=True |
| ), |
| ) |
| api.buildbucket.run([request], raise_if_unsuccessful=True) |
| return changes |
| |
| |
| def GenTests(api): |
| yield ( |
| api.test("basic") |
| + api.gerrit_auto_submit.changes_query_test_data() |
| + api.gerrit_auto_submit.changes_submitted_together_test_data() |
| + api.properties( |
| gerrit_hosts=["fuchsia-review.googlesource.com"], |
| submission_builder="auto-submit-cherry-pick", |
| ) |
| ) |