Search code examples
azure-devopsazure-boards

Automatically close a resolved work item after some time without any activity


We have a process for handling issues where the developer moves the work item to the "Resolved" state when he thinks he has finished his work. Then it is up to the person who reported that issue to check if the resolution meets his needs and move it to the "Closed" state. However people often don't bother moving the work items to the "Closed" state, so a lot of work items are piling up in the "Resolved" state.

Is it possible to automatically move work items from one state to another after an inactivity timeout (like 1 week or so)?


Solution

  • There is no built-in feature in Azure DevOps to automatically move work items from one state to another after an inactivity timeout.

    You could use below workaround:

    1. Get the work items via rest api Wiql - Query By Wiql, which changedate before 1 week.

    2. Update the work items state to closed via rest api Work Items - Update.

    my powershell script for your reference:

    # Define the variables
    $organization = "orgname"
    $project = "projectname"
    $token = "PAT"
    $baseurl = "https://dev.azure.com/$organization/$project"
    #$today = Get-Date 
    #$weekago = $today.AddDays(-7)
    #$weekagostr = $weekago.ToString("yyyy-MM-dd")
    
    # Encode the PAT token
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$token"))
    
    # Define the WIQL query
    $wiql = @{
        query = "SELECT [System.Id], [System.AssignedTo], [System.State], [System.Title] FROM workitems WHERE [System.TeamProject] = '$project' AND [System.State] = 'Resolved' AND [System.ChangedDate] < @today-7"
    } | ConvertTo-Json
    
    # Post the WIQL query to the REST API
    $wiqlurl = "$baseurl/_apis/wit/wiql?api-version=6.0"
    $wiqlresponse = Invoke-RestMethod -Uri $wiqlurl -Method Post -Body $wiql -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
    
    echo $wiqlresponse | ConvertTo-Json
    
    # Loop through each work item
    foreach ($workitem in $wiqlresponse.workitems) {
        # Get the work item id
        $id = $workitem.id
    
        # Update the state to "Closed"
        $updateurl = "https://dev.azure.com/$organization/_apis/wit/workitems/$id"+"?api-version=7.0"
        $body = '[{"op": "add", "path": "/fields/System.State", "value": "Closed"}]'
        $changestate = Invoke-RestMethod -Uri $updateurl -Method Patch -Body $body -ContentType "application/json-patch+json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
        Write-Output "Work item $id updated to Closed"
    }
    

    enter image description here

    You can run the script on a scheduled basis, for example, use devops pipeline with schedule trigger to run every week, or other schedule trigger tool like Azure Logic Apps, it's up to you.

    Edited: change to use @today-7 for System.ChangedDate value.