TF401398: The pull request cannot be activated because the source and/or the target branch no longer exists, or the requested refs are not branches

huangapple go评论82阅读模式
英文:

TF401398: The pull request cannot be activated because the source and/or the target branch no longer exists, or the requested refs are not branches

问题

在使用Azure DevOps的Golang API创建pull request时,我遇到了以下错误:

错误:
TF401398:无法激活pull request,因为源分支和/或目标分支不存在,或者请求的引用不是分支

代码:

connection := azuredevops.NewPatConnection("organizationUrl", "PAToken")
ctx := context.Background()
client, _ := azuregit.NewClient(ctx, connection)
pr := azuregit.CreatePullRequestArgs{}
repoId := git.repository.Name
proname := "MVC Test -Demo"
pr.Project = &proname
pr.RepositoryId = &repoId
sourceBranch := "feature-test"
targetBranch := "main"
pr.GitPullRequestToCreate = &azuregit.GitPullRequest{
TargetRefName: &targetBranch,
SourceRefName: &sourceBranch,
Description: &git.configuration.PrRequestMessage,
Title: &git.configuration.PrRequestTitle,
}
_, err := client.CreatePullRequest(ctx, pr)

英文:

I was getting the following errors while creating a pull-request using azuredevops golang API

error:
TF401398: The pull request cannot be activated because the source and/or the target branch no longer exists, or the requested refs are not branches

Code :

    connection := azuredevops.NewPatConnection("organizationUrl", "PAToken")
	ctx := context.Background()
	client, _ := azuregit.NewClient(ctx, connection)
	pr := azuregit.CreatePullRequestArgs{}
	repoId := git.repository.Name
	proname := "MVC Test -Demo"
	pr.Project = &proname
	pr.RepositoryId = &repoId
    sourceBranch:= "feature-test"
    targetBranch:= "main"
	pr.GitPullRequestToCreate = &azuregit.GitPullRequest{
		TargetRefName: &targetBranch,
		SourceRefName: &sourceBranch,
		Description:   &git.configuration.PrRequestMessage,
		Title:         &git.configuration.PrRequestTitle,
	}
	_, err := client.CreatePullRequest(ctx, pr)

答案1

得分: 4

这个问题通过在分支前加上refs/heads/来修复了。

sourceBranch := "refs/heads/feature-test"
targetBranch := "refs/heads/main"
pr.GitPullRequestToCreate = &azuregit.GitPullRequest{
    TargetRefName: &targetBranch,
    SourceRefName: &sourceBranch,
    Description:   &git.configuration.PrRequestMessage,
    Title:         &git.configuration.PrRequestTitle,
}
_, err := client.CreatePullRequest(ctx, pr)
英文:

This issue got fixed by prefixing a refs/heads/ to the branch

        sourceBranch:= "refs/heads/feature-test"
        targetBranch:= "refs/heads/main"
    	pr.GitPullRequestToCreate = &azuregit.GitPullRequest{
    		TargetRefName: &targetBranch,
    		SourceRefName: &sourceBranch,
    		Description:   &git.configuration.PrRequestMessage,
    		Title:         &git.configuration.PrRequestTitle,
    	}
        _, err := client.CreatePullRequest(ctx, pr)

答案2

得分: 0

我遇到了相同的问题(尽管是在Azure DevOps中),每次都有其他修复方法可行:

错误:使用状态码400创建拉取请求失败。消息:TF401398:无法激活拉取请求,因为源分支和/或目标分支不再存在,或者请求的引用不是分支(StandardError)

  1. 重新创建丢失的分支(查看流水线日志中的最后一个400错误)
  2. 在YAML中的main中添加refs/heads/(参见下面的示例)
  3. 转到项目设置 > 选择您的存储库 > 转到“安全性” > 选择“项目集合构建服务帐户用户” > 启用对拉取请求的“允许”和对创建分支的“允许”
    查看图像描述
  4. 为您的代理添加唯一的PAT(参见下面的示例)
---
schedules:
  - cron: '0 2 * * *'
    displayName: 'yourdisplayname'
    always: true
    branches:
      include:
        - main

trigger: none

stages:
  - stage: CheckDependencies
    displayName: 'dependabot run'
    jobs:
      - job: Dependabot
        displayName: 'Run Dependabot'
        pool:
          vmImage: 'ubuntu-latest'
        steps:
          - task: AzureKeyVault@2
            inputs:
              connectedServiceName: 'name'
              keyVaultName: 'kvname'
              secretsFilter: 'azureDevOpsAccessToken'
              runAsPreJob: true

          - task: dependabot@1
            inputs:
              azureDevOpsAccessToken: '$(azureDevOpsAccessToken)'  

          - task: dependabot@1
            inputs:
              packageManager: 'nuget'
              targetBranch: 'refs/heads/main'
英文:

I had the same issue (though in Azure DevOps) and each time another fix worked:

Error: Pull Request creation failed with status 400. Message: TF401398: The pull request cannot be activated because the source and/or the target branch no longer exists, or the requested refs are not branches (StandardError)

  1. recreate the missing branch (see last 400 error in your pipeline log)
  2. add refs/heads/ to main in your YAML (see below example)
  3. go to project settings > select your repo > go to 'security'> select Project Collection Build Service Accounts user > enable 'Allow' for contribute to pull requests and 'Allow' for Create branch
    enter image description here
  4. add unique PAT for your agent (see example below)
        ---
    schedules:
      - cron: '0 2 * * *'
        displayName: 'yourdisplayname'
        always: true
        branches:
          include:
            - main
    
    trigger: none
    
    stages:
      - stage: CheckDependencies
        displayName: 'dependabot run'
        jobs:
          - job: Dependabot
            displayName: 'Run Dependabot'
            pool:
              vmImage: 'ubuntu-latest'
            steps:
              - task: AzureKeyVault@2
                inputs:
                  connectedServiceName: 'name'
                  keyVaultName: 'kvname'
                  secretsFilter: 'azureDevOpsAccessToken'
                  runAsPreJob: true
    
              - task: dependabot@1
                inputs:
                  azureDevOpsAccessToken: '$(azureDevOpsAccessToken)'  
    
              - task: dependabot@1
                inputs:
                  packageManager: 'nuget'
                  targetBranch: 'refs/heads/main'

答案3

得分: 0

遇到相同的问题,让我告诉你一个秘密!

不要保留一个名为"dependaboat"的分支。
因此,对于每个依赖项,dependaboat尝试在"dependaboat"文件夹下创建一个分支,但是如果已经存在同名的分支,Azure无法创建文件夹。

英文:

Having the same issue and let me tell you a secret!

don't keep a branch with the name dependaboat.
so for each dependency, dependaboat tries to create a branch under the folder "dependaboat" but Azure fails to create a folder if a branch with the same name already exists.

huangapple
  • 本文由 发表于 2021年9月8日 13:25:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/69097402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定