Skip to content

Extract repeated _truncate() implementations into a shared app/utils/truncation.py helper #1056

@udit-rawat

Description

@udit-rawat

Why This Matters

Six modules independently define a private _truncate() function with inconsistent implementations, different ellipsis characters, and undocumented magic limits. Adding a new delivery channel or integration means writing yet another truncation function from scratch.

This is the same class of duplication fixed in #869 for the GitLab write-back flow.

Current State

File Limit Ellipsis Signature
app/utils/discord_delivery.py:83 configurable _truncate(text, limit)
app/utils/telegram_delivery.py:47 configurable _truncate(text, limit)
app/integrations/azure_sql.py:177 500 (_QUERY_TRUNCATE_LEN) ... _truncate(text, max_len)
app/integrations/mysql.py:203 500 (_QUERY_TRUNCATE_LEN) ... _truncate(text, max_len)
app/integrations/mariadb.py:154 200 (_QUERY_TRUNCATE_LEN) ... _truncate(text, max_len)
app/nodes/publish_findings/gitlab_writeback.py:14 4000 (inline) ... inlined, no function

discord_delivery.py and telegram_delivery.py use the Unicode ellipsis ; everything else uses "...". mariadb.py also has a second ad-hoc truncation at line 294 using "\n... (truncated)".

Proposed Fix

  1. Add app/utils/truncation.py with one shared helper:
def truncate(text: str, limit: int, ellipsis: str = "...") -> str:
    if len(text) <= limit:
        return text
    return text[: limit - len(ellipsis)] + ellipsis
  1. Migrate all six call sites to use it.
  2. Keep each module's limit constant where it is — only the function body moves.

Tests / Coverage

  • one parametrised test: text under limit → unchanged, text over limit → truncated with correct ellipsis
  • one test for Unicode ellipsis variant
  • existing tests for all six modules must stay green

Acceptance Criteria

  • _truncate is defined in exactly one place
  • ellipsis character is consistent or explicitly configurable per call site
  • no behaviour change for any existing caller
  • scope limited to the six files listed above

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions