octokit / octokit.net

A GitHub API client library for .NET

Home Page:https://octokitnet.readthedocs.io/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG]: Creating a new tree with a null sha tree object fails

Zaragor opened this issue · comments

What happened?

I'd like to delete multiple files within one commit, so was constructing a tree with multiple objects with a null sha, as per documentation. I get an API error Must supply either tree.sha or tree.content. Request will be rejected if both are present.. When I run an identical API request via Postmark with sha set to null, it succeeds in creating the tree. When I run the request with sha not present, I see that API error.

I imagine its something in the serializer stripping out null fields, when in this case I would like them to be preserved.

I can't use the client.Repository.Content.DeleteFile api, as that creates a new commit for every file you delete.

Example failing code below.

var toDelete = new List<string>() { "file1", "file2" };
var repository = await client.Repository.Get("ORGID", "REPONAME");
var headOfDefault = await client.Git.Reference.Get(repository.Id, $"heads/{repository.DefaultBranch}");
var tree = new NewTree() {
    BaseTree = headOfDefault.Object.Sha
};
foreach (var file in toDelete) {
    tree.Tree.Add(
        new NewTreeItem() 
        {
            Type = TreeType.Blob,
            Mode = FileMode.File,
            Path = file
            Sha = null
        });
}
var newTree = await client Git.Tree.Create(repository.Id, tree);

Versions

Octokit 9.0.0 running on net6.0

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

Reproduced here.😣