Skip to content

Commit ca37864

Browse files
authored
fix: pass repo name in export URL (#20)
1 parent 58ba98c commit ca37864

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

pytest_mergify/__init__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class PytestMergify:
4343
__name__ = "PytestMergify"
4444

4545
exporter: export.SpanExporter
46+
repo_name: str | None
4647

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

5557
span_processor: opentelemetry.sdk.trace.SpanProcessor
5658
if os.environ.get("PYTEST_MERGIFY_DEBUG"):
@@ -67,11 +69,12 @@ def pytest_configure(self, config: _pytest.config.Config) -> None:
6769
url = config.getoption("--mergify-api-url") or os.environ.get(
6870
"MERGIFY_API_URL", "https://api.mergify.com"
6971
)
70-
self.exporter = OTLPSpanExporter(
71-
endpoint=f"{url}/v1/ci/traces",
72-
headers={"Authorization": f"Bearer {self.token}"},
73-
compression=Compression.Gzip,
74-
)
72+
if self.repo_name is not None:
73+
self.exporter = OTLPSpanExporter(
74+
endpoint=f"{url}/v1/{self.repo_name}/ci/traces",
75+
headers={"Authorization": f"Bearer {self.token}"},
76+
compression=Compression.Gzip,
77+
)
7578
span_processor = export.BatchSpanProcessor(self.exporter)
7679
else:
7780
return

pytest_mergify/utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import re
23
import typing
34

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

2021

22+
def get_repository_name() -> str | None:
23+
provider = get_ci_provider()
24+
25+
if provider == "github_actions":
26+
return os.getenv("GITHUB_REPOSITORY")
27+
28+
if provider == "circleci":
29+
repository_url = os.getenv("CIRCLE_REPOSITORY_URL")
30+
if repository_url and (
31+
match := re.match(
32+
r"(https?://[\w.-]+/)?(?P<full_name>[\w.-]+/[\w.-]+)/?$",
33+
repository_url,
34+
)
35+
):
36+
return match.group("full_name")
37+
38+
if provider == "pytest_mergify_suite":
39+
return "Mergifyio/pytest-mergify"
40+
41+
return None
42+
43+
2144
def strtobool(string: str) -> bool:
2245
if string.lower() in {"y", "yes", "t", "true", "on", "1"}:
2346
return True

tests/test_plugin.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,9 @@ def test_foo():
5151
break
5252
else:
5353
pytest.fail("No trace id found")
54+
55+
56+
def test_repo_name(pytestconfig: _pytest.config.Config) -> None:
57+
plugin = pytestconfig.pluginmanager.get_plugin("PytestMergify")
58+
assert plugin is not None
59+
assert plugin.repo_name == "Mergifyio/pytest-mergify"

0 commit comments

Comments
 (0)