Skip to content
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
6 changes: 5 additions & 1 deletion src/loopzero/mergify.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ def redirect_request(self, *_args, **_kwargs):


def _open(request: urllib.request.Request):
return urllib.request.build_opener(_NoRedirect).open(request, timeout=30)
try:
return urllib.request.build_opener(_NoRedirect).open(request, timeout=30)
except ValueError:
# The HTTP client can include the Authorization value in a header error.
raise MergifyError("Mergify API request has an invalid URL or header") from None


def api_get(repo: str, suffix: str) -> object | None:
Expand Down
22 changes: 22 additions & 0 deletions tests/test_mergify.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations

import base64
import http.client
import io
import json
import subprocess
import sys
import traceback
import urllib.error
import urllib.request

Expand All @@ -17,6 +19,26 @@ def response(payload: object) -> io.BytesIO:
return io.BytesIO(json.dumps(payload).encode())



Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Extra blank line before the new parametrized test

The diff adds a third blank line after response(), so the new test sits under three blank lines instead of two. If the full check runs a pycodestyle/ruff E303-style rule, this fails the check. Even if not, it doesn't match the rest of the file. Smallest fix: delete the added blank line.

@pytest.mark.parametrize(("operation", "args"), [
(mergify.api_get, ("acme/widgets", "/merge-queue/status")),
(mergify.dequeue, ("acme/widgets", 7)),
])
def test_malformed_authorization_is_not_logged(monkeypatch, operation, args):
fake_key = "synthetic-mergify-key"
monkeypatch.setenv("MERGIFY_API_KEY", fake_key + "\nsecond-line")
monkeypatch.setattr(urllib.request, "getproxies", dict)
monkeypatch.setattr(mergify, "_retry_sleep", lambda _delay: None)

def reject_network(_connection):
pytest.fail("invalid Authorization header reached the network")

monkeypatch.setattr(http.client.HTTPSConnection, "connect", reject_network)
with pytest.raises(mergify.MergifyError, match="invalid URL or header") as failure:
operation(*args)
assert fake_key not in "".join(traceback.format_exception(failure.value))


def test_membership_uses_bearer_and_validates_queue(monkeypatch):
monkeypatch.setenv("MERGIFY_API_KEY", "secret")
seen = []
Expand Down
Loading