From b2b7dfaf379860e2bdf8313ad752f4c5d4825ef8 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Wed, 29 Aug 2018 19:05:19 -0700 Subject: [PATCH] Massaged the change to match the existing API design convention --- .../org/kohsuke/github/GHContentBuilder.java | 73 ++++++++++++++ .../github/GHContentUpdateRequest.java | 97 ------------------- .../java/org/kohsuke/github/GHRepository.java | 55 +++-------- .../org/kohsuke/github/PullRequestTest.java | 7 +- 4 files changed, 89 insertions(+), 143 deletions(-) create mode 100644 src/main/java/org/kohsuke/github/GHContentBuilder.java delete mode 100644 src/main/java/org/kohsuke/github/GHContentUpdateRequest.java diff --git a/src/main/java/org/kohsuke/github/GHContentBuilder.java b/src/main/java/org/kohsuke/github/GHContentBuilder.java new file mode 100644 index 000000000..bd4a80c3a --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHContentBuilder.java @@ -0,0 +1,73 @@ +package org.kohsuke.github; + +import org.apache.commons.codec.binary.Base64; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; + +/** + * Used to create/update content. + * + * @author Kohsuke Kawaguchi + * @see GHRepository#createContent() + */ +public final class GHContentBuilder { + private final GHRepository repo; + private final Requester req; + private String path; + + GHContentBuilder(GHRepository repo) { + this.repo = repo; + this.req = new Requester(repo.root).method("PUT"); + } + + public GHContentBuilder path(String path) { + this.path = path; + req.with("path",path); + return this; + } + + public GHContentBuilder branch(String branch) { + req.with("branch", branch); + return this; + } + + /** + * Used when updating (but not creating a new content) to specify + * Thetblob SHA of the file being replaced. + */ + public GHContentBuilder sha(String sha) { + req.with("sha", sha); + return this; + } + + public GHContentBuilder content(byte[] content) { + req.with("content", Base64.encodeBase64String(content)); + return this; + } + + public GHContentBuilder content(String content) { + try { + return content(content.getBytes("UTF-8")); + } catch (UnsupportedEncodingException x) { + throw new AssertionError(); + } + } + + public GHContentBuilder message(String commitMessage) { + req.with("message", commitMessage); + return this; + } + + /** + * Commits a new content. + */ + public GHContentUpdateResponse commit() throws IOException { + GHContentUpdateResponse response = req.to(repo.getApiTailUrl("contents/" + path), GHContentUpdateResponse.class); + + response.getContent().wrap(repo); + response.getCommit().wrapUp(repo); + + return response; + } +} diff --git a/src/main/java/org/kohsuke/github/GHContentUpdateRequest.java b/src/main/java/org/kohsuke/github/GHContentUpdateRequest.java deleted file mode 100644 index 80bd0f6aa..000000000 --- a/src/main/java/org/kohsuke/github/GHContentUpdateRequest.java +++ /dev/null @@ -1,97 +0,0 @@ -package org.kohsuke.github; - -public class GHContentUpdateRequest { - private final String path; - private final String branch; - private final String sha; - private final byte[] content; - private final String commitMessage; - - public static GHContentUpdateRequest.Builder getBuilder() { - return new GHContentUpdateRequest.Builder(); - } - - public GHContentUpdateRequest(String path, String branch, String sha, byte[] content, String commitMessage) { - this.path = path; - this.branch = branch; - this.sha = sha; - this.content = content; - this.commitMessage = commitMessage; - } - - private GHContentUpdateRequest(Builder builder) { - this.path = builder.path; - this.branch = builder.branch; - this.sha = builder.sha; - this.content = builder.content; - this.commitMessage = builder.commitMessage; - } - - public static Builder newGHContentUpdateRequest() { - return new Builder(); - } - - public String getPath() { - return path; - } - - public String getBranch() { - return branch; - } - - public String getSha() { - return sha; - } - - public byte[] getContent() { - return content; - } - - public String getCommitMessage() { - return commitMessage; - } - - public static final class Builder { - private String path; - private String branch; - private String sha; - private byte[] content; - private String commitMessage; - - private Builder() { - } - - public GHContentUpdateRequest build() { - return new GHContentUpdateRequest(this); - } - - public Builder path(String path) { - this.path = path; - return this; - } - - public Builder branch(String branch) { - this.branch = branch; - return this; - } - - public Builder sha(String sha) { - this.sha = sha; - return this; - } - - public Builder content(byte[] content) { - this.content = content; - return this; - } - public Builder content(String content) { - this.content = content.getBytes(); - return this; - } - - public Builder commitMessage(String commitMessage) { - this.commitMessage = commitMessage; - return this; - } - } -} diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 302903ae8..66688a1a6 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -26,7 +26,6 @@ package org.kohsuke.github; import com.fasterxml.jackson.annotation.JsonProperty; import com.infradna.tool.bridge_method_injector.WithBridgeMethods; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.StringUtils; import java.io.FileNotFoundException; @@ -35,7 +34,6 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.InterruptedIOException; import java.io.Reader; -import java.io.UnsupportedEncodingException; import java.net.URL; import java.util.AbstractSet; import java.util.ArrayList; @@ -1394,70 +1392,43 @@ public class GHRepository extends GHObject { return requester.to(getApiTailUrl("readme"), GHContent.class).wrap(this); } - public GHContentUpdateResponse createContent(GHContentUpdateRequest updateRequest) throws IOException { - return createContent(updateRequest.getContent(), updateRequest.getCommitMessage(), updateRequest.getPath(), updateRequest.getBranch(), updateRequest.getSha()); + /** + * Creates a new content, or update an existing content. + */ + public GHContentBuilder createContent() { + return new GHContentBuilder(this); } /** - * Use {@link GHContentUpdateRequest}. + * Use {@link #createContent()}. */ @Deprecated public GHContentUpdateResponse createContent(String content, String commitMessage, String path) throws IOException { - return createContent(content.getBytes(), commitMessage, path, null, null); + return createContent().content(content).message(commitMessage).path(path).commit(); } /** - * Use {@link GHContentUpdateRequest}. + * Use {@link #createContent()}. */ @Deprecated public GHContentUpdateResponse createContent(String content, String commitMessage, String path, String branch) throws IOException { - final byte[] payload; - try { - payload = content.getBytes("UTF-8"); - } catch (UnsupportedEncodingException ex) { - throw (IOException) new IOException("UTF-8 encoding is not supported").initCause(ex); - } - return createContent(payload, commitMessage, path, branch, null); + return createContent().content(content).message(commitMessage).path(path).branch(branch).commit(); } /** - * Use {@link GHContentUpdateRequest}. + * Use {@link #createContent()}. */ @Deprecated public GHContentUpdateResponse createContent(byte[] contentBytes, String commitMessage, String path) throws IOException { - return createContent(contentBytes, commitMessage, path, null, null); + return createContent().content(contentBytes).message(commitMessage).path(path).commit(); } /** - * Use {@link GHContentUpdateRequest}. + * Use {@link #createContent()}. */ @Deprecated public GHContentUpdateResponse createContent(byte[] contentBytes, String commitMessage, String path, String branch) throws IOException { - return createContent(contentBytes, commitMessage, path, branch, null); - } - - private GHContentUpdateResponse createContent(byte[] contentBytes, String commitMessage, String path, String branch, String sha1) throws IOException { - Requester requester = new Requester(root) - .with("path", path) - .with("message", commitMessage) - .with("content", Base64.encodeBase64String(contentBytes)) - .method("PUT"); - - if (sha1 != null) { - requester.with("sha", sha1); - } - - - if (branch != null) { - requester.with("branch", branch); - } - - GHContentUpdateResponse response = requester.to(getApiTailUrl("contents/" + path), GHContentUpdateResponse.class); - - response.getContent().wrap(this); - response.getCommit().wrapUp(this); - - return response; + return createContent().content(contentBytes).message(commitMessage).path(path).branch(branch).commit(); } public GHMilestone createMilestone(String title, String description) throws IOException { diff --git a/src/test/java/org/kohsuke/github/PullRequestTest.java b/src/test/java/org/kohsuke/github/PullRequestTest.java index 8bbada55b..ceb741bd3 100644 --- a/src/test/java/org/kohsuke/github/PullRequestTest.java +++ b/src/test/java/org/kohsuke/github/PullRequestTest.java @@ -122,14 +122,13 @@ public class PullRequestTest extends AbstractGitHubApiTestBase { GHContentUpdateResponse response = getRepository().createContent(name, name, name, name); Thread.sleep(1000); - GHContentUpdateRequest updateRequest = GHContentUpdateRequest.getBuilder() + getRepository().createContent() .content(name + name) .path(name) .branch(name) - .commitMessage(name) + .message(name) .sha(response.getContent().getSha()) - .build(); - getRepository().createContent(updateRequest); + .commit(); GHPullRequest p = getRepository().createPullRequest(name, name, "master", "## test squash"); Thread.sleep(1000); p.merge("squash merge", null, GHPullRequest.MergeMethod.SQUASH);