Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for GHA resource attributes #19

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pytest_mergify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(),
]
)

Expand Down
40 changes: 40 additions & 0 deletions pytest_mergify/resources/github_actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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():
if envvar in os.environ:
attributes[attribute_name] = os.environ[envvar]

return Resource(attributes)
18 changes: 18 additions & 0 deletions tests/test_resources.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

import _pytest.pytester
import _pytest.config

Expand All @@ -12,3 +14,19 @@ 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"] == utils.get_ci_provider()


@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"
)