From 5872a075d4a430ea8d9dabdc8fd9c6926a6f37a2 Mon Sep 17 00:00:00 2001 From: Henrique Truta Date: Thu, 23 Apr 2020 12:05:41 +0200 Subject: [PATCH 1/3] Add merge properties allowing to customize message and title --- .../github/v3/clients/PullRequestClient.java | 15 ++++ .../spotify/github/v3/prs/MergeMethod.java | 32 +++++++++ .../github/v3/prs/MergeParameters.java | 69 +++++++++++++++++++ .../github/v3/prs/PullRequestTest.java | 30 +++++--- .../github/v3/prs/merge_params_full.json | 6 ++ 5 files changed, 142 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/spotify/github/v3/prs/MergeMethod.java create mode 100644 src/main/java/com/spotify/github/v3/prs/MergeParameters.java create mode 100644 src/test/resources/com/spotify/github/v3/prs/merge_params_full.json 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..93f4a082 100644 --- a/src/main/java/com/spotify/github/v3/clients/PullRequestClient.java +++ b/src/main/java/com/spotify/github/v3/clients/PullRequestClient.java @@ -26,6 +26,7 @@ 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; @@ -145,6 +146,20 @@ public CompletableFuture merge(final int number, final String sha) { .thenAccept(IGNORE_RESPONSE_CONSUMER); } + /** + * Merges a pull request with a specific commit message. + * + * @param number pull request number + * @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 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, jsonPayload).thenAccept(IGNORE_RESPONSE_CONSUMER); + } + private CompletableFuture> list(final String parameterPath) { final String path = String.format(PR_TEMPLATE + parameterPath, owner, repo); log.debug("Fetching pull requests from " + path); 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..890f8626 --- /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..1fa3e7dd --- /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" +} From b9e1822885316eb0ed2f0fa2fef8646e44ff866f Mon Sep 17 00:00:00 2001 From: Henrique Truta Date: Thu, 23 Apr 2020 12:09:39 +0200 Subject: [PATCH 2/3] Fix license files --- src/main/java/com/spotify/github/v3/prs/MergeMethod.java | 4 ++-- src/main/java/com/spotify/github/v3/prs/MergeParameters.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/spotify/github/v3/prs/MergeMethod.java b/src/main/java/com/spotify/github/v3/prs/MergeMethod.java index 890f8626..c829595f 100644 --- a/src/main/java/com/spotify/github/v3/prs/MergeMethod.java +++ b/src/main/java/com/spotify/github/v3/prs/MergeMethod.java @@ -7,9 +7,9 @@ * 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. diff --git a/src/main/java/com/spotify/github/v3/prs/MergeParameters.java b/src/main/java/com/spotify/github/v3/prs/MergeParameters.java index 1fa3e7dd..4a58c0a7 100644 --- a/src/main/java/com/spotify/github/v3/prs/MergeParameters.java +++ b/src/main/java/com/spotify/github/v3/prs/MergeParameters.java @@ -7,9 +7,9 @@ * 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. From d4aff2535121d471467c8395ec6b3e87cb2844b9 Mon Sep 17 00:00:00 2001 From: Henrique Truta Date: Thu, 23 Apr 2020 12:12:23 +0200 Subject: [PATCH 3/3] Remove old merge method --- .../github/v3/clients/PullRequestClient.java | 15 --------------- 1 file changed, 15 deletions(-) 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 93f4a082..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,6 @@ 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; @@ -132,20 +131,6 @@ public CompletableFuture> listCommits(final int number) { return github.request(path, LIST_COMMIT_TYPE_REFERENCE); } - /** - * Merges this pull request. - * - * @param number pull request number - * @param sha the sha that should match this PR for the merge to happen - */ - public CompletableFuture merge(final int number, final String sha) { - final String path = String.format(PR_NUMBER_TEMPLATE + "/merge", owner, repo, number); - log.debug("Merging pr, running: {}", path); - return github - .put(path, github.json().toJsonUnchecked(ImmutableMap.of("sha", sha))) - .thenAccept(IGNORE_RESPONSE_CONSUMER); - } - /** * Merges a pull request with a specific commit message. *