0
votes

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://docs.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.

1

1 Answers

0
votes

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)