|
| 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 | + |
| 22 | +package com.spotify.github.v3.clients; |
| 23 | + |
| 24 | +import static com.spotify.github.v3.clients.GitHubClient.IGNORE_RESPONSE_CONSUMER; |
| 25 | +import static com.spotify.github.v3.clients.GitHubClient.LIST_PENDING_TEAM_INVITATIONS; |
| 26 | +import static com.spotify.github.v3.clients.GitHubClient.LIST_TEAMS; |
| 27 | +import static com.spotify.github.v3.clients.GitHubClient.LIST_TEAM_MEMBERS; |
| 28 | + |
| 29 | +import com.spotify.github.v3.Team; |
| 30 | +import com.spotify.github.v3.User; |
| 31 | +import com.spotify.github.v3.orgs.Membership; |
| 32 | +import com.spotify.github.v3.orgs.TeamInvitation; |
| 33 | +import com.spotify.github.v3.orgs.requests.MembershipCreate; |
| 34 | +import com.spotify.github.v3.orgs.requests.TeamCreate; |
| 35 | +import java.lang.invoke.MethodHandles; |
| 36 | +import java.util.List; |
| 37 | +import java.util.concurrent.CompletableFuture; |
| 38 | +import org.slf4j.Logger; |
| 39 | +import org.slf4j.LoggerFactory; |
| 40 | + |
| 41 | +public class TeamClient { |
| 42 | + |
| 43 | + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); |
| 44 | + |
| 45 | + private static final String TEAM_TEMPLATE = "/orgs/%s/teams"; |
| 46 | + |
| 47 | + private static final String TEAM_SLUG_TEMPLATE = "/orgs/%s/teams/%s"; |
| 48 | + |
| 49 | + private static final String MEMBERS_TEMPLATE = "/orgs/%s/teams/%s/members"; |
| 50 | + |
| 51 | + private static final String MEMBERSHIP_TEMPLATE = "/orgs/%s/teams/%s/memberships/%s"; |
| 52 | + |
| 53 | + private static final String INVITATIONS_TEMPLATE = "/orgs/%s/teams/%s/invitations"; |
| 54 | + |
| 55 | + private final GitHubClient github; |
| 56 | + |
| 57 | + private final String org; |
| 58 | + |
| 59 | + TeamClient(final GitHubClient github, final String org) { |
| 60 | + this.github = github; |
| 61 | + this.org = org; |
| 62 | + } |
| 63 | + |
| 64 | + static TeamClient create(final GitHubClient github, final String org) { |
| 65 | + return new TeamClient(github, org); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Create a team in an organisation. |
| 70 | + * |
| 71 | + * @param request create team request |
| 72 | + * @return team |
| 73 | + */ |
| 74 | + public CompletableFuture<Team> createTeam(final TeamCreate request) { |
| 75 | + final String path = String.format(TEAM_TEMPLATE, org); |
| 76 | + log.debug("Creating team in: " + path); |
| 77 | + return github.post(path, github.json().toJsonUnchecked(request), Team.class); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Get a specific team in an organisation. |
| 82 | + * |
| 83 | + * @param slug slug of the team name |
| 84 | + * @return team |
| 85 | + */ |
| 86 | + public CompletableFuture<Team> getTeam(final String slug) { |
| 87 | + final String path = String.format(TEAM_SLUG_TEMPLATE, org, slug); |
| 88 | + log.debug("Fetching team from " + path); |
| 89 | + return github.request(path, Team.class); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * List teams within an organisation. |
| 94 | + * |
| 95 | + * @return list of all teams in an organisation |
| 96 | + */ |
| 97 | + public CompletableFuture<List<Team>> listTeams() { |
| 98 | + final String path = String.format(TEAM_TEMPLATE, org); |
| 99 | + log.debug("Fetching teams from " + path); |
| 100 | + return github.request(path, LIST_TEAMS); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Update a team in an organisation. |
| 105 | + * |
| 106 | + * @param request update team request |
| 107 | + * @param slug slug of the team name |
| 108 | + * @return team |
| 109 | + */ |
| 110 | + public CompletableFuture<Team> updateTeam(final TeamCreate request, final String slug) { |
| 111 | + final String path = String.format(TEAM_SLUG_TEMPLATE, org, slug); |
| 112 | + log.debug("Updating team in: " + path); |
| 113 | + return github.patch(path, github.json().toJsonUnchecked(request), Team.class); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Delete a specific team in an organisation. |
| 118 | + * |
| 119 | + * @param slug slug of the team name |
| 120 | + * @return team |
| 121 | + */ |
| 122 | + public CompletableFuture<Void> deleteTeam(final String slug) { |
| 123 | + final String path = String.format(TEAM_SLUG_TEMPLATE, org, slug); |
| 124 | + log.debug("Deleting team from: " + path); |
| 125 | + return github.delete(path).thenAccept(IGNORE_RESPONSE_CONSUMER); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Add or update a team membership for a user. |
| 130 | + * |
| 131 | + * @param request update membership request |
| 132 | + * @return membership |
| 133 | + */ |
| 134 | + public CompletableFuture<Membership> updateMembership(final MembershipCreate request, final String slug, final String username) { |
| 135 | + final String path = String.format(MEMBERSHIP_TEMPLATE, org, slug, username); |
| 136 | + log.debug("Updating membership in: " + path); |
| 137 | + return github.put(path, github.json().toJsonUnchecked(request), Membership.class); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Get a team membership of a user. |
| 142 | + * |
| 143 | + * @param slug the team slug |
| 144 | + * @param username username of the team member |
| 145 | + * @return membership |
| 146 | + */ |
| 147 | + public CompletableFuture<Membership> getMembership(final String slug, final String username) { |
| 148 | + final String path = String.format(MEMBERSHIP_TEMPLATE, org, slug, username); |
| 149 | + log.debug("Fetching membership for: " + path); |
| 150 | + return github.request(path, Membership.class); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * List members of a specific team. |
| 155 | + * |
| 156 | + * @param slug the team slug |
| 157 | + * @return list of all users in a team |
| 158 | + */ |
| 159 | + public CompletableFuture<List<User>> listTeamMembers(final String slug) { |
| 160 | + final String path = String.format(MEMBERS_TEMPLATE, org, slug); |
| 161 | + log.debug("Fetching members for: " + path); |
| 162 | + return github.request(path, LIST_TEAM_MEMBERS); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Delete a membership for a user. |
| 167 | + * |
| 168 | + * @param slug slug of the team name |
| 169 | + * @return membership |
| 170 | + */ |
| 171 | + public CompletableFuture<Void> deleteMembership(final String slug, final String username) { |
| 172 | + final String path = String.format(MEMBERSHIP_TEMPLATE, org, slug, username); |
| 173 | + log.debug("Deleting membership from: " + path); |
| 174 | + return github.delete(path).thenAccept(IGNORE_RESPONSE_CONSUMER); |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * List pending invitations for a team. |
| 179 | + * |
| 180 | + * @param slug the team slug |
| 181 | + * @return list of pending invitations for a team |
| 182 | + */ |
| 183 | + public CompletableFuture<List<TeamInvitation>> listPendingTeamInvitations(final String slug) { |
| 184 | + final String path = String.format(INVITATIONS_TEMPLATE, org, slug); |
| 185 | + log.debug("Fetching pending invitations for: " + path); |
| 186 | + return github.request(path, LIST_PENDING_TEAM_INVITATIONS); |
| 187 | + } |
| 188 | +} |
0 commit comments