How to specify git author/committer for PullRequest merge in Azure DevOps?

Viewed 673

I have defined a release pipeline to synchronize code between two git repositories in Azure DevOps. The process requires a PullRequest in the destination repo which is created and completed using the WebAPI.

The pipeline is executed by the build agent running using a Windows domain service account. So, the PullRequest is created and completed on behalf of the service account, which is also mentioned as author, committer, etc. in the git history after merge is completed. (According to our rules the PR must be merged using squash commit.)

I would like see a different user in the git history after (squash) merge.

Can I specify the user (e.g. the user triggering the release pipeline) using WebAPI? I did not find such option in the API documentation.

Any other recommendation? Maybe convention like adding "co-authored-by" to commit message like github?

1 Answers

When you use Pull Requests-Create rest api to create a pull request, the pull request creator is determined by the creator of the PAT you used for authentication (e.g. I used the PAT of hughl01 user as the authentication to create the pull request, then the creator of the pull request is hughl01).

Test in Postman:

enter image description here

enter image description here

Sample test script in powershell task:

$token = "{User-created PAT}"

$url = "https://dev.azure.com/{org}/{pro}/_apis/git/repositories/{repoId}/pullrequests?api-version=6.0"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$JSON = @'
{
  "sourceRefName": "refs/heads/dev2",
  "targetRefName": "refs/heads/master",
  "title": "ForTest",
  "description": "Adding a new feature"
}
'@

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -ContentType application/json -body $JSON

You can create a variable group, with the release trigger as the variable name, and the PAT corresponding to the user as the variable value and set the value of the variable to secret. Then get the name of the triggerer through the predefined variable $(Release.RequestedFor) in the script, then obtain the corresponding PAT according to the trigger name to create pull request.

Related