Skip to content

Commit

Permalink
fix: pass repo name in export URL
Browse files Browse the repository at this point in the history
Change-Id: I518492437345f900a92338f6f8818fa63480d837
  • Loading branch information
jd committed Dec 17, 2024
1 parent 58ba98c commit bee5053
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pytest_mergify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class PytestMergify:
__name__ = "PytestMergify"

exporter: export.SpanExporter
repo_name: str | None

def ci_supports_trace_interception(self) -> bool:
return utils.get_ci_provider() == "github_actions"
Expand All @@ -51,6 +52,7 @@ def ci_supports_trace_interception(self) -> bool:
@pytest.hookimpl(trylast=True)
def pytest_configure(self, config: _pytest.config.Config) -> None:
self.token = os.environ.get("MERGIFY_TOKEN")
self.repo_name = utils.get_repository_name()

span_processor: opentelemetry.sdk.trace.SpanProcessor
if os.environ.get("PYTEST_MERGIFY_DEBUG"):
Expand All @@ -67,11 +69,12 @@ def pytest_configure(self, config: _pytest.config.Config) -> None:
url = config.getoption("--mergify-api-url") or os.environ.get(
"MERGIFY_API_URL", "https://api.mergify.com"
)
self.exporter = OTLPSpanExporter(
endpoint=f"{url}/v1/ci/traces",
headers={"Authorization": f"Bearer {self.token}"},
compression=Compression.Gzip,
)
if self.repo_name is not None:
self.exporter = OTLPSpanExporter(
endpoint=f"{url}/v1/{self.repo_name}/ci/traces",
headers={"Authorization": f"Bearer {self.token}"},
compression=Compression.Gzip,
)
span_processor = export.BatchSpanProcessor(self.exporter)
else:
return
Expand Down
23 changes: 23 additions & 0 deletions pytest_mergify/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import typing

CIProviderT = typing.Literal["github_actions", "circleci", "pytest_mergify_suite"]
Expand All @@ -18,6 +19,28 @@ def get_ci_provider() -> CIProviderT | None:
return None


def get_repository_name() -> str | None:
provider = get_ci_provider()

if provider == "github_actions":
return os.getenv("GITHUB_REPOSITORY")

if provider == "circleci":
repository_url = os.getenv("CIRCLE_REPOSITORY_URL")
if repository_url and (
match := re.match(
r"(https?://[\w.-]+/)?(?P<full_name>[\w.-]+/[\w.-]+)/?$",
repository_url,
)
):
return match.group("full_name")

if provider == "pytest_mergify_suite":
return "Mergifyio/pytest-mergify"

return None


def strtobool(string: str) -> bool:
if string.lower() in {"y", "yes", "t", "true", "on", "1"}:
return True
Expand Down
6 changes: 6 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@ def test_foo():
break
else:
pytest.fail("No trace id found")


def test_repo_name(pytestconfig: _pytest.config.Config) -> None:
plugin = pytestconfig.pluginmanager.get_plugin("PytestMergify")
assert plugin is not None
assert plugin.repo_name == "Mergifyio/pytest-mergify"

0 comments on commit bee5053

Please sign in to comment.