| # Copyright 2022 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. |
| """Roll a pin of a git repository in a Bazel WORKSPACE. |
| |
| Find a git repository pin in a WORKSPACE file like the following: |
| |
| git_repository( |
| name = "pigweed", |
| commit = "1111111111111111111111111111111111111111", |
| remote = "https://pigweed.googlesource.com/pigweed/pigweed.git", |
| ) |
| |
| Specifically, find the 'remote' line that matches the repository to be rolled. |
| Then check the two lines immediately before and the two lines immediately after |
| the remote line for a 'commit' line, checking the adjacent lines first. |
| Additionally, grab the name from these nearby lines (if not provided in a |
| property). |
| |
| Then, update the 'commit' line to point to the new commit, and push a change to |
| gerrit. |
| """ |
| |
| from __future__ import annotations |
| |
| import itertools |
| import re |
| from typing import TYPE_CHECKING |
| |
| import attrs |
| from PB.go.chromium.org.luci.buildbucket.proto import common as common_pb2 |
| from PB.recipe_modules.pigweed.bazel_roll.git_repository import GitRepository |
| from PB.recipes.pigweed.bazel_roller import InputProperties |
| from recipe_engine import post_process |
| |
| if TYPE_CHECKING: # pragma: no cover |
| from typing import Generator, Sequence, TypeVar |
| from recipe_engine import recipe_api, recipe_test_api |
| from RECIPE_MODULES.pigweed.checkout import api as checkout_api |
| |
| DEPS = [ |
| 'fuchsia/auto_roller', |
| 'pigweed/bazel_roll', |
| 'pigweed/checkout', |
| 'pigweed/roll_util', |
| '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.CheckoutContext = api.checkout( |
| props.checkout_options |
| ) |
| |
| if not props.git_repositories: |
| props.git_repositories.append( |
| GitRepository( |
| workspace_path=props.workspace_path, |
| name=props.project_name, |
| remote=props.project_remote, |
| branch=props.project_branch, |
| ) |
| ) |
| |
| assert len(props.git_repositories) == 1 |
| |
| rolls = api.bazel_roll.update_git_repository( |
| checkout=checkout, |
| git_repository=props.git_repositories[0], |
| ) |
| if not rolls: |
| return # pragma: no cover |
| |
| authors = api.roll_util.authors(*rolls) |
| |
| author_override: Optional[api.roll_util.Account] = None |
| if len(authors) == 1 and props.forge_author: |
| author_override = attrs.asdict( |
| api.roll_util.fake_author(next(iter(authors))) |
| ) |
| |
| change: api.auto_roller.GerritChange = api.auto_roller.attempt_roll( |
| props.auto_roller_options, |
| repo_dir=checkout.root, |
| commit_message=api.roll_util.message(*rolls), |
| author_override=author_override, |
| ) |
| |
| return api.auto_roller.raw_result(change) |
| |
| |
| def GenTests(api) -> Generator[recipe_test_api.TestData, None, None]: |
| """Create tests.""" |
| |
| def _url(x: str = 'pigweed/pigweed'): |
| assert ':' not in x |
| return f'https://pigweed.googlesource.com/{x}' |
| |
| def trigger(url, **kwargs): |
| return api.checkout.ci_test_data(git_repo=_url(url), **kwargs) |
| |
| def properties(**kwargs): |
| props = InputProperties(**kwargs) |
| props.checkout_options.CopyFrom(api.checkout.git_options()) |
| props.forge_author = True |
| props.auto_roller_options.CopyFrom( |
| api.auto_roller.Options(remote=api.checkout.pigweed_repo) |
| ) |
| return api.properties(props) |
| |
| def commit_data(name, **kwargs): |
| return api.roll_util.commit_data( |
| name, |
| api.roll_util.commit('a' * 40, 'foo\nbar\n\nChange-Id: I1111'), |
| **kwargs, |
| ) |
| |
| yield api.test( |
| 'success', |
| properties(project_remote=_url('pigweed/pigweed')), |
| api.roll_util.properties(commit_divider='--divider--'), |
| trigger('pigweed/pigweed'), |
| api.roll_util.forward_roll(), |
| commit_data('pigweed', prefix=''), |
| api.auto_roller.success(), |
| ) |