Skip to content

Commit 4032144

Browse files
feat: Update the connection between an external group and a team
1 parent 33065b9 commit 4032144

28 files changed

Lines changed: 1261 additions & 1 deletion

src/main/java/org/kohsuke/github/GHTeam.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,47 @@ public List<GHExternalGroup> getExternalGroups() throws IOException {
453453
}
454454
}
455455

456+
/**
457+
* Connect an external group to the team
458+
*
459+
* @param group
460+
* the group to connect
461+
* @return the external group
462+
* @throws IOException
463+
* in case of failure
464+
* @see <a href=
465+
* "https://docs.github.com/en/enterprise-cloud@latest/rest/teams/external-groups?apiVersion=2022-11-28#update-the-connection-between-an-external-group-and-a-team">documentation</a>
466+
*/
467+
public GHExternalGroup connectToExternalGroup(final GHExternalGroup group) throws IOException {
468+
return connectToExternalGroup(group.getId());
469+
}
470+
471+
/**
472+
* Connect an external group to the team
473+
*
474+
* @param group_id
475+
* the identifier of the group to connect
476+
* @return the external group
477+
* @throws IOException
478+
* in case of failure
479+
* @see <a href=
480+
* "https://docs.github.com/en/enterprise-cloud@latest/rest/teams/external-groups?apiVersion=2022-11-28#update-the-connection-between-an-external-group-and-a-team">documentation</a>
481+
*/
482+
public GHExternalGroup connectToExternalGroup(final long group_id) throws IOException {
483+
try {
484+
return root().createRequest()
485+
.method("PATCH")
486+
.with("group_id", group_id)
487+
.withUrlPath(publicApi(EXTERNAL_GROUPS))
488+
.fetch(GHExternalGroup.class)
489+
.wrapUp(getOrganization());
490+
} catch (final HttpException e) {
491+
throw EnterpriseManagedSupport.forOrganization(getOrganization())
492+
.handleException(e, "Could not connect team to external group")
493+
.orElse(e);
494+
}
495+
}
496+
456497
/**
457498
* Gets organization.
458499
*

src/test/java/org/kohsuke/github/GHTeamTest.java

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
import static org.hamcrest.Matchers.*;
1212
import static org.hamcrest.Matchers.hasItems;
1313
import static org.junit.Assert.assertThrows;
14+
import static org.kohsuke.github.ExternalGroupsTestingSupport.*;
1415
import static org.kohsuke.github.ExternalGroupsTestingSupport.Matchers.isExternalGroupSummary;
15-
import static org.kohsuke.github.ExternalGroupsTestingSupport.groupSummary;
1616

1717
// TODO: Auto-generated Javadoc
1818
/**
@@ -320,4 +320,95 @@ public void testGetExternalGroupsTeamCannotBeExternallyManaged() throws IOExcept
320320
assertThat(failure.getMessage(), equalTo("Could not retrieve team external groups"));
321321
}
322322

323+
/**
324+
* Test connect to external group by id.
325+
*
326+
* @throws IOException
327+
* Signals that an I/O exception has occurred.
328+
*/
329+
@Test
330+
public void testConnectToExternalGroupById() throws IOException {
331+
String teamSlug = "acme-developers";
332+
333+
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
334+
GHTeam team = org.getTeamBySlug(teamSlug);
335+
336+
final GHExternalGroup group = team.connectToExternalGroup(467431);
337+
338+
assertThat(group.getId(), equalTo(467431L));
339+
assertThat(group.getName(), equalTo("acme-developers"));
340+
assertThat(group.getUpdatedAt(), notNullValue());
341+
342+
assertThat(group.getMembers(), notNullValue());
343+
assertThat(membersSummary(group),
344+
hasItems("158311279:john-doe_acme:John Doe:[email protected]",
345+
"166731041:jane-doe_acme:Jane Doe:[email protected]"));
346+
347+
assertThat(group.getTeams(), notNullValue());
348+
assertThat(teamSummary(group), hasItems("34519919:ACME-DEVELOPERS"));
349+
}
350+
351+
/**
352+
* Test fail to connect to external group from other organization.
353+
*
354+
* @throws IOException
355+
* Signals that an I/O exception has occurred.
356+
*/
357+
@Test
358+
public void testConnectToExternalGroupByGroup() throws IOException {
359+
String teamSlug = "acme-developers";
360+
361+
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
362+
GHTeam team = org.getTeamBySlug(teamSlug);
363+
GHExternalGroup group = org.getExternalGroup(467431);
364+
365+
GHExternalGroup connectedGroup = team.connectToExternalGroup(group);
366+
367+
assertThat(connectedGroup.getId(), equalTo(467431L));
368+
assertThat(connectedGroup.getName(), equalTo("acme-developers"));
369+
assertThat(connectedGroup.getUpdatedAt(), notNullValue());
370+
371+
assertThat(connectedGroup.getMembers(), notNullValue());
372+
assertThat(membersSummary(connectedGroup),
373+
hasItems("158311279:john-doe_acme:John Doe:[email protected]",
374+
"166731041:jane-doe_acme:Jane Doe:[email protected]"));
375+
376+
assertThat(group.getTeams(), notNullValue());
377+
assertThat(teamSummary(connectedGroup), hasItems("34519919:ACME-DEVELOPERS"));
378+
}
379+
380+
/**
381+
* Test failure when connecting to external group by id.
382+
*
383+
* @throws IOException
384+
* Signals that an I/O exception has occurred.
385+
*/
386+
@Test
387+
public void testFailConnectToExternalGroupWhenTeamHasMembers() throws IOException {
388+
String teamSlug = "acme-developers";
389+
390+
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
391+
GHTeam team = org.getTeamBySlug(teamSlug);
392+
393+
final GHIOException failure = assertThrows(GHTeamCannotBeExternallyManagedException.class,
394+
() -> team.connectToExternalGroup(467431));
395+
assertThat(failure.getMessage(), equalTo("Could not connect team to external group"));
396+
}
397+
398+
/**
399+
* Test failure when connecting to external group by id.
400+
*
401+
* @throws IOException
402+
* Signals that an I/O exception has occurred.
403+
*/
404+
@Test
405+
public void testFailConnectToExternalGroupTeamIsNotAvailableInOrg() throws IOException {
406+
String teamSlug = "acme-developers";
407+
408+
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
409+
GHTeam team = org.getTeamBySlug(teamSlug);
410+
411+
assertThrows(GHFileNotFoundException.class, () -> team.connectToExternalGroup(12345));
412+
}
413+
323414
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"login": "hub4j-test-org",
3+
"id": 7544739,
4+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
5+
"url": "https://api.github.com/orgs/hub4j-test-org",
6+
"repos_url": "https://api.github.com/orgs/hub4j-test-org/repos",
7+
"events_url": "https://api.github.com/orgs/hub4j-test-org/events",
8+
"hooks_url": "https://api.github.com/orgs/hub4j-test-org/hooks",
9+
"issues_url": "https://api.github.com/orgs/hub4j-test-org/issues",
10+
"members_url": "https://api.github.com/orgs/hub4j-test-org/members{/member}",
11+
"public_members_url": "https://api.github.com/orgs/hub4j-test-org/public_members{/member}",
12+
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
13+
"description": "Hub4j Test Org Description (this could be null or blank too)",
14+
"name": "Hub4j Test Org Name (this could be null or blank too)",
15+
"company": null,
16+
"blog": "https://hub4j.url.io/could/be/null",
17+
"location": "Hub4j Test Org Location (this could be null or blank too)",
18+
"email": "[email protected]",
19+
"twitter_username": null,
20+
"is_verified": false,
21+
"has_organization_projects": true,
22+
"has_repository_projects": true,
23+
"public_repos": 49,
24+
"public_gists": 0,
25+
"followers": 0,
26+
"following": 0,
27+
"html_url": "https://github.com/hub4j-test-org",
28+
"created_at": "2014-05-10T19:39:11Z",
29+
"updated_at": "2020-06-04T05:56:10Z",
30+
"type": "Organization",
31+
"total_private_repos": 3,
32+
"owned_private_repos": 3,
33+
"private_gists": 0,
34+
"disk_usage": 11979,
35+
"collaborators": 0,
36+
"billing_email": "[email protected]",
37+
"default_repository_permission": "none",
38+
"members_can_create_repositories": false,
39+
"two_factor_requirement_enabled": false,
40+
"members_allowed_repository_creation_type": "none",
41+
"members_can_create_public_repositories": false,
42+
"members_can_create_private_repositories": false,
43+
"members_can_create_internal_repositories": false,
44+
"members_can_create_pages": true,
45+
"members_can_fork_private_repositories": false,
46+
"members_can_create_public_pages": true,
47+
"members_can_create_private_pages": true,
48+
"plan": {
49+
"name": "free",
50+
"space": 976562499,
51+
"private_repos": 10000,
52+
"filled_seats": 35,
53+
"seats": 3
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "ACME-DEVELOPERS",
3+
"id": 34519919,
4+
"node_id": "MDQ6VGVhbTM0NTE5OTY=",
5+
"slug": "acme-developers",
6+
"description": "Updated by API TestModified",
7+
"privacy": "closed",
8+
"url": "https://api.github.com/organizations/7544739/team/3451996",
9+
"html_url": "https://github.com/orgs/hub4j-test-org/teams/acme-developers",
10+
"members_url": "https://api.github.com/organizations/7544739/team/3451996/members{/member}",
11+
"repositories_url": "https://api.github.com/organizations/7544739/team/3451996/repos",
12+
"permission": "pull",
13+
"created_at": "2019-10-03T21:46:12Z",
14+
"updated_at": "2022-03-04T10:36:59Z",
15+
"members_count": 1,
16+
"repos_count": 1,
17+
"organization": {
18+
"login": "hub4j-test-org",
19+
"id": 7544739,
20+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
21+
"url": "https://api.github.com/orgs/hub4j-test-org",
22+
"repos_url": "https://api.github.com/orgs/hub4j-test-org/repos",
23+
"events_url": "https://api.github.com/orgs/hub4j-test-org/events",
24+
"hooks_url": "https://api.github.com/orgs/hub4j-test-org/hooks",
25+
"issues_url": "https://api.github.com/orgs/hub4j-test-org/issues",
26+
"members_url": "https://api.github.com/orgs/hub4j-test-org/members{/member}",
27+
"public_members_url": "https://api.github.com/orgs/hub4j-test-org/public_members{/member}",
28+
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
29+
"description": "Hub4j Test Org Description (this could be null or blank too)",
30+
"name": "Hub4j Test Org Name (this could be null or blank too)",
31+
"company": null,
32+
"blog": "https://hub4j.url.io/could/be/null",
33+
"location": "Hub4j Test Org Location (this could be null or blank too)",
34+
"email": "[email protected]",
35+
"twitter_username": null,
36+
"is_verified": false,
37+
"has_organization_projects": true,
38+
"has_repository_projects": true,
39+
"public_repos": 49,
40+
"public_gists": 0,
41+
"followers": 0,
42+
"following": 0,
43+
"html_url": "https://github.com/hub4j-test-org",
44+
"created_at": "2014-05-10T19:39:11Z",
45+
"updated_at": "2020-06-04T05:56:10Z",
46+
"type": "Organization"
47+
},
48+
"parent": null
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"group_id": 467431,
3+
"group_name": "acme-developers",
4+
"updated_at": "2023-09-13T16:41:28Z",
5+
"members": [
6+
{
7+
"member_id": 158311279,
8+
"member_login": "john-doe_acme",
9+
"member_name": "John Doe",
10+
"member_email": "[email protected]"
11+
},
12+
{
13+
"member_id": 166731041,
14+
"member_login": "jane-doe_acme",
15+
"member_name": "Jane Doe",
16+
"member_email": "[email protected]"
17+
}
18+
]
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"group_id": 467431,
3+
"group_name": "acme-developers",
4+
"updated_at": "2023-09-13T16:41:28Z",
5+
"members": [
6+
{
7+
"member_id": 158311279,
8+
"member_login": "john-doe_acme",
9+
"member_name": "John Doe",
10+
"member_email": "[email protected]"
11+
},
12+
{
13+
"member_id": 166731041,
14+
"member_login": "jane-doe_acme",
15+
"member_name": "Jane Doe",
16+
"member_email": "[email protected]"
17+
}
18+
],
19+
"teams": [
20+
{
21+
"team_id": 34519919,
22+
"team_name": "ACME-DEVELOPERS"
23+
}
24+
]
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"id": "780bda7a-e76d-4840-87e4-ba6411cfc9c2",
3+
"name": "orgs_hub4j-test-org",
4+
"request": {
5+
"url": "/orgs/hub4j-test-org",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "application/vnd.github.v3+json"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 200,
15+
"bodyFileName": "1-orgs_hub4j-test-org.json",
16+
"headers": {
17+
"Server": "GitHub.com",
18+
"Date": "Fri, 04 Mar 2022 10:36:59 GMT",
19+
"Content-Type": "application/json; charset=utf-8",
20+
"Cache-Control": "private, max-age=60, s-maxage=60",
21+
"Vary": [
22+
"Accept, Authorization, Cookie, X-GitHub-OTP",
23+
"Accept-Encoding, Accept, X-Requested-With"
24+
],
25+
"ETag": "W/\"861b38147d37bd59e507771e76ec048def20dd6e5ac5b75aceb01c8b9f0ef4f5\"",
26+
"Last-Modified": "Thu, 04 Jun 2020 05:56:10 GMT",
27+
"X-OAuth-Scopes": "admin:gpg_key, admin:org, admin:org_hook, admin:public_key, admin:repo_hook, gist, notifications, repo, user, workflow, write:discussion",
28+
"X-Accepted-OAuth-Scopes": "admin:org, read:org, repo, user, write:org",
29+
"X-GitHub-Media-Type": "github.v3; format=json",
30+
"X-RateLimit-Limit": "5000",
31+
"X-RateLimit-Remaining": "4990",
32+
"X-RateLimit-Reset": "1646393817",
33+
"X-RateLimit-Used": "10",
34+
"X-RateLimit-Resource": "core",
35+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
36+
"X-Frame-Options": "deny",
37+
"X-Content-Type-Options": "nosniff",
38+
"X-XSS-Protection": "0",
39+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
40+
"Content-Security-Policy": "default-src 'none'",
41+
"X-GitHub-Request-Id": "B9F4:5E65:1A3BC41:1AAFE9E:6221EBCB"
42+
}
43+
},
44+
"uuid": "780bda7a-e76d-4840-87e4-ba6411cfc9c2",
45+
"persistent": true,
46+
"insertionIndex": 1
47+
}

0 commit comments

Comments
 (0)