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..a96997f --- /dev/null +++ b/pytest_mergify/resources/github_actions.py @@ -0,0 +1,39 @@ +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.""" + + def detect(self) -> Resource: + if utils.get_ci_provider() != "github_actions": + return Resource({}) + + if "GITHUB_SERVER_URL" in os.environ and "GITHUB_REPOSITORY" in os.environ: + url = os.environ.get("GITHUB_SERVER_URL") + os.environ.get("GITHUB_REPOSITORY") + else: + url = None + + return Resource( + { + cicd_attributes.CICD_PIPELINE_NAME: os.environ.get("GITHUB_JOB"), + cicd_attributes.CICD_PIPELINE_RUN_ID: os.environ.get( + "GITHUB_RUN_ID" + ), + cicd_attributes.CICD_PIPELINE_TASK_NAME: os.environ.get( + "GITHUB_ACTION" + ), + vcs_attributes.VCS_REPOSITORY_URL_FULL: url, + vcs_attributes.VCS_REF_HEAD_NAME: os.environ.get("GITHUB_REF_NAME"), + vcs_attributes.VCS_REF_HEAD_TYPE: os.environ.get("GITHUB_REF_TYPE"), + vcs_attributes.VCS_REF_BASE_NAME: os.environ.get("GITHUB_BASE_REF"), + "vcs.repository.name": os.environ.get("GITHUB_REPOSITORY"), + "vcs.repository.id": os.environ.get("GITHUB_REPOSITORY_ID"), + } + ) diff --git a/tests/test_resources.py b/tests/test_resources.py index c045863..1eca158 100644 --- a/tests/test_resources.py +++ b/tests/test_resources.py @@ -1,6 +1,9 @@ +import pytest + import _pytest.pytester import _pytest.config +from pytest_mergify import utils def test_span_resources_attributes( pytestconfig: _pytest.config.Config, @@ -10,3 +13,14 @@ def test_span_resources_attributes( assert plugin.exporter is not None spans = plugin.exporter.get_finished_spans() assert spans[0].resource.attributes["cicd.provider.name"] == "pytest_mergify_suite" + + +@pytest.mark.skipif(utils.get_ci_provider() != "github_actions", reason="This test only supports GHA") +def test_span_github_actions( + pytestconfig: _pytest.config.Config, +) -> None: + plugin = pytestconfig.pluginmanager.get_plugin("PytestMergify") + assert plugin is not None + assert plugin.exporter is not None + spans = plugin.exporter.get_finished_spans() + assert spans[0].resource.attributes["vcs.repository.name"] == "Mergifyio/pytest-mergify"