Getting GitRPC::BadObjectState while setting nil to delete an entry
Trying to delete the a subdirectory/folder. I am using "github.com/google/go-github/v35/github" I have tried following two methods:
-
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 []
-
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?
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.
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?
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?
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
Thanks for the quick reply, happy holidays
Wonderful! Glad it helped! Merry Christmas to you and your loved ones!
Closing as resolved.