Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract Pipeline sometimes fails to link work item #77

Open
tdashworth opened this issue May 12, 2021 · 3 comments
Open

Extract Pipeline sometimes fails to link work item #77

tdashworth opened this issue May 12, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@tdashworth
Copy link
Contributor

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.

@tdashworth tdashworth added the bug Something isn't working label May 12, 2021
@ewingjm
Copy link
Owner

ewingjm commented May 28, 2021

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
Copy link
Contributor Author

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
Copy link
Owner

ewingjm commented Aug 10, 2021

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
          }
        }
      }
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants