Skip to content

Commit b2b7dfa

Browse files
committed
Massaged the change to match the existing API design convention
1 parent 2fcfb2f commit b2b7dfa

File tree

4 files changed

+89
-143
lines changed

4 files changed

+89
-143
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.kohsuke.github;
2+
3+
import org.apache.commons.codec.binary.Base64;
4+
5+
import java.io.IOException;
6+
import java.io.UnsupportedEncodingException;
7+
8+
/**
9+
* Used to create/update content.
10+
*
11+
* @author Kohsuke Kawaguchi
12+
* @see GHRepository#createContent()
13+
*/
14+
public final class GHContentBuilder {
15+
private final GHRepository repo;
16+
private final Requester req;
17+
private String path;
18+
19+
GHContentBuilder(GHRepository repo) {
20+
this.repo = repo;
21+
this.req = new Requester(repo.root).method("PUT");
22+
}
23+
24+
public GHContentBuilder path(String path) {
25+
this.path = path;
26+
req.with("path",path);
27+
return this;
28+
}
29+
30+
public GHContentBuilder branch(String branch) {
31+
req.with("branch", branch);
32+
return this;
33+
}
34+
35+
/**
36+
* Used when updating (but not creating a new content) to specify
37+
* Thetblob SHA of the file being replaced.
38+
*/
39+
public GHContentBuilder sha(String sha) {
40+
req.with("sha", sha);
41+
return this;
42+
}
43+
44+
public GHContentBuilder content(byte[] content) {
45+
req.with("content", Base64.encodeBase64String(content));
46+
return this;
47+
}
48+
49+
public GHContentBuilder content(String content) {
50+
try {
51+
return content(content.getBytes("UTF-8"));
52+
} catch (UnsupportedEncodingException x) {
53+
throw new AssertionError();
54+
}
55+
}
56+
57+
public GHContentBuilder message(String commitMessage) {
58+
req.with("message", commitMessage);
59+
return this;
60+
}
61+
62+
/**
63+
* Commits a new content.
64+
*/
65+
public GHContentUpdateResponse commit() throws IOException {
66+
GHContentUpdateResponse response = req.to(repo.getApiTailUrl("contents/" + path), GHContentUpdateResponse.class);
67+
68+
response.getContent().wrap(repo);
69+
response.getCommit().wrapUp(repo);
70+
71+
return response;
72+
}
73+
}

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

Lines changed: 0 additions & 97 deletions
This file was deleted.

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

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import com.fasterxml.jackson.annotation.JsonProperty;
2727
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
2828
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
29-
import org.apache.commons.codec.binary.Base64;
3029
import org.apache.commons.lang3.StringUtils;
3130

3231
import java.io.FileNotFoundException;
@@ -35,7 +34,6 @@
3534
import java.io.InputStreamReader;
3635
import java.io.InterruptedIOException;
3736
import java.io.Reader;
38-
import java.io.UnsupportedEncodingException;
3937
import java.net.URL;
4038
import java.util.AbstractSet;
4139
import java.util.ArrayList;
@@ -1394,70 +1392,43 @@ public GHContent getReadme() throws IOException {
13941392
return requester.to(getApiTailUrl("readme"), GHContent.class).wrap(this);
13951393
}
13961394

1397-
public GHContentUpdateResponse createContent(GHContentUpdateRequest updateRequest) throws IOException {
1398-
return createContent(updateRequest.getContent(), updateRequest.getCommitMessage(), updateRequest.getPath(), updateRequest.getBranch(), updateRequest.getSha());
1395+
/**
1396+
* Creates a new content, or update an existing content.
1397+
*/
1398+
public GHContentBuilder createContent() {
1399+
return new GHContentBuilder(this);
13991400
}
14001401

14011402
/**
1402-
* Use {@link GHContentUpdateRequest}.
1403+
* Use {@link #createContent()}.
14031404
*/
14041405
@Deprecated
14051406
public GHContentUpdateResponse createContent(String content, String commitMessage, String path) throws IOException {
1406-
return createContent(content.getBytes(), commitMessage, path, null, null);
1407+
return createContent().content(content).message(commitMessage).path(path).commit();
14071408
}
14081409

14091410
/**
1410-
* Use {@link GHContentUpdateRequest}.
1411+
* Use {@link #createContent()}.
14111412
*/
14121413
@Deprecated
14131414
public GHContentUpdateResponse createContent(String content, String commitMessage, String path, String branch) throws IOException {
1414-
final byte[] payload;
1415-
try {
1416-
payload = content.getBytes("UTF-8");
1417-
} catch (UnsupportedEncodingException ex) {
1418-
throw (IOException) new IOException("UTF-8 encoding is not supported").initCause(ex);
1419-
}
1420-
return createContent(payload, commitMessage, path, branch, null);
1415+
return createContent().content(content).message(commitMessage).path(path).branch(branch).commit();
14211416
}
14221417

14231418
/**
1424-
* Use {@link GHContentUpdateRequest}.
1419+
* Use {@link #createContent()}.
14251420
*/
14261421
@Deprecated
14271422
public GHContentUpdateResponse createContent(byte[] contentBytes, String commitMessage, String path) throws IOException {
1428-
return createContent(contentBytes, commitMessage, path, null, null);
1423+
return createContent().content(contentBytes).message(commitMessage).path(path).commit();
14291424
}
14301425

14311426
/**
1432-
* Use {@link GHContentUpdateRequest}.
1427+
* Use {@link #createContent()}.
14331428
*/
14341429
@Deprecated
14351430
public GHContentUpdateResponse createContent(byte[] contentBytes, String commitMessage, String path, String branch) throws IOException {
1436-
return createContent(contentBytes, commitMessage, path, branch, null);
1437-
}
1438-
1439-
private GHContentUpdateResponse createContent(byte[] contentBytes, String commitMessage, String path, String branch, String sha1) throws IOException {
1440-
Requester requester = new Requester(root)
1441-
.with("path", path)
1442-
.with("message", commitMessage)
1443-
.with("content", Base64.encodeBase64String(contentBytes))
1444-
.method("PUT");
1445-
1446-
if (sha1 != null) {
1447-
requester.with("sha", sha1);
1448-
}
1449-
1450-
1451-
if (branch != null) {
1452-
requester.with("branch", branch);
1453-
}
1454-
1455-
GHContentUpdateResponse response = requester.to(getApiTailUrl("contents/" + path), GHContentUpdateResponse.class);
1456-
1457-
response.getContent().wrap(this);
1458-
response.getCommit().wrapUp(this);
1459-
1460-
return response;
1431+
return createContent().content(contentBytes).message(commitMessage).path(path).branch(branch).commit();
14611432
}
14621433

14631434
public GHMilestone createMilestone(String title, String description) throws IOException {

src/test/java/org/kohsuke/github/PullRequestTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,13 @@ public void testUpdateContentSquashMerge() throws Exception {
122122
GHContentUpdateResponse response = getRepository().createContent(name, name, name, name);
123123
Thread.sleep(1000);
124124

125-
GHContentUpdateRequest updateRequest = GHContentUpdateRequest.getBuilder()
125+
getRepository().createContent()
126126
.content(name + name)
127127
.path(name)
128128
.branch(name)
129-
.commitMessage(name)
129+
.message(name)
130130
.sha(response.getContent().getSha())
131-
.build();
132-
getRepository().createContent(updateRequest);
131+
.commit();
133132
GHPullRequest p = getRepository().createPullRequest(name, name, "master", "## test squash");
134133
Thread.sleep(1000);
135134
p.merge("squash merge", null, GHPullRequest.MergeMethod.SQUASH);

0 commit comments

Comments
 (0)