Skip to content

Commit

Permalink
fix(gha): set the right type of data in attribute (#65)
Browse files Browse the repository at this point in the history
Some of the attributes should be set as int, not as a str.
  • Loading branch information
jd authored Feb 10, 2025
1 parent 31698b1 commit ca62a75
Showing 2 changed files with 16 additions and 11 deletions.
22 changes: 11 additions & 11 deletions pytest_mergify/resources/github_actions.py
Original file line number Diff line number Diff line change
@@ -26,15 +26,15 @@ def get_github_actions_head_sha() -> str | None:
return os.getenv("GITHUB_SHA")

OPENTELEMETRY_GHA_MAPPING = {
cicd_attributes.CICD_PIPELINE_NAME: "GITHUB_JOB",
cicd_attributes.CICD_PIPELINE_RUN_ID: "GITHUB_RUN_ID",
"cicd.pipeline.run.attempt": "GITHUB_RUN_ATTEMPT",
cicd_attributes.CICD_PIPELINE_TASK_NAME: "GITHUB_ACTION",
vcs_attributes.VCS_REF_HEAD_NAME: "GITHUB_REF_NAME",
vcs_attributes.VCS_REF_HEAD_TYPE: "GITHUB_REF_TYPE",
vcs_attributes.VCS_REF_BASE_NAME: "GITHUB_BASE_REF",
"vcs.repository.name": "GITHUB_REPOSITORY",
"vcs.repository.id": "GITHUB_REPOSITORY_ID",
cicd_attributes.CICD_PIPELINE_NAME: (str, "GITHUB_JOB"),
cicd_attributes.CICD_PIPELINE_RUN_ID: (int, "GITHUB_RUN_ID"),
"cicd.pipeline.run.attempt": (int, "GITHUB_RUN_ATTEMPT"),
cicd_attributes.CICD_PIPELINE_TASK_NAME: (str, "GITHUB_ACTION"),
vcs_attributes.VCS_REF_HEAD_NAME: (str, "GITHUB_REF_NAME"),
vcs_attributes.VCS_REF_HEAD_TYPE: (str, "GITHUB_REF_TYPE"),
vcs_attributes.VCS_REF_BASE_NAME: (str, "GITHUB_BASE_REF"),
"vcs.repository.name": (str, "GITHUB_REPOSITORY"),
"vcs.repository.id": (int, "GITHUB_REPOSITORY_ID"),
}

def detect(self) -> Resource:
@@ -52,8 +52,8 @@ def detect(self) -> Resource:
if head_sha is not None:
attributes[vcs_attributes.VCS_REF_HEAD_REVISION] = head_sha

for attribute_name, envvar in self.OPENTELEMETRY_GHA_MAPPING.items():
for attribute_name, (type_, envvar) in self.OPENTELEMETRY_GHA_MAPPING.items():
if envvar in os.environ:
attributes[attribute_name] = os.environ[envvar]
attributes[attribute_name] = type_(os.environ[envvar])

return Resource(attributes)
5 changes: 5 additions & 0 deletions tests/test_resources.py
Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@ def test_span_github_actions(
monkeypatch.setenv("GITHUB_ACTIONS", "true")
monkeypatch.setenv("GITHUB_REPOSITORY", "Mergifyio/pytest-mergify")
monkeypatch.setenv("GITHUB_SERVER_URL", "https://github.com")
monkeypatch.setenv("GITHUB_RUN_ID", "3213121312")
result, spans = pytester_with_spans()
assert all(
span.resource.attributes["vcs.repository.name"] == "Mergifyio/pytest-mergify"
@@ -50,3 +51,7 @@ def test_span_github_actions(
== "https://github.com/Mergifyio/pytest-mergify"
for span in spans.values()
)
assert all(
span.resource.attributes["cicd.pipeline.run.id"] == 3213121312
for span in spans.values()
)

0 comments on commit ca62a75

Please sign in to comment.