go-github icon indicating copy to clipboard operation
go-github copied to clipboard

Getting GitRPC::BadObjectState while setting nil to delete an entry

Open mimiandcanada opened this issue 3 years ago • 1 comments

Trying to delete the a subdirectory/folder. I am using "github.com/google/go-github/v35/github" I have tried following two methods:

  1. Get the entries under a tree i want to delete by calling GetTree then setting the SHA of BLOB entries to nil This resulted in 422 GitRPC::BadObjectState []

  2. Tried setting the Type tree of the subdirectory's SHA to nil This also resulted in 422 GitRPC::BadObjectState []

1st method calls: subDirMode := "tree" tree, _, err := gc.git.GetTree(context, OWNER, Repo, SHA, true) for i := 0; i < len(tree.Entries); i++ { if tree.Entries[i].GetType() != subDirMode { tree.Entries[i].SHA = nil } } tree, _, err := gc.git.CreateTree(gc.ctx, cfg.GithubOwner, cfg.GithubRepo, *ref.Object.SHA, tree.Entries)

2nd method calls: entry := &github.TreeEntry{Path: github.String(fpath), Type: github.String("tree"), SHA: nil, Mode: github.String("040000")} tree, _, err := gc.git.CreateTree(gc.ctx, cfg.GithubOwner, cfg.GithubRepo, *ref.Object.SHA, []*github.TreeEntry{entry})

Any ideas?

mimiandcanada avatar Jul 26 '22 16:07 mimiandcanada

To delete a subdir/folder, I think you will most likely prefer to use git or an equivalent git client like https://github.com/go-git/go-git

This repo is a client library for the GitHub v3 API: https://docs.github.com/en/rest and as such, makes it easy to call this API from Go, but is otherwise limited to the functionality that the v3 API provides, and I don't recall API endpoints to delete subdirs/folders, as that is more in the realm of a git client, which this repo is not.

I hope that helps.

gmlewis avatar Jul 26 '22 17:07 gmlewis

The create tree API endpoint indeed doesn't allow deleting a folder but it does allow deleting a file, allowing the user to create to create a new Tree that deletes all the files in a folder, affectively deleting the folder.

The problem I'm currently having is that the omitempty flag on SHA key in the TreeEntry struct doesn't allow me to send a nil value by setting a nil value to SHA key:

treeEntry := github.TreeEntry{
  Path: github.String("aFile.txt"),
  Mode: github.String("100644")
  Type: github.String("blob"),
  SHA:  nil,
}

According to my tests removing the omitempty solves my issue - but might break some other functionality with the content key

Am I missing some way to force the go-github to pass sha=null to the API?

Oded-B avatar Dec 24 '22 14:12 Oded-B

I believe you want this: https://github.com/google/go-github/blob/master/github/git_trees.go#L47-L59 which says to make the Content and SHA fields empty to delete the contents. Does that help?

gmlewis avatar Dec 24 '22 16:12 gmlewis

It helped by pointing me the fact I've been using an ancient version of this library 🤦 🤦 🤦 This issue was fixed 3 years ago but because of some Go dependency issue I've been using something older(!).

Thanks for the quick reply, happy holidays

Oded-B avatar Dec 24 '22 17:12 Oded-B

Thanks for the quick reply, happy holidays

Wonderful! Glad it helped! Merry Christmas to you and your loved ones!

Closing as resolved.

gmlewis avatar Dec 24 '22 17:12 gmlewis