| # Copyright 2021 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 submodules of a git repository.""" |
| |
| from __future__ import annotations |
| |
| from typing import TYPE_CHECKING |
| |
| import attrs |
| from PB.recipes.pigweed.submodule_roller import InputProperties |
| from recipe_engine import post_process |
| |
| if TYPE_CHECKING: # pragma: no cover |
| from typing import Generator |
| from recipe_engine import recipe_test_api |
| |
| DEPS = [ |
| 'fuchsia/auto_roller', |
| 'pigweed/checkout', |
| 'pigweed/roll_util', |
| 'pigweed/submodule_roll', |
| 'recipe_engine/properties', |
| 'recipe_engine/step', |
| ] |
| |
| PROPERTIES = InputProperties |
| |
| |
| def RunSteps( # pylint: disable=invalid-name |
| api: recipe_api.RecipeScriptApi, |
| props: InputProperties, |
| ): |
| cc_authors_on_rolls = props.cc_authors_on_rolls |
| cc_reviewers_on_rolls = props.cc_reviewers_on_rolls |
| cc_domains = props.cc_domains |
| always_cc = props.always_cc |
| |
| # 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) |
| |
| rolls = [] |
| for submodule in props.submodules: |
| rolls.extend(api.submodule_roll.update(checkout, submodule)) |
| |
| if not rolls: |
| with api.step.nest('nothing to roll, exiting'): |
| return |
| |
| authors = api.roll_util.authors(*rolls) |
| num_commits = sum(len(x.commits) for x in rolls) |
| |
| max_commits_for_ccing = props.max_commits_for_ccing or 10 |
| if num_commits <= max_commits_for_ccing: |
| cc = set() |
| if cc_authors_on_rolls: |
| cc.update(authors) |
| if cc_reviewers_on_rolls: |
| cc.update(api.roll_util.reviewers(*rolls)) |
| |
| def include_cc(email): |
| return api.roll_util.include_cc( |
| email, cc_domains, checkout.gerrit_host() |
| ) |
| |
| # include_cc() writes steps, so we want things sorted before calling it. |
| cc = sorted(set(cc)) |
| cc_emails = [x.email for x in cc if include_cc(x)] |
| |
| if always_cc: |
| props.auto_roller_options.cc_emails.extend(cc_emails) |
| else: |
| props.auto_roller_options.cc_on_failure_emails.extend(cc_emails) |
| |
| author_override = None |
| with api.step.nest('authors') as pres: |
| pres.step_summary_text = repr(authors) |
| if len(authors) == 1 and props.forge_author: |
| author_override = attrs.asdict( |
| api.roll_util.fake_author(next(iter(authors))) |
| ) |
| |
| # merge auto_roller_options and override_auto_roller_options. |
| complete_auto_roller_options = api.roll_util.merge_auto_roller_overrides( |
| props.auto_roller_options, props.override_auto_roller_options |
| ) |
| |
| change = api.auto_roller.attempt_roll( |
| complete_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 properties(submodules, **kwargs): |
| props = InputProperties(**kwargs) |
| props.checkout_options.CopyFrom(api.checkout.git_options()) |
| props.submodules.extend(submodules) |
| props.auto_roller_options.dry_run = True |
| props.auto_roller_options.remote = api.checkout.pigweed_repo |
| return api.properties(props) |
| |
| yield api.test( |
| 'success', |
| properties( |
| api.submodule_roll.submodules('a1', 'b2'), |
| cc_authors_on_rolls=True, |
| always_cc=True, |
| forge_author=True, |
| ), |
| api.submodule_roll.commit_data('a1', prefix=''), |
| api.submodule_roll.commit_data('b2', prefix=''), |
| api.submodule_roll.gitmodules(a1='sso://foo/a1', b2='sso://foo/b2'), |
| api.roll_util.forward_roll('a1'), |
| api.roll_util.forward_roll('b2'), |
| api.auto_roller.dry_run_success(), |
| ) |
| |
| yield api.test( |
| 'partial_noop', |
| properties( |
| api.submodule_roll.submodules('a1', 'b2'), |
| cc_reviewers_on_rolls=True, |
| ), |
| api.submodule_roll.commit_data('a1', prefix=''), |
| api.submodule_roll.gitmodules(a1='sso://foo/a1', b2='sso://foo/b2'), |
| api.roll_util.forward_roll('a1'), |
| api.roll_util.noop_roll('b2'), |
| api.auto_roller.dry_run_success(), |
| ) |
| |
| yield api.test( |
| 'noop', |
| properties(api.submodule_roll.submodules('a1', {'path': 'b2'})), |
| api.submodule_roll.gitmodules(a1='a1', b2='b2', b2_branch='branch'), |
| api.roll_util.noop_roll('a1'), |
| api.roll_util.noop_roll('b2'), |
| ) |