From fe1b90542c5ee57371832145639b630b3028e1b1 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Tue, 17 Dec 2024 10:42:33 +0100 Subject: [PATCH] feat: add support for GHA resource attributes Change-Id: Ia8791aa527ccc8e0420a86bb023bdf7ae43f5775 --- pytest_mergify/__init__.py | 3 ++ pytest_mergify/resources/github_actions.py | 37 ++++++++++++++++++++++ tests/test_resources.py | 4 +++ 3 files changed, 44 insertions(+) create mode 100644 pytest_mergify/resources/github_actions.py diff --git a/pytest_mergify/__init__.py b/pytest_mergify/__init__.py index d1f5db1..ff3c4dd 100644 --- a/pytest_mergify/__init__.py +++ b/pytest_mergify/__init__.py @@ -19,6 +19,7 @@ from pytest_mergify import utils import pytest_mergify.resources.ci as resources_ci +import pytest_mergify.resources.github_actions as resources_gha import pytest_opentelemetry.instrumentation @@ -75,9 +76,11 @@ def pytest_configure(self, config: _pytest.config.Config) -> None: else: return + resources_gha.GitHubActionsResourceDetector().detect() resource = opentelemetry.sdk.resources.get_aggregated_resources( [ resources_ci.CIResourceDetector(), + resources_gha.GitHubActionsResourceDetector(), ] ) diff --git a/pytest_mergify/resources/github_actions.py b/pytest_mergify/resources/github_actions.py new file mode 100644 index 0000000..f95a97c --- /dev/null +++ b/pytest_mergify/resources/github_actions.py @@ -0,0 +1,37 @@ +import os + +from opentelemetry.sdk.resources import Resource, ResourceDetector + +from opentelemetry.semconv._incubating.attributes import cicd_attributes +from opentelemetry.semconv._incubating.attributes import vcs_attributes + +from pytest_mergify import utils + + +class GitHubActionsResourceDetector(ResourceDetector): + """Detects OpenTelemetry Resource attributes for GitHub Actions.""" + + OPENTELEMETRY_GHA_MAPPING = { + cicd_attributes.CICD_PIPELINE_NAME: "GITHUB_JOB", + cicd_attributes.CICD_PIPELINE_RUN_ID: "GITHUB_RUN_ID", + 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", + } + + def detect(self) -> Resource: + if utils.get_ci_provider() != "github_actions": + return Resource({}) + + attributes = {} + + if "GITHUB_SERVER_URL" in os.environ and "GITHUB_REPOSITORY" in os.environ: + attributes[vcs_attributes.VCS_REPOSITORY_URL_FULL] = os.environ["GITHUB_SERVER_URL"] + os.environ["GITHUB_REPOSITORY"] + + for attribute_name, envvar in self.OPENTELEMETRY_GHA_MAPPING.items(): + attributes[attribute_name] = os.environ.get(envvar) + + return Resource(attributes) diff --git a/tests/test_resources.py b/tests/test_resources.py index c045863..c4376ca 100644 --- a/tests/test_resources.py +++ b/tests/test_resources.py @@ -1,6 +1,10 @@ +import pytest + import _pytest.pytester import _pytest.config +from pytest_mergify import utils + def test_span_resources_attributes( pytestconfig: _pytest.config.Config,