Skip to content

Commit

Permalink
feat: get memberships of an organisation by username (#1850)
Browse files Browse the repository at this point in the history
* feat: get memberships of an organisation by username

* chore: link appropriate GitHub docs

* chore: spotless apply

* chore: clean comments and remove tool-versions

* chore: replace custom org with officially supported hub4j-test-org
  • Loading branch information
fv316 authored Jun 10, 2024
1 parent bf6220c commit 3043691
Show file tree
Hide file tree
Showing 8 changed files with 334 additions and 10 deletions.
29 changes: 22 additions & 7 deletions src/main/java/org/kohsuke/github/GHOrganization.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.*;

import static org.kohsuke.github.internal.Previews.INERTIA;

// TODO: Auto-generated Javadoc

/**
* The type GHOrganization.
*
Expand Down Expand Up @@ -240,6 +235,26 @@ public boolean hasMember(GHUser user) {
}
}

/**
* Obtains the object that represents the user membership. In order to get a user's membership with an organization,
* the authenticated user must be an organization member. The state parameter in the response can be used to
* identify the user's membership status.
*
* @param username
* the user's username
* @return the GHMembership if the username belongs to the organisation, otherwise null
* @throws IOException
* the io exception
*
* @see <a href=
* "https://docs.github.com/en/rest/orgs/members?apiVersion=2022-11-28#get-organization-membership-for-a-user">documentation</a>
*/
public GHMembership getMembership(String username) throws IOException {
return root().createRequest()
.withUrlPath("/orgs/" + login + "/memberships/" + username)
.fetch(GHMembership.class);
}

/**
* Remove a member of the organisation - which will remove them from all teams, and remove their access to the
* organization’s repositories.
Expand Down
26 changes: 23 additions & 3 deletions src/test/java/org/kohsuke/github/GHOrganizationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.junit.Assert.assertThrows;

// TODO: Auto-generated Javadoc

/**
* The Class GHOrganizationTest.
*/
Expand Down Expand Up @@ -242,6 +243,25 @@ public void testInviteUser() throws IOException {
// assertTrue(org.hasMember(user));
}

/**
* Test get user membership
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void testGetMembership() throws IOException {
GHOrganization org = gitHub.getOrganization("hub4j-test-org");

GHMembership membership = org.getMembership("fv316");

assertThat(membership, notNullValue());
assertThat(membership.getRole(), equalTo(GHMembership.Role.ADMIN));
assertThat(membership.getState(), equalTo(GHMembership.State.ACTIVE));
assertThat(membership.getUser().getLogin(), equalTo("fv316"));
assertThat(membership.getOrganization().login, equalTo("hub4j-test-org"));
}

/**
* Test list members with filter.
*
Expand Down Expand Up @@ -375,7 +395,7 @@ public void testCreateTeamWithRepoAccess() throws IOException {
GHRepository repo = org.getRepository(REPO_NAME);

// Create team with access to repository. Check access was granted.
GHTeam team = org.createTeam(TEAM_NAME_CREATE, GHOrganization.Permission.PUSH, repo);
GHTeam team = org.createTeam(TEAM_NAME_CREATE, Permission.PUSH, repo);
assertThat(team.getRepositories().containsKey(REPO_NAME), is(true));
assertThat(team.getPermission(), equalTo(Permission.PUSH.toString().toLowerCase()));
}
Expand Down Expand Up @@ -424,7 +444,7 @@ public void testCreateTeamWithRepoPerm() throws Exception {
// Create team with access to repository. Check access was granted.
GHTeam team = org.createTeam(TEAM_NAME_CREATE).create();

team.add(repo, GHOrganization.Permission.PUSH);
team.add(repo, Permission.PUSH);

assertThat(
repo.getTeams()
Expand Down Expand Up @@ -453,7 +473,7 @@ public void testCreateTeamWithRepoRole() throws IOException {
// Create team with access to repository. Check access was granted.
GHTeam team = org.createTeam(TEAM_NAME_CREATE).create();

RepositoryRole role = RepositoryRole.from(GHOrganization.Permission.TRIAGE);
RepositoryRole role = RepositoryRole.from(Permission.TRIAGE);
team.add(repo, role);

// 'getPermission' does not return triage even though the UI shows that value
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"login": "fv316",
"id": 34072742,
"node_id": "MDQ6VXNlcjM0MDcyNzQy",
"avatar_url": "https://avatars.githubusercontent.com/u/34072742?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/fv316",
"html_url": "https://github.com/fv316",
"followers_url": "https://api.github.com/users/fv316/followers",
"following_url": "https://api.github.com/users/fv316/following{/other_user}",
"gists_url": "https://api.github.com/users/fv316/gists{/gist_id}",
"starred_url": "https://api.github.com/users/fv316/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/fv316/subscriptions",
"organizations_url": "https://api.github.com/users/fv316/orgs",
"repos_url": "https://api.github.com/users/fv316/repos",
"events_url": "https://api.github.com/users/fv316/events{/privacy}",
"received_events_url": "https://api.github.com/users/fv316/received_events",
"type": "User",
"site_admin": false,
"name": "Francisco Correia",
"company": "Teya",
"blog": "",
"location": "Lisbon/ London",
"email": null,
"hireable": null,
"bio": "Software developer at Teya. Electrical Engineer @ Imperial College. Data scientist @ École Polytechnique",
"twitter_username": null,
"public_repos": 29,
"public_gists": 0,
"followers": 6,
"following": 5,
"created_at": "2017-11-28T18:40:02Z",
"updated_at": "2024-06-10T08:34:28Z"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"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://avatars.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": 27,
"public_gists": 0,
"followers": 2,
"following": 0,
"html_url": "https://github.com/hub4j-test-org",
"created_at": "2014-05-10T19:39:11Z",
"updated_at": "2020-06-04T05:56:10Z",
"archived_at": null,
"type": "Organization",
"total_private_repos": 6,
"owned_private_repos": 6,
"private_gists": 0,
"disk_usage": 12014,
"collaborators": 1,
"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",
"members_can_create_public_repositories": false,
"members_can_create_private_repositories": false,
"members_can_create_internal_repositories": false,
"members_can_create_pages": true,
"members_can_fork_private_repositories": false,
"web_commit_signoff_required": false,
"members_can_create_public_pages": true,
"members_can_create_private_pages": true,
"plan": {
"name": "free",
"space": 976562499,
"private_repos": 10000,
"filled_seats": 52,
"seats": 3
},
"advanced_security_enabled_for_new_repositories": false,
"dependabot_alerts_enabled_for_new_repositories": false,
"dependabot_security_updates_enabled_for_new_repositories": false,
"dependency_graph_enabled_for_new_repositories": false,
"secret_scanning_enabled_for_new_repositories": false,
"secret_scanning_push_protection_enabled_for_new_repositories": false,
"secret_scanning_push_protection_custom_link_enabled": false,
"secret_scanning_push_protection_custom_link": null,
"secret_scanning_validity_checks_enabled": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"url": "https://api.github.com/orgs/hub4j-test-org/memberships/fv316",
"state": "active",
"role": "admin",
"organization_url": "https://api.github.com/orgs/hub4j-test-org",
"user": {
"login": "fv316",
"id": 34072742,
"node_id": "MDQ6VXNlcjM0MDcyNzQy",
"avatar_url": "https://avatars.githubusercontent.com/u/34072742?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/fv316",
"html_url": "https://github.com/fv316",
"followers_url": "https://api.github.com/users/fv316/followers",
"following_url": "https://api.github.com/users/fv316/following{/other_user}",
"gists_url": "https://api.github.com/users/fv316/gists{/gist_id}",
"starred_url": "https://api.github.com/users/fv316/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/fv316/subscriptions",
"organizations_url": "https://api.github.com/users/fv316/orgs",
"repos_url": "https://api.github.com/users/fv316/repos",
"events_url": "https://api.github.com/users/fv316/events{/privacy}",
"received_events_url": "https://api.github.com/users/fv316/received_events",
"type": "User",
"site_admin": false
},
"organization": {
"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://avatars.githubusercontent.com/u/7544739?v=4",
"description": "Hub4j Test Org Description (this could be null or blank too)"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"id": "78c86a2e-48b0-4b78-a9c8-c409b4cc58e3",
"name": "user",
"request": {
"url": "/user",
"method": "GET",
"headers": {
"Accept": {
"equalTo": "application/vnd.github.v3+json"
}
}
},
"response": {
"status": 200,
"bodyFileName": "1-user.json",
"headers": {
"Server": "GitHub.com",
"Date": "Mon, 10 Jun 2024 09:07:17 GMT",
"Content-Type": "application/json; charset=utf-8",
"Cache-Control": "private, max-age=60, s-maxage=60",
"Vary": [
"Accept, Authorization, Cookie, X-GitHub-OTP",
"Accept-Encoding, Accept, X-Requested-With"
],
"ETag": "W/\"eeffd28da9e86bf9a8b2cf03b36620ad72f1bccfccd1a5ee0a51a95dab4e05e9\"",
"Last-Modified": "Mon, 10 Jun 2024 08:34:28 GMT",
"github-authentication-token-expiration": "2024-06-17 10:06:09 +0100",
"X-GitHub-Media-Type": "github.v3; format=json",
"x-accepted-github-permissions": "allows_permissionless_access=true",
"x-github-api-version-selected": "2022-11-28",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4982",
"X-RateLimit-Reset": "1718013850",
"X-RateLimit-Used": "18",
"X-RateLimit-Resource": "core",
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
"Access-Control-Allow-Origin": "*",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options": "deny",
"X-Content-Type-Options": "nosniff",
"X-XSS-Protection": "0",
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
"Content-Security-Policy": "default-src 'none'",
"X-GitHub-Request-Id": "914E:19FF74:39926C:39E38C:6666C245"
}
},
"uuid": "78c86a2e-48b0-4b78-a9c8-c409b4cc58e3",
"persistent": true,
"insertionIndex": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"id": "9f6e328b-67ee-4481-8bcc-8b33b990dbd2",
"name": "orgs_hub4j-test-org",
"request": {
"url": "/orgs/hub4j-test-org",
"method": "GET",
"headers": {
"Accept": {
"equalTo": "application/vnd.github.v3+json"
}
}
},
"response": {
"status": 200,
"bodyFileName": "2-orgs_hub4j-test-org.json",
"headers": {
"Server": "GitHub.com",
"Date": "Mon, 10 Jun 2024 09:07:19 GMT",
"Content-Type": "application/json; charset=utf-8",
"Cache-Control": "private, max-age=60, s-maxage=60",
"Vary": [
"Accept, Authorization, Cookie, X-GitHub-OTP",
"Accept-Encoding, Accept, X-Requested-With"
],
"ETag": "W/\"67969e1a2c33b92087f1e2d76d07a944a80920a84bf07593fff4ec8c8d00f612\"",
"Last-Modified": "Thu, 04 Jun 2020 05:56:10 GMT",
"github-authentication-token-expiration": "2024-06-17 10:06:09 +0100",
"X-GitHub-Media-Type": "github.v3; format=json",
"x-accepted-github-permissions": "allows_permissionless_access=true",
"x-github-api-version-selected": "2022-11-28",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4977",
"X-RateLimit-Reset": "1718013850",
"X-RateLimit-Used": "23",
"X-RateLimit-Resource": "core",
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
"Access-Control-Allow-Origin": "*",
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options": "deny",
"X-Content-Type-Options": "nosniff",
"X-XSS-Protection": "0",
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
"Content-Security-Policy": "default-src 'none'",
"X-GitHub-Request-Id": "56D5:19B6F6:69C4020:6A85446:6666C247"
}
},
"uuid": "9f6e328b-67ee-4481-8bcc-8b33b990dbd2",
"persistent": true,
"insertionIndex": 2
}
Loading

0 comments on commit 3043691

Please sign in to comment.