Enhanced Stop-Task with progress bar and array support - #17
Conversation
There was a problem hiding this comment.
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
Taskparameter to accept arrays using newTaskTransformationclass and changed pipeline binding fromValueFromPipelineByPropertyNametoValueFromPipeline - Removed
ValueFromPipelinefromRegardingparameter 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())" |
There was a problem hiding this comment.
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.
| elseif ($item -is [Octopus.Client.Model.TaskResource]) { | ||
| # Already a TaskResource, keep it | ||
| } |
There was a problem hiding this comment.
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.
| # Combine states into a regex pattern | ||
| $stateRegex = ($State -join '|') -replace ' ', '' |
There was a problem hiding this comment.
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.
| # Initialize an empty array to store tasks to cancel | ||
| $tasksToCancel = @() |
There was a problem hiding this comment.
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.
| # 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 } |
There was a problem hiding this comment.
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.
| $tasksToCancel = Get-Task -Regarding $r | Where-Object { $_.State -match $stateRegex } | |
| $tasksToCancel += Get-Task -Regarding $r | Where-Object { $_.State -match $stateRegex } |
Enhanced Stop-Task with Progress Bar and Array Support
Summary
Improved the
Stop-Taskfunction 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
ValueFromPipelinefrom theRegardingparameter to resolve conflicts when pipingTaskResourceobjects. Tasks can now be piped directly without parameter binding errors.✨ Enhancements
Taskparameter to accept arrays of tasks using the newTaskTransformationclassGet-Task🔧 Technical Changes
TaskTransformationclass inTransformerClasses.psm1for handling task arraysbeginblock for accurate progress trackingUsage Examples