pipeline: Add pipeline-related input properties
These properties can be accessed from presubmit steps by running a
command like the following.
bb get -json -p {ctx.luci.buildbucket_id}
Bug: b/245788264
Change-Id: I3893d3b5b39df23eccb577eeab781a9a3796466f
Reviewed-on: https://pigweed-review.googlesource.com/c/infra/recipes/+/122731
Reviewed-by: Ted Pudlik <tpudlik@google.com>
Commit-Queue: Rob Mohr <mohrr@google.com>
diff --git a/recipe_modules/pipeline/__init__.py b/recipe_modules/pipeline/__init__.py
new file mode 100644
index 0000000..e808c47
--- /dev/null
+++ b/recipe_modules/pipeline/__init__.py
@@ -0,0 +1,23 @@
+# 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.
+
+# pylint: disable=missing-docstring
+
+from PB.recipe_modules.pigweed.pipeline import properties
+
+DEPS = [
+ 'recipe_engine/properties',
+]
+
+PROPERTIES = properties.InputProperties
diff --git a/recipe_modules/pipeline/api.py b/recipe_modules/pipeline/api.py
new file mode 100644
index 0000000..d23f3a7
--- /dev/null
+++ b/recipe_modules/pipeline/api.py
@@ -0,0 +1,38 @@
+# 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.
+"""Calls to build code."""
+
+from recipe_engine import recipe_api
+
+
+class PipelineApi(recipe_api.RecipeApi):
+ """Calls to build code."""
+
+ def __init__(self, props, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self._enabled = props.inside_a_pipeline
+ self._round = props.round
+ self._prev_iteration_builds = props.builds_from_previous_iteration
+
+ @property
+ def in_pipeline(self):
+ return self._enabled
+
+ @property
+ def round(self):
+ return self._round if self._enabled else None
+
+ @property
+ def builds_from_previous_iteration(self):
+ return tuple(self._prev_iteration_builds) if self._enabled else None
diff --git a/recipe_modules/pipeline/properties.proto b/recipe_modules/pipeline/properties.proto
new file mode 100644
index 0000000..a0cb0c6
--- /dev/null
+++ b/recipe_modules/pipeline/properties.proto
@@ -0,0 +1,27 @@
+// 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.
+syntax = "proto3";
+
+package recipe_modules.pigweed.pipeline;
+
+message InputProperties {
+ // Whether this build has been launched from a pipeline.
+ bool inside_a_pipeline = 1;
+
+ // Number of the round, zero-indexed.
+ int32 round = 2;
+
+ // List of all builds that were run in the previous iteration.
+ repeated int64 builds_from_previous_iteration = 3;
+}
diff --git a/recipe_modules/pipeline/test_api.py b/recipe_modules/pipeline/test_api.py
new file mode 100644
index 0000000..e26432f
--- /dev/null
+++ b/recipe_modules/pipeline/test_api.py
@@ -0,0 +1,31 @@
+# Copyright 2020 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.
+"""Test API for pipeline."""
+
+from recipe_engine import recipe_test_api
+
+
+class EnvironmentTestApi(recipe_test_api.RecipeTestApi):
+ """Test API for pipeline."""
+
+ def props(self, round, build_ids=()):
+ return self.m.properties(
+ **{
+ '$pigweed/pipeline': {
+ 'inside_a_pipeline': True,
+ 'round': round,
+ 'builds_from_previous_iteration': list(build_ids),
+ },
+ }
+ )
diff --git a/recipe_modules/pipeline/tests/full.expected/empty.json b/recipe_modules/pipeline/tests/full.expected/empty.json
new file mode 100644
index 0000000..7d97243
--- /dev/null
+++ b/recipe_modules/pipeline/tests/full.expected/empty.json
@@ -0,0 +1,9 @@
+[
+ {
+ "cmd": [],
+ "name": "not in pipeline"
+ },
+ {
+ "name": "$result"
+ }
+]
\ No newline at end of file
diff --git a/recipe_modules/pipeline/tests/full.expected/nopipeline.json b/recipe_modules/pipeline/tests/full.expected/nopipeline.json
new file mode 100644
index 0000000..7d97243
--- /dev/null
+++ b/recipe_modules/pipeline/tests/full.expected/nopipeline.json
@@ -0,0 +1,9 @@
+[
+ {
+ "cmd": [],
+ "name": "not in pipeline"
+ },
+ {
+ "name": "$result"
+ }
+]
\ No newline at end of file
diff --git a/recipe_modules/pipeline/tests/full.expected/round_0.json b/recipe_modules/pipeline/tests/full.expected/round_0.json
new file mode 100644
index 0000000..cc68ad8
--- /dev/null
+++ b/recipe_modules/pipeline/tests/full.expected/round_0.json
@@ -0,0 +1,17 @@
+[
+ {
+ "cmd": [],
+ "name": "in pipeline"
+ },
+ {
+ "cmd": [],
+ "name": "round 0"
+ },
+ {
+ "cmd": [],
+ "name": "0 previous builds"
+ },
+ {
+ "name": "$result"
+ }
+]
\ No newline at end of file
diff --git a/recipe_modules/pipeline/tests/full.expected/round_1.json b/recipe_modules/pipeline/tests/full.expected/round_1.json
new file mode 100644
index 0000000..b0e3d4e
--- /dev/null
+++ b/recipe_modules/pipeline/tests/full.expected/round_1.json
@@ -0,0 +1,21 @@
+[
+ {
+ "cmd": [],
+ "name": "in pipeline"
+ },
+ {
+ "cmd": [],
+ "name": "round 1"
+ },
+ {
+ "cmd": [],
+ "name": "1 previous builds"
+ },
+ {
+ "cmd": [],
+ "name": "previous build 123"
+ },
+ {
+ "name": "$result"
+ }
+]
\ No newline at end of file
diff --git a/recipe_modules/pipeline/tests/full.expected/round_4.json b/recipe_modules/pipeline/tests/full.expected/round_4.json
new file mode 100644
index 0000000..90cad80
--- /dev/null
+++ b/recipe_modules/pipeline/tests/full.expected/round_4.json
@@ -0,0 +1,29 @@
+[
+ {
+ "cmd": [],
+ "name": "in pipeline"
+ },
+ {
+ "cmd": [],
+ "name": "round 4"
+ },
+ {
+ "cmd": [],
+ "name": "3 previous builds"
+ },
+ {
+ "cmd": [],
+ "name": "previous build 123"
+ },
+ {
+ "cmd": [],
+ "name": "previous build 456"
+ },
+ {
+ "cmd": [],
+ "name": "previous build 789"
+ },
+ {
+ "name": "$result"
+ }
+]
\ No newline at end of file
diff --git a/recipe_modules/pipeline/tests/full.py b/recipe_modules/pipeline/tests/full.py
new file mode 100644
index 0000000..1f8431c
--- /dev/null
+++ b/recipe_modules/pipeline/tests/full.py
@@ -0,0 +1,74 @@
+# 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.
+"""Full test of pipeline module."""
+
+from recipe_engine import post_process
+
+DEPS = [
+ 'fuchsia/status_check',
+ 'pigweed/pipeline',
+ 'recipe_engine/step',
+]
+
+
+def RunSteps(api):
+ if not api.pipeline.in_pipeline:
+ api.step('not in pipeline', None)
+ return
+
+ api.step('in pipeline', None)
+
+ api.step(f'round {api.pipeline.round}', None)
+
+ prev_builds = api.pipeline.builds_from_previous_iteration
+ api.step(f'{len(prev_builds)} previous builds', None)
+ for build in prev_builds:
+ api.step(f'previous build {build}', None)
+
+
+def GenTests(api): # pylint: disable=invalid-name
+ def ran(x):
+ return api.post_process(post_process.MustRun, x)
+
+ yield (api.status_check.test('nopipeline') + ran('not in pipeline'))
+
+ yield (
+ api.status_check.test('round_0')
+ + api.pipeline.props(0, [])
+ + ran('in pipeline')
+ + ran('round 0')
+ + ran('0 previous builds')
+ )
+
+ yield (
+ api.status_check.test('round_1')
+ + api.pipeline.props(1, [123])
+ + ran('in pipeline')
+ + ran('round 1')
+ + ran('1 previous builds')
+ + ran('previous build 123')
+ )
+
+ yield (
+ api.status_check.test('round_4')
+ + api.pipeline.props(4, [123, 456, 789])
+ + ran('in pipeline')
+ + ran('round 4')
+ + ran('3 previous builds')
+ + ran('previous build 123')
+ + ran('previous build 456')
+ + ran('previous build 789')
+ )
+
+ yield api.status_check.test('empty')