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

Add fork branch sync #1898

Merged
merged 6 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
58 changes: 58 additions & 0 deletions src/main/java/org/kohsuke/github/GHBranchSync.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.kohsuke.github;

import com.fasterxml.jackson.annotation.JsonProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public class GHBranchSync extends GitHubInteractiveObject {

private GHRepository owner;

private String message;

@JsonProperty("merge_type")
private String mergeType;

@JsonProperty("base_branch")
private String baseBranch;
bitwiseman marked this conversation as resolved.
Show resolved Hide resolved

/**
* Gets owner.
*
* @return the repository that this branch is in.
*/
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
public GHRepository getOwner() {
return owner;
}

public String getMessage() {
return message;
}

public String getMergeType() {
return mergeType;
}

public String getBaseBranch() {
return baseBranch;
}

@Override
public String toString() {
return "GHBranchSync{" + "message='" + message + '\'' + ", mergeType='" + mergeType + '\'' + ", baseBranch='"

Check warning on line 42 in src/main/java/org/kohsuke/github/GHBranchSync.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/kohsuke/github/GHBranchSync.java#L42

Added line #L42 was not covered by tests
+ baseBranch + '\'' + '}';
}

/**
* Wrap.
*
* @param repo
* the repo
* @return the GH branch sync
*/
GHBranchSync wrap(GHRepository repo) {
this.owner = repo;
return this;
}

}
18 changes: 18 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,24 @@ public GHRepository fork() throws IOException {
throw new IOException(this + " was forked but can't find the new repository");
}

/**
* Sync this repository fork branch
*
* @param branch
* the branch to sync
* @return The current repository
* @throws IOException
* the io exception
*/
public GHBranchSync sync(String branch) throws IOException {
return root().createRequest()
.method("POST")
.with("branch", branch)
.withUrlPath(getApiTailUrl("merge-upstream"))
.fetch(GHBranchSync.class)
.wrap(this);
}

/**
* Forks this repository into an organization.
*
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/org/kohsuke/github/GHRepositoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,36 @@ private GHRepository getRepository(GitHub gitHub) throws IOException {
return gitHub.getOrganization("hub4j-test-org").getRepository("github-api");
}

/**
* Test sync of fork
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void sync() throws IOException {
GHRepository r = getRepository();
GHBranchSync sync = r.sync("main");
assertThat(sync.getOwner().getFullName(), equalTo("hub4j-test-org/github-api"));
assertThat(sync.getMessage(), equalTo("Successfully fetched and fast-forwarded from upstream github-api:main"));
assertThat(sync.getMergeType(), equalTo("fast-forward"));
assertThat(sync.getBaseBranch(), equalTo("github-api:main"));
}

/**
* Test sync of repository not a fork
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test(expected = HttpException.class)
public void syncNoFork() throws IOException {
GHRepository r = getRepository();
GHBranchSync sync = r.sync("main");
fail("Should have thrown an exception");
Comment on lines +75 to +76
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an assertThrows() method that I prefer for this, but I'm going to block merge for it.


}

/**
* Test zipball.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"login": "bitwiseman",
"id": 1958953,
"node_id": "MDQ6VXNlcjE5NTg5NTM=",
"avatar_url": "https://avatars3.githubusercontent.com/u/1958953?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/bitwiseman",
"html_url": "https://github.com/bitwiseman",
"followers_url": "https://api.github.com/users/bitwiseman/followers",
"following_url": "https://api.github.com/users/bitwiseman/following{/other_user}",
"gists_url": "https://api.github.com/users/bitwiseman/gists{/gist_id}",
"starred_url": "https://api.github.com/users/bitwiseman/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/bitwiseman/subscriptions",
"organizations_url": "https://api.github.com/users/bitwiseman/orgs",
"repos_url": "https://api.github.com/users/bitwiseman/repos",
"events_url": "https://api.github.com/users/bitwiseman/events{/privacy}",
"received_events_url": "https://api.github.com/users/bitwiseman/received_events",
"type": "User",
"site_admin": false,
"name": "Liam Newman",
"company": "Cloudbees, Inc.",
"blog": "",
"location": "Seattle, WA, USA",
"email": "bitwiseman@gmail.com",
"hireable": null,
"bio": "https://twitter.com/bitwiseman",
"public_repos": 166,
"public_gists": 4,
"followers": 135,
"following": 9,
"created_at": "2012-07-11T20:38:33Z",
"updated_at": "2019-09-24T19:32:29Z"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"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": null,
"is_verified": false,
"has_organization_projects": true,
"has_repository_projects": true,
"public_repos": 9,
"public_gists": 0,
"followers": 0,
"following": 0,
"html_url": "https://github.com/hub4j-test-org",
"created_at": "2014-05-10T19:39:11Z",
"updated_at": "2015-04-20T00:42:30Z",
"type": "Organization",
"total_private_repos": 0,
"owned_private_repos": 0,
"private_gists": 0,
"disk_usage": 132,
"collaborators": 0,
"billing_email": "kk@kohsuke.org",
"default_repository_permission": "none",
"members_can_create_repositories": false,
"two_factor_requirement_enabled": false,
"members_allowed_repository_creation_type": "none",
"plan": {
"name": "free",
"space": 976562499,
"private_repos": 0,
"filled_seats": 3,
"seats": 0
}
}
Loading
Loading