Skip to content

refactor: reduce complexity of resolve_reviewer_identity in create_pull_request.rs#1149

Draft
github-actions[bot] wants to merge 1 commit into
mainfrom
refactor/reduce-complexity-resolve-reviewer-identity-923298b1db79e557
Draft

refactor: reduce complexity of resolve_reviewer_identity in create_pull_request.rs#1149
github-actions[bot] wants to merge 1 commit into
mainfrom
refactor/reduce-complexity-resolve-reviewer-identity-923298b1db79e557

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

What was complex

resolve_reviewer_identity in src/safeoutputs/create_pull_request.rs had a cognitive complexity of 18 (threshold: 15). The complexity came from three layers stacked together:

  1. Three-condition GUID check inline in the function body (&&-chained boolean expressions)
  2. Doubly-nested match — outer on the HTTP send result, inner on JSON parsing
  3. Triple &&-let chain inside the success arm to navigate results[0].identities plus a for loop with its own &&-let condition and a separate fallback &&-let

What changed

Two focused helpers extracted:

fn is_reviewer_guid(s: &str) -> bool

Encapsulates the three-condition GUID check (length 36, exactly 4 hyphens, all hex/hyphen chars). Makes the call site read as intent, not implementation.

fn find_identity_in_response(data: &Value, reviewer: &str) -> Option<String>

All Identity Picker JSON navigation in one place:

  • Uses ? for early return when fields are missing (replaces the triple &&-let chain)
  • Contains the exact-match loop (by display name or mail)
  • Falls back to first result when no exact match

async fn resolve_reviewer_identity (simplified)

Now reads as four sequential steps:

  1. GUID fast-path → is_reviewer_guid
  2. Send HTTP request → early return on error
  3. Check status code → early return on non-success
  4. Parse JSON → find_identity_in_response; warn if None

Before / After complexity

Function Before After
resolve_reviewer_identity 18 < 15 (zero warnings)
find_identity_in_response — (new) < 15
is_reviewer_guid — (new) < 5

Verification

  • cargo build — clean
  • cargo test — all tests pass
  • cargo clippy --all-targets --all-features — clean (fixed one collapsible-if suggestion in the extracted helper)
  • cargo clippy -W clippy::cognitive_complexity with threshold=15 — zero warnings for create_pull_request.rs

Warning

Firewall blocked 2 domains

The following domains were blocked by the firewall during workflow execution:

  • spsprodeus21.vssps.visualstudio.com
  • spsprodweu4.vssps.visualstudio.com

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "spsprodeus21.vssps.visualstudio.com"
    - "spsprodweu4.vssps.visualstudio.com"

See Network Configuration for more information.

Generated by Cyclomatic Complexity Reducer · 758.5 AIC · ⌖ 19.7 AIC · ⊞ 35.9K ·

…ll_request.rs

Extract is_reviewer_guid() and find_identity_in_response() from the
monolithic resolve_reviewer_identity() function (cognitive complexity
18 → below threshold).

- is_reviewer_guid: encapsulates the 3-condition GUID check that was
  inline in the function body
- find_identity_in_response: extracts all Identity Picker JSON navigation
  (triple &&-let chain, exact-match loop, first-result fallback) into a
  standalone helper that uses ? for early return; caller just calls the
  helper and emits a single warning on None
- resolve_reviewer_identity: flattened with an early-return on HTTP error
  and a separate early-return on non-success status, removing one level
  of match nesting

All tests pass; cargo clippy is clean.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

0 participants