From c2c06da75b02b1df75ec3e54982d3cbb339d6bbf Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Fri, 13 Nov 2026 11:37:32 -0500 Subject: [PATCH 1/4] gh-75: Always initialize Sentry If you initialize the SDK with no DSN, the entire module acts as a noop. We don't need our own module to implement the same logic, so remove that. --- src/psrt_ghsa_bot/_sentry_monitoring.py | 18 +++------------- src/psrt_ghsa_bot/app.py | 15 +++++++------ tests/conftest.py | 7 +++++++ tests/test_sentry_monitoring.py | 28 +++++++------------------ 4 files changed, 25 insertions(+), 43 deletions(-) create mode 100644 tests/conftest.py diff --git a/src/psrt_ghsa_bot/_sentry_monitoring.py b/src/psrt_ghsa_bot/_sentry_monitoring.py index 2e20399..b305295 100644 --- a/src/psrt_ghsa_bot/_sentry_monitoring.py +++ b/src/psrt_ghsa_bot/_sentry_monitoring.py @@ -14,12 +14,10 @@ def init_sentry() -> None: """Initialize the Sentry SDK with the DSN from the env""" - dsn = os.environ.get("SENTRY_DSN") - if not dsn: + if dsn := os.environ.get("SENTRY_DSN"): + print("Initializing Sentry monitoring") + else: print("No SENTRY_DSN set, skipping Sentry monitoring") - return - - print("Initializing Sentry monitoring") sentry_sdk.init( dsn=dsn, enable_tracing=False, @@ -28,9 +26,6 @@ def init_sentry() -> None: def capture_checkin(monitor_slug, status, duration=None, check_in_id=None) -> None | str: """Capture a Sentry cron check-in.""" - if not os.environ.get("SENTRY_DSN"): - return None - try: return crons.capture_checkin( monitor_slug=monitor_slug, @@ -40,10 +35,3 @@ def capture_checkin(monitor_slug, status, duration=None, check_in_id=None) -> No ) except ImportError, AttributeError: return None - - -def capture_exception() -> None: - """Capture an exception if Sentry is active.""" - if not os.environ.get("SENTRY_DSN"): - return - sentry_sdk.capture_exception() diff --git a/src/psrt_ghsa_bot/app.py b/src/psrt_ghsa_bot/app.py index 0ae4094..4d711bf 100644 --- a/src/psrt_ghsa_bot/app.py +++ b/src/psrt_ghsa_bot/app.py @@ -24,7 +24,6 @@ STATUS_IN_PROGRESS, STATUS_OK, capture_checkin, - capture_exception, init_sentry, ) @@ -87,7 +86,7 @@ def get_repository_advisories( return # Capture the original exception in Sentry (private) # and emit a sanitized public exception. - capture_exception() + sentry_sdk.capture_exception() raise RuntimeError("Request to paginate advisories failed.") @@ -136,7 +135,7 @@ def credit_if_uncredited(login: str, type: str) -> None: print(f" ⚠️ Pull requests not found for {private_fork_owner}/{private_fork_repo}!") pull_requests = [] else: - capture_exception() + sentry_sdk.capture_exception() raise RuntimeError("Request to list pull requests failed") from None for pull_request in pull_requests: @@ -158,7 +157,7 @@ def credit_if_uncredited(login: str, type: str) -> None: f" ⚠️ Reviews not found for {private_fork_owner}/{private_fork_repo}#{pull_request['number']}!" ) continue - capture_exception() + sentry_sdk.capture_exception() raise RuntimeError("Request to list pull requests reviews failed") from None for review in reviews: @@ -217,7 +216,7 @@ def iter_cve_refs_with_tags(cve_api: CveApi, cve_id: str, tags: set[str]) -> abc ) return except RequestException: - capture_exception() + sentry_sdk.capture_exception() return try: @@ -226,7 +225,7 @@ def iter_cve_refs_with_tags(cve_api: CveApi, cve_id: str, tags: set[str]) -> abc except KeyError, TypeError: # Note TypeError covers tests where cve_api is a mock without a # return value for show_cve_record. - capture_exception() + sentry_sdk.capture_exception() return if cve_state != "PUBLISHED": @@ -267,7 +266,7 @@ def apply_to_repo(github: GitHub, owner: str, repo: str, cve_api: CveApi, *, res except RequestFailed: # Capture the original exception in Sentry (private) # and emit a sanitized public exception. - capture_exception() + sentry_sdk.capture_exception() raise RuntimeError("Request to create a private fork failed") from None # Keep draft GHSAs in sync with CVEs: @@ -308,7 +307,7 @@ def apply_to_repo(github: GitHub, owner: str, repo: str, cve_api: CveApi, *, res except RequestFailed: # Capture the original exception in Sentry (private) # and emit a sanitized public exception. - capture_exception() + sentry_sdk.capture_exception() raise RuntimeError("Request to update advisory failed") from None print(" 💾 Updated advisory") else: diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..6f7c46f --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,7 @@ +import pytest +import sentry_sdk + + +@pytest.fixture(scope="session", autouse=True) +def sentry_init(): + sentry_sdk.init(dsn=None, enable_tracing=False) diff --git a/tests/test_sentry_monitoring.py b/tests/test_sentry_monitoring.py index 5e15cf8..f1a158e 100644 --- a/tests/test_sentry_monitoring.py +++ b/tests/test_sentry_monitoring.py @@ -5,31 +5,20 @@ from psrt_ghsa_bot import _sentry_monitoring as sm from psrt_ghsa_bot import app -DSN = "https://public@o0.ingest.sentry.io/0" - -def test_noop_without_dsn(monkeypatch) -> None: - monkeypatch.delenv("SENTRY_DSN", raising=False) - with mock.patch.object(sm.sentry_sdk, "init") as init: - sm.init_sentry() - init.assert_not_called() - - with mock.patch.object(sm.crons, "capture_checkin") as capture: - result = sm.capture_checkin(sm.MONITOR_SLUG_GHSA, sm.STATUS_OK) - assert result is None - capture.assert_not_called() - - -def test_init_with_dsn(monkeypatch) -> None: - monkeypatch.setenv("SENTRY_DSN", DSN) +@pytest.mark.parametrize("dsn", [None, "https://public@o0.ingest.sentry.io/0"]) +def test_init_with_dsn(monkeypatch, dsn) -> None: + if dsn: + monkeypatch.setenv("SENTRY_DSN", dsn) + else: + monkeypatch.delenv("SENTRY_DSN", raising=False) with mock.patch.object(sm.sentry_sdk, "init") as init: sm.init_sentry() init.assert_called_once() - assert init.call_args.kwargs["dsn"] == DSN + assert init.call_args.kwargs["dsn"] == dsn -def test_capture_checkin_with_dsn(monkeypatch) -> None: - monkeypatch.setenv("SENTRY_DSN", DSN) +def test_capture_checkin(monkeypatch) -> None: with mock.patch.object(sm.crons, "capture_checkin", return_value="stanstan") as capture: result = sm.capture_checkin(sm.MONITOR_SLUG_GHSA, sm.STATUS_OK, duration=1.5, check_in_id="stanstan") assert result == "stanstan" @@ -42,7 +31,6 @@ def test_capture_checkin_with_dsn(monkeypatch) -> None: def test_capture_checkin_swallow_sdk_errors(monkeypatch) -> None: - monkeypatch.setenv("SENTRY_DSN", DSN) with mock.patch.object(sm.crons, "capture_checkin", side_effect=AttributeError): assert sm.capture_checkin(sm.MONITOR_SLUG_GHSA, sm.STATUS_OK) is None From 1c074df17fb1be2f497b641d9cd828acfc85a89e Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Fri, 13 Nov 2026 11:38:31 -0500 Subject: [PATCH 2/4] gh-75: Log HTTP errors unconditionally We no longer need to check for Sentry initialization since we do it unconditionally. But this does break the existing HTTPError test, so add a more specialized one. --- src/psrt_ghsa_bot/app.py | 13 ++++++------- tests/test_app.py | 18 +++++++++++++++++- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/psrt_ghsa_bot/app.py b/src/psrt_ghsa_bot/app.py index 4d711bf..a8d34fe 100644 --- a/src/psrt_ghsa_bot/app.py +++ b/src/psrt_ghsa_bot/app.py @@ -207,13 +207,12 @@ def iter_cve_refs_with_tags(cve_api: CveApi, cve_id: str, tags: set[str]) -> abc try: cve = cve_api.show_cve_record(cve_id) except HTTPError as err: - if sentry_sdk.is_initialized(): - sentry_sdk.logger.warning( - "{cve_id} lookup returned {status_code} {reason}", - cve_id=cve_id, - status_code=err.response.status_code, - reason=err.response.reason, - ) + sentry_sdk.logger.warning( + "{cve_id} lookup returned {status_code} {reason}", + cve_id=cve_id, + status_code=err.response.status_code, + reason=err.response.reason, + ) return except RequestException: sentry_sdk.capture_exception() diff --git a/tests/test_app.py b/tests/test_app.py index 067ba0c..49ba4e3 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -5,6 +5,7 @@ from unittest import mock import pytest +import requests import requests.exceptions as req_exc from psrt_ghsa_bot import app @@ -495,7 +496,6 @@ def test_only_draft_ghsa_closed_on_cve(show_cve_response, state) -> None: "errcls", [ req_exc.RequestException, - req_exc.HTTPError, req_exc.ConnectionError, req_exc.ConnectTimeout, req_exc.ReadTimeout, @@ -506,3 +506,19 @@ def test_show_cve_errors_ignored(errcls, cve_id): cve_api.show_cve_record.side_effect = errcls("test error") assert list(app.iter_cve_refs_with_tags(cve_api, cve_id, {"patch"})) == [] cve_api.show_cve_record.assert_called() + + +@pytest.mark.parametrize("code", ["bad_request", "internal_server_error"]) +def test_show_cve_bad_responses_logged(code, cve_id): + response = requests.Response() + response.status_code = requests.codes[code] + response.reason = code.replace("_", " ").title() + cve_api = mock.Mock() + cve_api.show_cve_record.side_effect = req_exc.HTTPError(response=response) + with mock.patch("sentry_sdk.logger.warning") as logger: + assert list(app.iter_cve_refs_with_tags(cve_api, cve_id, {"patch"})) == [] + cve_api.show_cve_record.assert_called() + assert logger.call_args is not None + log_kwargs = logger.call_args.kwargs + assert log_kwargs.get("status_code") == response.status_code + assert log_kwargs.get("reason") == response.reason From 81c771aa1d4fd08ca4c97e82b7712eaab1490d43 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Fri, 13 Nov 2026 11:57:04 -0500 Subject: [PATCH 3/4] gh-75: Manage cron checkins with a context manager This keeps all the Sentry reporting code in the dedicated module and lets us DRY up some of the logic. --- src/psrt_ghsa_bot/_sentry_monitoring.py | 33 +++++++++++++++------ src/psrt_ghsa_bot/app.py | 20 ++----------- tests/test_sentry_monitoring.py | 38 +++++++++++-------------- 3 files changed, 44 insertions(+), 47 deletions(-) diff --git a/src/psrt_ghsa_bot/_sentry_monitoring.py b/src/psrt_ghsa_bot/_sentry_monitoring.py index b305295..433ee9c 100644 --- a/src/psrt_ghsa_bot/_sentry_monitoring.py +++ b/src/psrt_ghsa_bot/_sentry_monitoring.py @@ -1,6 +1,8 @@ """Sentry cron monitoring integration for the PSRT GHSA bot.""" +import contextlib import os +import time import sentry_sdk from sentry_sdk import crons @@ -24,14 +26,29 @@ def init_sentry() -> None: ) -def capture_checkin(monitor_slug, status, duration=None, check_in_id=None) -> None | str: - """Capture a Sentry cron check-in.""" +@contextlib.contextmanager +def cron(): + start_time = time.monotonic() try: - return crons.capture_checkin( - monitor_slug=monitor_slug, - status=status, - duration=duration, - check_in_id=check_in_id, + check_in_id = crons.capture_checkin( + monitor_slug=MONITOR_SLUG_GHSA, + status=STATUS_IN_PROGRESS, ) except ImportError, AttributeError: - return None + check_in_id = None + try: + yield + except Exception: + sentry_sdk.capture_exception() + status = STATUS_ERROR + raise + else: + status = STATUS_OK + finally: + if check_in_id is not None: + crons.capture_checkin( + monitor_slug=MONITOR_SLUG_GHSA, + status=status, + duration=time.monotonic() - start_time, + check_in_id=check_in_id, + ) diff --git a/src/psrt_ghsa_bot/app.py b/src/psrt_ghsa_bot/app.py index a8d34fe..e91eade 100644 --- a/src/psrt_ghsa_bot/app.py +++ b/src/psrt_ghsa_bot/app.py @@ -5,7 +5,6 @@ import json import os import re -import time import typing import urllib.parse from collections import abc @@ -18,14 +17,7 @@ from githubkit.exception import RequestFailed, RequestError from requests.exceptions import HTTPError, RequestException -from psrt_ghsa_bot._sentry_monitoring import ( - MONITOR_SLUG_GHSA, - STATUS_ERROR, - STATUS_IN_PROGRESS, - STATUS_OK, - capture_checkin, - init_sentry, -) +from psrt_ghsa_bot._sentry_monitoring import cron, init_sentry load_dotenv() @@ -375,16 +367,8 @@ def run() -> None: def main() -> None: # Report the cron run to Sentry so we're alerted if it fails or stops running. init_sentry() - check_in_id = capture_checkin(MONITOR_SLUG_GHSA, STATUS_IN_PROGRESS) - start_time = time.monotonic() - try: + with cron(): run() - except Exception: - capture_checkin( - MONITOR_SLUG_GHSA, STATUS_ERROR, duration=time.monotonic() - start_time, check_in_id=check_in_id - ) - raise - capture_checkin(MONITOR_SLUG_GHSA, STATUS_OK, duration=time.monotonic() - start_time, check_in_id=check_in_id) if __name__ == "__main__": diff --git a/tests/test_sentry_monitoring.py b/tests/test_sentry_monitoring.py index f1a158e..c0bdb05 100644 --- a/tests/test_sentry_monitoring.py +++ b/tests/test_sentry_monitoring.py @@ -18,34 +18,17 @@ def test_init_with_dsn(monkeypatch, dsn) -> None: assert init.call_args.kwargs["dsn"] == dsn -def test_capture_checkin(monkeypatch) -> None: - with mock.patch.object(sm.crons, "capture_checkin", return_value="stanstan") as capture: - result = sm.capture_checkin(sm.MONITOR_SLUG_GHSA, sm.STATUS_OK, duration=1.5, check_in_id="stanstan") - assert result == "stanstan" - capture.assert_called_once_with( - monitor_slug=sm.MONITOR_SLUG_GHSA, - status=sm.STATUS_OK, - duration=1.5, - check_in_id="stanstan", - ) - - -def test_capture_checkin_swallow_sdk_errors(monkeypatch) -> None: - with mock.patch.object(sm.crons, "capture_checkin", side_effect=AttributeError): - assert sm.capture_checkin(sm.MONITOR_SLUG_GHSA, sm.STATUS_OK) is None - - def test_main_reports_ok_on_success() -> None: with ( mock.patch("psrt_ghsa_bot.app.init_sentry") as init, mock.patch("psrt_ghsa_bot.app.run") as run, - mock.patch("psrt_ghsa_bot.app.capture_checkin", return_value="cid") as capture, + mock.patch("sentry_sdk.crons.capture_checkin", return_value="cid") as capture, ): app.main() init.assert_called_once() run.assert_called_once() - assert [c.args[1] for c in capture.call_args_list] == [app.STATUS_IN_PROGRESS, app.STATUS_OK] + assert [c.kwargs["status"] for c in capture.call_args_list] == [sm.STATUS_IN_PROGRESS, sm.STATUS_OK] assert capture.call_args_list[-1].kwargs["check_in_id"] == "cid" @@ -53,10 +36,23 @@ def test_main_reports_error_and_reraises() -> None: with ( mock.patch("psrt_ghsa_bot.app.init_sentry"), mock.patch("psrt_ghsa_bot.app.run", side_effect=RuntimeError("uh oh")), - mock.patch("psrt_ghsa_bot.app.capture_checkin", return_value="cid") as capture, + mock.patch("sentry_sdk.crons.capture_checkin", return_value="cid") as capture, ): with pytest.raises(RuntimeError, match="uh oh"): app.main() - assert [c.args[1] for c in capture.call_args_list] == [app.STATUS_IN_PROGRESS, app.STATUS_ERROR] + assert [c.kwargs["status"] for c in capture.call_args_list] == [sm.STATUS_IN_PROGRESS, sm.STATUS_ERROR] assert capture.call_args_list[-1].kwargs["check_in_id"] == "cid" + + +def test_main_runs_on_checkin_error() -> None: + with ( + mock.patch("psrt_ghsa_bot.app.init_sentry") as init, + mock.patch("psrt_ghsa_bot.app.run") as run, + mock.patch("sentry_sdk.crons.capture_checkin", side_effect=AttributeError) as capture, + ): + app.main() + + init.assert_called_once() + run.assert_called_once() + capture.assert_called_once() From 74bcd385d97bf9dbe9dd4fec257cde3f41315cf2 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Mon, 16 Nov 2026 16:07:36 -0500 Subject: [PATCH 4/4] gh-75: Use .example domain for test DSN Ensure the SDK does not contact a real server if the patch fails. --- tests/test_sentry_monitoring.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_sentry_monitoring.py b/tests/test_sentry_monitoring.py index c0bdb05..8fbe294 100644 --- a/tests/test_sentry_monitoring.py +++ b/tests/test_sentry_monitoring.py @@ -6,7 +6,7 @@ from psrt_ghsa_bot import app -@pytest.mark.parametrize("dsn", [None, "https://public@o0.ingest.sentry.io/0"]) +@pytest.mark.parametrize("dsn", [None, "https://public@o0.ingest.sentry.example/0"]) def test_init_with_dsn(monkeypatch, dsn) -> None: if dsn: monkeypatch.setenv("SENTRY_DSN", dsn)