Skip to content

Commit 57c4613

Browse files
authored
Merge pull request #683 from timja/add-full-create-team-parameters
Add support for all create team parameters
2 parents 756d470 + e008021 commit 57c4613

File tree

31 files changed

+1470
-6
lines changed

31 files changed

+1470
-6
lines changed

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public GHRepository createRepository(String name,
8888
*
8989
* <p>
9090
* You use the returned builder to set various properties, then call {@link GHCreateRepositoryBuilder#create()} to
91-
* finally createa repository.
91+
* finally create a repository.
9292
*
9393
* @param name
9494
* the name
@@ -386,7 +386,7 @@ public enum Permission {
386386
* @throws IOException
387387
* the io exception
388388
* @deprecated https://developer.github.com/v3/teams/#create-team deprecates permission field use
389-
* {@link #createTeam(String, Collection)}
389+
* {@link #createTeam(String)}
390390
*/
391391
@Deprecated
392392
public GHTeam createTeam(String name, Permission p, Collection<GHRepository> repositories) throws IOException {
@@ -412,7 +412,7 @@ public GHTeam createTeam(String name, Permission p, Collection<GHRepository> rep
412412
* @throws IOException
413413
* the io exception
414414
* @deprecated https://developer.github.com/v3/teams/#create-team deprecates permission field use
415-
* {@link #createTeam(String, GHRepository...)}
415+
* {@link #createTeam(String)}
416416
*/
417417
@Deprecated
418418
public GHTeam createTeam(String name, Permission p, GHRepository... repositories) throws IOException {
@@ -429,7 +429,9 @@ public GHTeam createTeam(String name, Permission p, GHRepository... repositories
429429
* @return the gh team
430430
* @throws IOException
431431
* the io exception
432+
* @deprecated Use {@link #createTeam(String)} that uses a builder pattern to let you control every aspect.
432433
*/
434+
@Deprecated
433435
public GHTeam createTeam(String name, Collection<GHRepository> repositories) throws IOException {
434436
Requester post = root.createRequest().method("POST").with("name", name);
435437
List<String> repo_names = new ArrayList<String>();
@@ -450,11 +452,28 @@ public GHTeam createTeam(String name, Collection<GHRepository> repositories) thr
450452
* @return the gh team
451453
* @throws IOException
452454
* the io exception
455+
* @deprecated Use {@link #createTeam(String)} that uses a builder pattern to let you control every aspect.
453456
*/
457+
@Deprecated
454458
public GHTeam createTeam(String name, GHRepository... repositories) throws IOException {
455459
return createTeam(name, Arrays.asList(repositories));
456460
}
457461

462+
/**
463+
* Starts a builder that creates a new team.
464+
*
465+
* <p>
466+
* You use the returned builder to set various properties, then call {@link GHTeamBuilder#create()} to finally
467+
* create a team.
468+
*
469+
* @param name
470+
* the name
471+
* @return the gh create repository builder
472+
*/
473+
public GHTeamBuilder createTeam(String name) {
474+
return new GHTeamBuilder(root, login, name);
475+
}
476+
458477
/**
459478
* List up repositories that has some open pull requests.
460479
* <p>

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,22 @@
1212
* @author Kohsuke Kawaguchi
1313
*/
1414
public class GHTeam implements Refreshable {
15-
private String name, permission, slug, description;
15+
private String name;
16+
private String permission;
17+
private String slug;
18+
private String description;
19+
private Privacy privacy;
20+
1621
private int id;
1722
private GHOrganization organization; // populated by GET /user/teams where Teams+Orgs are returned together
1823

1924
protected /* final */ GitHub root;
2025

26+
public enum Privacy {
27+
SECRET, // only visible to organization owners and members of this team.
28+
CLOSED // visible to all members of this organization.
29+
}
30+
2131
/**
2232
* Member's role in a team
2333
*/
@@ -94,6 +104,15 @@ public String getDescription() {
94104
return description;
95105
}
96106

107+
/**
108+
* Gets the privacy state.
109+
*
110+
* @return the privacy state.
111+
*/
112+
public Privacy getPrivacy() {
113+
return privacy;
114+
}
115+
97116
/**
98117
* Sets description.
99118
*
@@ -106,6 +125,18 @@ public void setDescription(String description) throws IOException {
106125
root.createRequest().method("PATCH").with("description", description).withUrlPath(api("")).send();
107126
}
108127

128+
/**
129+
* Updates the team's privacy setting.
130+
*
131+
* @param privacy
132+
* the privacy
133+
* @throws IOException
134+
* the io exception
135+
*/
136+
public void setPrivacy(Privacy privacy) throws IOException {
137+
root.createRequest().method("PATCH").with("privacy", privacy).withUrlPath(api("")).send();
138+
}
139+
109140
/**
110141
* Gets id.
111142
*
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package org.kohsuke.github;
2+
3+
import java.io.IOException;
4+
5+
/**
6+
* Creates a team.
7+
*
8+
* https://developer.github.com/v3/teams/#create-team
9+
*/
10+
public class GHTeamBuilder {
11+
12+
private final GitHub root;
13+
protected final Requester builder;
14+
private final String orgName;
15+
16+
public GHTeamBuilder(GitHub root, String orgName, String name) {
17+
this.root = root;
18+
this.orgName = orgName;
19+
this.builder = root.createRequest();
20+
this.builder.with("name", name);
21+
}
22+
23+
/**
24+
* Description for this team.
25+
*
26+
* @param description
27+
* description of team
28+
* @return a builder to continue with building
29+
*/
30+
public GHTeamBuilder description(String description) {
31+
this.builder.with("description", description);
32+
return this;
33+
}
34+
35+
/**
36+
* Maintainers for this team.
37+
*
38+
* @param maintainers
39+
* maintainers of team
40+
* @return a builder to continue with building
41+
*/
42+
public GHTeamBuilder maintainers(String... maintainers) {
43+
this.builder.with("maintainers", maintainers);
44+
return this;
45+
}
46+
47+
/**
48+
* Repository names to add this team to.
49+
*
50+
* @param repoNames
51+
* repoNames to add team to
52+
* @return a builder to continue with building
53+
*/
54+
public GHTeamBuilder repositories(String... repoNames) {
55+
this.builder.with("repo_names", repoNames);
56+
return this;
57+
}
58+
59+
/**
60+
* Description for this team
61+
*
62+
* @param privacy
63+
* privacy of team
64+
* @return a builder to continue with building
65+
*/
66+
public GHTeamBuilder privacy(GHTeam.Privacy privacy) {
67+
this.builder.with("privacy", privacy);
68+
return this;
69+
}
70+
71+
/**
72+
* Parent team id for this team
73+
*
74+
* @param parentTeamId
75+
* parentTeamId of team
76+
* @return a builder to continue with building
77+
*/
78+
public GHTeamBuilder parentTeamId(int parentTeamId) {
79+
this.builder.with("parent_team_id", parentTeamId);
80+
return this;
81+
}
82+
83+
/**
84+
* Creates a team with all the parameters.
85+
*
86+
* @return the gh team
87+
* @throws IOException
88+
* if team cannot be created
89+
*/
90+
public GHTeam create() throws IOException {
91+
return builder.method("POST").withUrlPath("/orgs/" + orgName + "/teams").fetch(GHTeam.class).wrapUp(root);
92+
}
93+
}

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void testCreateTeamWithRepoAccess() throws IOException {
8686
// Create team with access to repository. Check access was granted.
8787
GHTeam team = org.createTeam(TEAM_NAME_CREATE, GHOrganization.Permission.PUSH, repo);
8888
Assert.assertTrue(team.getRepositories().containsKey(REPO_NAME));
89-
Assert.assertEquals(Permission.PUSH.toString().toLowerCase(), team.getPermission());
89+
assertEquals(Permission.PUSH.toString().toLowerCase(), team.getPermission());
9090
}
9191

9292
@Test
@@ -100,6 +100,30 @@ public void testCreateTeam() throws IOException {
100100
// Create team with no permission field. Verify that default permission is pull
101101
GHTeam team = org.createTeam(TEAM_NAME_CREATE, repo);
102102
Assert.assertTrue(team.getRepositories().containsKey(REPO_NAME));
103-
Assert.assertEquals(DEFAULT_PERMISSION, team.getPermission());
103+
assertEquals(DEFAULT_PERMISSION, team.getPermission());
104+
}
105+
106+
@Test
107+
public void testCreateVisibleTeam() throws IOException {
108+
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
109+
110+
GHTeam team = org.createTeam(TEAM_NAME_CREATE).privacy(GHTeam.Privacy.CLOSED).create();
111+
assertEquals(GHTeam.Privacy.CLOSED, team.getPrivacy());
112+
}
113+
114+
@Test
115+
public void testCreateAllArgsTeam() throws IOException {
116+
String REPO_NAME = "github-api";
117+
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
118+
119+
GHTeam team = org.createTeam(TEAM_NAME_CREATE)
120+
.description("Team description")
121+
.maintainers("bitwiseman")
122+
.repositories(REPO_NAME)
123+
.privacy(GHTeam.Privacy.CLOSED)
124+
.parentTeamId(3617900)
125+
.create();
126+
assertEquals("Team description", team.getDescription());
127+
assertEquals(GHTeam.Privacy.CLOSED, team.getPrivacy());
104128
}
105129
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.kohsuke.github;
22

33
import org.junit.Test;
4+
import org.kohsuke.github.GHTeam.Privacy;
45

56
import java.io.IOException;
67

@@ -30,4 +31,27 @@ public void testSetDescription() throws IOException {
3031
assertEquals(description, team.getDescription());
3132
}
3233

34+
@Test
35+
public void testSetPrivacy() throws IOException {
36+
String teamSlug = "dummy-team";
37+
Privacy privacy = Privacy.CLOSED;
38+
39+
// Set the privacy.
40+
GHTeam team = gitHub.getOrganization(GITHUB_API_TEST_ORG).getTeamBySlug(teamSlug);
41+
team.setPrivacy(privacy);
42+
43+
// Check that it was set correctly.
44+
team = gitHub.getOrganization(GITHUB_API_TEST_ORG).getTeamBySlug(teamSlug);
45+
assertEquals(privacy, team.getPrivacy());
46+
47+
privacy = Privacy.SECRET;
48+
49+
// Set the privacy.
50+
team.setPrivacy(privacy);
51+
52+
// Check that it was set correctly.
53+
team = gitHub.getOrganization(GITHUB_API_TEST_ORG).getTeamBySlug(teamSlug);
54+
assertEquals(privacy, team.getPrivacy());
55+
}
56+
3357
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"login": "github-api-test-org",
3+
"id": 7544739,
4+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
5+
"url": "https://api.github.com/orgs/github-api-test-org",
6+
"repos_url": "https://api.github.com/orgs/github-api-test-org/repos",
7+
"events_url": "https://api.github.com/orgs/github-api-test-org/events",
8+
"hooks_url": "https://api.github.com/orgs/github-api-test-org/hooks",
9+
"issues_url": "https://api.github.com/orgs/github-api-test-org/issues",
10+
"members_url": "https://api.github.com/orgs/github-api-test-org/members{/member}",
11+
"public_members_url": "https://api.github.com/orgs/github-api-test-org/public_members{/member}",
12+
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
13+
"description": null,
14+
"is_verified": false,
15+
"has_organization_projects": true,
16+
"has_repository_projects": true,
17+
"public_repos": 10,
18+
"public_gists": 0,
19+
"followers": 0,
20+
"following": 0,
21+
"html_url": "https://github.com/github-api-test-org",
22+
"created_at": "2014-05-10T19:39:11Z",
23+
"updated_at": "2015-04-20T00:42:30Z",
24+
"type": "Organization",
25+
"total_private_repos": 0,
26+
"owned_private_repos": 0,
27+
"private_gists": 0,
28+
"disk_usage": 132,
29+
"collaborators": 0,
30+
"billing_email": "[email protected]",
31+
"default_repository_permission": "none",
32+
"members_can_create_repositories": false,
33+
"two_factor_requirement_enabled": false,
34+
"plan": {
35+
"name": "free",
36+
"space": 976562499,
37+
"private_repos": 0,
38+
"filled_seats": 7,
39+
"seats": 0
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"name": "create-team-test",
3+
"id": 3618001,
4+
"node_id": "MDQ6VGVhbTM2MTgwMDE=",
5+
"slug": "create-team-test",
6+
"description": "Team description",
7+
"privacy": "closed",
8+
"url": "https://api.github.com/organizations/49127317/team/3618001",
9+
"html_url": "https://github.com/orgs/github-api-test-org/teams/create-team-test",
10+
"members_url": "https://api.github.com/organizations/49127317/team/3618001/members{/member}",
11+
"repositories_url": "https://api.github.com/organizations/49127317/team/3618001/repos",
12+
"permission": "pull",
13+
"parent": {
14+
"name": "Core Developers",
15+
"id": 3617900,
16+
"node_id": "MDQ6VGVhbTM2MTc5MDA=",
17+
"slug": "core-developers",
18+
"description": "",
19+
"privacy": "closed",
20+
"url": "https://api.github.com/organizations/49127317/team/3617900",
21+
"html_url": "https://github.com/orgs/github-api-test-org/teams/core-developers",
22+
"members_url": "https://api.github.com/organizations/49127317/team/3617900/members{/member}",
23+
"repositories_url": "https://api.github.com/organizations/49127317/team/3617900/repos",
24+
"permission": "pull"
25+
},
26+
"created_at": "2020-01-25T19:41:44Z",
27+
"updated_at": "2020-01-25T19:41:44Z",
28+
"members_count": 1,
29+
"repos_count": 1,
30+
"organization": {
31+
"login": "github-api-test-org",
32+
"id": 49127317,
33+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjQ5MTI3MzE3",
34+
"url": "https://api.github.com/orgs/github-api-test-org",
35+
"repos_url": "https://api.github.com/orgs/github-api-test-org/repos",
36+
"events_url": "https://api.github.com/orgs/github-api-test-org/events",
37+
"hooks_url": "https://api.github.com/orgs/github-api-test-org/hooks",
38+
"issues_url": "https://api.github.com/orgs/github-api-test-org/issues",
39+
"members_url": "https://api.github.com/orgs/github-api-test-org/members{/member}",
40+
"public_members_url": "https://api.github.com/orgs/github-api-test-org/public_members{/member}",
41+
"avatar_url": "https://avatars3.githubusercontent.com/u/49127317?v=4",
42+
"description": null,
43+
"is_verified": false,
44+
"has_organization_projects": true,
45+
"has_repository_projects": true,
46+
"public_repos": 4,
47+
"public_gists": 0,
48+
"followers": 0,
49+
"following": 0,
50+
"html_url": "https://github.com/github-api-test-org",
51+
"created_at": "2019-03-31T17:42:10Z",
52+
"updated_at": "2019-10-07T20:06:18Z",
53+
"type": "Organization"
54+
}
55+
}

0 commit comments

Comments
 (0)