hub4j / github-api

Java API for GitHub
https://github-api.kohsuke.org/
MIT License
1.15k stars 728 forks source link

Issues for tree in commit #1217

Open Osiris-Team opened 3 years ago

Osiris-Team commented 3 years ago

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", "pandomium@builder.com", new Date())
                .committer("committer", "pandomium@builder.com", 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):

Osiris-Team commented 3 years ago

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

bitwiseman commented 3 years ago

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