Skip to content

Commit 3fe5fdf

Browse files
authored
Merge of #613
2 parents 1b38678 + f9ca635 commit 3fe5fdf

File tree

3 files changed

+54
-118
lines changed

3 files changed

+54
-118
lines changed

mergify_cli/ci/detector.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,25 @@ async def get_head_sha() -> str | None:
7373
return None
7474

7575

76+
def get_cicd_pipeline_run_id() -> int | None:
77+
if get_ci_provider() == "github_actions" and "GITHUB_RUN_ID" in os.environ:
78+
return int(os.environ["GITHUB_RUN_ID"])
79+
80+
if get_ci_provider() == "circleci" and "CIRCLE_WORKFLOW_ID" in os.environ:
81+
return int(os.environ["CIRCLE_WORKFLOW_ID"])
82+
83+
return None
84+
85+
86+
def get_cicd_pipeline_run_attempt() -> int | None:
87+
if get_ci_provider() == "github_actions" and "GITHUB_RUN_ATTEMPT" in os.environ:
88+
return int(os.environ["GITHUB_RUN_ATTEMPT"])
89+
if get_ci_provider() == "circleci" and "CIRCLE_BUILD_NUM" in os.environ:
90+
return int(os.environ["CIRCLE_BUILD_NUM"])
91+
92+
return None
93+
94+
7695
def get_github_repository() -> str | None:
7796
if get_ci_provider() == "github_actions":
7897
return os.getenv("GITHUB_REPOSITORY")

