forked from hub4j/github-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGHCommitBuilder.java
More file actions
136 lines (122 loc) · 3.58 KB
/
Copy pathGHCommitBuilder.java
File metadata and controls
136 lines (122 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package org.kohsuke.github;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
/**
* Builder pattern for creating a new commit. Based on https://developer.github.com/v3/git/commits/#create-a-commit
*/
public class GHCommitBuilder {
private final GHRepository repo;
private final Requester req;
private final List<String> parents = new ArrayList<String>();
private static final class UserInfo {
private final String name;
private final String email;
private final String date;
private UserInfo(String name, String email, Date date) {
this.name = name;
this.email = email;
TimeZone tz = TimeZone.getTimeZone("UTC");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(tz);
this.date = df.format((date != null) ? date : new Date());
}
}
GHCommitBuilder(GHRepository repo) {
this.repo = repo;
req = repo.root().createRequest().method("POST");
}
/**
* Message gh commit builder.
*
* @param message
* the commit message
* @return the gh commit builder
*/
public GHCommitBuilder message(String message) {
req.with("message", message);
return this;
}
/**
* Tree gh commit builder.
*
* @param tree
* the SHA of the tree object this commit points to
* @return the gh commit builder
*/
public GHCommitBuilder tree(String tree) {
req.with("tree", tree);
return this;
}
/**
* Parent gh commit builder.
*
* @param parent
* the SHA of a parent commit.
* @return the gh commit builder
*/
public GHCommitBuilder parent(String parent) {
parents.add(parent);
return this;
}
/**
* Configures the author of this commit.
*
* @param name
* the name
* @param email
* the email
* @param date
* the date
* @return the gh commit builder
*/
public GHCommitBuilder author(String name, String email, Date date) {
req.with("author", new UserInfo(name, email, date));
return this;
}
/**
* Configures the PGP signature of this commit.
*
* @param signature
* the signature calculated from the commit
*
* @return the gh commit builder
*/
public GHCommitBuilder withSignature(String signature) {
req.with("signature", signature);
return this;
}
/**
* Configures the committer of this commit.
*
* @param name
* the name
* @param email
* the email
* @param date
* the date
* @return the gh commit builder
*/
public GHCommitBuilder committer(String name, String email, Date date) {
req.with("committer", new UserInfo(name, email, date));
return this;
}
private String getApiTail() {
return String.format("/repos/%s/%s/git/commits", repo.getOwnerName(), repo.getName());
}
/**
* Creates a blob based on the parameters specified thus far.
*
* @return the gh commit
* @throws IOException
* the io exception
*/
public GHCommit create() throws IOException {
req.with("parents", parents);
return req.method("POST").withUrlPath(getApiTail()).fetch(GHCommit.class).wrapUp(repo);
}
}