Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update branch & change base on PRs #921

Merged
merged 2 commits into from Sep 22, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/org/kohsuke/github/GHPullRequest.java
Expand Up @@ -37,6 +37,7 @@

import javax.annotation.CheckForNull;

import static org.kohsuke.github.Previews.LYDIAN;
import static org.kohsuke.github.Previews.SHADOW_CAT;

/**
Expand Down Expand Up @@ -565,6 +566,41 @@ public void requestTeamReviewers(List<GHTeam> teams) throws IOException {
.send();
}

/**
* Set the base branch on the pull request
*
* @param newBaseBranch
* the name of the new base branch
* @throws IOException
* the io exception
* @return the updated pull request
*/
public GHPullRequest setBaseBranch(String newBaseBranch) throws IOException {
return root.createRequest()
.method("PATCH")
.with("base", newBaseBranch)
.withUrlPath(getApiRoute())
.fetch(GHPullRequest.class)
.wrapUp(root);
}

/**
* Updates the branch. The same as pressing the button in the web GUI.
*
* @throws IOException
* the io exception
*/
@Preview
@Deprecated
public void updateBranch() throws IOException {
root.createRequest()
.withPreview(LYDIAN)
.method("PUT")
.with("expected_head_sha", head.getSha())
.withUrlPath(getApiRoute() + "/update-branch")
.send();
}

/**
* Merge this pull request.
* <p>
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/kohsuke/github/Previews.java
Expand Up @@ -44,6 +44,13 @@ class Previews {
*/
static final String INERTIA = "application/vnd.github.inertia-preview+json";

/**
* Update a pull request branch
*
* @see <a href="https://developer.github.com/v3/previews/#update-a-pull-request-branch">GitHub API Previews</a>
*/
static final String LYDIAN = "application/vnd.github.lydian-preview+json";

/**
* Require multiple approving reviews
*
Expand Down
100 changes: 100 additions & 0 deletions src/test/java/org/kohsuke/github/GHPullRequestTest.java
Expand Up @@ -211,6 +211,106 @@ public void mergeCommitSHA() throws Exception {
fail();
}

@Test
public void setBaseBranch() throws Exception {
String prName = "testSetBaseBranch";
String originalBaseBranch = "master";
String newBaseBranch = "gh-pages";

GHPullRequest pullRequest = getRepository().createPullRequest(prName, "test/stable", "master", "## test");

assertEquals("Pull request base branch is supposed to be " + originalBaseBranch,
originalBaseBranch,
pullRequest.getBase().getRef());

GHPullRequest responsePullRequest = pullRequest.setBaseBranch(newBaseBranch);

assertEquals("Pull request base branch is supposed to be " + newBaseBranch,
newBaseBranch,
responsePullRequest.getBase().getRef());
}

@Test
public void setBaseBranchNonExisting() throws Exception {
String prName = "testSetBaseBranchNonExisting";
String originalBaseBranch = "master";
String newBaseBranch = "non-existing";

GHPullRequest pullRequest = getRepository().createPullRequest(prName, "test/stable", "master", "## test");

assertEquals("Pull request base branch is supposed to be " + originalBaseBranch,
originalBaseBranch,
pullRequest.getBase().getRef());

try {
pullRequest.setBaseBranch(newBaseBranch);
} catch (HttpException e) {
assertThat(e, instanceOf(HttpException.class));
assertThat(e.toString(), containsString("Proposed base branch 'non-existing' was not found"));
}

pullRequest.close();
}

@Test
public void updateOutdatedBranchesUnexpectedHead() throws Exception {
String prName = "testUpdateOutdatedBranches";
String outdatedRefName = "refs/heads/outdated";
GHRepository repository = gitHub.getOrganization("hub4j-test-org").getRepository("updateOutdatedBranches");

GHRef outdatedRef = repository.getRef(outdatedRefName);
outdatedRef.updateTo("6440189369f9f33b2366556a94dbc26f2cfdd969", true);

GHPullRequest outdatedPullRequest = repository.createPullRequest(prName, "outdated", "master", "## test");

do {
Thread.sleep(5000);
outdatedPullRequest.refresh();
} while (outdatedPullRequest.getMergeableState().equalsIgnoreCase("unknown"));

assertEquals("Pull request is supposed to be not up to date",
"behind",
outdatedPullRequest.getMergeableState());

outdatedRef.updateTo("f567328eb81270487864963b7d7446953353f2b5", true);

try {
outdatedPullRequest.updateBranch();
} catch (HttpException e) {
assertThat(e, instanceOf(HttpException.class));
assertThat(e.toString(), containsString("expected head sha didn’t match current head ref."));
}

outdatedPullRequest.close();
}

@Test
public void updateOutdatedBranches() throws Exception {
String prName = "testUpdateOutdatedBranches";
String outdatedRefName = "refs/heads/outdated";
GHRepository repository = gitHub.getOrganization("hub4j-test-org").getRepository("updateOutdatedBranches");

repository.getRef(outdatedRefName).updateTo("6440189369f9f33b2366556a94dbc26f2cfdd969", true);

GHPullRequest outdatedPullRequest = repository.createPullRequest(prName, "outdated", "master", "## test");

do {
Thread.sleep(5000);
outdatedPullRequest.refresh();
} while (outdatedPullRequest.getMergeableState().equalsIgnoreCase("unknown"));

assertEquals("Pull request is supposed to be not up to date",
"behind",
outdatedPullRequest.getMergeableState());

outdatedPullRequest.updateBranch();
outdatedPullRequest.refresh();

assertNotEquals("Pull request is supposed to be up to date", "behind", outdatedPullRequest.getMergeableState());

outdatedPullRequest.close();
}

@Test
public void squashMerge() throws Exception {
String name = "squashMerge";
Expand Down
@@ -0,0 +1,47 @@
{
"login": "hub4j-test-org",
"id": 7544739,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
"url": "https://api.github.com/orgs/hub4j-test-org",
"repos_url": "https://api.github.com/orgs/hub4j-test-org/repos",
"events_url": "https://api.github.com/orgs/hub4j-test-org/events",
"hooks_url": "https://api.github.com/orgs/hub4j-test-org/hooks",
"issues_url": "https://api.github.com/orgs/hub4j-test-org/issues",
"members_url": "https://api.github.com/orgs/hub4j-test-org/members{/member}",
"public_members_url": "https://api.github.com/orgs/hub4j-test-org/public_members{/member}",
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
"description": "Hub4j Test Org Description (this could be null or blank too)",
"name": "Hub4j Test Org Name (this could be null or blank too)",
"company": null,
"blog": "https://hub4j.url.io/could/be/null",
"location": "Hub4j Test Org Location (this could be null or blank too)",
"email": "hub4jtestorgemail@could.be.null.com",
"twitter_username": null,
"is_verified": false,
"has_organization_projects": true,
"has_repository_projects": true,
"public_repos": 13,
"public_gists": 0,
"followers": 0,
"following": 0,
"html_url": "https://github.com/hub4j-test-org",
"created_at": "2014-05-10T19:39:11Z",
"updated_at": "2020-06-04T05:56:10Z",
"type": "Organization",
"total_private_repos": 0,
"owned_private_repos": 0,
"private_gists": 0,
"disk_usage": 149,
"collaborators": 0,
"billing_email": "kk@kohsuke.org",
"default_repository_permission": "none",
"members_can_create_repositories": false,
"two_factor_requirement_enabled": false,
"plan": {
"name": "free",
"space": 976562499,
"private_repos": 10000,
"filled_seats": 18,
"seats": 3
}
}