Skip to content

Enhanced Stop-Task with progress bar and array support - #17

Merged
LindnerBrewery merged 1 commit into
mainfrom
feature/stop-task-improvements
Jan 27, 2026
Merged

Enhanced Stop-Task with progress bar and array support#17
LindnerBrewery merged 1 commit into
mainfrom
feature/stop-task-improvements

Conversation

@LindnerBrewery

Copy link
Copy Markdown
Owner

Enhanced Stop-Task with Progress Bar and Array Support

Summary

Improved the Stop-Task function to support cancelling multiple tasks efficiently with better user feedback through progress indicators and resolved parameter binding issues when piping task objects.

Changes

🐛 Bug Fixes

  • Fixed parameter set ambiguity: Removed ValueFromPipeline from the Regarding parameter to resolve conflicts when piping TaskResource objects. Tasks can now be piped directly without parameter binding errors.

✨ Enhancements

  • Array support: Enhanced Task parameter to accept arrays of tasks using the new TaskTransformation class
  • Progress bar: Added visual progress indicator when cancelling multiple tasks, displaying:
    • Current task number and total count
    • Task description being cancelled
    • Percentage completion
  • Better pipeline support: Tasks can now be piped individually or as arrays from Get-Task

🔧 Technical Changes

  • Created TaskTransformation class in TransformerClasses.psm1 for handling task arrays
  • Refactored task collection to accumulate pipeline input in the begin block for accurate progress tracking
  • Updated CHANGELOG.md with all changes

Usage Examples

# Pipe multiple tasks (now shows progress bar)
Get-Task -TaskType Deploy | Where-Object State -eq Queued | Stop-Task

# Pass array of tasks directly
$tasks = Get-Task | Select-Object -First 10
Stop-Task -Task $tasks

# Still works with single tasks
Get-Task -TaskID "ServerTasks-12345" | Stop-Task

Copilot AI review requested due to automatic review settings January 22, 2026 16:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR enhances the Stop-Task function to support cancelling arrays of tasks with improved user feedback through progress indicators, and resolves parameter binding conflicts when piping task objects.

Changes:

  • Modified Task parameter to accept arrays using new TaskTransformation class and changed pipeline binding from ValueFromPipelineByPropertyName to ValueFromPipeline
  • Removed ValueFromPipeline from Regarding parameter to resolve parameter set ambiguity
  • Added progress bar functionality when cancelling multiple tasks
  • Refactored task collection logic to use begin/process/end blocks for better pipeline handling

Reviewed changes

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

File Description
OctopusDeploy/Public/Stop-Task.ps1 Updated parameter attributes, refactored to accumulate tasks across pipeline input, and added progress bar for user feedback
OctopusDeploy/Classes/TransformerClasses.psm1 Modified TaskSingleTransformation to throw on invalid input and added new TaskTransformation class for array support
CHANGELOG.md Documented the new features, changes, and bug fixes in the Unreleased section

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

return $item
}
return ($item)
throw "Invalid Task input: $($item.toString())"

Copilot AI Jan 22, 2026

Copy link

Choose a reason for hiding this comment

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

The TaskSingleTransformation class now throws an exception for string inputs that don't match the "ServerTasks-*" pattern, whereas the previous implementation returned $null. This is a breaking change that could cause errors in existing code that previously handled null returns gracefully. Consider whether this breaking change is intentional and necessary.

Copilot uses AI. Check for mistakes.
Comment on lines +283 to +285
elseif ($item -is [Octopus.Client.Model.TaskResource]) {
# Already a TaskResource, keep it
}

Copilot AI Jan 22, 2026

Copy link

Choose a reason for hiding this comment

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

When the item is already a TaskResource (line 283-285), it is not added to the $result array. The code should include $result += $item in the elseif block to ensure TaskResource objects are properly included in the output array.

Copilot uses AI. Check for mistakes.
Comment on lines 88 to 89
# Combine states into a regex pattern
$stateRegex = ($State -join '|') -replace ' ', ''

Copilot AI Jan 22, 2026

Copy link

Choose a reason for hiding this comment

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

The $stateRegex variable is recalculated on every iteration of the process block, even though it depends only on the $State parameter which doesn't change between iterations. This is inefficient when processing multiple piped items. Consider moving this calculation to the begin block to avoid redundant computation.

Copilot uses AI. Check for mistakes.
Comment on lines 85 to 86
# Initialize an empty array to store tasks to cancel
$tasksToCancel = @()

Copilot AI Jan 22, 2026

Copy link

Choose a reason for hiding this comment

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

The comment on line 85-86 states "Initialize an empty array to store tasks to cancel", but this initialization is redundant since $tasksToCancel is immediately reassigned in all code paths (lines 94, 99, 104) without ever using the empty array. This initialization should be removed as it serves no purpose and may cause confusion.

Copilot uses AI. Check for mistakes.
# Cancel tasks regarding a specific object
foreach ($r in $Regarding) {
$tasksToCancel += Get-Task -Regarding $r | Where-Object { $_.State -match $stateRegex }
$tasksToCancel = Get-Task -Regarding $r | Where-Object { $_.State -match $stateRegex }

Copilot AI Jan 22, 2026

Copy link

Choose a reason for hiding this comment

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

The assignment on line 99 overwrites $tasksToCancel on each iteration instead of accumulating results. When multiple $Regarding items are provided in the loop, only the tasks from the last item will be retained. This should use += to accumulate tasks across all iterations.

Suggested change
$tasksToCancel = Get-Task -Regarding $r | Where-Object { $_.State -match $stateRegex }
$tasksToCancel += Get-Task -Regarding $r | Where-Object { $_.State -match $stateRegex }

Copilot uses AI. Check for mistakes.
@LindnerBrewery
LindnerBrewery merged commit 2e2c7f0 into main Jan 27, 2026
12 checks passed
@LindnerBrewery
LindnerBrewery deleted the feature/stop-task-improvements branch January 27, 2026 15:02
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.

2 participants