Skip to content

Commit 2b6f37a

Browse files
committed
Merge pull request hub4j#361
2 parents be49eb2 + f3a3b87 commit 2b6f37a

4 files changed

Lines changed: 239 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.kohsuke.github;
2+
3+
import org.apache.commons.codec.binary.Base64;
4+
5+
import java.io.IOException;
6+
7+
/**
8+
* Builder pattern for creating a new blob.
9+
* Based on https://developer.github.com/v3/git/blobs/#create-a-blob
10+
*/
11+
public class GHBlobBuilder {
12+
private final GHRepository repo;
13+
private final Requester req;
14+
15+
GHBlobBuilder(GHRepository repo) {
16+
this.repo = repo;
17+
req = new Requester(repo.root);
18+
}
19+
20+
/**
21+
* Configures a blob with the specified text {@code content}.
22+
*/
23+
public GHBlobBuilder textContent(String content) {
24+
req.with("content", content);
25+
req.with("encoding", "utf-8");
26+
return this;
27+
}
28+
29+
/**
30+
* Configures a blob with the specified binary {@code content}.
31+
*/
32+
public GHBlobBuilder binaryContent(byte[] content) {
33+
String base64Content = Base64.encodeBase64String(content);
34+
req.with("content", base64Content);
35+
req.with("encoding", "base64");
36+
return this;
37+
}
38+
39+
private String getApiTail() {
40+
return String.format("/repos/%s/%s/git/blobs", repo.getOwnerName(), repo.getName());
41+
}
42+
43+
/**
44+
* Creates a blob based on the parameters specified thus far.
45+
*/
46+
public GHBlob create() throws IOException {
47+
return req.method("POST").to(getApiTail(), GHBlob.class);
48+
}
49+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package org.kohsuke.github;
2+
3+
import java.io.IOException;
4+
import java.text.DateFormat;
5+
import java.text.SimpleDateFormat;
6+
import java.util.ArrayList;
7+
import java.util.Date;
8+
import java.util.List;
9+
import java.util.TimeZone;
10+
11+
/**
12+
* Builder pattern for creating a new commit.
13+
* Based on https://developer.github.com/v3/git/commits/#create-a-commit
14+
*/
15+
public class GHCommitBuilder {
16+
private final GHRepository repo;
17+
private final Requester req;
18+
19+
private final List<String> parents = new ArrayList<String>();
20+
21+
private static final class UserInfo {
22+
private final String name;
23+
private final String email;
24+
private final String date;
25+
26+
private UserInfo(String name, String email, Date date) {
27+
this.name = name;
28+
this.email = email;
29+
TimeZone tz = TimeZone.getTimeZone("UTC");
30+
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
31+
df.setTimeZone(tz);
32+
this.date = df.format((date != null) ? date : new Date());
33+
}
34+
}
35+
36+
GHCommitBuilder(GHRepository repo) {
37+
this.repo = repo;
38+
req = new Requester(repo.root);
39+
}
40+
41+
/**
42+
* @param message the commit message
43+
*/
44+
public GHCommitBuilder message(String message) {
45+
req.with("message", message);
46+
return this;
47+
}
48+
49+
/**
50+
* @param tree the SHA of the tree object this commit points to
51+
*/
52+
public GHCommitBuilder tree(String tree) {
53+
req.with("tree", tree);
54+
return this;
55+
}
56+
57+
/**
58+
* @param parent the SHA of a parent commit.
59+
*/
60+
public GHCommitBuilder parent(String parent) {
61+
parents.add(parent);
62+
return this;
63+
}
64+
65+
/**
66+
* Configures the author of this commit.
67+
*/
68+
public GHCommitBuilder author(String name, String email, Date date) {
69+
req._with("author", new UserInfo(name, email, date));
70+
return this;
71+
}
72+
73+
/**
74+
* Configures the committer of this commit.
75+
*/
76+
public GHCommitBuilder committer(String name, String email, Date date) {
77+
req._with("committer", new UserInfo(name, email, date));
78+
return this;
79+
}
80+
81+
private String getApiTail() {
82+
return String.format("/repos/%s/%s/git/commits", repo.getOwnerName(), repo.getName());
83+
}
84+
85+
/**
86+
* Creates a blob based on the parameters specified thus far.
87+
*/
88+
public GHCommit create() throws IOException {
89+
req._with("parents", parents);
90+
return req.method("POST").to(getApiTail(), GHCommit.class).wrapUp(repo);
91+
}
92+
}

src/main/java/org/kohsuke/github/GHRepository.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,10 @@ public GHTree getTree(String sha) throws IOException {
883883
return root.retrieve().to(url, GHTree.class).wrap(this);
884884
}
885885

886+
public GHTreeBuilder createTree() {
887+
return new GHTreeBuilder(this);
888+
}
889+
886890
/**
887891
* Retrieves the tree for the current GitHub repository, recursively as described in here:
888892
* https://developer.github.com/v3/git/trees/#get-a-tree-recursively
@@ -912,6 +916,10 @@ public GHBlob getBlob(String blobSha) throws IOException {
912916
return root.retrieve().to(target, GHBlob.class);
913917
}
914918

919+
public GHBlobBuilder createBlob() {
920+
return new GHBlobBuilder(this);
921+
}
922+
915923
/**
916924
* Reads the content of a blob as a stream for better efficiency.
917925
*
@@ -935,6 +943,10 @@ public GHCommit getCommit(String sha1) throws IOException {
935943
return c;
936944
}
937945

946+
public GHCommitBuilder createCommit() {
947+
return new GHCommitBuilder(this);
948+
}
949+
938950
/**
939951
* Lists all the commits.
940952
*/
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)