使用Azure DevOps Go API创建PR报TF401398错误如何解决?
错误原因
该报错的核心触发原因是传入的分支引用格式不符合Azure DevOps API要求,具体问题如下:
- Azure DevOps Git的分支引用必须携带
refs/heads/前缀,你当前直接传入feature-test、main这类短分支名,API无法识别为有效分支引用,因此抛出错误。 - 同时可排查两个次要可能原因:
- 确认填写的项目名、仓库ID正确,对应仓库下确实存在你要使用的源分支
feature-test和目标分支main - 确认使用的PAT有对应仓库的代码读取、PR创建权限
- 确认填写的项目名、仓库ID正确,对应仓库下确实存在你要使用的源分支
修复方案
修改代码中源分支、目标分支的赋值逻辑,补充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
相关产品推荐
相关产品推荐

