Skip to content

Commit f446e8a

Browse files
authored
fix: better error report when token/repo are empty (#138)
It's very easy in GitHub Actions to do: run: env: - MERGIFY_TOKEN: ${{ secrets.MERGIFY_TOKEN }} And having secrets.MERGIFY_TOKEN empty because of typo, secrets permission issues, misconfigured reusable workflow. This configuration effectively set the the env but it's an empty string. The current code don't start a tracer as expected, but didn't report anything. This adds: * the reporting for when repo_name and token are empty. * an generic error message if the tracer didn't start for some reason and we didn't provide the root cause. Fixes MRGFY-5471
1 parent c5e63bc commit f446e8a

File tree

2 files changed

+64
-25
lines changed

2 files changed

+64
-25
lines changed

pytest_mergify/__init__.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,43 +39,46 @@ def pytest_terminal_summary(
3939

4040
terminalreporter.section("Mergify CI")
4141

42-
# Make sure we shutdown and flush traces before existing: this makes
43-
# sure that we capture the possible error logs, otherwise they are
44-
# emitted on exit (atexit()).
45-
if self.mergify_tracer.tracer_provider is not None:
46-
try:
47-
self.mergify_tracer.tracer_provider.force_flush()
48-
except Exception as e:
42+
if self.mergify_tracer.tracer_provider is None:
43+
if not self.mergify_tracer.token:
4944
terminalreporter.write_line(
50-
f"Error while exporting traces: {e}",
51-
red=True,
52-
)
53-
else:
54-
terminalreporter.write_line(
55-
f"MERGIFY_TEST_RUN_ID={self.mergify_tracer.test_run_id}",
45+
"No token configured for Mergify; test results will not be uploaded",
46+
yellow=True,
5647
)
48+
return
5749

58-
try:
59-
self.mergify_tracer.tracer_provider.shutdown()
60-
except Exception as e:
50+
if not self.mergify_tracer.repo_name:
6151
terminalreporter.write_line(
62-
f"Error while shutting down the tracer: {e}",
52+
"Unable to determine repository name; test results will not be uploaded",
6353
red=True,
6454
)
55+
return
6556

66-
if self.mergify_tracer.token is None:
6757
terminalreporter.write_line(
68-
"No token configured for Mergify; test results will not be uploaded",
69-
yellow=True,
58+
"Mergify Tracer didn't start for unexpected reason (Please contact Mergify support); test results will not be uploaded",
59+
red=True,
7060
)
7161
return
7262

73-
if self.mergify_tracer.repo_name is None:
63+
try:
64+
self.mergify_tracer.tracer_provider.force_flush()
65+
except Exception as e:
7466
terminalreporter.write_line(
75-
"Unable to determine repository name; test results will not be uploaded",
67+
f"Error while exporting traces: {e}",
68+
red=True,
69+
)
70+
else:
71+
terminalreporter.write_line(
72+
f"MERGIFY_TEST_RUN_ID={self.mergify_tracer.test_run_id}",
73+
)
74+
75+
try:
76+
self.mergify_tracer.tracer_provider.shutdown()
77+
except Exception as e:
78+
terminalreporter.write_line(
79+
f"Error while shutting down the tracer: {e}",
7680
red=True,
7781
)
78-
return
7982

8083
@property
8184
def tracer(self) -> typing.Optional[opentelemetry.trace.Tracer]:

tests/test_plugin.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,28 @@ def test_enabled(
3333
assert any("Mergify CI" in line for line in result.stdout.lines)
3434

3535

36+
def test_empty_token(pytester_with_spans: conftest.PytesterWithSpanT) -> None:
37+
result, spans = pytester_with_spans(
38+
setenv={
39+
"MERGIFY_TOKEN": "",
40+
"_PYTEST_MERGIFY_TEST": None,
41+
}
42+
)
43+
assert spans is None
44+
assert (
45+
"No token configured for Mergify; test results will not be uploaded"
46+
in result.stdout.lines
47+
)
48+
49+
3650
def test_no_token(pytester_with_spans: conftest.PytesterWithSpanT) -> None:
37-
result, spans = pytester_with_spans()
38-
assert spans is not None
51+
result, spans = pytester_with_spans(
52+
setenv={
53+
"MERGIFY_TOKEN": None,
54+
"_PYTEST_MERGIFY_TEST": None,
55+
}
56+
)
57+
assert spans is None
3958
assert (
4059
"No token configured for Mergify; test results will not be uploaded"
4160
in result.stdout.lines
@@ -84,6 +103,23 @@ def test_repo_name_github_actions(
84103
assert plugin.mergify_tracer.repo_name == "Mergifyio/pytest-mergify"
85104

86105

106+
def test_with_token_empty_repo(
107+
pytester_with_spans: conftest.PytesterWithSpanT,
108+
) -> None:
109+
result, spans = pytester_with_spans(
110+
setenv={
111+
"GITHUB_ACTIONS": "true",
112+
"MERGIFY_TOKEN": "x",
113+
"_PYTEST_MERGIFY_TEST": "false",
114+
"GITHUB_REPOSITORY": "",
115+
}
116+
)
117+
assert (
118+
"Unable to determine repository name; test results will not be uploaded"
119+
in result.stdout.lines
120+
)
121+
122+
87123
def test_with_token_no_repo(
88124
pytester_with_spans: conftest.PytesterWithSpanT,
89125
) -> None:

0 commit comments

Comments
 (0)