Tree SHA is not a tree object error in go-github library

huangapple go评论87阅读模式
英文:

Tree SHA is not a tree object error in go-github library

问题

我正在尝试使用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 []

我无法在任何地方找到有关此错误的相关信息。

有任何想法吗?

英文:

I am trying to create an empty commit in GitHub using go-github.

The following code:

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
}

Fails in

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

with

422 Tree SHA is not a tree object []

I cannot find any relevant info anywhere about this error.

Any ideas?

答案1

得分: 2

当你使用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()
英文:

When you go through git cli, git itself runs the translations that "make sense" -- e.g: replace a commit with the sha of its tree where relevant.


Using this lower level API, you have to do this translation explicitly.

Using go-github, you can do this with one extra query:

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

huangapple
  • 本文由 发表于 2023年4月5日 20:08:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75939249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定