首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Azure DevOps中向管道中的远程分支发出拉取请求?

如何在Azure DevOps中向管道中的远程分支发出拉取请求?
EN

Stack Overflow用户
提问于 2020-04-24 04:53:31
回答 3查看 1.3K关注 0票数 1

这个问题类似于下面的问题:How to do a Git pull request on remote branches via the command line

我有一个管道,它生成一个带有新文档的分支,该文档需要集成到主分支的远程存储库中。这需要通过拉取请求来完成,我已经尝试了git命令git request-pull,但没有用,因为我在可能中看不到pr。这就是我目前所拥有的(它直接将新的分支合并到master,而不使用pr)。

代码语言:javascript
复制
- 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'
EN

回答 3

Stack Overflow用户

发布于 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令牌的脚本访问才能进行身份验证。

票数 0
EN

Stack Overflow用户

发布于 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。请查看以下示例:

代码语言:javascript
复制
 - 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

票数 0
EN

Stack Overflow用户

发布于 2021-01-20 20:01:31

如果你喜欢简单性并且是命令行的粉丝,你可以使用Azure CLI。@jessehouwing在他的回答中提到了这一点,但我会添加更多细节。

先决条件:构建服务用户必须拥有对存储库的“PullRequestContribute”访问权限。然后,您可以添加一个powershell任务,如下所示。

代码语言:javascript
复制
  - 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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61396789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档