Skip to content

Extract admin page helpers from app.main#372

Merged
ramimbo merged 1 commit into
ramimbo:mainfrom
TUPM96:codex/bounty-320-admin-page-helpers
May 26, 2026
Merged

Extract admin page helpers from app.main#372
ramimbo merged 1 commit into
ramimbo:mainfrom
TUPM96:codex/bounty-320-admin-page-helpers

Conversation

@TUPM96
Copy link
Copy Markdown
Contributor

@TUPM96 TUPM96 commented May 26, 2026

Refs #320

Summary

  • Move admin page webhook dashboard context assembly into app.admin.admin_page_context.
  • Move admin form bounty creation into app.admin.create_admin_bounty_from_form, leaving the route focused on auth, CSRF, and redirect behavior.
  • Add direct admin helper coverage for filtered webhook dashboard context and admin bounty form creation.
  • Preserve the existing OAuth encoded-backslash next-path safety expectation required by the test suite.

Complexity reduced

  • app.main no longer owns admin page query composition, webhook summary context, limit options, or admin form bounty construction. Admin route handlers now mostly wire dependencies and render/redirect, while admin-specific data shaping is testable in app.admin.

Tests

  • python -m pytest tests\test_admin_helpers.py tests\test_wallet_api.py::test_github_login_stores_safe_default_for_backslash_next -q
  • python -m pytest -q
  • python -m ruff check .
  • git diff --check

Copilot AI review requested due to automatic review settings May 26, 2026 02:25
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 26, 2026

Warning

Review limit reached

@TUPM96, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 4 minutes and 13 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: d53f9596-95e5-4ea2-a907-cc1fa5c43c6c

📥 Commits

Reviewing files that changed from the base of the PR and between ac99d79 and 9dea484.

📒 Files selected for processing (3)
  • app/admin.py
  • app/main.py
  • tests/test_admin_helpers.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Refactors admin-page logic into reusable helper functions and expands tests around admin context building and bounty creation, while hardening redirect safety checks.

Changes:

  • Centralized admin template context generation in admin_page_context.
  • Added create_admin_bounty_from_form helper and updated admin POST handler to use it.
  • Hardened _safe_next_path by validating decoded redirect targets.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
tests/test_admin_helpers.py Adds coverage for new admin helpers (admin_page_context, create_admin_bounty_from_form).
app/main.py Uses admin_page_context/create_admin_bounty_from_form and strengthens _safe_next_path redirect validation.
app/admin.py Introduces shared admin helpers and a shared webhook-limit options constant.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread app/main.py
Comment on lines 280 to 294
def _safe_next_path(next_path: str | None) -> str:
decoded_next_path = unquote(next_path) if next_path else ""
if (
not next_path
or not next_path.startswith("/")
or next_path.startswith("//")
or len(next_path) > 2048
or "\\" in next_path
or decoded_next_path.startswith("//")
or "\\" in decoded_next_path
or any(ord(char) < 32 or 127 <= ord(char) < 160 for char in next_path)
or any(ord(char) < 32 or 127 <= ord(char) < 160 for char in decoded_next_path)
):
return "/me"
return next_path
Comment thread app/admin.py
from app.ledger.service import create_bounty
from app.models import WebhookEvent

ADMIN_WEBHOOK_LIMIT_OPTIONS = [10, 25, 50, 100]
Copy link
Copy Markdown
Contributor

@tolga-tom-nook tolga-tom-nook left a comment

Choose a reason for hiding this comment

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

No blockers from my review. I inspected app/admin.py, app/main.py, and tests/test_admin_helpers.py on head 9dea4845.

What I checked:

  • admin_page_context() preserves the existing /admin template context keys, normalizes the webhook status filter before rendering, keeps the same event ordering/summary helpers, and keeps the CSRF token supplied by the route instead of creating a new auth path.
  • create_admin_bounty_from_form() is a thin wrapper around create_bounty(), returns the created id, and leaves LedgerError handling in the route unchanged.
  • The OAuth next hardening rejects decoded protocol-relative paths and decoded backslashes/control characters while still returning the original relative path, which is consistent with validating the submitted redirect target before RedirectResponse receives it.

Validation run locally on this PR head:

  • ./.venv/bin/python -m pytest tests/test_admin_helpers.py tests/test_security.py -q -> 58 passed
  • ./.venv/bin/python -m pytest -q -> 337 passed
  • ./.venv/bin/python -m ruff check app/admin.py app/main.py tests/test_admin_helpers.py -> passed
  • ./.venv/bin/python -m ruff format --check app/admin.py app/main.py tests/test_admin_helpers.py -> passed
  • ./.venv/bin/python -m mypy app -> passed
  • git diff --check origin/main...HEAD -> passed

Copy link
Copy Markdown
Contributor

@ayskobtw-lil ayskobtw-lil left a comment

Choose a reason for hiding this comment

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

No blockers from my review on current head 9dea484.

Evidence checked:

  • Inspected app/admin.py, the admin route wiring in app/main.py, and tests/test_admin_helpers.py.
  • Verified admin_page_context() preserves the /admin template context shape: login, CSRF token supplied by the route, normalized webhook status, event list, status summary, selected limit, and the existing limit options.
  • Verified create_admin_bounty_from_form() remains a thin wrapper over create_bounty(), returns the created id, keeps repo canonicalization/max-awards behavior from the ledger service, and leaves LedgerError handling in the route.
  • Rechecked the route-level admin page and CSRF-backed admin bounty form paths, not just the new helper unit tests.
  • Verified the decoded next hardening still rejects encoded redirect ambiguity/backslash cases through existing OAuth regressions.
  • Hosted Quality/readiness/docs/image check is green.

Validation run locally:

  • python -m pytest tests/test_admin_helpers.py tests/test_wallet_api.py::test_github_login_stores_safe_default_for_backslash_next -q -> 6 passed.
  • python -m pytest tests/test_security.py::test_admin_page_renders_safe_webhook_events_for_cookie_admin tests/test_security.py::test_admin_bounty_form_requires_csrf_for_cookie_auth tests/test_admin_helpers.py -q -> 7 passed.
  • python -m pytest tests/test_wallet_api.py::test_oauth_next_path_rejects_redirect_ambiguity tests/test_wallet_api.py::test_github_login_stores_safe_default_for_backslash_next -q -> 4 passed.
  • python -m ruff check app/admin.py app/main.py tests/test_admin_helpers.py -> passed.
  • python -m ruff format --check app/admin.py app/main.py tests/test_admin_helpers.py -> 3 files already formatted.
  • python -m mypy app/admin.py app/main.py -> success.
  • git diff --check origin/main...HEAD -> clean.

Residual risk is low and mostly around existing admin/page behavior; this PR moves admin-specific data shaping into a smaller module and keeps the route as auth/CSRF/render wiring.

@ramimbo ramimbo added the mrwk:needs-info More information needed label May 26, 2026
@ramimbo
Copy link
Copy Markdown
Owner

ramimbo commented May 26, 2026

Maintainer note after #320 filled: this PR still references closed bounty #320. If you want it considered for the current code-health round, rebase against main, update the bounty reference to #377, and keep the scope distinct from already accepted #328, #337, #364, #375, and #376. This is not accepted or paid as-is.

@ramimbo ramimbo merged commit 537fca2 into ramimbo:main May 26, 2026
2 checks passed
@ramimbo ramimbo added mrwk:accepted Maintainer accepted for payout mrwk:paid Ledger payment recorded and removed mrwk:needs-info More information needed labels May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mrwk:accepted Maintainer accepted for payout mrwk:paid Ledger payment recorded

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants