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 support for all create team parameters #683

Merged
merged 8 commits into from
Jan 27, 2020
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
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 updatePrivacy(Privacy privacy) throws IOException {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public void updatePrivacy(Privacy privacy) throws IOException {
public void setPrivacy(Privacy privacy) throws IOException {

root.createRequest().method("PATCH").with("privacy", privacy).withUrlPath(api("")).send();
Copy link
Member

Choose a reason for hiding this comment

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

@timja
This matches the existing behavior of other set methods on this class. There have been issues filed describing this is not great.

This is not a blocker for this PR, but I'd like your thoughts on #589.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

agree, changed to update.
I didn't like the name but I followed the other method for consistency in the class

Copy link
Member

Choose a reason for hiding this comment

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

Glad you agree. But leave this for now. I don't want to do this unevenly (some update some set). 😄

}

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

import java.io.IOException;
import java.util.List;

/**
* Creates a team.
*
* https://developer.github.com/v3/teams/#parameters
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* https://developer.github.com/v3/teams/#parameters
* 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(List<String> maintainers) {
timja marked this conversation as resolved.
Show resolved Hide resolved
this.builder.with("maintainers", maintainers);
return this;
}

/**
* Repo names to add this team to.
*
* @param repoNames
* repoNames to add team to
* @return a builder to continue with building
*/
public GHTeamBuilder repoNames(List<String> repoNames) {
timja marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
30 changes: 28 additions & 2 deletions src/test/java/org/kohsuke/github/GHOrganizationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import java.io.IOException;

import static java.util.Collections.singletonList;

public class GHOrganizationTest extends AbstractGitHubWireMockTest {

public static final String GITHUB_API_TEST = "github-api-test";
Expand Down Expand Up @@ -86,7 +88,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 +102,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(singletonList("bitwiseman"))
.repoNames(singletonList(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 testUpdatePrivacy() throws IOException {
String teamSlug = "dummy-team";
Privacy privacy = Privacy.CLOSED;

// Set the privacy.
GHTeam team = gitHub.getOrganization(GITHUB_API_TEST_ORG).getTeamBySlug(teamSlug);
team.updatePrivacy(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.updatePrivacy(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
}
}
Loading