Skip to content

refactor: reduce complexity of execute_impl in upload_workitem_attachment.rs - #1830

Draft
github-actions[bot] wants to merge 1 commit into
mainfrom
refactor/reduce-complexity-upload-workitem-attachment-9d04993d5c03a384
Draft

refactor: reduce complexity of execute_impl in upload_workitem_attachment.rs#1830
github-actions[bot] wants to merge 1 commit into
mainfrom
refactor/reduce-complexity-upload-workitem-attachment-9d04993d5c03a384

Conversation

@github-actions

@github-actions github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

What was complex

UploadWorkitemAttachmentResult::execute_impl was a 178-line function flagged by clippy::too_many_lines (178/100). It inlined five distinct phases in one body: extension-allowlist validation, symlink-escape-safe path resolution, file-size validation, ##vso[ command-injection scanning, the ADO attachment upload POST, and the work-item link PATCH — each with its own success/failure branching.

What changed

Extracted three focused helpers, keeping execute_impl as a thin orchestrator:

  • validate_and_read_file — extension allowlist, symlink-escape check, size limit, and ##vso[ injection scan, then reads the file bytes. Returns Result<Result<Vec<u8>, ExecutionResult>> so policy rejections surface as normal ExecutionResult::failure values rather than being conflated with I/O errors.
  • upload_attachment_bytes — POSTs the file to the ADO attachments endpoint and extracts the returned attachment URL.
  • link_attachment_to_work_item — PATCHes the AttachedFile relation onto the work item and builds the final success/failure ExecutionResult.

No behavior change: same validation order, same error messages, same success/failure payloads.

Before / after

  • Before: clippy::too_many_lines (178/100) on execute_impl.
  • After: warning cleared (verified via cargo clippy --all-targets --all-features -- -W clippy::cognitive_complexity).

Verification

  • cargo check --all-targets — clean
  • cargo test --bin ado-aw upload_workitem_attachment — all 19 tests pass (plus 2 related tests in execute::tests)
  • cargo clippy --all-targets --all-features -- -D warnings — no new warnings on the changed file

Generated by Cyclomatic Complexity Reducer · auto · 66.6 AIC · ⌖ 7.04 AIC · ⊞ 11K ·

…ment.rs

Extracts the 178-line execute_impl (clippy::too_many_lines 178/100) into
four focused helpers:
- validate_and_read_file: extension/size/symlink-escape/injection checks + read
- upload_attachment_bytes: POST upload to ADO attachments endpoint
- link_attachment_to_work_item: PATCH linking the attachment as a work item relation

execute_impl now just orchestrates these steps. No behaviour change; all
existing tests pass and the too_many_lines warning is cleared.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

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