ewingjm / development-hub

A continuous integration solution for Power Apps.
MIT License
35 stars 8 forks source link

Extract Pipeline sometimes fails to link work item #77

Open tdashworth opened 3 years ago

tdashworth commented 3 years ago

Description Occasionally, the merge pipeline fails to link the related work item. This is not a massive issue as the commit is already made and the link can be made manually. I'm not sure why this is occurring as I don't know what changes happen in between what timeframe.

Linking pull request to work item 31796
Invoke-RestMethod : {"$id":"1","innerException":null,"message":"TF26071: This work item has been changed by someone 
else since you opened it.  You will need to refresh it and discard your 
changes.","typeName":"Microsoft.TeamFoundation.WorkItemTracking.Server.WorkItemRevisionMismatchException, Microsoft.Tea
mFoundation.WorkItemTracking.Server","typeKey":"WorkItemRevisionMismatchException","errorCode":600122,"eventId":3200}
At D:\a\1\s\pipelines\scripts\Merge-SolutionMerge.ps1:211 char:17
+       $result = Invoke-RestMethod `
+                 ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc 
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Steps to reproduce Follow the standard process for approving a merge. This merge did have manual activities and we are using the PR process.

Expected behaviour The work item to be linked and for no error to occur.

ewingjm commented 3 years ago

Hi @tdashworth. We were getting this a lot as well. In the end I added a retry to the Merge-SolutionMerge.ps1 script and it resolved it. I need to feed that change back into the sample here. Never got to the bottom of what's going on. Perhaps Azure DevOps is automatically linking the work item to the extract pipeline at the same time as we're trying to link the work item to the pull request?

tdashworth commented 3 years ago

I've spent some time this evening exploring this and my guess is when committing with a work item, ADO will auto-link it which might be the conflicting change.

Could you share your updated version if it's working well? 😄

ewingjm commented 3 years ago

Hi, @tdashworth. Sorry I've been slow to respond to this. I'm sure you've sorted something out yourself by now. This is what we've got. It's a little agricultural, but it works -

    if ($solutionMerge.devhub_Issue.devhub_azuredevopsworkitemid) {
      Write-Host "Linking pull request to work item $($solutionMerge.devhub_Issue.devhub_azuredevopsworkitemid)"

      $success = $false
      $attempt = 1
      while (!$success) {
        try {
          $result = Invoke-RestMethod `
            -Uri "https://dev.azure.com/$org/$project/_apis/wit/workItems/$($solutionMerge.devhub_Issue.devhub_azuredevopsworkitemid)?api-version=4.0-preview" `
            -Headers @{ 'authorization' = "Bearer $env:SYSTEM_ACCESSTOKEN"; 'content-type' = 'application/json-patch+json' } `
            -Method PATCH `
            -Body (ConvertTo-Json -Depth 100 @(
              @{
                op    = 'add';
                path  = '/relations/-';
                value = 
                @{
                  rel        = "ArtifactLink";
                  url        = $($result.artifactId)
                  attributes = @{
                    name = "Pull Request"
                  }
                }
              }
            )
          )
          $success = $true
        }
        catch {
          if ($attempt -lt 3) {
            Write-Warning "Failed to link pull request to work item on attempt $attempt of 3."
            Start-Sleep -Seconds 5
            $attempt++
          }
          else {
            throw
          }
        }
      }
    }
  }