这个问题类似于下面的问题:How to do a Git pull request on remote branches via the command line
我有一个管道,它生成一个带有新文档的分支,该文档需要集成到主分支的远程存储库中。这需要通过拉取请求来完成,我已经尝试了git命令git request-pull,但没有用,因为我在可能中看不到pr。这就是我目前所拥有的(它直接将新的分支合并到master,而不使用pr)。
- script: |
cd documentation
git config --global user.email "myemail@someOrganization.com" && git config --global user.name "John Doe"
git checkout -b release-notes
dir
mv $(System.DefaultWorkingDirectory)\\..\\tempdocs.md $(System.DefaultWorkingDirectory)\\..\\"$(SourceBranch)".md
mv $(System.DefaultWorkingDirectory)\\..\\"$(SourceBranch)".md docs\\applications\\app\\versions\\"$(SourceBranch)".md
git add .
git commit -m "add release notes for $(SourceBranch)"
git checkout feature/automatic-docs && git merge release-notes && git push origin
displayName: 'Publish Documentation'发布于 2020-04-24 05:44:11
我的同事桑德得了created an extension which does all the hard work of creating the PR。
如果你想看看是怎么做的,check out the source to the extension。
另一种选择是使用az repos pr createcommand in the Azure CLI with the DevOps extension. 。我假设您需要启用对管道的OAuth令牌的脚本访问才能进行身份验证。
发布于 2020-04-24 15:13:45
您可以使用Azure devops服务REST API来create a pull request。
POST https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests?api-version=5.1
因此,您只需在powershell任务中运行git命令,并添加更多代码来调用上面的api。请查看以下示例:
- powershell: |
git config --global user.email "@email.com"
git config --global user.name "name"
git checkout -b new_branch -q
git ....
$PAT= "Personal access token"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
$url = "$(System.TeamFoundationCollectionUri)$(System.TeamProject)/_apis/git/repositories/$(Build.Repository.ID)/pullrequests?api-version=5.1"
$body = '{
"sourceRefName": "refs/heads/sourcebranch",
"targetRefName": "refs/heads/targetbranch",
"title": "A new feature",
"description": "Adding a new feature",
}'
Invoke-RestMethod -Uri $url -Method post -Header @{Authorization = "Basic $base64AuthInfo"} -ContentType "application/json" -Body $body在上述powershell脚本的末尾,调用拉取请求接口,在云中创建PR。要获取个人访问令牌,请检查this document。
发布于 2021-01-20 20:01:31
如果你喜欢简单性并且是命令行的粉丝,你可以使用Azure CLI。@jessehouwing在他的回答中提到了这一点,但我会添加更多细节。
先决条件:构建服务用户必须拥有对存储库的“PullRequestContribute”访问权限。然后,您可以添加一个powershell任务,如下所示。
- task: PowerShell@2
inputs:
targetType: inline
Script: 'az repos pr create -r <YOUR_REPO> -s <SOURCE_BRANCH> -t <TARGET_BRANCH> --title "Auto PR: XXX" --delete-source-branch true -d "Created by $(System.CollectionUri)$(System.TeamProject)/_build/results?buildId=$(Build.BuildID)" --merge-commit-message "Integration [skip ci]" --org $(System.CollectionUri) -p $(System.TeamProject)'
displayName: Create PR
name: CreatePR
env:
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)因为AZ CLI可以在任何操作系统上运行,所以您不需要限制自己使用powershell。
身份验证- https://docs.microsoft.com/en-us/azure/devops/cli/log-in-via-pat?view=azure-devops&tabs=windows
Azure Devops CLI - https://docs.microsoft.com/en-us/cli/azure/repos/pr?view=azure-cli-latest#az
https://stackoverflow.com/questions/61396789
复制相似问题