Skip to content

Commit 20c01ec

Browse files
committed
feat: Add updateTeamPermissions for TeamClient
1 parent 9493ca9 commit 20c01ec

File tree

2 files changed

+94
-13
lines changed

2 files changed

+94
-13
lines changed

src/main/java/com/spotify/github/v3/clients/TeamClient.java

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
* -/-/-
1919
*/
2020

21-
2221
package com.spotify.github.v3.clients;
2322

2423
import static com.spotify.github.v3.clients.GitHubClient.*;
@@ -28,13 +27,17 @@
2827
import com.spotify.github.v3.User;
2928
import com.spotify.github.v3.orgs.Membership;
3029
import com.spotify.github.v3.orgs.TeamInvitation;
30+
import com.spotify.github.v3.orgs.requests.ImmutableTeamRepoPermissionUpdate;
3131
import com.spotify.github.v3.orgs.requests.MembershipCreate;
3232
import com.spotify.github.v3.orgs.requests.TeamCreate;
33+
import com.spotify.github.v3.orgs.requests.TeamRepoPermissionUpdate;
3334
import com.spotify.github.v3.orgs.requests.TeamUpdate;
3435
import java.lang.invoke.MethodHandles;
3536
import java.util.Iterator;
3637
import java.util.List;
3738
import java.util.concurrent.CompletableFuture;
39+
40+
import com.spotify.github.v3.repos.RepositoryPermission;
3841
import org.slf4j.Logger;
3942
import org.slf4j.LoggerFactory;
4043

