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

Git 如何在Azure DevOps中向管道中的远程分支发出请求?,git,azure-devops,azure-pipelines,pull-request,Git,Azure Devops,Azure Pipelines,Pull Request,此问题与以下问题类似: 我有一个管道,可以生成一个分支,其中包含一个新文档,该文档需要集成到主分支的远程存储库中。这需要通过pull请求来完成,我尝试了git命令git-request-pull,但没有成功,因为我没有在can中看到pr。这就是我目前拥有的(直接将新分支合并到master,而无需pr) 我的同事桑德有 如果你想看看是怎么做的 另一个选项是使用。我想,您需要启用对管道的OAuth令牌的脚本访问以进行身份验证。我的同事Sander已经这样做了 如果你想看看是怎么做的 另一个选项是使用

此问题与以下问题类似:

我有一个管道,可以生成一个分支,其中包含一个新文档,该文档需要集成到主分支的远程存储库中。这需要通过pull请求来完成,我尝试了git命令
git-request-pull
,但没有成功,因为我没有在can中看到pr。这就是我目前拥有的(直接将新分支合并到master,而无需pr)

我的同事桑德有

如果你想看看是怎么做的

另一个选项是使用。我想,您需要启用对管道的OAuth令牌的脚本访问以进行身份验证。

我的同事Sander已经这样做了

如果你想看看是怎么做的


另一个选项是使用。我想,您需要启用对管道的OAuth令牌的脚本访问以进行身份验证。

您可以使用Azure devops services REST API来进行身份验证

POSThttps://dev.azure.com/{organization}/{project}/_-api/git/repositories/{repositoryId}/pullrequests?api版本=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脚本的最后,调用pull请求api在云中创建PR。要获取个人访问令牌,请选中。

您可以使用Azure devops服务REST API来访问

POSThttps://dev.azure.com/{organization}/{project}/_-api/git/repositories/{repositoryId}/pullrequests?api版本=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脚本的最后,调用pull请求api在云中创建PR。要获取个人访问令牌,请选中。

如果您喜欢简单性并且喜欢命令行,可以使用Azure CLI@jessehouwing在回答中提到了这一点,但我会补充更多细节

先决条件:生成服务用户必须具有对存储库的“PullRequestContrible”访问权限。然后,您可以添加如下所示的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)
-任务:PowerShell@2
投入:
targetType:内联
脚本:“az repos pr create-r-s-t--title“Auto pr:XXX”--删除源分支true-d”由$(System.CollectionUri)$(System.TeamProject)/_build/results?buildId=$(build.buildId)”--合并提交消息“Integration[skip ci]--org$(System.CollectionUri)-p$(System.TeamProject)”
显示名称:创建PR
姓名:CreatePR
环境:
AZURE_DEVOPS_EXT_PAT:$(System.AccessToken)
由于AZ CLI在任何操作系统中运行,因此您不需要将自己限制在powershell中

认证-


Azure Devops CLI-

如果您喜欢简单性并且喜欢命令行,那么您可以使用Azure CLI@jessehouwing在回答中提到了这一点,但我会补充更多细节

先决条件:生成服务用户必须具有对存储库的“PullRequestContrible”访问权限。然后,您可以添加如下所示的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)
-任务:PowerShell@2
投入:
targetType:内联
脚本:“az repos pr create-r-s-t--title“Auto pr:XXX”--删除源分支true-d”由$(System.CollectionUri)$(System.TeamProject)/_build/results?buildId=$(build.buildId)”--合并提交消息“Integration[skip ci]--org$(System.CollectionUri)-p$(System.TeamProject)”
显示名称:创建PR
姓名:CreatePR
环境:
AZURE_DEVOPS_EXT_PAT:$(System.AccessToken)
由于AZ CLI在任何操作系统中运行,因此您不需要将自己限制在powershell中

认证-


Azure Devops CLI-

您好,您是否有机会查看下面回答中提到的rest api。进展如何?您好,您是否有机会查看下面答案中提到的RESTAPI。进展如何?