树SHA不是go-github库中的树对象错误



我正在尝试使用go-github在GitHub中创建一个空提交。

以下代码:

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
}

在失败

newCommitResponse, _, err := client.Git.CreateCommit(ctx, owner, repo, newCommit)

422 Tree SHA is not a tree object []

我找不到任何关于这个错误的相关信息。

任何想法?

当你使用git cli时,git自己会运行"有意义"的翻译。——例如:用相关树的sha替换提交。


使用这个较低级别的API,你必须显式地完成这个转换。

使用go-github,您可以通过一个额外的查询来完成此操作:

commit, _, err := client.Git.GetCommit(ctx, owner, repo, latestCommitSHA)
if err != nil {
return "", err
}
treeSHA := commit.GetTree().GetSHA()

相关内容

  • 没有找到相关文章

最新更新