mergify_cli/ci/junit.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,17 @@ async def junit_to_spans(
6969
if test_language is not None:
7070
common_attributes["test.language"] = test_language
7171

72-
resource_attributes = {}
72+
resource_attributes: dict[str, typing.Any] = {}
7373

7474
if (job_name := detector.get_job_name()) is not None:
7575
resource_attributes[cicd_attributes.CICD_PIPELINE_NAME] = job_name
7676

77+
if (run_id := detector.get_cicd_pipeline_run_id()) is not None:
78+
resource_attributes[cicd_attributes.CICD_PIPELINE_RUN_ID] = run_id
79+
80+
if (run_attempt := detector.get_cicd_pipeline_run_attempt()) is not None:
81+
resource_attributes["cicd.pipeline.run.attempt"] = run_attempt
82+
7783
if (head_revision := (await detector.get_head_sha())) is not None:
7884
resource_attributes[vcs_attributes.VCS_REF_HEAD_REVISION] = head_revision
7985

mergify_cli/tests/ci/test_junit.py

Lines changed: 28 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
@mock.patch.object(detector, "get_ci_provider", return_value="github_actions")
1313
@mock.patch.object(detector, "get_job_name", return_value="JOB")
14+
@mock.patch.object(detector, "get_cicd_pipeline_run_id", return_value=123)
15+
@mock.patch.object(detector, "get_cicd_pipeline_run_attempt", return_value=1)
1416
@mock.patch.object(
1517
detector,
1618
"get_head_sha",
@@ -20,6 +22,8 @@ async def test_parse(
2022
_get_ci_provider: mock.Mock,
2123
_get_job_name: mock.Mock,
2224
_get_head_sha: mock.Mock,
25+
_get_cicd_pipeline_run_id: mock.Mock,
26+
_get_cicd_pipeline_run_attempt: mock.Mock,
2327
) -> None:
2428
filename = pathlib.Path(__file__).parent / "junit_example.xml"
2529
spans = await junit.junit_to_spans(
@@ -32,6 +36,17 @@ async def test_parse(
3236
trace_id = "0x" + opentelemetry.trace.span.format_trace_id(
3337
spans[1].context.trace_id,
3438
)
39+
resource_attributes = {
40+
"cicd.pipeline.name": "JOB",
41+
"cicd.pipeline.run.id": 123,
42+
"cicd.pipeline.run.attempt": 1,
43+
"cicd.provider.name": "github_actions",
44+
"vcs.ref.head.revision": "3af96aa24f1d32fcfbb7067793cacc6dc0c6b199",
45+
"service.name": "unknown_service",
46+
"telemetry.sdk.language": "python",
47+
"telemetry.sdk.name": "opentelemetry",
48+
"telemetry.sdk.version": anys.ANY_STR,
49+
}
3550
assert dictified_spans == [
3651
{
3752
"attributes": {
@@ -52,15 +67,7 @@ async def test_parse(
5267
"name": "Tests.Registration",
5368
"parent_id": None,
5469
"resource": {
55-
"attributes": {
56-
"cicd.pipeline.name": "JOB",
57-
"cicd.provider.name": "github_actions",
58-
"vcs.ref.head.revision": "3af96aa24f1d32fcfbb7067793cacc6dc0c6b199",
59-
"service.name": "unknown_service",
60-
"telemetry.sdk.language": "python",
61-
"telemetry.sdk.name": "opentelemetry",
62-
"telemetry.sdk.version": anys.ANY_STR,
63-
},
70+
"attributes": resource_attributes,
6471
"schema_url": "",
6572
},
6673
"start_time": anys.ANY_DATETIME_STR,
@@ -88,15 +95,7 @@ async def test_parse(
8895
"name": "Tests.Registration.testCase1",
8996
"parent_id": anys.ANY_STR,
9097
"resource": {
91-
"attributes": {
92-
"cicd.pipeline.name": "JOB",
93-
"cicd.provider.name": "github_actions",
94-
"vcs.ref.head.revision": "3af96aa24f1d32fcfbb7067793cacc6dc0c6b199",
95-
"service.name": "unknown_service",
96-
"telemetry.sdk.language": "python",
97-
"telemetry.sdk.name": "opentelemetry",
98-
"telemetry.sdk.version": anys.ANY_STR,
99-
},
98+
"attributes": resource_attributes,
10099
"schema_url": "",
101100
},
102101
"start_time": anys.ANY_DATETIME_STR,
@@ -124,15 +123,7 @@ async def test_parse(
124123
"name": "Tests.Registration.testCase2",
125124
"parent_id": anys.ANY_STR,
126125
"resource": {
127-
"attributes": {
128-
"cicd.pipeline.name": "JOB",
129-
"cicd.provider.name": "github_actions",
130-
"vcs.ref.head.revision": "3af96aa24f1d32fcfbb7067793cacc6dc0c6b199",
131-
"service.name": "unknown_service",
132-
"telemetry.sdk.language": "python",
133-
"telemetry.sdk.name": "opentelemetry",
134-
"telemetry.sdk.version": anys.ANY_STR,
135-
},
126+
"attributes": resource_attributes,
136127
"schema_url": "",
137128
},
138129
"start_time": anys.ANY_DATETIME_STR,
@@ -163,15 +154,7 @@ async def test_parse(
163154
"name": "Tests.Registration.testCase3",
164155
"parent_id": anys.ANY_STR,
165156
"resource": {
166-
"attributes": {
167-
"cicd.pipeline.name": "JOB",
168-
"cicd.provider.name": "github_actions",
169-
"vcs.ref.head.revision": "3af96aa24f1d32fcfbb7067793cacc6dc0c6b199",
170-
"service.name": "unknown_service",
171-
"telemetry.sdk.language": "python",
172-
"telemetry.sdk.name": "opentelemetry",
173-
"telemetry.sdk.version": anys.ANY_STR,
174-
},
157+
"attributes": resource_attributes,
175158
"schema_url": "",
176159
},
177160
"start_time": anys.ANY_DATETIME_STR,
@@ -198,15 +181,7 @@ async def test_parse(
198181
"name": "Tests.Authentication",
199182
"parent_id": None,
200183
"resource": {
201-
"attributes": {
202-
"cicd.pipeline.name": "JOB",
203-
"cicd.provider.name": "github_actions",
204-
"vcs.ref.head.revision": "3af96aa24f1d32fcfbb7067793cacc6dc0c6b199",
205-
"service.name": "unknown_service",
206-
"telemetry.sdk.language": "python",
207-
"telemetry.sdk.name": "opentelemetry",
208-
"telemetry.sdk.version": anys.ANY_STR,
209-
},
184+
"attributes": resource_attributes,
210185
"schema_url": "",
211186
},
212187
"start_time": anys.ANY_DATETIME_STR,
@@ -234,15 +209,7 @@ async def test_parse(
234209
"name": "Tests.Authentication.testCase7",
235210
"parent_id": anys.ANY_STR,
236211
"resource": {
237-
"attributes": {
238-
"cicd.pipeline.name": "JOB",
239-
"cicd.provider.name": "github_actions",
240-
"vcs.ref.head.revision": "3af96aa24f1d32fcfbb7067793cacc6dc0c6b199",
241-
"service.name": "unknown_service",
242-
"telemetry.sdk.language": "python",
243-
"telemetry.sdk.name": "opentelemetry",
244-
"telemetry.sdk.version": anys.ANY_STR,
245-
},
212+
"attributes": resource_attributes,
246213
"schema_url": "",
247214
},
248215
"start_time": anys.ANY_DATETIME_STR,
@@ -270,15 +237,7 @@ async def test_parse(
270237
"name": "Tests.Authentication.testCase8",
271238
"parent_id": anys.ANY_STR,
272239
"resource": {
273-
"attributes": {
274-
"cicd.pipeline.name": "JOB",
275-
"cicd.provider.name": "github_actions",
276-
"vcs.ref.head.revision": "3af96aa24f1d32fcfbb7067793cacc6dc0c6b199",
277-
"service.name": "unknown_service",
278-
"telemetry.sdk.language": "python",
279-
"telemetry.sdk.name": "opentelemetry",
280-
"telemetry.sdk.version": anys.ANY_STR,
281-
},
240+
"attributes": resource_attributes,
282241
"schema_url": "",
283242
},
284243
"start_time": anys.ANY_DATETIME_STR,
@@ -309,15 +268,7 @@ async def test_parse(
309268
"name": "Tests.Authentication.testCase9",
310269
"parent_id": anys.ANY_STR,
311270
"resource": {
312-
"attributes": {
313-
"cicd.pipeline.name": "JOB",
314-
"cicd.provider.name": "github_actions",
315-
"vcs.ref.head.revision": "3af96aa24f1d32fcfbb7067793cacc6dc0c6b199",
316-
"service.name": "unknown_service",
317-
"telemetry.sdk.language": "python",
318-
"telemetry.sdk.name": "opentelemetry",
319-
"telemetry.sdk.version": anys.ANY_STR,
320-
},
271+
"attributes": resource_attributes,
321272
"schema_url": "",
322273
},
323274
"start_time": anys.ANY_DATETIME_STR,
@@ -349,15 +300,7 @@ async def test_parse(
349300
"name": "Tests.Permission.testCase10",
350301
"parent_id": anys.ANY_STR,
351302
"resource": {
352-
"attributes": {
353-
"cicd.pipeline.name": "JOB",
354-
"cicd.provider.name": "github_actions",
355-
"vcs.ref.head.revision": "3af96aa24f1d32fcfbb7067793cacc6dc0c6b199",
356-
"service.name": "unknown_service",
357-
"telemetry.sdk.language": "python",
358-
"telemetry.sdk.name": "opentelemetry",
359-
"telemetry.sdk.version": anys.ANY_STR,
360-
},
303+
"attributes": resource_attributes,
361304
"schema_url": "",
362305
},
363306
"start_time": anys.ANY_DATETIME_STR,
@@ -384,15 +327,7 @@ async def test_parse(
384327
"name": "Tests.Authentication.Login",
385328
"parent_id": None,
386329
"resource": {
387-
"attributes": {
388-
"cicd.pipeline.name": "JOB",
389-
"cicd.provider.name": "github_actions",
390-
"vcs.ref.head.revision": "3af96aa24f1d32fcfbb7067793cacc6dc0c6b199",
391-
"service.name": "unknown_service",
392-
"telemetry.sdk.language": "python",
393-
"telemetry.sdk.name": "opentelemetry",
394-
"telemetry.sdk.version": anys.ANY_STR,
395-
},
330+
"attributes": resource_attributes,
396331
"schema_url": "",
397332
},
398333
"start_time": anys.ANY_DATETIME_STR,
@@ -420,15 +355,7 @@ async def test_parse(
420355
"name": "Tests.Authentication.Login.testCase4",
421356
"parent_id": anys.ANY_STR,
422357
"resource": {
423-
"attributes": {
424-
"cicd.pipeline.name": "JOB",
425-
"cicd.provider.name": "github_actions",
426-
"vcs.ref.head.revision": "3af96aa24f1d32fcfbb7067793cacc6dc0c6b199",
427-
"service.name": "unknown_service",
428-
"telemetry.sdk.language": "python",
429-
"telemetry.sdk.name": "opentelemetry",
430-
"telemetry.sdk.version": anys.ANY_STR,
431-
},
358+
"attributes": resource_attributes,
432359
"schema_url": "",
433360
},
434361
"start_time": anys.ANY_DATETIME_STR,
@@ -459,15 +386,7 @@ async def test_parse(
459386
"name": "Tests.Authentication.Login.testCase5",
460387
"parent_id": anys.ANY_STR,
461388
"resource": {
462-
"attributes": {
463-
"service.name": "unknown_service",
464-
"telemetry.sdk.language": "python",
465-
"telemetry.sdk.name": "opentelemetry",
466-
"telemetry.sdk.version": anys.ANY_STR,
467-
"cicd.pipeline.name": "JOB",
468-
"cicd.provider.name": "github_actions",
469-
"vcs.ref.head.revision": "3af96aa24f1d32fcfbb7067793cacc6dc0c6b199",
470-
},
389+
"attributes": resource_attributes,
471390
"schema_url": "",
472391
},
473392
"start_time": anys.ANY_DATETIME_STR,
@@ -495,15 +414,7 @@ async def test_parse(
495414
"name": "Tests.Authentication.Login.testCase6",
496415
"parent_id": anys.ANY_STR,
497416
"resource": {
498-
"attributes": {
499-
"cicd.pipeline.name": "JOB",
500-
"cicd.provider.name": "github_actions",
501-
"vcs.ref.head.revision": "3af96aa24f1d32fcfbb7067793cacc6dc0c6b199",
502-
"service.name": "unknown_service",
503-
"telemetry.sdk.language": "python",
504-
"telemetry.sdk.name": "opentelemetry",
505-
"telemetry.sdk.version": anys.ANY_STR,
506-
},
417+
"attributes": resource_attributes,
507418
"schema_url": "",
508419
},
509420
"start_time": anys.ANY_DATETIME_STR,

0 commit comments

Comments
 (0)