在VSTS中,拉取请求批准后能否指定关联工作项为"ready for testing"状态?
Automating Work Item State Updates on PR Approval in VSTS
Absolutely, you can pull this off in VSTS (now often referred to as Azure DevOps) — I’ve set up similar automation for teams before, and there are a couple of solid ways to make it happen. Here’s how to auto-update linked work items to a ready for testing state once a PR gets approved:
Method 1: Built-in Service Hooks + REST API Script
This is the most flexible option if you want full control over the workflow:
- Head to your VSTS project settings, then find Service Hooks under the "General" section.
- Create a new service hook subscription:
- Pick Pull Request as the trigger service.
- Choose the Pull request updated event, then set filters so it only fires when the PR’s status changes to Approved. You can even refine this to require a specific number of approvals if your team uses multi-approval rules.
- Select the action to run: Go with Run a PowerShell script (using a self-hosted agent) or Invoke Azure Function if you prefer serverless execution.
- Write a script to update linked work items:
You’ll use the Azure DevOps REST API to fetch linked work items and update their state. Here’s a PowerShell snippet you can adapt:# Configure your VSTS details and PAT (Personal Access Token) $orgUrl = "https://dev.azure.com/your-organization" $projName = "your-project-name" $pat = "your-personal-access-token" # Needs Work Items (Read/Write) and Pull Requests (Read) permissions $authHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat"))} # Pull PR ID from the service hook payload (passed as an environment variable) $prId = $env:PR_ID $repoId = "your-repository-id" # Find this in your repo settings # Get all work items linked to the approved PR $workItemsUrl = "$orgUrl/$projName/_apis/git/repositories/$repoId/pullRequests/$prId/workitems?api-version=7.1-preview.1" $linkedWorkItems = Invoke-RestMethod -Uri $workItemsUrl -Headers $authHeader -Method Get # Update each work item's state to "ready for testing" foreach ($wi in $linkedWorkItems.value) { $wiId = $wi.id $updateUrl = "$orgUrl/$projName/_apis/wit/workitems/$wiId?api-version=7.1-preview.3" $updateBody = @( @{ op = "replace" path = "/fields/System.State" value = "ready for testing" } ) | ConvertTo-Json Invoke-RestMethod -Uri $updateUrl -Headers $authHeader -Method Patch -Body $updateBody -ContentType "application/json-patch+json" }- Don’t forget to replace placeholder values (like
your-organizationandyour-repository-id) with your actual project details.
- Don’t forget to replace placeholder values (like
Method 2: Use a Pre-built Marketplace Extension (No Code)
If writing scripts isn’t your thing, you can use a pre-made extension from the Azure DevOps Marketplace. Tools like Pull Request Work Item Updater let you set up rules directly in your PR settings to auto-update linked work items when specific events (like approval) happen. Just:
- Search the marketplace for PR/work item automation extensions, install one that fits your needs.
- Configure the rule to trigger when a PR is approved, and set the target state to ready for testing.
Key Notes to Remember
- Make sure your work item type (e.g., User Story, Task) actually has a ready for testing state defined in its workflow. If not, you’ll need to edit the process template first to add it.
- Double-check your service hook filters to ensure the automation only runs when the PR is fully approved (not just when a single approval is added, if your team requires multiple sign-offs).
内容的提问来源于stack exchange,提问作者Troy
相关产品推荐
相关产品推荐