@@ -54,6 +57,8 @@ public class TeamClient {
5457

5558
private static final String INVITATIONS_TEMPLATE = "/orgs/%s/teams/%s/invitations";
5659

60+
private static final String REPO_TEMPLATE = "/orgs/%s/teams/%s/repos/%s/%s";
61+
5762
private final GitHubClient github;
5863

5964
private final String org;
@@ -75,7 +80,7 @@ static TeamClient create(final GitHubClient github, final String org) {
7580
*/
7681
public CompletableFuture<Team> createTeam(final TeamCreate request) {
7782
final String path = String.format(TEAM_TEMPLATE, org);
78-
log.debug("Creating team in: " + path);
83+
log.debug("Creating team in: {}", path);
7984
return github.post(path, github.json().toJsonUnchecked(request), Team.class);
8085
}
8186

@@ -87,7 +92,7 @@ public CompletableFuture<Team> createTeam(final TeamCreate request) {
8792
*/
8893
public CompletableFuture<Team> getTeam(final String slug) {
8994
final String path = String.format(TEAM_SLUG_TEMPLATE, org, slug);
90-
log.debug("Fetching team from " + path);
95+
log.debug("Fetching team from {}", path);
9196
return github.request(path, Team.class);
9297
}
9398

@@ -98,7 +103,7 @@ public CompletableFuture<Team> getTeam(final String slug) {
98103
*/
99104
public CompletableFuture<List<Team>> listTeams() {
100105
final String path = String.format(TEAM_TEMPLATE, org);
101-
log.debug("Fetching teams from " + path);
106+
log.debug("Fetching teams from {}", path);
102107
return github.request(path, LIST_TEAMS);
103108
}
104109

@@ -111,7 +116,7 @@ public CompletableFuture<List<Team>> listTeams() {
111116
*/
112117
public CompletableFuture<Team> updateTeam(final TeamUpdate request, final String slug) {
113118
final String path = String.format(TEAM_SLUG_TEMPLATE, org, slug);
114-
log.debug("Updating team in: " + path);
119+
log.debug("Updating team in: {}", path);
115120
return github.patch(path, github.json().toJsonUnchecked(request), Team.class);
116121
}
117122

@@ -123,7 +128,7 @@ public CompletableFuture<Team> updateTeam(final TeamUpdate request, final String
123128
*/
124129
public CompletableFuture<Void> deleteTeam(final String slug) {
125130
final String path = String.format(TEAM_SLUG_TEMPLATE, org, slug);
126-
log.debug("Deleting team from: " + path);
131+
log.debug("Deleting team from: {}", path);
127132
return github.delete(path).thenAccept(IGNORE_RESPONSE_CONSUMER);
128133
}
129134

@@ -133,9 +138,10 @@ public CompletableFuture<Void> deleteTeam(final String slug) {
133138
* @param request update membership request
134139
* @return membership
135140
*/
136-
public CompletableFuture<Membership> updateMembership(final MembershipCreate request, final String slug, final String username) {
141+
public CompletableFuture<Membership> updateMembership(
142+
final MembershipCreate request, final String slug, final String username) {
137143
final String path = String.format(MEMBERSHIP_TEMPLATE, org, slug, username);
138-
log.debug("Updating membership in: " + path);
144+
log.debug("Updating membership in: {}", path);
139145
return github.put(path, github.json().toJsonUnchecked(request), Membership.class);
140146
}
141147

@@ -148,7 +154,7 @@ public CompletableFuture<Membership> updateMembership(final MembershipCreate req
148154
*/
149155
public CompletableFuture<Membership> getMembership(final String slug, final String username) {
150156
final String path = String.format(MEMBERSHIP_TEMPLATE, org, slug, username);
151-
log.debug("Fetching membership for: " + path);
157+
log.debug("Fetching membership for: {}", path);
152158
return github.request(path, Membership.class);
153159
}
154160

@@ -160,7 +166,7 @@ public CompletableFuture<Membership> getMembership(final String slug, final Stri
160166
*/
161167
public CompletableFuture<List<User>> listTeamMembers(final String slug) {
162168
final String path = String.format(MEMBERS_TEMPLATE, org, slug);
163-
log.debug("Fetching members for: " + path);
169+
log.debug("Fetching members for: {}", path);
164170
return github.request(path, LIST_TEAM_MEMBERS);
165171
}
166172

@@ -173,7 +179,7 @@ public CompletableFuture<List<User>> listTeamMembers(final String slug) {
173179
*/
174180
public Iterator<AsyncPage<User>> listTeamMembers(final String slug, final int pageSize) {
175181
final String path = String.format(PAGED_MEMBERS_TEMPLATE, org, slug, pageSize);
176-
log.debug("Fetching members for: " + path);
182+
log.debug("Fetching members for: {}", path);
177183
return new GithubPageIterator<>(new GithubPage<>(github, path, LIST_TEAM_MEMBERS));
178184
}
179185

@@ -185,7 +191,7 @@ public Iterator<AsyncPage<User>> listTeamMembers(final String slug, final int pa
185191
*/
186192
public CompletableFuture<Void> deleteMembership(final String slug, final String username) {
187193
final String path = String.format(MEMBERSHIP_TEMPLATE, org, slug, username);
188-
log.debug("Deleting membership from: " + path);
194+
log.debug("Deleting membership from: {}", path);
189195
return github.delete(path).thenAccept(IGNORE_RESPONSE_CONSUMER);
190196
}
191197

@@ -197,7 +203,44 @@ public CompletableFuture<Void> deleteMembership(final String slug, final String
197203
*/
198204
public CompletableFuture<List<TeamInvitation>> listPendingTeamInvitations(final String slug) {
199205
final String path = String.format(INVITATIONS_TEMPLATE, org, slug);
200-
log.debug("Fetching pending invitations for: " + path);
206+
log.debug("Fetching pending invitations for: {}", path);
201207
return github.request(path, LIST_PENDING_TEAM_INVITATIONS);
202208
}
209+
210+
/**
211+
* Update permissions for a team on a specific repository.
212+
*
213+
* @param slug the team slug
214+
* @param repo the repository name
215+
* @param permission the permission level (pull, push, maintain, triage, admin, or a custom repo defined role name)
216+
* @return void status code 204 if successful
217+
*/
218+
public CompletableFuture<Void> updateTeamPermissions(
219+
final String slug, final String repo, final String permission) {
220+
final String path = String.format(REPO_TEMPLATE, org, slug, org, repo);
221+
final TeamRepoPermissionUpdate request =
222+
ImmutableTeamRepoPermissionUpdate.builder()
223+
.org(org)
224+
.repo(repo)
225+
.teamSlug(slug)
226+
.permission(permission)
227+
.build();
228+
log.debug("Updating team permissions for: {}", path);
229+
return github
230+
.put(path, github.json().toJsonUnchecked(request))
231+
.thenAccept(IGNORE_RESPONSE_CONSUMER);
232+
}
233+
234+
/**
235+
* Update permissions for a team on a specific repository.
236+
*
237+
* @param slug the team slug
238+
* @param repo the repository name
239+
* @param permission the permission level (pull, push, maintain, triage, admin, or a custom repo defined role name)
240+
* @return void status code 204 if successful
241+
*/
242+
public CompletableFuture<Void> updateTeamPermissions(
243+
final String slug, final String repo, final RepositoryPermission permission) {
244+
return updateTeamPermissions(slug, repo, permission.toString());
245+
}
203246
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2016 - 2020 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.v3.orgs.requests;
22+
23+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
24+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
25+
import com.spotify.github.GithubStyle;
26+
import org.immutables.value.Value;
27+
28+
/** Request to update permissions of a team for a specific repo */
29+
@Value.Immutable
30+
@GithubStyle
31+
@JsonSerialize(as = ImmutableTeamRepoPermissionUpdate.class)
32+
@JsonDeserialize(as = ImmutableTeamRepoPermissionUpdate.class)
33+
public interface TeamRepoPermissionUpdate {
34+
String org();
35+
String repo();
36+
String teamSlug();
37+
String permission();
38+
}

0 commit comments

Comments
 (0)