Skip to content
This repository was archived by the owner on Apr 20, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -132,17 +132,17 @@ public CompletableFuture<List<CommitItem>> 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<Void> merge(final int number, final String sha) {
public CompletableFuture<Void> 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<PullRequestItem>> list(final String parameterPath) {
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/spotify/github/v3/prs/MergeMethod.java
Original file line number Diff line number Diff line change
@@ -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
}
69 changes: 69 additions & 0 deletions src/main/java/com/spotify/github/v3/prs/MergeParameters.java
Original file line number Diff line number Diff line change
@@ -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<String> commitMessage();

/**
* Title for the automatic commit message.
*
* @return the optional commit title
*/
public abstract Optional<String> commitTitle();

/**
* Merge method to use.
*
* @return the merge method enum value
*/
@Value.Default
public MergeMethod mergeMethod() {
return MergeMethod.merge;
}
}
30 changes: 20 additions & 10 deletions src/test/java/com/spotify/github/v3/prs/PullRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"commit_title": "a title",
"commit_message": "a message",
"sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
"merge_method": "merge"
}