diff --git a/src/main/java/org/kohsuke/github/GHContentUpdateRequest.java b/src/main/java/org/kohsuke/github/GHContentUpdateRequest.java new file mode 100644 index 000000000..80bd0f6aa --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHContentUpdateRequest.java @@ -0,0 +1,97 @@ +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 869d11c54..302903ae8 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -1394,32 +1394,59 @@ public class GHRepository extends GHObject { return requester.to(getApiTailUrl("readme"), GHContent.class).wrap(this); } - public GHContentUpdateResponse createContent(String content, String commitMessage, String path,String sha1) throws IOException { - return createContent(content, commitMessage, path, null,sha1); + public GHContentUpdateResponse createContent(GHContentUpdateRequest updateRequest) throws IOException { + return createContent(updateRequest.getContent(), updateRequest.getCommitMessage(), updateRequest.getPath(), updateRequest.getBranch(), updateRequest.getSha()); } - public GHContentUpdateResponse createContent(String content, String commitMessage, String path, String branch, String sha1) throws IOException { + /** + * Use {@link GHContentUpdateRequest}. + */ + @Deprecated + public GHContentUpdateResponse createContent(String content, String commitMessage, String path) throws IOException { + return createContent(content.getBytes(), commitMessage, path, null, null); + } + + /** + * Use {@link GHContentUpdateRequest}. + */ + @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,sha1); + return createContent(payload, commitMessage, path, branch, null); } - public GHContentUpdateResponse createContent(byte[] contentBytes, String commitMessage, String path,String sha1) throws IOException { - return createContent(contentBytes, commitMessage, path, null,sha1); + /** + * Use {@link GHContentUpdateRequest}. + */ + @Deprecated + public GHContentUpdateResponse createContent(byte[] contentBytes, String commitMessage, String path) throws IOException { + return createContent(contentBytes, commitMessage, path, null, null); } - public GHContentUpdateResponse createContent(byte[] contentBytes, String commitMessage, String path, String branch, - String sha1) throws IOException { + /** + * Use {@link GHContentUpdateRequest}. + */ + @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("sha", sha1) - .with("message", commitMessage) - .with("content", Base64.encodeBase64String(contentBytes)) - .method("PUT"); + .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); diff --git a/src/test/java/org/kohsuke/github/PullRequestTest.java b/src/test/java/org/kohsuke/github/PullRequestTest.java index c2a6a2705..8bbada55b 100644 --- a/src/test/java/org/kohsuke/github/PullRequestTest.java +++ b/src/test/java/org/kohsuke/github/PullRequestTest.java @@ -105,6 +105,7 @@ public class PullRequestTest extends AbstractGitHubApiTestBase { String name = rnd.next(); GHRef masterRef = getRepository().getRef("heads/master"); GHRef branchRef = getRepository().createRef("refs/heads/" + name, masterRef.getObject().getSha()); + getRepository().createContent(name, name, name, name); Thread.sleep(1000); GHPullRequest p = getRepository().createPullRequest(name, name, "master", "## test squash"); @@ -112,6 +113,28 @@ public class PullRequestTest extends AbstractGitHubApiTestBase { p.merge("squash merge", null, GHPullRequest.MergeMethod.SQUASH); branchRef.delete(); } + @Test + public void testUpdateContentSquashMerge() throws Exception { + String name = rnd.next(); + GHRef masterRef = getRepository().getRef("heads/master"); + GHRef branchRef = getRepository().createRef("refs/heads/" + name, masterRef.getObject().getSha()); + + GHContentUpdateResponse response = getRepository().createContent(name, name, name, name); + Thread.sleep(1000); + + GHContentUpdateRequest updateRequest = GHContentUpdateRequest.getBuilder() + .content(name + name) + .path(name) + .branch(name) + .commitMessage(name) + .sha(response.getContent().getSha()) + .build(); + getRepository().createContent(updateRequest); + GHPullRequest p = getRepository().createPullRequest(name, name, "master", "## test squash"); + Thread.sleep(1000); + p.merge("squash merge", null, GHPullRequest.MergeMethod.SQUASH); + branchRef.delete(); + } @Test // Requires push access to the test repo to pass