azure-devopsnotifications

Azure DevOps notifications - email is being sent when it sits too long in one state


I would like to set up a notification that an email is being sent, if an item sits too long (more than 2 days) in one particular state. How can I do that in Notification feature?

I tried to set it up, but not sure what formula to use to indicate that number of days.


Solution

  • I would like to set up a notification that an email is being sent, if an item sits too long (more than 2 days) in one particular state. How can I do that in Notification feature?

    I'm afraid there's no out-of-box way to achieve this currently. The Notification only monitors on the work item change only.

    enter image description here

    The mostly nearby way is to check if there are any changes(not only state change) on these work items, send email notification of these work items in DevOps pipeline.

    Details below:

    1. we can try to list the work item from query like below for a check. To list user story with New state, and have no changes in 2 days on my side: enter image description here

    2. We can use rest api Wiql - Query By Wiql to achieve the same. enter image description here

    3. Hence, in devops pipeline, we can run the pipeline every 2 days with schedule trigger, send out these work items in email:

    Yaml sample below:

    trigger: none
    
    schedules:
    - cron: "0 0 */2 * *"
      displayName: Run every two days
      branches:
        include:
        - main
    
    pool:
      vmImage: Windows-latest
    
    steps:
    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          # Define your organization, project, and personal access token
          $organization = "orgname"
          $project = "projectname"
          $pat = "$(pat)"
      
          # Define the date 2 days ago
          $date = (Get-Date).AddDays(-2).ToString('yyyy-MM-dd')
      
      
          # Define the API endpoint
          $url = "https://dev.azure.com/$organization/$project/_apis/wit/wiql?api-version=6.0"
    
      
          # Define the query
          $query = @{
              query = "SELECT [System.Id] FROM workitems WHERE [System.TeamProject] = '$project' AND [System.WorkItemType] = 'User Story' AND [System.ChangedDate] < '$date' AND [System.State] = 'New'"
          } | ConvertTo-Json
      
          # Convert the PAT to a Base64 string
          $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($pat)"))
      
          # Make the API request
          $response = Invoke-RestMethod -Uri $url -Method Post -Body $query -ContentType "application/json" -Headers @{Authorization="Basic $base64AuthInfo"}
    
          # Output the response
          $response.workItems.id
    
          # Get the work item IDs
          $workItemIDs = $response.workItems.id
    
          # Convert the work item IDs to a string
          $workItemIDString = $workItemIDs -join ", "
    
          Write-Host "##vso[task.setvariable variable=WITlist;]$workItemIDString"
    
    - task: SendEmail@1
      inputs:
        To: 'zwxxxx5@hxxil.com'
        From: 'zwxxx@hoxxxxl.com'
        Subject: 'The work item not changed in 2 days'
        Body: |
          This is the work items not changed in 2 days for "New" state "User stories": 
          $(WITlist)
        BodyAsHtml: false
        AddAttachment: false
        SmtpServer: 'smtp-mail.outlook.com'
        SmtpUsername: 'zwxxxx4@hotmail.com'
        SmtpPassword: '$(pwd)'
    

    I can receive the work item list: enter image description here

    enter image description here