-
Notifications
You must be signed in to change notification settings - Fork 776
Expand file tree
/
Copy pathGHContentBuilder.java
More file actions
231 lines (210 loc) · 6.56 KB
/
GHContentBuilder.java
File metadata and controls
231 lines (210 loc) · 6.56 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package org.kohsuke.github;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.Base64;
import java.util.Date;
// TODO: Auto-generated Javadoc
/**
* Used to create/update content.
*
* <p>
* Call various methods to build up parameters, then call {@link #commit()} to make the change effective.
*
* @author Kohsuke Kawaguchi
* @see GHRepository#createContent() GHRepository#createContent()
*/
public final class GHContentBuilder {
@JsonInclude(Include.NON_NULL)
private static final class UserInfo {
private final String date;
private final String email;
private final String name;
private UserInfo(String name, String email, Instant date) {
this.name = name;
this.email = email;
this.date = date != null ? GitHubClient.printInstant(date) : null;
}
}
private String path;
private final GHRepository repo;
private final Requester req;
/**
* Instantiates a new GH content builder.
*
* @param repo
* the repo
*/
GHContentBuilder(GHRepository repo) {
this.repo = repo;
this.req = repo.root().createRequest().method("PUT");
}
/**
* Configures the author of the commit. If not specified, the authenticated user is used as the author.
*
* @param name
* the name of the author
* @param email
* the email of the author
* @return the gh content builder
*/
public GHContentBuilder author(String name, String email) {
return author(name, email, (Instant) null);
}
/**
* Configures the author of the commit. If not specified, the authenticated user is used as the author.
*
* @param name
* the name of the author
* @param email
* the email of the author
* @param date
* the date of the authoring
* @return the gh content builder
* @deprecated use {@link #author(String, String, Instant)} instead
*/
@Deprecated
public GHContentBuilder author(String name, String email, Date date) {
return author(name, email, GitHubClient.toInstantOrNull(date));
}
/**
* Configures the author of the commit. If not specified, the authenticated user is used as the author.
*
* @param name
* the name of the author
* @param email
* the email of the author
* @param date
* the timestamp for the authoring
* @return the gh content builder
*/
public GHContentBuilder author(String name, String email, Instant date) {
req.with("author", new UserInfo(name, email, date));
return this;
}
/**
* Branch gh content builder.
*
* @param branch
* the branch
* @return the gh content builder
*/
public GHContentBuilder branch(String branch) {
req.with("branch", branch);
return this;
}
/**
* Commits a new content.
*
* @return the gh content update response
* @throws IOException
* the io exception
*/
public GHContentUpdateResponse commit() throws IOException {
GHContentUpdateResponse response = req.withUrlPath(GHContent.getApiRoute(repo, path))
.fetch(GHContentUpdateResponse.class);
response.getContent().wrap(repo);
response.getCommit().wrapUp(repo);
return response;
}
/**
* Configures the committer of the commit. If not specified, the authenticated user is used as the committer.
*
* @param name
* the name of the committer
* @param email
* the email of the committer
* @return the gh content builder
*/
public GHContentBuilder committer(String name, String email) {
return committer(name, email, (Instant) null);
}
/**
* Configures the committer of the commit. If not specified, the authenticated user is used as the committer.
*
* @param name
* the name of the committer
* @param email
* the email of the committer
* @param date
* the date of the commit
* @return the gh content builder
* @deprecated use {@link #committer(String, String, Instant)} instead
*/
@Deprecated
public GHContentBuilder committer(String name, String email, Date date) {
return committer(name, email, GitHubClient.toInstantOrNull(date));
}
/**
* Configures the committer of the commit. If not specified, the authenticated user is used as the committer.
*
* @param name
* the name of the committer
* @param email
* the email of the committer
* @param date
* the timestamp of the commit
* @return the gh content builder
*/
public GHContentBuilder committer(String name, String email, Instant date) {
req.with("committer", new UserInfo(name, email, date));
return this;
}
/**
* Content gh content builder.
*
* @param content
* the content
* @return the gh content builder
*/
public GHContentBuilder content(String content) {
return content(content.getBytes(StandardCharsets.UTF_8));
}
/**
* Content gh content builder.
*
* @param content
* the content
* @return the gh content builder
*/
public GHContentBuilder content(byte[] content) {
req.with("content", Base64.getEncoder().encodeToString(content));
return this;
}
/**
* Message gh content builder.
*
* @param commitMessage
* the commit message
* @return the gh content builder
*/
public GHContentBuilder message(String commitMessage) {
req.with("message", commitMessage);
return this;
}
/**
* Path gh content builder.
*
* @param path
* the path
* @return the gh content builder
*/
public GHContentBuilder path(String path) {
this.path = path;
req.with("path", path);
return this;
}
/**
* Used when updating (but not creating a new content) to specify the blob SHA of the file being replaced.
*
* @param sha
* the sha
* @return the gh content builder
*/
public GHContentBuilder sha(String sha) {
req.with("sha", sha);
return this;
}
}