Skip to content

Commit

Permalink
refactor: switch to test run id
Browse files Browse the repository at this point in the history
Change-Id: I5fd263dd3934fd22183817aa5b03f3055d181a2f
  • Loading branch information
jd committed Feb 13, 2025
1 parent 96c7fb0 commit d0df035
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
9 changes: 6 additions & 3 deletions mergify_cli/ci/junit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions mergify_cli/ci/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand All @@ -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,
Expand All @@ -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)[/]")
3 changes: 3 additions & 0 deletions mergify_cli/tests/ci/test_junit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.to_bytes(8, "big"),
filename.read_bytes(),
"python",
"unittest",
Expand All @@ -36,6 +38,7 @@ async def test_parse(
spans[1].context.trace_id,
)
resource_attributes = {
"test.run.id": run_id.to_bytes(8, "big").decode(),
"cicd.pipeline.name": "JOB",
"cicd.pipeline.run.id": 123,
"cicd.pipeline.run.attempt": 1,
Expand Down
4 changes: 2 additions & 2 deletions mergify_cli/tests/ci/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d0df035

Please sign in to comment.