diff --git a/src/main/java/com/spotify/github/v3/clients/PullRequestClient.java b/src/main/java/com/spotify/github/v3/clients/PullRequestClient.java index df643e79..a1c068de 100644 --- a/src/main/java/com/spotify/github/v3/clients/PullRequestClient.java +++ b/src/main/java/com/spotify/github/v3/clients/PullRequestClient.java @@ -25,7 +25,7 @@ import static com.spotify.github.v3.clients.GitHubClient.LIST_PR_TYPE_REFERENCE; import com.google.common.base.Strings; -import com.google.common.collect.ImmutableMap; +import com.spotify.github.v3.prs.MergeParameters; import com.spotify.github.v3.prs.PullRequest; import com.spotify.github.v3.prs.PullRequestItem; import com.spotify.github.v3.prs.requests.PullRequestCreate; @@ -132,17 +132,17 @@ public CompletableFuture> listCommits(final int number) { } /** - * Merges this pull request. + * Merges a pull request with a specific commit message. * * @param number pull request number - * @param sha the sha that should match this PR for the merge to happen + * @param properties the properties on merging the PR, such as title, message and sha + * @see "https://developer.github.com/v3/pulls/#merge-a-pull-request-merge-button" */ - public CompletableFuture merge(final int number, final String sha) { + public CompletableFuture merge(final int number, final MergeParameters properties) { final String path = String.format(PR_NUMBER_TEMPLATE + "/merge", owner, repo, number); + final String jsonPayload = github.json().toJsonUnchecked(properties); log.debug("Merging pr, running: {}", path); - return github - .put(path, github.json().toJsonUnchecked(ImmutableMap.of("sha", sha))) - .thenAccept(IGNORE_RESPONSE_CONSUMER); + return github.put(path, jsonPayload).thenAccept(IGNORE_RESPONSE_CONSUMER); } private CompletableFuture> list(final String parameterPath) { diff --git a/src/main/java/com/spotify/github/v3/prs/MergeMethod.java b/src/main/java/com/spotify/github/v3/prs/MergeMethod.java new file mode 100644 index 00000000..c829595f --- /dev/null +++ b/src/main/java/com/spotify/github/v3/prs/MergeMethod.java @@ -0,0 +1,32 @@ +/*- + * -\-\- + * github-client + * -- + * Copyright (C) 2016 - 2020 Spotify AB + * -- + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * -/-/- + */ + +package com.spotify.github.v3.prs; + +/** + * The enum MergeMethod. + * + * @see "https://developer.github.com/v3/pulls/#input-3" + */ +public enum MergeMethod { + merge, + squash, + rebase +} diff --git a/src/main/java/com/spotify/github/v3/prs/MergeParameters.java b/src/main/java/com/spotify/github/v3/prs/MergeParameters.java new file mode 100644 index 00000000..4a58c0a7 --- /dev/null +++ b/src/main/java/com/spotify/github/v3/prs/MergeParameters.java @@ -0,0 +1,69 @@ +/*- + * -\-\- + * github-client + * -- + * Copyright (C) 2016 - 2020 Spotify AB + * -- + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * -/-/- + */ + +package com.spotify.github.v3.prs; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.spotify.github.GithubStyle; +import java.util.Optional; +import org.immutables.value.Value; + +/** + * The parameters for merging a Pull Request. + * + * @see "https://developer.github.com/v3/pulls/#input-3" + */ +@Value.Immutable +@GithubStyle +@JsonSerialize(as = ImmutableMergeParameters.class) +@JsonDeserialize(as = ImmutableMergeParameters.class) +public abstract class MergeParameters { + /** + * SHA that pull request head must match to allow merge. + * + * @return the string + */ + public abstract String sha(); + + /** + * Extra detail to append to automatic commit message. + * + * @return the optional + */ + public abstract Optional commitMessage(); + + /** + * Title for the automatic commit message. + * + * @return the optional commit title + */ + public abstract Optional commitTitle(); + + /** + * Merge method to use. + * + * @return the merge method enum value + */ + @Value.Default + public MergeMethod mergeMethod() { + return MergeMethod.merge; + } +} diff --git a/src/test/java/com/spotify/github/v3/prs/PullRequestTest.java b/src/test/java/com/spotify/github/v3/prs/PullRequestTest.java index 8b146ee9..97530dd3 100644 --- a/src/test/java/com/spotify/github/v3/prs/PullRequestTest.java +++ b/src/test/java/com/spotify/github/v3/prs/PullRequestTest.java @@ -28,21 +28,14 @@ import com.google.common.io.Resources; import com.spotify.github.jackson.Json; import java.io.IOException; -import org.junit.Before; import org.junit.Test; public class PullRequestTest { - private String fixture; - - @Before - public void setUp() throws Exception { - fixture = - Resources.toString(getResource(this.getClass(), "pull_request.json"), defaultCharset()); - } - @Test - public void testDeserialization() throws IOException { + public void testDeserializationPr() throws IOException { + String fixture = + Resources.toString(getResource(this.getClass(), "pull_request.json"), defaultCharset()); final PullRequest pr = Json.create().fromJson(fixture, PullRequest.class); assertThat(pr.mergeCommitSha().get(), is("e5bd3914e2e596debea16f433f57875b5b90bcd6")); assertThat(pr.merged(), is(false)); @@ -52,4 +45,21 @@ public void testDeserialization() throws IOException { assertThat(pr.deletions(), is(3)); assertThat(pr.changedFiles(), is(5)); } + + @Test + public void testSerializationMergeParams() throws IOException { + String fixture = + Resources.toString(getResource(this.getClass(), "merge_params_full.json"), defaultCharset()); + final MergeParameters fixtureParams = Json.create().fromJson(fixture, MergeParameters.class); + + final MergeParameters params = ImmutableMergeParameters.builder() + .commitTitle("a title") + .commitMessage("a message") + .sha("6dcb09b5b57875f334f61aebed695e2e4193db5e") + .build(); + assertThat(params.commitMessage(), is(fixtureParams.commitMessage())); + assertThat(params.commitTitle(), is(fixtureParams.commitTitle())); + assertThat(params.sha(), is(fixtureParams.sha())); + assertThat(params.mergeMethod(), is(MergeMethod.merge)); + } } diff --git a/src/test/resources/com/spotify/github/v3/prs/merge_params_full.json b/src/test/resources/com/spotify/github/v3/prs/merge_params_full.json new file mode 100644 index 00000000..34a892d3 --- /dev/null +++ b/src/test/resources/com/spotify/github/v3/prs/merge_params_full.json @@ -0,0 +1,6 @@ +{ + "commit_title": "a title", + "commit_message": "a message", + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", + "merge_method": "merge" +}