Skip to content

Commit c30213c

Browse files
authored
refactor: switch to test run id (#621)
1 parent 96c7fb0 commit c30213c

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

mergify_cli/ci/junit.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class InvalidJunitXMLError(Exception):
2525

2626

2727
async def junit_to_spans(
28+
run_id: int,
2829
xml_content: bytes,
2930
test_language: str | None = None,
3031
test_framework: str | None = None,
@@ -59,13 +60,15 @@ async def junit_to_spans(
5960
if test_language is not None:
6061
common_attributes["test.language"] = test_language
6162

62-
resource_attributes: dict[str, typing.Any] = {}
63+
resource_attributes: dict[str, typing.Any] = {
64+
"test.run.id": run_id,
65+
}
6366

6467
if (job_name := detector.get_job_name()) is not None:
6568
resource_attributes[cicd_attributes.CICD_PIPELINE_NAME] = job_name
6669

67-
if (run_id := detector.get_cicd_pipeline_run_id()) is not None:
68-
resource_attributes[cicd_attributes.CICD_PIPELINE_RUN_ID] = run_id
70+
if (cicd_run_id := detector.get_cicd_pipeline_run_id()) is not None:
71+
resource_attributes[cicd_attributes.CICD_PIPELINE_RUN_ID] = cicd_run_id
6972

7073
if (run_attempt := detector.get_cicd_pipeline_run_attempt()) is not None:
7174
resource_attributes["cicd.pipeline.run.attempt"] = run_attempt

mergify_cli/ci/upload.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,10 @@ def upload_spans(
5555
raise UploadError(logstr.getvalue())
5656

5757

58-
def connect_traces(spans: list[ReadableSpan]) -> None:
59-
if detector.get_ci_provider() == "github_actions" and spans:
60-
root_span_id = spans[0].context.span_id
58+
def connect_traces(run_id: int) -> None:
59+
if detector.get_ci_provider() == "github_actions":
6160
console.print(
62-
f"::notice title=Mergify CI::MERGIFY_TEST_ROOT_SPAN_ID={root_span_id}",
61+
f"::notice title=Mergify CI::MERGIFY_TEST_RUN_ID={run_id.to_bytes(8, 'big').hex()}",
6362
soft_wrap=True,
6463
)
6564

@@ -74,10 +73,13 @@ async def upload( # noqa: PLR0913, PLR0917
7473
) -> None:
7574
spans = []
7675

76+
run_id = junit.ID_GENERATOR.generate_span_id()
77+
7778
for filename in files:
7879
try:
7980
spans.extend(
8081
await junit.junit_to_spans(
82+
run_id,
8183
pathlib.Path(filename).read_bytes(),
8284
test_language=test_language,
8385
test_framework=test_framework,
@@ -95,7 +97,7 @@ async def upload( # noqa: PLR0913, PLR0917
9597
except UploadError as e:
9698
console.log(f"Error uploading spans: {e}", style="red")
9799
else:
98-
connect_traces(spans)
100+
connect_traces(run_id)
99101
console.log("[green]:tada: File(s) uploaded[/]")
100102
else:
101103
console.log("[orange]No tests were detected in the JUnit file(s)[/]")

mergify_cli/tests/ci/test_junit.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ async def test_parse(
2626
_get_cicd_pipeline_run_attempt: mock.Mock,
2727
) -> None:
2828
filename = pathlib.Path(__file__).parent / "junit_example.xml"
29+
run_id = 32312
2930
spans = await junit.junit_to_spans(
31+
run_id,
3032
filename.read_bytes(),
3133
"python",
3234
"unittest",
@@ -36,6 +38,7 @@ async def test_parse(
3638
spans[1].context.trace_id,
3739
)
3840
resource_attributes = {
41+
"test.run.id": run_id,
3942
"cicd.pipeline.name": "JOB",
4043
"cicd.pipeline.run.id": 123,
4144
"cicd.pipeline.run.attempt": 1,

mergify_cli/tests/ci/test_upload.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ async def test_junit_upload(
6464
captured = capsys.readouterr()
6565
if env["GITHUB_ACTIONS"] == "true":
6666
matched = re.search(
67-
r"^::notice title=Mergify CI::MERGIFY_TEST_ROOT_SPAN_ID=(\d+)",
67+
r"^::notice title=Mergify CI::MERGIFY_TEST_RUN_ID=(.+)",
6868
captured.out,
6969
re.MULTILINE,
7070
)
7171
assert matched is not None
72-
assert int(matched.group(1)) > 0
72+
assert len(bytes.fromhex(matched.group(1))) == 8
7373

7474
assert "🎉 File(s) uploaded" in captured.out
7575

0 commit comments

Comments
 (0)