Skip to content

Commit b537f99

Browse files
committed
issue hub4j#360: Add support for committing multiple files
1 parent f2fe8ea commit b537f99

3 files changed

Lines changed: 228 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
public 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+
new String(content);
34+
String base64Content = Base64.encodeBase64String(content);
35+
req.with("content", base64Content);
36+
req.with("encoding", "base64");
37+
return this;
38+
}
39+
40+
private String getApiTail() {
41+
return String.format("/repos/%s/%s/git/blobs", repo.getOwnerName(), repo.getName());
42+
}
43+
44+
/**
45+
* Creates a blob based on the parameters specified thus far.
46+
*/
47+
public GHBlob create() throws IOException {
48+
return req.method("POST").to(getApiTail(), GHBlob.class);
49+
}
50+
}
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+
public 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+
}
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+
public 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)