Skip to content

Commit

Permalink
Merge pull request #683 from timja/add-full-create-team-parameters
Browse files Browse the repository at this point in the history
Add support for all create team parameters
  • Loading branch information
bitwiseman committed Jan 27, 2020
2 parents 756d470 + e008021 commit 57c4613
Show file tree
Hide file tree
Showing 31 changed files with 1,470 additions and 6 deletions.
25 changes: 22 additions & 3 deletions src/main/java/org/kohsuke/github/GHOrganization.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public GHRepository createRepository(String name,
*
* <p>
* You use the returned builder to set various properties, then call {@link GHCreateRepositoryBuilder#create()} to
* finally createa repository.
* finally create a repository.
*
* @param name
* the name
Expand Down Expand Up @@ -386,7 +386,7 @@ public enum Permission {
* @throws IOException
* the io exception
* @deprecated https://developer.github.com/v3/teams/#create-team deprecates permission field use
* {@link #createTeam(String, Collection)}
* {@link #createTeam(String)}
*/
@Deprecated
public GHTeam createTeam(String name, Permission p, Collection<GHRepository> repositories) throws IOException {
Expand All @@ -412,7 +412,7 @@ public GHTeam createTeam(String name, Permission p, Collection<GHRepository> rep
* @throws IOException
* the io exception
* @deprecated https://developer.github.com/v3/teams/#create-team deprecates permission field use
* {@link #createTeam(String, GHRepository...)}
* {@link #createTeam(String)}
*/
@Deprecated
public GHTeam createTeam(String name, Permission p, GHRepository... repositories) throws IOException {
Expand All @@ -429,7 +429,9 @@ public GHTeam createTeam(String name, Permission p, GHRepository... repositories
* @return the gh team
* @throws IOException
* the io exception
* @deprecated Use {@link #createTeam(String)} that uses a builder pattern to let you control every aspect.
*/
@Deprecated
public GHTeam createTeam(String name, Collection<GHRepository> repositories) throws IOException {
Requester post = root.createRequest().method("POST").with("name", name);
List<String> repo_names = new ArrayList<String>();
Expand All @@ -450,11 +452,28 @@ public GHTeam createTeam(String name, Collection<GHRepository> repositories) thr
* @return the gh team
* @throws IOException
* the io exception
* @deprecated Use {@link #createTeam(String)} that uses a builder pattern to let you control every aspect.
*/
@Deprecated
public GHTeam createTeam(String name, GHRepository... repositories) throws IOException {
return createTeam(name, Arrays.asList(repositories));
}

/**
* Starts a builder that creates a new team.
*
* <p>
* You use the returned builder to set various properties, then call {@link GHTeamBuilder#create()} to finally
* create a team.
*
* @param name
* the name
* @return the gh create repository builder
*/
public GHTeamBuilder createTeam(String name) {
return new GHTeamBuilder(root, login, name);
}

/**
* List up repositories that has some open pull requests.
* <p>
Expand Down
33 changes: 32 additions & 1 deletion src/main/java/org/kohsuke/github/GHTeam.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,22 @@
* @author Kohsuke Kawaguchi
*/
public class GHTeam implements Refreshable {
private String name, permission, slug, description;
private String name;
private String permission;
private String slug;
private String description;
private Privacy privacy;

private int id;
private GHOrganization organization; // populated by GET /user/teams where Teams+Orgs are returned together

protected /* final */ GitHub root;

public enum Privacy {
SECRET, // only visible to organization owners and members of this team.
CLOSED // visible to all members of this organization.
}

/**
* Member's role in a team
*/
Expand Down Expand Up @@ -94,6 +104,15 @@ public String getDescription() {
return description;
}

/**
* Gets the privacy state.
*
* @return the privacy state.
*/
public Privacy getPrivacy() {
return privacy;
}

/**
* Sets description.
*
Expand All @@ -106,6 +125,18 @@ public void setDescription(String description) throws IOException {
root.createRequest().method("PATCH").with("description", description).withUrlPath(api("")).send();
}

/**
* Updates the team's privacy setting.
*
* @param privacy
* the privacy
* @throws IOException
* the io exception
*/
public void setPrivacy(Privacy privacy) throws IOException {
root.createRequest().method("PATCH").with("privacy", privacy).withUrlPath(api("")).send();
}

/**
* Gets id.
*
Expand Down
93 changes: 93 additions & 0 deletions src/main/java/org/kohsuke/github/GHTeamBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.kohsuke.github;

import java.io.IOException;

/**
* Creates a team.
*
* https://developer.github.com/v3/teams/#create-team
*/
public class GHTeamBuilder {

private final GitHub root;
protected final Requester builder;
private final String orgName;

public GHTeamBuilder(GitHub root, String orgName, String name) {
this.root = root;
this.orgName = orgName;
this.builder = root.createRequest();
this.builder.with("name", name);
}

/**
* Description for this team.
*
* @param description
* description of team
* @return a builder to continue with building
*/
public GHTeamBuilder description(String description) {
this.builder.with("description", description);
return this;
}

/**
* Maintainers for this team.
*
* @param maintainers
* maintainers of team
* @return a builder to continue with building
*/
public GHTeamBuilder maintainers(String... maintainers) {
this.builder.with("maintainers", maintainers);
return this;
}

/**
* Repository names to add this team to.
*
* @param repoNames
* repoNames to add team to
* @return a builder to continue with building
*/
public GHTeamBuilder repositories(String... repoNames) {
this.builder.with("repo_names", repoNames);
return this;
}

/**
* Description for this team
*
* @param privacy
* privacy of team
* @return a builder to continue with building
*/
public GHTeamBuilder privacy(GHTeam.Privacy privacy) {
this.builder.with("privacy", privacy);
return this;
}

/**
* Parent team id for this team
*
* @param parentTeamId
* parentTeamId of team
* @return a builder to continue with building
*/
public GHTeamBuilder parentTeamId(int parentTeamId) {
this.builder.with("parent_team_id", parentTeamId);
return this;
}

/**
* Creates a team with all the parameters.
*
* @return the gh team
* @throws IOException
* if team cannot be created
*/
public GHTeam create() throws IOException {
return builder.method("POST").withUrlPath("/orgs/" + orgName + "/teams").fetch(GHTeam.class).wrapUp(root);
}
}
28 changes: 26 additions & 2 deletions src/test/java/org/kohsuke/github/GHOrganizationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void testCreateTeamWithRepoAccess() throws IOException {
// Create team with access to repository. Check access was granted.
GHTeam team = org.createTeam(TEAM_NAME_CREATE, GHOrganization.Permission.PUSH, repo);
Assert.assertTrue(team.getRepositories().containsKey(REPO_NAME));
Assert.assertEquals(Permission.PUSH.toString().toLowerCase(), team.getPermission());
assertEquals(Permission.PUSH.toString().toLowerCase(), team.getPermission());
}

@Test
Expand All @@ -100,6 +100,30 @@ public void testCreateTeam() throws IOException {
// Create team with no permission field. Verify that default permission is pull
GHTeam team = org.createTeam(TEAM_NAME_CREATE, repo);
Assert.assertTrue(team.getRepositories().containsKey(REPO_NAME));
Assert.assertEquals(DEFAULT_PERMISSION, team.getPermission());
assertEquals(DEFAULT_PERMISSION, team.getPermission());
}

@Test
public void testCreateVisibleTeam() throws IOException {
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);

GHTeam team = org.createTeam(TEAM_NAME_CREATE).privacy(GHTeam.Privacy.CLOSED).create();
assertEquals(GHTeam.Privacy.CLOSED, team.getPrivacy());
}

@Test
public void testCreateAllArgsTeam() throws IOException {
String REPO_NAME = "github-api";
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);

GHTeam team = org.createTeam(TEAM_NAME_CREATE)
.description("Team description")
.maintainers("bitwiseman")
.repositories(REPO_NAME)
.privacy(GHTeam.Privacy.CLOSED)
.parentTeamId(3617900)
.create();
assertEquals("Team description", team.getDescription());
assertEquals(GHTeam.Privacy.CLOSED, team.getPrivacy());
}
}
24 changes: 24 additions & 0 deletions src/test/java/org/kohsuke/github/GHTeamTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.kohsuke.github;

import org.junit.Test;
import org.kohsuke.github.GHTeam.Privacy;

import java.io.IOException;

Expand Down Expand Up @@ -30,4 +31,27 @@ public void testSetDescription() throws IOException {
assertEquals(description, team.getDescription());
}

@Test
public void testSetPrivacy() throws IOException {
String teamSlug = "dummy-team";
Privacy privacy = Privacy.CLOSED;

// Set the privacy.
GHTeam team = gitHub.getOrganization(GITHUB_API_TEST_ORG).getTeamBySlug(teamSlug);
team.setPrivacy(privacy);

// Check that it was set correctly.
team = gitHub.getOrganization(GITHUB_API_TEST_ORG).getTeamBySlug(teamSlug);
assertEquals(privacy, team.getPrivacy());

privacy = Privacy.SECRET;

// Set the privacy.
team.setPrivacy(privacy);

// Check that it was set correctly.
team = gitHub.getOrganization(GITHUB_API_TEST_ORG).getTeamBySlug(teamSlug);
assertEquals(privacy, team.getPrivacy());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"login": "github-api-test-org",
"id": 7544739,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
"url": "https://api.github.com/orgs/github-api-test-org",
"repos_url": "https://api.github.com/orgs/github-api-test-org/repos",
"events_url": "https://api.github.com/orgs/github-api-test-org/events",
"hooks_url": "https://api.github.com/orgs/github-api-test-org/hooks",
"issues_url": "https://api.github.com/orgs/github-api-test-org/issues",
"members_url": "https://api.github.com/orgs/github-api-test-org/members{/member}",
"public_members_url": "https://api.github.com/orgs/github-api-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": 10,
"public_gists": 0,
"followers": 0,
"following": 0,
"html_url": "https://github.com/github-api-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,
"plan": {
"name": "free",
"space": 976562499,
"private_repos": 0,
"filled_seats": 7,
"seats": 0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "create-team-test",
"id": 3618001,
"node_id": "MDQ6VGVhbTM2MTgwMDE=",
"slug": "create-team-test",
"description": "Team description",
"privacy": "closed",
"url": "https://api.github.com/organizations/49127317/team/3618001",
"html_url": "https://github.com/orgs/github-api-test-org/teams/create-team-test",
"members_url": "https://api.github.com/organizations/49127317/team/3618001/members{/member}",
"repositories_url": "https://api.github.com/organizations/49127317/team/3618001/repos",
"permission": "pull",
"parent": {
"name": "Core Developers",
"id": 3617900,
"node_id": "MDQ6VGVhbTM2MTc5MDA=",
"slug": "core-developers",
"description": "",
"privacy": "closed",
"url": "https://api.github.com/organizations/49127317/team/3617900",
"html_url": "https://github.com/orgs/github-api-test-org/teams/core-developers",
"members_url": "https://api.github.com/organizations/49127317/team/3617900/members{/member}",
"repositories_url": "https://api.github.com/organizations/49127317/team/3617900/repos",
"permission": "pull"
},
"created_at": "2020-01-25T19:41:44Z",
"updated_at": "2020-01-25T19:41:44Z",
"members_count": 1,
"repos_count": 1,
"organization": {
"login": "github-api-test-org",
"id": 49127317,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjQ5MTI3MzE3",
"url": "https://api.github.com/orgs/github-api-test-org",
"repos_url": "https://api.github.com/orgs/github-api-test-org/repos",
"events_url": "https://api.github.com/orgs/github-api-test-org/events",
"hooks_url": "https://api.github.com/orgs/github-api-test-org/hooks",
"issues_url": "https://api.github.com/orgs/github-api-test-org/issues",
"members_url": "https://api.github.com/orgs/github-api-test-org/members{/member}",
"public_members_url": "https://api.github.com/orgs/github-api-test-org/public_members{/member}",
"avatar_url": "https://avatars3.githubusercontent.com/u/49127317?v=4",
"description": null,
"is_verified": false,
"has_organization_projects": true,
"has_repository_projects": true,
"public_repos": 4,
"public_gists": 0,
"followers": 0,
"following": 0,
"html_url": "https://github.com/github-api-test-org",
"created_at": "2019-03-31T17:42:10Z",
"updated_at": "2019-10-07T20:06:18Z",
"type": "Organization"
}
}
Loading

0 comments on commit 57c4613

Please sign in to comment.