| # Copyright 2024 The Pigweed Authors |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| # use this file except in compliance with the License. You may obtain a copy of |
| # the License at |
| # |
| # https://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| # License for the specific language governing permissions and limitations under |
| # the License. |
| """Automatically copy a file from one repository to another.""" |
| |
| from __future__ import annotations |
| |
| import re |
| from typing import TYPE_CHECKING |
| |
| import attrs |
| from PB.go.chromium.org.luci.buildbucket.proto import common as common_pb2 |
| from PB.recipes.pigweed.copy_roller import InputProperties |
| from PB.recipe_modules.pigweed.checkout.options import ( |
| Options as CheckoutOptions, |
| ) |
| from PB.recipe_modules.pigweed.copy_roll.copy_entry import CopyEntry |
| from recipe_engine import post_process |
| |
| if TYPE_CHECKING: # pragma: no cover |
| from typing import Generator |
| from recipe_engine import recipe_api, recipe_test_api |
| |
| DEPS = [ |
| 'fuchsia/auto_roller', |
| 'fuchsia/gitiles', |
| 'fuchsia/roll_commit_message', |
| 'pigweed/checkout', |
| 'pigweed/copy_roll', |
| 'recipe_engine/file', |
| 'recipe_engine/properties', |
| ] |
| |
| PROPERTIES = InputProperties |
| |
| |
| def RunSteps( # pylint: disable=invalid-name |
| api: recipe_api.RecipeScriptApi, |
| props: InputProperties, |
| ): |
| # The checkout module will try to use trigger data to pull in a specific |
| # patch. Since the triggering commit is in a different repository that |
| # needs to be disabled. |
| props.checkout_options.use_trigger = False |
| checkout = api.checkout(props.checkout_options) |
| |
| if not props.copy_entries: |
| props.copy_entries.append( |
| CopyEntry( |
| remote=props.source_project_remote, |
| branch=props.source_project_branch, |
| source_path=props.source_path, |
| destination_path=props.destination_path, |
| ) |
| ) |
| |
| rolls = [] |
| for entry in props.copy_entries: |
| rolls.extend(api.copy_roll.update(checkout, entry)) |
| |
| if not rolls: |
| return # pragma: no cover |
| |
| change = api.auto_roller.attempt_roll( |
| props.auto_roller_options, |
| repo_dir=checkout.root, |
| commit_message=api.roll_commit_message.format( |
| *rolls, |
| roll_prefix='roll:', |
| send_comment=True, |
| ), |
| ) |
| |
| return api.auto_roller.raw_result(change) |
| |
| |
| def GenTests(api) -> Generator[recipe_test_api.TestData, None, None]: |
| """Create tests.""" |
| |
| def properties(**kwargs): |
| props = InputProperties(**kwargs) |
| props.source_project_remote = 'http://host/source' |
| props.source_path = 'path/to/source.txt' |
| props.destination_path = 'path/to/destination.txt' |
| props.checkout_options.CopyFrom(api.checkout.git_options()) |
| props.auto_roller_options.CopyFrom( |
| api.auto_roller.Options(remote=api.checkout.pigweed_repo) |
| ) |
| return api.properties(props) |
| |
| yield api.test( |
| 'success', |
| properties(), |
| api.auto_roller.success(), |
| api.step_data( |
| 'path/to/destination.txt.read destination path/to/destination.txt', |
| api.file.read_text('old'), |
| ), |
| api.gitiles.fetch( |
| 'path/to/destination.txt.read source path/to/source.txt', |
| 'new', |
| ), |
| ) |