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

Issues for tree in commit

Open Osiris-Team opened this issue 4 years ago • 2 comments

Describe the bug

Error: Exception in thread "main" org.kohsuke.github.HttpException: {"message":"tree.path cannot start with a slash","documentation_url":"https://docs.github.com/rest/reference/git#create-a-tree"}
	at org.kohsuke.github.GitHubClient.interpretApiError(GitHubClient.java:483)
	at org.kohsuke.github.GitHubClient.sendRequest(GitHubClient.java:407)
	at org.kohsuke.github.GitHubClient.sendRequest(GitHubClient.java:355)
	at org.kohsuke.github.Requester.fetch(Requester.java:76)
	at org.kohsuke.github.GHTreeBuilder.create(GHTreeBuilder.java:171)
	at com.osiris.pandomiumbuilder.STEP3.<init>(STEP3.java:140)
	at com.osiris.pandomiumbuilder.Main.main(Main.java:49)
Caused by: java.io.IOException: Server returned HTTP response code: 422 for URL: https://api.github.com/repos/Osiris-Team/pandomium/git/trees
...

To Reproduce

...
        System.out.println("Authenticating with token...");
        GitHub github = new GitHubBuilder().withOAuthToken(O_AUTH_TOKEN).build();
        System.out.println("Success!");
        System.out.println("Getting repo: '"+OWNER_AND_REPO+"'...");
        repo = github.getRepository(OWNER_AND_REPO);
        System.out.println("Success!");
        System.out.println("Committing updated files...");
        GHRef mainRef = repo.getRef("heads/master");
        String mainTreeSha = repo.getTreeRecursive("master", 1).getSha();
        GHTreeBuilder treeBuilder = repo.createTree().baseTree(mainTreeSha);
        commitFile(treeBuilder, mavenInstallScript);
        commitFile(treeBuilder, pandomiumParentPomFile);
        commitFile(treeBuilder, pandomiumPomFile);
        for (File fatJar :
                fatJars) {
            commitFile(treeBuilder, fatJar);
        }
        String treeSha = treeBuilder.create().getSha(); // THIS IS LINE 140 WHERE THE EXCEPTION GETS THROWN
        GHCommit commit = repo.createCommit().message("Add files")
                .tree(treeSha)
                .message(fullTagName+" - Updated files")
                .author("author", "[email protected]", new Date())
                .committer("committer", "[email protected]", new Date())
                .parent(mainRef.getObject().getSha())
                .create();
        String commitSha = commit.getSHA1();
        mainRef.updateTo(commitSha);
        System.out.println("Success!");
    }

    private void commitFile(GHTreeBuilder treeBuilder, File file) throws IOException {
        treeBuilder.add(file.getAbsolutePath(), Files.readAllBytes(file.toPath()), false);
    }

Expected behavior Create commit without exception and push it to the repo.

Desktop (please complete the following information):

  • OS: Windows
  • Browser Chrome
  • Version latest

Osiris-Team avatar Aug 30 '21 11:08 Osiris-Team

Fixed by removing the full files paths and instead giving the relative path from the github dir.

Osiris-Team avatar Aug 30 '21 16:08 Osiris-Team

I'd like to keep this open as a documentation bug. Perhaps we should add something to the JavaDoc.

bitwiseman avatar Sep 24 '21 06:09 bitwiseman