You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Azure DevOps Go API创建PR报TF401398错误如何解决?

错误原因

该报错的核心触发原因是传入的分支引用格式不符合Azure DevOps API要求,具体问题如下:

  • Azure DevOps Git的分支引用必须携带refs/heads/前缀,你当前直接传入feature-test、main这类短分支名,API无法识别为有效分支引用,因此抛出错误。
  • 同时可排查两个次要可能原因:
    • 确认填写的项目名、仓库ID正确,对应仓库下确实存在你要使用的源分支feature-test和目标分支main
    • 确认使用的PAT有对应仓库的代码读取、PR创建权限
修复方案

修改代码中源分支、目标分支的赋值逻辑,补充refs/heads/前缀即可,修改后的代码片段如下:

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
    // 修正分支名格式,添加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)

内容的提问来源于stack exchange,提问作者Niyas Ali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 22:18:01