Skip to content

Commit eb2577e

Browse files
committed
Add builder for updating multiple GHTeam properties.
1 parent cc69c75 commit eb2577e

3 files changed

Lines changed: 243 additions & 0 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ public enum Role {
4848
MEMBER
4949
}
5050

51+
/**
52+
* Notification setting across a team
53+
*/
54+
public enum NotificationSetting {
55+
/**
56+
* Team members receive notifications when the team is @mentioned.
57+
*/
58+
NOTIFICATIONS_ENABLED,
59+
/**
60+
* No one receives notifications.
61+
*/
62+
NOTIFICATIONS_DISABLED
63+
}
64+
5165
/**
5266
* Path for external group-related operations
5367
*/
@@ -511,6 +525,15 @@ public void setPrivacy(Privacy privacy) throws IOException {
511525
root().createRequest().method("PATCH").with("privacy", privacy).withUrlPath(api("")).send();
512526
}
513527

528+
/**
529+
* Start constructing an update request for multiple properties of this team.
530+
*
531+
* @return a builder to update this team
532+
*/
533+
public GHTeamUpdateBuilder updateTeam() {
534+
return new GHTeamUpdateBuilder(root(), organization.getLogin(), name);
535+
}
536+
514537
private String api(String tail) {
515538
if (organization == null) {
516539
// Teams returned from pull requests to do not have an organization. Attempt to use url.
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package org.kohsuke.github;
2+
3+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
4+
5+
import java.io.IOException;
6+
7+
/**
8+
* Updates a team.
9+
*
10+
* @see <a href=https://developer.github.com/v3/teams/#update-team>Update team docs</a>
11+
* @author Rory Kelly
12+
*/
13+
public class GHTeamUpdateBuilder extends GitHubInteractiveObject {
14+
15+
private final String orgName;
16+
private final String existingName;
17+
/** The builder. */
18+
protected final Requester builder;
19+
20+
/**
21+
* Instantiates a new GH team update builder.
22+
*
23+
* @param root
24+
* the root
25+
* @param orgName
26+
* the org name
27+
* @param existingName
28+
* the current name of the existing team
29+
*/
30+
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected")
31+
public GHTeamUpdateBuilder(GitHub root, String orgName, String existingName) {
32+
super(root);
33+
this.orgName = orgName;
34+
this.existingName = existingName;
35+
this.builder = root.createRequest();
36+
}
37+
38+
/**
39+
* Updates a team with all the provided parameters.
40+
*
41+
* @return the gh team
42+
* @throws IOException
43+
* if team cannot be updated
44+
*/
45+
public GHTeam update() throws IOException {
46+
return builder.method("PATCH").withUrlPath("/orgs/" + orgName + "/teams/" + existingName).fetch(GHTeam.class).wrapUp(root());
47+
}
48+
49+
/**
50+
* Name for this team.
51+
*
52+
* @param name
53+
* name of team
54+
* @return a builder to continue with building
55+
*/
56+
public GHTeamUpdateBuilder name(String name) {
57+
this.builder.with("name", name);
58+
return this;
59+
}
60+
61+
/**
62+
* Description for this team.
63+
*
64+
* @param description
65+
* description of team
66+
* @return a builder to continue with building
67+
*/
68+
public GHTeamUpdateBuilder description(String description) {
69+
this.builder.with("description", description);
70+
return this;
71+
}
72+
73+
/**
74+
* Privacy for this team.
75+
*
76+
* @param privacy
77+
* privacy of team
78+
* @return a builder to continue with building
79+
*/
80+
public GHTeamUpdateBuilder privacy(GHTeam.Privacy privacy) {
81+
this.builder.with("privacy", privacy);
82+
return this;
83+
}
84+
85+
/**
86+
* The notification setting explicitly set for this team.
87+
*
88+
* @param notificationSetting
89+
* notification setting to be applied
90+
* @return a builder to continue with building
91+
*/
92+
public GHTeamUpdateBuilder notifications(GHTeam.NotificationSetting notificationSetting) {
93+
this.builder.with("notification_setting", notificationSetting);
94+
return this;
95+
}
96+
97+
/**
98+
* The permission that new repositories will be added to the team with when none is specified.
99+
*
100+
* @param permission
101+
* permission to be applied
102+
* @return a builder to continue with building
103+
* @deprecated see
104+
* <a href=https://developer.github.com/v3/teams/#update-team>Update team docs</a>
105+
*/
106+
@Deprecated
107+
public GHTeamUpdateBuilder permission(GHOrganization.Permission permission) {
108+
this.builder.with("permission", permission);
109+
return this;
110+
}
111+
112+
/**
113+
* Parent team id for this team.
114+
*
115+
* @param parentTeamId
116+
* parentTeamId of team, or null if you are removing the parent
117+
* @return a builder to continue with building
118+
*/
119+
public GHTeamUpdateBuilder parentTeamId(Long parentTeamId) {
120+
this.builder.with("parent_team_id", parentTeamId);
121+
return this;
122+
}
123+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package org.kohsuke.github;
2+
3+
import org.junit.Test;
4+
5+
import java.io.IOException;
6+
7+
import static org.hamcrest.Matchers.equalTo;
8+
9+
// TODO: Auto-generated Javadoc
10+
11+
/**
12+
* The Class GHTeamUpdateBuilderTest.
13+
*
14+
* @author Rory Kelly
15+
*/
16+
public class GHTeamUpdateBuilderTest extends AbstractGitHubWireMockTest {
17+
18+
private static final String TEAM_TO_UPDATE_SLUG = "dummy-team-to-update";
19+
20+
private static final String TEAM_TO_UPDATE_NEW_NAME = "dummy-team-updated";
21+
private static final String TEAM_TO_UPDATE_NEW_DESCRIPTION = "This is an updated description!";
22+
private static final GHTeam.Privacy TEAM_TO_UPDATE_NEW_PRIVACY = GHTeam.Privacy.SECRET;
23+
private static final GHTeam.NotificationSetting TEAM_TO_UPDATE_NEW_NOTIFICATIONS = GHTeam.NotificationSetting.NOTIFICATIONS_DISABLED;
24+
@Deprecated
25+
private static final GHOrganization.Permission TEAM_TO_UPDATE_NEW_PERMISSIONS = GHOrganization.Permission.PUSH;
26+
27+
// private static final String CURRENT_PARENT_TEAM_SLUG = "dummy-current-parent-team";
28+
private static final String NEW_PARENT_TEAM_SLUG = "dummy-new-parent-team";
29+
30+
/**
31+
* Create default GHTeamBuilderTest instance
32+
*/
33+
public GHTeamUpdateBuilderTest() {
34+
}
35+
36+
// update name, description, privacy, notifications, permission, no change to parent team
37+
// update parent team
38+
// update removal of parent team
39+
40+
@Test
41+
public void testUpdateTeamWithNewParentTeam() throws IOException {
42+
// Get the parent team
43+
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
44+
GHTeam teamToUpdate = org.getTeamBySlug(TEAM_TO_UPDATE_SLUG);
45+
GHTeam newParentTeam = org.getTeamBySlug(NEW_PARENT_TEAM_SLUG);
46+
47+
GHTeam updatedTeam = getCommonBuilder(teamToUpdate)
48+
.parentTeamId(newParentTeam.getId())
49+
.update();
50+
51+
assertUpdatedTeam(updatedTeam);
52+
// assertThat(updatedTeam.getParentTeam().getId(), equalTo(newParentTeam.getId()));
53+
}
54+
55+
@Test
56+
public void testUpdateTeamWithNoChangeToParentTeam() throws IOException {
57+
// Get the parent team
58+
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
59+
GHTeam teamToUpdate = org.getTeamBySlug(TEAM_TO_UPDATE_SLUG);
60+
// GHTeam existingParentTeam = org.getTeamBySlug(CURRENT_PARENT_TEAM_SLUG);
61+
62+
GHTeam updatedTeam = getCommonBuilder(teamToUpdate).update();
63+
64+
assertUpdatedTeam(updatedTeam);
65+
// assertThat(teamToUpdate.getParentTeam().getId(), equalTo(existingParentTeam.getId()));
66+
}
67+
68+
@Test
69+
public void testUpdateTeamWithRemovedParentTeam() throws IOException {
70+
GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
71+
GHTeam teamToUpdate = org.getTeamBySlug(TEAM_TO_UPDATE_SLUG);
72+
73+
GHTeam updatedTeam = getCommonBuilder(teamToUpdate)
74+
.parentTeamId(null)
75+
.update();
76+
77+
assertUpdatedTeam(updatedTeam);
78+
// assertThat(teamToUpdate.getParentTeam(), equalTo(null));
79+
}
80+
81+
private GHTeamUpdateBuilder getCommonBuilder(GHTeam teamToUpdate) {
82+
return teamToUpdate.updateTeam()
83+
.name(TEAM_TO_UPDATE_NEW_NAME)
84+
.description(TEAM_TO_UPDATE_NEW_DESCRIPTION)
85+
.privacy(TEAM_TO_UPDATE_NEW_PRIVACY)
86+
.notifications(TEAM_TO_UPDATE_NEW_NOTIFICATIONS)
87+
.permission(TEAM_TO_UPDATE_NEW_PERMISSIONS);
88+
}
89+
90+
private void assertUpdatedTeam(GHTeam updatedTeam) {
91+
assertThat(updatedTeam.getName(), equalTo(TEAM_TO_UPDATE_NEW_NAME));
92+
assertThat(updatedTeam.getDescription(), equalTo(TEAM_TO_UPDATE_NEW_DESCRIPTION));
93+
assertThat(updatedTeam.getPrivacy(), equalTo(TEAM_TO_UPDATE_NEW_PRIVACY));
94+
// assertThat(updatedTeam.getNotificationSetting(), equalTo(TEAM_TO_UPDATE_NEW_NOTIFICATIONS));
95+
assertThat(updatedTeam.getPermission(), equalTo(TEAM_TO_UPDATE_NEW_PERMISSIONS));
96+
}
97+
}

0 commit comments

Comments
 (0)