|
| 1 | +package org.kohsuke.github; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +/** |
| 7 | + * Builder pattern for creating a new tree. |
| 8 | + * Based on https://developer.github.com/v3/git/trees/#create-a-tree |
| 9 | + */ |
| 10 | +public class GHTreeBuilder { |
| 11 | + private final GHRepository repo; |
| 12 | + private final Requester req; |
| 13 | + |
| 14 | + private final List<TreeEntry> treeEntries = new ArrayList<TreeEntry>(); |
| 15 | + |
| 16 | + private static final class TreeEntry { |
| 17 | + private final String path; |
| 18 | + private final String mode; |
| 19 | + private final String type; |
| 20 | + private String sha; |
| 21 | + private String content; |
| 22 | + |
| 23 | + private TreeEntry(String path, String mode, String type) { |
| 24 | + this.path = path; |
| 25 | + this.mode = mode; |
| 26 | + this.type = type; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + GHTreeBuilder(GHRepository repo) { |
| 31 | + this.repo = repo; |
| 32 | + req = new Requester(repo.root); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @param baseTree the SHA of tree you want to update with new data |
| 37 | + */ |
| 38 | + public GHTreeBuilder baseTree(String baseTree) { |
| 39 | + req.with("base_tree", baseTree); |
| 40 | + return this; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Adds a new entry to the tree. |
| 45 | + * Exactly one of the parameters {@code sha} and {@code content} must be non-null. |
| 46 | + */ |
| 47 | + public GHTreeBuilder entry(String path, String mode, String type, String sha, String content) { |
| 48 | + TreeEntry entry = new TreeEntry(path, mode, type); |
| 49 | + entry.sha = sha; |
| 50 | + entry.content = content; |
| 51 | + treeEntries.add(entry); |
| 52 | + return this; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Specialized version of {@link #entry(String, String, String, String, String)} for adding an existing blob referred by its SHA. |
| 57 | + */ |
| 58 | + public GHTreeBuilder shaEntry(String path, String sha, boolean executable) { |
| 59 | + TreeEntry entry = new TreeEntry(path, executable ? "100755" : "100644", "blob"); |
| 60 | + entry.sha = sha; |
| 61 | + treeEntries.add(entry); |
| 62 | + return this; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Specialized version of {@link #entry(String, String, String, String, String)} for adding a text file with the specified {@code content}. |
| 67 | + */ |
| 68 | + public GHTreeBuilder textEntry(String path, String content, boolean executable) { |
| 69 | + TreeEntry entry = new TreeEntry(path, executable ? "100755" : "100644", "blob"); |
| 70 | + entry.content = content; |
| 71 | + treeEntries.add(entry); |
| 72 | + return this; |
| 73 | + } |
| 74 | + |
| 75 | + private String getApiTail() { |
| 76 | + return String.format("/repos/%s/%s/git/trees", repo.getOwnerName(), repo.getName()); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Creates a tree based on the parameters specified thus far. |
| 81 | + */ |
| 82 | + public GHTree create() throws IOException { |
| 83 | + req._with("tree", treeEntries); |
| 84 | + return req.method("POST").to(getApiTail(), GHTree.class).wrap(repo); |
| 85 | + } |
| 86 | +} |
0 commit comments