使用go-github创建空提交报错:Tree SHA is not a tree object
使用go-github创建空提交时出现422错误的解决思路
问题场景
执行client.Git.CreateCommit时触发报错:
422 Tree SHA is not a tree object []
相关代码如下:
func createHeadBranchForPR(ctx context.Context, baseBranch, repo, owner string, client *github.Client) (newBranch string, err error) { newBranch = createRandomBranchName() baseBranchRef, _, err := client.Git.GetRef(ctx, owner, repo, "heads/"+baseBranch) if err != nil { return "", err } latestCommitSHA := baseBranchRef.Object.GetSHA() // Create a new tree with no changes from the latest commit on the base branch newTree := &github.Tree{ SHA: &latestCommitSHA, } currentTime := time.Now() newCommit := &github.Commit{ Message: github.String("Test commit"), Tree: newTree, Parents: []github.Commit{ { SHA: github.String(latestCommitSHA), }, }, Author: &github.CommitAuthor{ Name: github.String(prCommitterAuthorName), Email: github.String(prCommitterAuthorEmail), Date: ¤tTime, }, Committer: &github.CommitAuthor{ Name: github.String(prCommitterAuthorName), Email: github.String(prCommitterAuthorName), Date: ¤tTime, }, SHA: &latestCommitSHA, } newCommitResponse, _, err := client.Git.CreateCommit(ctx, owner, repo, newCommit) if err != nil { return "", err } // Create a new branch based on the new commit newBranchRef := &github.Reference{ Ref: github.String("refs/heads/" + newBranch), Object: &github.GitObject{SHA: newCommitResponse.SHA}, } _, _, err = client.Git.CreateRef(ctx, owner, repo, newBranchRef) if err != nil { return "", err } return newBranch, nil }
错误根源
代码中直接将提交对象的SHA赋值给了Tree的SHA字段,但GitHub API要求创建提交时,Tree字段必须指向树对象(Tree Object)的SHA,而非提交对象的SHA,因此触发422参数验证错误。
此外代码还存在两处小问题:
newCommit中冗余设置了SHA字段,该字段是GitHub返回的结果,不是创建提交的请求参数- 提交者的Email字段错误使用了用户名
prCommitterAuthorName,而非邮箱地址
修复步骤
- 获取正确的树对象SHA:通过
client.Git.GetCommit获取基准提交的完整信息,从中提取树对象的SHA - 修正Tree字段赋值:使用树对象SHA初始化
newTree - 移除冗余字段:删除
newCommit中的SHA字段 - 修复邮箱笔误:将提交者的Email改为正确的
prCommitterAuthorEmail
修复后的完整代码
func createHeadBranchForPR(ctx context.Context, baseBranch, repo, owner string, client *github.Client) (newBranch string, err error) { newBranch = createRandomBranchName() baseBranchRef, _, err := client.Git.GetRef(ctx, owner, repo, "heads/"+baseBranch) if err != nil { return "", err } latestCommitSHA := baseBranchRef.Object.GetSHA() // 获取基准提交的完整信息,提取树对象SHA latestCommit, _, err := client.Git.GetCommit(ctx, owner, repo, latestCommitSHA) if err != nil { return "", err } treeSHA := latestCommit.Tree.GetSHA() // 使用树对象SHA创建空提交的Tree newTree := &github.Tree{ SHA: &treeSHA, } currentTime := time.Now() newCommit := &github.Commit{ Message: github.String("Test commit"), Tree: newTree, Parents: []github.Commit{ { SHA: github.String(latestCommitSHA), }, }, Author: &github.CommitAuthor{ Name: github.String(prCommitterAuthorName), Email: github.String(prCommitterAuthorEmail), Date: ¤tTime, }, Committer: &github.CommitAuthor{ Name: github.String(prCommitterAuthorName), Email: github.String(prCommitterAuthorEmail), // 修复邮箱笔误 Date: ¤tTime, }, } newCommitResponse, _, err := client.Git.CreateCommit(ctx, owner, repo, newCommit) if err != nil { return "", err } // 创建新分支 newBranchRef := &github.Reference{ Ref: github.String("refs/heads/" + newBranch), Object: &github.GitObject{SHA: newCommitResponse.SHA}, } _, _, err = client.Git.CreateRef(ctx, owner, repo, newBranchRef) if err != nil { return "", err } return newBranch, nil }
内容的提问来源于stack exchange,提问作者pkaramol
相关产品推荐
相关产品推荐

