Skip to content

refactor: extract router helpers into utils.py (closes #582)#945

Open
ScarsAndSource wants to merge 1 commit into
imDarshanGK:mainfrom
ScarsAndSource:refactor/router-utils-issue-582
Open

refactor: extract router helpers into utils.py (closes #582)#945
ScarsAndSource wants to merge 1 commit into
imDarshanGK:mainfrom
ScarsAndSource:refactor/router-utils-issue-582

Conversation

@ScarsAndSource
Copy link
Copy Markdown

Description

Identified and extracted two repeated code patterns from the analysis routers into a new backend/app/routers/utils.py module:

Duplicate 1 — 13-line issue-count + payload block existed identically in debugging.py and analyze.py/_stream_analysis:

  • Both files independently counted errors/warnings/infos by severity
  • Both built the same summary string
  • Both assembled the same response dict

Duplicate 2 — detect_language(req.code, req.language) repeated in every analysis router (debugging.py, explanation.py, suggestions.py)

Files Changed

Created:

  • backend/app/routers/utils.py — new utilities module with two helpers:
    • build_debugging_payload(issues, code=None) — centralises severity counting, summary formatting, and payload assembly
    • resolve_language(req) — wraps detect_language(req.code, req.language) used across all routers
  • backend/tests/test_router_utils.py — 10 unit tests covering all branches of both helpers

Replaced:

  • backend/app/routers/debugging.py — removed 10 inline lines; now calls resolve_language() and build_debugging_payload()
  • backend/app/routers/analyze.py — removed 13-line duplicate block inside _stream_analysis; now calls build_debugging_payload()
  • backend/app/routers/explanation.py — swapped detect_language(req.code, req.language)resolve_language(req)
  • backend/app/routers/suggestions.py — swapped detect_language(req.code, req.language)resolve_language(req)

No behaviour changes: all existing tests pass.

Related Issue

Fixes #582

Type of change

  • Bug fix
  • New feature / enhancement
  • Documentation update
  • Test addition
  • Refactor

Checklist

  • I have read CONTRIBUTING.md
  • My branch is up to date with main
  • I have run pytest -v and all tests pass
  • I have not introduced duplicate issues or features
  • My PR title follows the format: feat/fix/docs/test: short description
  • I have added tests for new features (Level 2 and 3 issues)
  • No hardcoded secrets or API keys in my code
  • This PR is linked to a GSSoC 2026 issue

Screenshots (if frontend change)

Test evidence

pytest -v
# paste output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reduce duplicate code in routers with helper functions

1 participant