From c30213cb8be93e21df2b979a019034a445e8c50a Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Thu, 13 Feb 2025 17:21:15 +0100 Subject: [PATCH] refactor: switch to test run id (#621) --- mergify_cli/ci/junit.py | 9 ++++++--- mergify_cli/ci/upload.py | 12 +++++++----- mergify_cli/tests/ci/test_junit.py | 3 +++ mergify_cli/tests/ci/test_upload.py | 4 ++-- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/mergify_cli/ci/junit.py b/mergify_cli/ci/junit.py index 45a3b69..54cb704 100644 --- a/mergify_cli/ci/junit.py +++ b/mergify_cli/ci/junit.py @@ -25,6 +25,7 @@ class InvalidJunitXMLError(Exception): async def junit_to_spans( + run_id: int, xml_content: bytes, test_language: str | None = None, test_framework: str | None = None, @@ -59,13 +60,15 @@ async def junit_to_spans( if test_language is not None: common_attributes["test.language"] = test_language - resource_attributes: dict[str, typing.Any] = {} + resource_attributes: dict[str, typing.Any] = { + "test.run.id": run_id, + } if (job_name := detector.get_job_name()) is not None: resource_attributes[cicd_attributes.CICD_PIPELINE_NAME] = job_name - if (run_id := detector.get_cicd_pipeline_run_id()) is not None: - resource_attributes[cicd_attributes.CICD_PIPELINE_RUN_ID] = run_id + if (cicd_run_id := detector.get_cicd_pipeline_run_id()) is not None: + resource_attributes[cicd_attributes.CICD_PIPELINE_RUN_ID] = cicd_run_id if (run_attempt := detector.get_cicd_pipeline_run_attempt()) is not None: resource_attributes["cicd.pipeline.run.attempt"] = run_attempt diff --git a/mergify_cli/ci/upload.py b/mergify_cli/ci/upload.py index 2c93e73..e970294 100644 --- a/mergify_cli/ci/upload.py +++ b/mergify_cli/ci/upload.py @@ -55,11 +55,10 @@ def upload_spans( raise UploadError(logstr.getvalue()) -def connect_traces(spans: list[ReadableSpan]) -> None: - if detector.get_ci_provider() == "github_actions" and spans: - root_span_id = spans[0].context.span_id +def connect_traces(run_id: int) -> None: + if detector.get_ci_provider() == "github_actions": console.print( - f"::notice title=Mergify CI::MERGIFY_TEST_ROOT_SPAN_ID={root_span_id}", + f"::notice title=Mergify CI::MERGIFY_TEST_RUN_ID={run_id.to_bytes(8, 'big').hex()}", soft_wrap=True, ) @@ -74,10 +73,13 @@ async def upload( # noqa: PLR0913, PLR0917 ) -> None: spans = [] + run_id = junit.ID_GENERATOR.generate_span_id() + for filename in files: try: spans.extend( await junit.junit_to_spans( + run_id, pathlib.Path(filename).read_bytes(), test_language=test_language, test_framework=test_framework, @@ -95,7 +97,7 @@ async def upload( # noqa: PLR0913, PLR0917 except UploadError as e: console.log(f"Error uploading spans: {e}", style="red") else: - connect_traces(spans) + connect_traces(run_id) console.log("[green]:tada: File(s) uploaded[/]") else: console.log("[orange]No tests were detected in the JUnit file(s)[/]") diff --git a/mergify_cli/tests/ci/test_junit.py b/mergify_cli/tests/ci/test_junit.py index 53bb0b6..5812135 100644 --- a/mergify_cli/tests/ci/test_junit.py +++ b/mergify_cli/tests/ci/test_junit.py @@ -26,7 +26,9 @@ async def test_parse( _get_cicd_pipeline_run_attempt: mock.Mock, ) -> None: filename = pathlib.Path(__file__).parent / "junit_example.xml" + run_id = 32312 spans = await junit.junit_to_spans( + run_id, filename.read_bytes(), "python", "unittest", @@ -36,6 +38,7 @@ async def test_parse( spans[1].context.trace_id, ) resource_attributes = { + "test.run.id": run_id, "cicd.pipeline.name": "JOB", "cicd.pipeline.run.id": 123, "cicd.pipeline.run.attempt": 1, diff --git a/mergify_cli/tests/ci/test_upload.py b/mergify_cli/tests/ci/test_upload.py index 70a2f9c..415a534 100644 --- a/mergify_cli/tests/ci/test_upload.py +++ b/mergify_cli/tests/ci/test_upload.py @@ -64,12 +64,12 @@ async def test_junit_upload( captured = capsys.readouterr() if env["GITHUB_ACTIONS"] == "true": matched = re.search( - r"^::notice title=Mergify CI::MERGIFY_TEST_ROOT_SPAN_ID=(\d+)", + r"^::notice title=Mergify CI::MERGIFY_TEST_RUN_ID=(.+)", captured.out, re.MULTILINE, ) assert matched is not None - assert int(matched.group(1)) > 0 + assert len(bytes.fromhex(matched.group(1))) == 8 assert "🎉 File(s) uploaded" in captured.out