Skip to content

Commit 4092ad8

Browse files
Add merge properties (spotify#7)
* Add merge properties allowing to customize message and title * Fix license files * Remove old merge method
1 parent f89b7ad commit 4092ad8

5 files changed

Lines changed: 134 additions & 17 deletions

File tree

src/main/java/com/spotify/github/v3/clients/PullRequestClient.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import static com.spotify.github.v3.clients.GitHubClient.LIST_PR_TYPE_REFERENCE;
2626

2727
import com.google.common.base.Strings;
28-
import com.google.common.collect.ImmutableMap;
28+
import com.spotify.github.v3.prs.MergeParameters;
2929
import com.spotify.github.v3.prs.PullRequest;
3030
import com.spotify.github.v3.prs.PullRequestItem;
3131
import com.spotify.github.v3.prs.requests.PullRequestCreate;
@@ -132,17 +132,17 @@ public CompletableFuture<List<CommitItem>> listCommits(final int number) {
132132
}
133133

134134
/**
135-
* Merges this pull request.
135+
* Merges a pull request with a specific commit message.
136136
*
137137
* @param number pull request number
138-
* @param sha the sha that should match this PR for the merge to happen
138+
* @param properties the properties on merging the PR, such as title, message and sha
139+
* @see "https://developer.github.com/v3/pulls/#merge-a-pull-request-merge-button"
139140
*/
140-
public CompletableFuture<Void> merge(final int number, final String sha) {
141+
public CompletableFuture<Void> merge(final int number, final MergeParameters properties) {
141142
final String path = String.format(PR_NUMBER_TEMPLATE + "/merge", owner, repo, number);
143+
final String jsonPayload = github.json().toJsonUnchecked(properties);
142144
log.debug("Merging pr, running: {}", path);
143-
return github
144-
.put(path, github.json().toJsonUnchecked(ImmutableMap.of("sha", sha)))
145-
.thenAccept(IGNORE_RESPONSE_CONSUMER);
145+
return github.put(path, jsonPayload).thenAccept(IGNORE_RESPONSE_CONSUMER);
146146
}
147147

148148
private CompletableFuture<List<PullRequestItem>> list(final String parameterPath) {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*-
2+
* -\-\-
3+
* github-client
4+
* --
5+
* Copyright (C) 2016 - 2020 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.v3.prs;
22+
23+
/**
24+
* The enum MergeMethod.
25+
*
26+
* @see "https://developer.github.com/v3/pulls/#input-3"
27+
*/
28+
public enum MergeMethod {
29+
merge,
30+
squash,
31+
rebase
32+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*-
2+
* -\-\-
3+
* github-client
4+
* --
5+
* Copyright (C) 2016 - 2020 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.v3.prs;
22+
23+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
24+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
25+
import com.spotify.github.GithubStyle;
26+
import java.util.Optional;
27+
import org.immutables.value.Value;
28+
29+
/**
30+
* The parameters for merging a Pull Request.
31+
*
32+
* @see "https://developer.github.com/v3/pulls/#input-3"
33+
*/
34+
@Value.Immutable
35+
@GithubStyle
36+
@JsonSerialize(as = ImmutableMergeParameters.class)
37+
@JsonDeserialize(as = ImmutableMergeParameters.class)
38+
public abstract class MergeParameters {
39+
/**
40+
* SHA that pull request head must match to allow merge.
41+
*
42+
* @return the string
43+
*/
44+
public abstract String sha();
45+
46+
/**
47+
* Extra detail to append to automatic commit message.
48+
*
49+
* @return the optional
50+
*/
51+
public abstract Optional<String> commitMessage();
52+
53+
/**
54+
* Title for the automatic commit message.
55+
*
56+
* @return the optional commit title
57+
*/
58+
public abstract Optional<String> commitTitle();
59+
60+
/**
61+
* Merge method to use.
62+
*
63+
* @return the merge method enum value
64+
*/
65+
@Value.Default
66+
public MergeMethod mergeMethod() {
67+
return MergeMethod.merge;
68+
}
69+
}

src/test/java/com/spotify/github/v3/prs/PullRequestTest.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,14 @@
2828
import com.google.common.io.Resources;
2929
import com.spotify.github.jackson.Json;
3030
import java.io.IOException;
31-
import org.junit.Before;
3231
import org.junit.Test;
3332

3433
public class PullRequestTest {
3534

36-
private String fixture;
37-
38-
@Before
39-
public void setUp() throws Exception {
40-
fixture =
41-
Resources.toString(getResource(this.getClass(), "pull_request.json"), defaultCharset());
42-
}
43-
4435
@Test
45-
public void testDeserialization() throws IOException {
36+
public void testDeserializationPr() throws IOException {
37+
String fixture =
38+
Resources.toString(getResource(this.getClass(), "pull_request.json"), defaultCharset());
4639
final PullRequest pr = Json.create().fromJson(fixture, PullRequest.class);
4740
assertThat(pr.mergeCommitSha().get(), is("e5bd3914e2e596debea16f433f57875b5b90bcd6"));
4841
assertThat(pr.merged(), is(false));
@@ -52,4 +45,21 @@ public void testDeserialization() throws IOException {
5245
assertThat(pr.deletions(), is(3));
5346
assertThat(pr.changedFiles(), is(5));
5447
}
48+
49+
@Test
50+
public void testSerializationMergeParams() throws IOException {
51+
String fixture =
52+
Resources.toString(getResource(this.getClass(), "merge_params_full.json"), defaultCharset());
53+
final MergeParameters fixtureParams = Json.create().fromJson(fixture, MergeParameters.class);
54+
55+
final MergeParameters params = ImmutableMergeParameters.builder()
56+
.commitTitle("a title")
57+
.commitMessage("a message")
58+
.sha("6dcb09b5b57875f334f61aebed695e2e4193db5e")
59+
.build();
60+
assertThat(params.commitMessage(), is(fixtureParams.commitMessage()));
61+
assertThat(params.commitTitle(), is(fixtureParams.commitTitle()));
62+
assertThat(params.sha(), is(fixtureParams.sha()));
63+
assertThat(params.mergeMethod(), is(MergeMethod.merge));
64+
}
5565
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"commit_title": "a title",
3+
"commit_message": "a message",
4+
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
5+
"merge_method": "merge"
6+
}

0 commit comments

Comments
 (0)