| # Copyright 2026 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. |
| |
| from __future__ import annotations |
| |
| from typing import Any |
| |
| from recipe_engine import recipe_api |
| |
| from PB.recipe_engine import result as result_pb |
| |
| from RECIPE_MODULES.pigweed.checkout import api as checkout_api |
| |
| |
| class InitializationApi(recipe_api.RecipeApi): |
| """Common initialization steps for Pigweed recipes.""" |
| |
| def __call__( |
| self, |
| checkout_options: Any, |
| ) -> tuple[result_pb.RawResult | None, checkout_api.CheckoutContext | None]: |
| """Runs common initialization steps. |
| |
| Args: |
| checkout_options: Options for the checkout step. |
| |
| Returns: |
| A tuple of (RawResult, None) if an early exit is triggered, |
| or (None, CheckoutContext) on success. |
| """ |
| self.m.abandon_old_changes() |
| |
| if res := self.m.ci_status.exit_early_in_recipe_testing_if_failing(): |
| return res, None |
| |
| if res := self.m.split_triggers(): |
| return res, None |
| |
| checkout = self.m.checkout(checkout_options) |
| |
| if res := self.m.roll_tryjob_reuse.exit_early_if_applicable( |
| checkout.changes |
| ): |
| return res, None |
| |
| if res := self.m.limited_cq.exit_early_if_not_approved( |
| checkout.changes |
| ): |
| return res, None |
| |
| return None, checkout |