Created
November 20, 2019 23:04
-
-
Save siordache/f2a895569de6f866877174376cf777d0 to your computer and use it in GitHub Desktop.
Workaround for github-api issue (https://github.com/github-api/github-api/issues/504)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.beryx.test; | |
import org.kohsuke.github.*; | |
import java.io.IOException; | |
import java.nio.charset.StandardCharsets; | |
public class GHApiTest { | |
private static String REPO_NAME = "sandboxx/GHTreeBuilderTest"; | |
private static String GITHUB_OAUTH = System.getenv("GITHUB_OAUTH"); | |
public static GHTreeBuilder add(GHTreeBuilder builder, GHRepository repo, String path, String content, boolean executable) { | |
return add(builder, repo, path, content.getBytes(StandardCharsets.UTF_8), executable); | |
} | |
public static GHTreeBuilder add(GHTreeBuilder builder, GHRepository repo, String path, byte[] content, boolean executable) { | |
try { | |
String dataSha = repo.createBlob() | |
.binaryContent(content) | |
.create() | |
.getSha(); | |
return builder.shaEntry(path, dataSha, executable); | |
} catch (IOException e) { | |
throw new GHException("Cannot create binary content of '" + path + "'", e); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
GitHub gitHub = GitHub.connectUsingOAuth(GITHUB_OAUTH); | |
GHRepository repo = gitHub.getRepository(REPO_NAME); | |
GHRef masterRef = repo.getRef("heads/master"); | |
String masterTreeSha = repo | |
.getTreeRecursive("master", 1) | |
.getSha(); | |
GHTreeBuilder treeBuilder = repo.createTree().baseTree(masterTreeSha); | |
add(treeBuilder, repo, "app/run.sh", "#!/bin/bash\necho Hello\n", true); | |
add(treeBuilder, repo, "doc/readme.txt", "Thanks for using our application!\n", false); | |
add(treeBuilder, repo, "data/val1.dat", new byte[] {0x01, 0x02, 0x03}, false); | |
String treeSha = treeBuilder.create().getSha(); | |
String commitSha = repo.createCommit() | |
.message("Add content") | |
.tree(treeSha) | |
.parent(masterRef.getObject().getSha()) | |
.create() | |
.getSHA1(); | |
masterRef.updateTo(commitSha); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment