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

使用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:  &currentTime,
        },
        Committer: &github.CommitAuthor{
            Name:  github.String(prCommitterAuthorName),
            Email: github.String(prCommitterAuthorName),
            Date:  &currentTime,
        },
        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,而非邮箱地址

修复步骤

  1. 获取正确的树对象SHA:通过client.Git.GetCommit获取基准提交的完整信息,从中提取树对象的SHA
  2. 修正Tree字段赋值:使用树对象SHA初始化newTree
  3. 移除冗余字段:删除newCommit中的SHA字段
  4. 修复邮箱笔误:将提交者的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:  &currentTime,
        },
        Committer: &github.CommitAuthor{
            Name:  github.String(prCommitterAuthorName),
            Email: github.String(prCommitterAuthorEmail), // 修复邮箱笔误
            Date:  &currentTime,
        },
    }
    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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 04:29:57