blob: 852bc5a118b8bbd5905da520337a7e233dbc3b35 [file] [log] [blame]
# 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.
"""Update CIPD ensure files in a Git repository."""
from PB.recipes.fuchsia.cipd_ensure_file_roller import InputProperties
PYTHON_VERSION_COMPATIBILITY = "PY3"
DEPS = [
"fuchsia/auto_roller",
"fuchsia/buildbucket_util",
"fuchsia/cipd_resolver",
"fuchsia/cipd_util",
"fuchsia/gerrit",
"fuchsia/git_checkout",
"recipe_engine/cipd",
"recipe_engine/json",
"recipe_engine/path",
"recipe_engine/properties",
]
PROPERTIES = InputProperties
def RunSteps(api, props):
props.tag = props.tag or "version"
props.ref = props.ref or "latest"
# Note that we only support Git checkouts. Jiri checkouts are not expected
# to have CIPD ensure files.
checkout_dir = api.path.mkdtemp("checkout")
api.git_checkout(props.remote, path=checkout_dir)
all_packages = []
for ensure_file, packages in props.packages_by_ensure_file.items():
platforms = api.cipd_util.get_platforms(
"get platforms for %s" % ensure_file,
ensure_file=checkout_dir.join(ensure_file),
)
all_packages += api.cipd_util.expand_packages_by_platforms(
"expand %s packages by platforms" % ensure_file,
packages=packages,
platforms=platforms,
)
all_packages = sorted(all_packages)
candidate_versions = api.cipd_resolver.resolve_common_tags(
ref=props.ref,
tag_name=props.tag,
packages=all_packages,
)
version = candidate_versions[0]
for ensure_file, packages in props.packages_by_ensure_file.items():
api.cipd_util.update_packages(
"update %s" % ensure_file,
ensure_file=checkout_dir.join(ensure_file),
packages=packages,
tag_name=props.tag,
version=version,
)
api.cipd.ensure_file_resolve(checkout_dir.join(ensure_file))
change = api.auto_roller.attempt_roll(
api.gerrit.host_from_remote_url(props.remote),
gerrit_project=api.gerrit.project_from_remote_url(props.remote),
repo_dir=checkout_dir,
# TODO(atyfto): Improve the commit message.
commit_message="[roll] Update cipd ensure files to %s" % props.ref,
cl_notify_option=props.cl_notify_option,
create_unique_id=props.create_unique_change_id,
dry_run=props.dry_run,
)
return api.auto_roller.raw_result(change)
def GenTests(api):
ensure_file = "foo.ensure"
packages = ["fuchsia/foo/${platform}", "fuchsia/bar/${platform}"]
default_props = api.properties(
packages_by_ensure_file={ensure_file: packages},
remote="https://fuchsia.googlesource.com/integration",
)
def resolved_tags(tags):
return api.step_data("resolve common tags", api.json.output_stream(tags))
yield (
api.buildbucket_util.test("roll", builder="cipd-roller")
+ resolved_tags(["version:newest", "version:newer"])
+ api.cipd_util.get_platforms(
"get platforms for %s" % ensure_file,
["linux-amd64", "linux-arm64", "mac-amd64"],
)
+ api.cipd_util.update_packages("update %s" % ensure_file, packages)
+ api.auto_roller.success()
+ default_props
)