0

I am trying to use the Git-Cherry API, in power shell to automate the process of cherry-picking a PR by its PRid.

$Body = @{
"generatedRefName" = "refs/test";
"ontoRefName" = $BranhcName;
"repository" = $RepoName;
"source" = $PRid #Prid is an integer value
}
#Cherr-Pick: https://learn.microsoft.com/en-us/rest/api/azure/devops/git/cherry%20picks/create?view=azure-devops-rest-6.0#gitasyncrefoperationsource
Invoke-WebRequest @req -Method POST -Uri "${baseuri}/git/repositories/${RepoName}/cherryPicks?${api}" -Body ($Body|ConvertTo-Json)

I get an issue as below when I try to cherry-pick this way,

Invoke-WebRequest : {"$id":"1","innerException":null,"message":"Exactly one source for a cherry-pick must be specified.","typeName":"Microsoft.TeamFoundation.Git.Server.GitAsyncRefOperationInvalidSourceException, Microsoft.TeamFo undation.Git.Server","typeKey":"GitAsyncRefOperationInvalidSourceException","errorCode":0,"eventId":3000} At line:99 char:1 + Invoke-WebRequest @req -Method POST -Uri "${baseuri}/git/repositories ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExce ption + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

What is this error? - I am giving exactly one source as per my understanding.

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114

1 Answers1

0

The repository and source parameters in the request body are object type. But i see you assigned string and integer to them. See the rest api here (click the highlighted in below screenshot to check out details of the object type).

enter image description here

You should define repository and source as object type. See below example:

$Body = @{
"generatedRefName" = "refs/heads/dev-on-master";
"ontoRefName" = "refs/heads/master";
"repository" = @{
                  "name"= $RepoName
                };
"source" = @{
               "pullRequestId"= $PRid
             } 
}

Update:

I tested with below single commitId. It worked fine.

$Body= @{
"generatedRefName" = "refs/heads/commit-on-master";
"ontoRefName" = "refs/heads/master";
"repository" = @{
                  "name"= $RepoName
                };
"source" = @{
               "commitList"= @(
               @{
               "commitId" = "5cedf148826ed783786e5b9b6932cc07ec9d745e"
               }
               )
             } 
}

Above request body needs to be convert to json with deeper depth: -Body ($Body|ConvertTo-Json -Depth 100)

Invoke-WebRequest @req -Method POST -Uri "${baseuri}/git/repositories/${RepoName}/cherryPicks?${api}" -Body ($Body|ConvertTo-Json -Depth 100)

Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43
  • Yes, I checked that. The document says the Source accepts two types of input, in which on that accepts an integer value ( PrID) or a list of commitIDs. Only the list of commitIDs work with a limitation that if there is one commitid in the list it still fails with above issue. – Tejaswini N May 26 '20 at 05:25
  • Hi @TejaswiniN I tested with a single commitId in the commitlists. It worked fine. Check above update. – Levi Lu-MSFT May 26 '20 at 06:19
  • Hi @TejaswiniN Did you check out above update. How did it go? – Levi Lu-MSFT May 29 '20 at 09:31