From e80316cbcdc8d99691fb760caa01095e4d9745c4 Mon Sep 17 00:00:00 2001 From: Tobias Persson Date: Tue, 21 Jan 2025 14:13:35 +0100 Subject: [PATCH 1/2] Pass parent activity to ETOS API if added Will take an input parameter or read ActT from an environment variable. --- cli/src/etos_client/etos/v0/schema/request.py | 2 ++ cli/src/etos_client/etos/v0/subcommands/start.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/cli/src/etos_client/etos/v0/schema/request.py b/cli/src/etos_client/etos/v0/schema/request.py index 1016e458..2fe77902 100644 --- a/cli/src/etos_client/etos/v0/schema/request.py +++ b/cli/src/etos_client/etos/v0/schema/request.py @@ -26,6 +26,7 @@ class RequestSchema(BaseModel): artifact_id: Optional[str] artifact_identity: Optional[str] + parent_activity: Optional[str] test_suite_url: str dataset: Optional[Union[dict, list]] = {} execution_space_provider: Optional[str] = "default" @@ -38,6 +39,7 @@ def from_args(cls, args: dict) -> "RequestSchema": return RequestSchema( artifact_id=args["--identity"], artifact_identity=args["--identity"], + parent_activity=args["--parent-activity"] or None, test_suite_url=args["--test-suite"], dataset=args["--dataset"], execution_space_provider=args["--execution-space-provider"] or "default", diff --git a/cli/src/etos_client/etos/v0/subcommands/start.py b/cli/src/etos_client/etos/v0/subcommands/start.py index 4f8d0cb2..fc6f61fa 100644 --- a/cli/src/etos_client/etos/v0/subcommands/start.py +++ b/cli/src/etos_client/etos/v0/subcommands/start.py @@ -18,6 +18,8 @@ import sys import os import logging +import json +from json import JSONDecodeError import warnings from etos_client.types.result import Conclusion, Verdict @@ -38,6 +40,7 @@ class Start(SubCommand): -h, --help Show this help message and exit -i IDENTITY, --identity IDENTITY Artifact created identity purl or ID to execute test suite on. -s TEST_SUITE, --test-suite TEST_SUITE Test suite execute. Either URL or name. + -p PARENT_ACTIVITY, --parent-activity PARENT_ACTIVITY Activity for the TERCC to link to as a parent activity. --no-tty This parameter is no longer in use. -w WORKSPACE, --workspace WORKSPACE Which workspace to do all the work in. -a ARTIFACT_DIR, --artifact-dir ARTIFACT_DIR Where test artifacts should be stored. Relative to workspace. @@ -56,6 +59,17 @@ class Start(SubCommand): name="start", description="Start an ETOSv0 testrun.", version="v0" ) + def activity_triggered(self, argv: list[str]): + """Get an activity triggered event ID from environment or arguments.""" + if ("-p" not in argv and "--parent-activity" not in argv) and os.getenv( + "EIFFEL_ACTIVITY_TRIGGERED" + ) is not None: + try: + actt = json.loads(os.getenv("EIFFEL_ACTIVITY_TRIGGERED", "")) + argv.extend(["--parent-activity", actt["meta"]["id"]]) + except (JSONDecodeError, IndexError): + self.logger.warning("Could not load EIFFEL_ACTIVITY_TRIGGERED from environment due it not being formatted JSON") + def parse_args(self, argv: list[str]) -> dict: """Parse arguments for etosctl testrun start.""" if ("-i" not in argv and "--identity" not in argv) and os.getenv("IDENTITY") is not None: @@ -64,6 +78,7 @@ def parse_args(self, argv: list[str]) -> dict: "TEST_SUITE" ) is not None: argv.extend(["--test-suite", os.getenv("TEST_SUITE", "")]) + self.activity_triggered(argv) return super().parse_args(argv) def run(self, args: dict) -> None: From df434eb5cc6963cf62d6e9c114f4d6d511313101 Mon Sep 17 00:00:00 2001 From: Tobias Persson Date: Wed, 22 Jan 2025 09:08:22 +0100 Subject: [PATCH 2/2] Fix a few review comments --- cli/src/etos_client/etos/v0/subcommands/start.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cli/src/etos_client/etos/v0/subcommands/start.py b/cli/src/etos_client/etos/v0/subcommands/start.py index fc6f61fa..1a594585 100644 --- a/cli/src/etos_client/etos/v0/subcommands/start.py +++ b/cli/src/etos_client/etos/v0/subcommands/start.py @@ -20,6 +20,7 @@ import logging import json from json import JSONDecodeError +from typing import Optional import warnings from etos_client.types.result import Conclusion, Verdict @@ -40,7 +41,7 @@ class Start(SubCommand): -h, --help Show this help message and exit -i IDENTITY, --identity IDENTITY Artifact created identity purl or ID to execute test suite on. -s TEST_SUITE, --test-suite TEST_SUITE Test suite execute. Either URL or name. - -p PARENT_ACTIVITY, --parent-activity PARENT_ACTIVITY Activity for the TERCC to link to as a parent activity. + -p PARENT_ACTIVITY, --parent-activity PARENT_ACTIVITY Activity for the TERCC to link to as the cause of the test execution. --no-tty This parameter is no longer in use. -w WORKSPACE, --workspace WORKSPACE Which workspace to do all the work in. -a ARTIFACT_DIR, --artifact-dir ARTIFACT_DIR Where test artifacts should be stored. Relative to workspace. @@ -59,16 +60,17 @@ class Start(SubCommand): name="start", description="Start an ETOSv0 testrun.", version="v0" ) - def activity_triggered(self, argv: list[str]): + def activity_triggered(self, argv: list[str]) -> Optional[str]: """Get an activity triggered event ID from environment or arguments.""" if ("-p" not in argv and "--parent-activity" not in argv) and os.getenv( "EIFFEL_ACTIVITY_TRIGGERED" ) is not None: try: actt = json.loads(os.getenv("EIFFEL_ACTIVITY_TRIGGERED", "")) - argv.extend(["--parent-activity", actt["meta"]["id"]]) + return actt["meta"]["id"] except (JSONDecodeError, IndexError): self.logger.warning("Could not load EIFFEL_ACTIVITY_TRIGGERED from environment due it not being formatted JSON") + return None def parse_args(self, argv: list[str]) -> dict: """Parse arguments for etosctl testrun start.""" @@ -78,7 +80,9 @@ def parse_args(self, argv: list[str]) -> dict: "TEST_SUITE" ) is not None: argv.extend(["--test-suite", os.getenv("TEST_SUITE", "")]) - self.activity_triggered(argv) + parent_activity_id = self.activity_triggered(argv) + if parent_activity_id is not None: + argv.extend(["--parent-activity", parent_activity_id]) return super().parse_args(argv) def run(self, args: dict) -> None: