| 1 | package edu.ucsb.cs156.frontiers.services; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.core.type.TypeReference; | |
| 5 | import com.fasterxml.jackson.databind.DeserializationFeature; | |
| 6 | import com.fasterxml.jackson.databind.JsonNode; | |
| 7 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 8 | import edu.ucsb.cs156.frontiers.entities.Course; | |
| 9 | import edu.ucsb.cs156.frontiers.entities.Team; | |
| 10 | import edu.ucsb.cs156.frontiers.enums.TeamStatus; | |
| 11 | import java.security.NoSuchAlgorithmException; | |
| 12 | import java.security.spec.InvalidKeySpecException; | |
| 13 | import java.time.Duration; | |
| 14 | import java.util.ArrayList; | |
| 15 | import java.util.HashMap; | |
| 16 | import java.util.List; | |
| 17 | import java.util.Map; | |
| 18 | import java.util.regex.Matcher; | |
| 19 | import java.util.regex.Pattern; | |
| 20 | import lombok.extern.slf4j.Slf4j; | |
| 21 | import org.springframework.boot.web.client.RestTemplateBuilder; | |
| 22 | import org.springframework.http.HttpEntity; | |
| 23 | import org.springframework.http.HttpHeaders; | |
| 24 | import org.springframework.http.HttpMethod; | |
| 25 | import org.springframework.http.ResponseEntity; | |
| 26 | import org.springframework.stereotype.Service; | |
| 27 | import org.springframework.web.client.HttpClientErrorException; | |
| 28 | import org.springframework.web.client.RestTemplate; | |
| 29 | ||
| 30 | @Slf4j | |
| 31 | @Service | |
| 32 | public class GithubTeamService { | |
| 33 | ||
| 34 | public record GithubTeamInfo(Integer id, String name, String slug) { | |
| 35 | public GithubTeamInfo(Integer id, String name) { | |
| 36 | this(id, name, null); | |
| 37 | } | |
| 38 | } | |
| 39 | ||
| 40 | public record GithubTeamMemberInfo(String login) {} | |
| 41 | ||
| 42 | /* | |
| 43 | * A RestTemplate built with no timeout blocks its calling thread forever on a hung external | |
| 44 | * call. That's a real incident lib-jobs' single-threaded jobsExecutor hit on another app | |
| 45 | * (proj-scaffold): a job stuck this way permanently wedged the executor, with no way to recover | |
| 46 | * short of restarting the app (see lib-jobs DESIGN.md 9 -- cooperative job cancellation only | |
| 47 | * helps a job that reaches another checkpoint, which a truly hung thread never will). Generous | |
| 48 | * but finite: long enough to never trip on legitimate slowness, short enough to guarantee a job | |
| 49 | * can't hang forever. | |
| 50 | */ | |
| 51 | private static final Duration CONNECT_TIMEOUT = Duration.ofSeconds(10); | |
| 52 | private static final Duration READ_TIMEOUT = Duration.ofSeconds(60); | |
| 53 | ||
| 54 | private final JwtService jwtService; | |
| 55 | private final ObjectMapper objectMapper; | |
| 56 | private final RestTemplate restTemplate; | |
| 57 | ||
| 58 | public GithubTeamService( | |
| 59 | JwtService jwtService, ObjectMapper objectMapper, RestTemplateBuilder builder) { | |
| 60 | this.jwtService = jwtService; | |
| 61 | this.objectMapper = objectMapper; | |
| 62 | this.objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | |
| 63 | this.restTemplate = builder.connectTimeout(CONNECT_TIMEOUT).readTimeout(READ_TIMEOUT).build(); | |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * Creates a team on GitHub if it doesn't exist, or returns the existing team ID. | |
| 68 | * | |
| 69 | * @param team The team to create | |
| 70 | * @param course The course containing the organization | |
| 71 | * @return The GitHub team ID | |
| 72 | * @throws JsonProcessingException if there is an error processing JSON | |
| 73 | * @throws NoSuchAlgorithmException if there is an algorithm error | |
| 74 | * @throws InvalidKeySpecException if there is a key specification error | |
| 75 | */ | |
| 76 | public Integer createOrGetTeamId(Team team, Course course) | |
| 77 | throws JsonProcessingException, NoSuchAlgorithmException, InvalidKeySpecException { | |
| 78 | GithubTeamInfo teamInfo = createOrGetTeamInfo(team, course); | |
| 79 |
2
1. createOrGetTeamId : negated conditional → KILLED 2. createOrGetTeamId : replaced Integer return value with 0 for edu/ucsb/cs156/frontiers/services/GithubTeamService::createOrGetTeamId → KILLED |
return teamInfo == null ? null : teamInfo.id(); |
| 80 | } | |
| 81 | ||
| 82 | /** | |
| 83 | * Creates a team on GitHub if it doesn't exist, or returns the existing team info. | |
| 84 | * | |
| 85 | * @param team The team to create | |
| 86 | * @param course The course containing the organization | |
| 87 | * @return The GitHub team info | |
| 88 | * @throws JsonProcessingException if there is an error processing JSON | |
| 89 | * @throws NoSuchAlgorithmException if there is an algorithm error | |
| 90 | * @throws InvalidKeySpecException if there is a key specification error | |
| 91 | */ | |
| 92 | public GithubTeamInfo createOrGetTeamInfo(Team team, Course course) | |
| 93 | throws JsonProcessingException, NoSuchAlgorithmException, InvalidKeySpecException { | |
| 94 | GithubTeamInfo existingTeamInfo = null; | |
| 95 |
2
1. createOrGetTeamInfo : negated conditional → KILLED 2. createOrGetTeamInfo : negated conditional → KILLED |
if (team.getGithubTeamSlug() != null && !team.getGithubTeamSlug().isBlank()) { |
| 96 | existingTeamInfo = getTeamInfo(team.getGithubTeamSlug(), course); | |
| 97 | } | |
| 98 |
1
1. createOrGetTeamInfo : negated conditional → KILLED |
if (existingTeamInfo == null) { |
| 99 | existingTeamInfo = getTeamInfoByName(team.getName(), course); | |
| 100 | } | |
| 101 |
1
1. createOrGetTeamInfo : negated conditional → KILLED |
if (existingTeamInfo != null) { |
| 102 |
1
1. createOrGetTeamInfo : replaced return value with null for edu/ucsb/cs156/frontiers/services/GithubTeamService::createOrGetTeamInfo → KILLED |
return existingTeamInfo; |
| 103 | } | |
| 104 | ||
| 105 | // Create the team if it doesn't exist | |
| 106 |
1
1. createOrGetTeamInfo : replaced return value with null for edu/ucsb/cs156/frontiers/services/GithubTeamService::createOrGetTeamInfo → KILLED |
return createTeamInfo(team.getName(), course); |
| 107 | } | |
| 108 | ||
| 109 | /** | |
| 110 | * Get the org id, given the org name. | |
| 111 | * | |
| 112 | * <p>Note: in the future, it would be better to cache this value in the Course row in the | |
| 113 | * database at the time the Github App is linked to the org, since it doesn't change. | |
| 114 | * | |
| 115 | * @param orgName | |
| 116 | * @param course | |
| 117 | * @return | |
| 118 | * @throws JsonProcessingException | |
| 119 | * @throws NoSuchAlgorithmException | |
| 120 | * @throws InvalidKeySpecException | |
| 121 | */ | |
| 122 | public Integer getOrgId(String orgName, Course course) | |
| 123 | throws JsonProcessingException, NoSuchAlgorithmException, InvalidKeySpecException { | |
| 124 | String endpoint = "https://api.github.com/orgs/" + orgName; | |
| 125 | HttpHeaders headers = new HttpHeaders(); | |
| 126 | String token = jwtService.getInstallationToken(course); | |
| 127 |
1
1. getOrgId : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 128 |
1
1. getOrgId : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 129 |
1
1. getOrgId : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 130 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 131 | ||
| 132 | ResponseEntity<String> response = | |
| 133 | restTemplate.exchange(endpoint, HttpMethod.GET, entity, String.class); | |
| 134 | JsonNode responseJson = objectMapper.readTree(response.getBody()); | |
| 135 |
1
1. getOrgId : replaced Integer return value with 0 for edu/ucsb/cs156/frontiers/services/GithubTeamService::getOrgId → KILLED |
return responseJson.get("id").asInt(); |
| 136 | } | |
| 137 | ||
| 138 | /** | |
| 139 | * Gets the team ID for a team slug, returns null if team doesn't exist. | |
| 140 | * | |
| 141 | * @param teamSlug The slug of the team | |
| 142 | * @param course The course containing the organization | |
| 143 | * @return The GitHub team ID or null if not found | |
| 144 | * @throws JsonProcessingException if there is an error processing JSON | |
| 145 | * @throws NoSuchAlgorithmException if there is an algorithm error | |
| 146 | * @throws InvalidKeySpecException if there is a key specification error | |
| 147 | */ | |
| 148 | public Integer getTeamId(String teamSlug, Course course) | |
| 149 | throws JsonProcessingException, NoSuchAlgorithmException, InvalidKeySpecException { | |
| 150 | GithubTeamInfo teamInfo = getTeamInfo(teamSlug, course); | |
| 151 |
2
1. getTeamId : replaced Integer return value with 0 for edu/ucsb/cs156/frontiers/services/GithubTeamService::getTeamId → KILLED 2. getTeamId : negated conditional → KILLED |
return teamInfo == null ? null : teamInfo.id(); |
| 152 | } | |
| 153 | ||
| 154 | /** | |
| 155 | * Gets the team info for a team slug, returns null if team doesn't exist. | |
| 156 | * | |
| 157 | * @param teamSlug The slug of the team | |
| 158 | * @param course The course containing the organization | |
| 159 | * @return The GitHub team info or null if not found | |
| 160 | * @throws JsonProcessingException if there is an error processing JSON | |
| 161 | * @throws NoSuchAlgorithmException if there is an algorithm error | |
| 162 | * @throws InvalidKeySpecException if there is a key specification error | |
| 163 | */ | |
| 164 | public GithubTeamInfo getTeamInfo(String teamSlug, Course course) | |
| 165 | throws JsonProcessingException, NoSuchAlgorithmException, InvalidKeySpecException { | |
| 166 | String endpoint = "https://api.github.com/orgs/" + course.getOrgName() + "/teams/" + teamSlug; | |
| 167 | HttpHeaders headers = new HttpHeaders(); | |
| 168 | String token = jwtService.getInstallationToken(course); | |
| 169 |
1
1. getTeamInfo : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 170 |
1
1. getTeamInfo : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 171 |
1
1. getTeamInfo : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 172 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 173 | ||
| 174 | try { | |
| 175 | ResponseEntity<String> response = | |
| 176 | restTemplate.exchange(endpoint, HttpMethod.GET, entity, String.class); | |
| 177 |
1
1. getTeamInfo : replaced return value with null for edu/ucsb/cs156/frontiers/services/GithubTeamService::getTeamInfo → KILLED |
return objectMapper.readValue(response.getBody(), GithubTeamInfo.class); |
| 178 | } catch (HttpClientErrorException e) { | |
| 179 |
1
1. getTeamInfo : negated conditional → KILLED |
if (e.getStatusCode().value() == 404) { |
| 180 | return null; // Team doesn't exist | |
| 181 | } | |
| 182 | throw e; | |
| 183 | } | |
| 184 | } | |
| 185 | ||
| 186 | /** | |
| 187 | * Gets the team info for a team ID, returns null if team doesn't exist. | |
| 188 | * | |
| 189 | * @param orgId The GitHub organization ID | |
| 190 | * @param teamId The GitHub team ID | |
| 191 | * @param course The course containing the organization | |
| 192 | * @return The GitHub team info or null if not found | |
| 193 | * @throws JsonProcessingException if there is an error processing JSON | |
| 194 | * @throws NoSuchAlgorithmException if there is an algorithm error | |
| 195 | * @throws InvalidKeySpecException if there is a key specification error | |
| 196 | */ | |
| 197 | public GithubTeamInfo getTeamInfoById(Integer orgId, Integer teamId, Course course) | |
| 198 | throws JsonProcessingException, NoSuchAlgorithmException, InvalidKeySpecException { | |
| 199 | String endpoint = "https://api.github.com/organizations/" + orgId + "/team/" + teamId; | |
| 200 | HttpHeaders headers = new HttpHeaders(); | |
| 201 | String token = jwtService.getInstallationToken(course); | |
| 202 |
1
1. getTeamInfoById : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 203 |
1
1. getTeamInfoById : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 204 |
1
1. getTeamInfoById : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 205 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 206 | ||
| 207 | try { | |
| 208 | ResponseEntity<String> response = | |
| 209 | restTemplate.exchange(endpoint, HttpMethod.GET, entity, String.class); | |
| 210 |
1
1. getTeamInfoById : replaced return value with null for edu/ucsb/cs156/frontiers/services/GithubTeamService::getTeamInfoById → KILLED |
return objectMapper.readValue(response.getBody(), GithubTeamInfo.class); |
| 211 | } catch (HttpClientErrorException e) { | |
| 212 |
1
1. getTeamInfoById : negated conditional → KILLED |
if (e.getStatusCode().value() == 404) { |
| 213 | return null; | |
| 214 | } | |
| 215 | throw e; | |
| 216 | } | |
| 217 | } | |
| 218 | ||
| 219 | /** | |
| 220 | * Finds team info by display name by searching the organization's teams. | |
| 221 | * | |
| 222 | * @param teamName The display name of the team | |
| 223 | * @param course The course containing the organization | |
| 224 | * @return The GitHub team info or null if not found | |
| 225 | * @throws JsonProcessingException if there is an error processing JSON | |
| 226 | * @throws NoSuchAlgorithmException if there is an algorithm error | |
| 227 | * @throws InvalidKeySpecException if there is a key specification error | |
| 228 | */ | |
| 229 | public GithubTeamInfo getTeamInfoByName(String teamName, Course course) | |
| 230 | throws JsonProcessingException, NoSuchAlgorithmException, InvalidKeySpecException { | |
| 231 | for (GithubTeamInfo teamInfo : getAllTeams(course)) { | |
| 232 |
1
1. getTeamInfoByName : negated conditional → KILLED |
if (teamName.equals(teamInfo.name())) { |
| 233 |
1
1. getTeamInfoByName : replaced return value with null for edu/ucsb/cs156/frontiers/services/GithubTeamService::getTeamInfoByName → KILLED |
return teamInfo; |
| 234 | } | |
| 235 | } | |
| 236 | return null; | |
| 237 | } | |
| 238 | ||
| 239 | /** | |
| 240 | * Creates a new team on GitHub. | |
| 241 | * | |
| 242 | * @param teamName The name of the team to create | |
| 243 | * @param course The course containing the organization | |
| 244 | * @return The GitHub team ID | |
| 245 | * @throws JsonProcessingException if there is an error processing JSON | |
| 246 | * @throws NoSuchAlgorithmException if there is an algorithm error | |
| 247 | * @throws InvalidKeySpecException if there is a key specification error | |
| 248 | */ | |
| 249 | public Integer createTeam(String teamName, Course course) | |
| 250 | throws JsonProcessingException, NoSuchAlgorithmException, InvalidKeySpecException { | |
| 251 | GithubTeamInfo teamInfo = createTeamInfo(teamName, course); | |
| 252 |
2
1. createTeam : replaced Integer return value with 0 for edu/ucsb/cs156/frontiers/services/GithubTeamService::createTeam → KILLED 2. createTeam : negated conditional → KILLED |
return teamInfo == null ? null : teamInfo.id(); |
| 253 | } | |
| 254 | ||
| 255 | /** | |
| 256 | * Creates a new team on GitHub. | |
| 257 | * | |
| 258 | * @param teamName The name of the team to create | |
| 259 | * @param course The course containing the organization | |
| 260 | * @return The GitHub team info | |
| 261 | * @throws JsonProcessingException if there is an error processing JSON | |
| 262 | * @throws NoSuchAlgorithmException if there is an algorithm error | |
| 263 | * @throws InvalidKeySpecException if there is a key specification error | |
| 264 | */ | |
| 265 | public GithubTeamInfo createTeamInfo(String teamName, Course course) | |
| 266 | throws JsonProcessingException, NoSuchAlgorithmException, InvalidKeySpecException { | |
| 267 | String endpoint = "https://api.github.com/orgs/" + course.getOrgName() + "/teams"; | |
| 268 | HttpHeaders headers = new HttpHeaders(); | |
| 269 | String token = jwtService.getInstallationToken(course); | |
| 270 |
1
1. createTeamInfo : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 271 |
1
1. createTeamInfo : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 272 |
1
1. createTeamInfo : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 273 | ||
| 274 | Map<String, Object> body = new HashMap<>(); | |
| 275 | body.put("name", teamName); | |
| 276 | body.put("privacy", "closed"); // Teams are private by default | |
| 277 | String bodyAsJson = objectMapper.writeValueAsString(body); | |
| 278 | HttpEntity<String> entity = new HttpEntity<>(bodyAsJson, headers); | |
| 279 | ||
| 280 | ResponseEntity<String> response = | |
| 281 | restTemplate.exchange(endpoint, HttpMethod.POST, entity, String.class); | |
| 282 | GithubTeamInfo teamInfo = objectMapper.readValue(response.getBody(), GithubTeamInfo.class); | |
| 283 | log.info( | |
| 284 | "Created team '{}' with ID {} in organization {}", | |
| 285 | teamName, | |
| 286 | teamInfo.id(), | |
| 287 | course.getOrgName()); | |
| 288 |
1
1. createTeamInfo : replaced return value with null for edu/ucsb/cs156/frontiers/services/GithubTeamService::createTeamInfo → KILLED |
return teamInfo; |
| 289 | } | |
| 290 | ||
| 291 | /** | |
| 292 | * Deletes a team on GitHub. | |
| 293 | * | |
| 294 | * @param orgId The ID of the organization | |
| 295 | * @param githubTeamId The ID of the team to delete | |
| 296 | * @param course The course containing the organization | |
| 297 | * @throws JsonProcessingException if there is an error processing JSON | |
| 298 | * @throws NoSuchAlgorithmException if there is an algorithm error | |
| 299 | * @throws InvalidKeySpecException if there is a key specification error | |
| 300 | */ | |
| 301 | public void deleteGithubTeam(Integer orgId, Integer teamId, Course course) | |
| 302 | throws JsonProcessingException, NoSuchAlgorithmException, InvalidKeySpecException { | |
| 303 | String endpoint = "https://api.github.com/organizations/" + orgId + "/team/" + teamId; | |
| 304 | HttpHeaders headers = new HttpHeaders(); | |
| 305 | String token = jwtService.getInstallationToken(course); | |
| 306 |
1
1. deleteGithubTeam : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 307 |
1
1. deleteGithubTeam : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 308 |
1
1. deleteGithubTeam : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 309 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 310 | ||
| 311 | restTemplate.exchange(endpoint, HttpMethod.DELETE, entity, String.class); | |
| 312 | log.info("Deleted team with ID {} in organization {}", teamId, course.getOrgName()); | |
| 313 | } | |
| 314 | ||
| 315 | /** | |
| 316 | * Gets the current team membership status for a user. | |
| 317 | * | |
| 318 | * @param githubLogin The GitHub login of the user | |
| 319 | * @param teamId The GitHub team ID | |
| 320 | * @param course The course containing the organization | |
| 321 | * @param orgId The GitHub organization ID | |
| 322 | * @return The team status of the user | |
| 323 | * @throws JsonProcessingException if there is an error processing JSON | |
| 324 | * @throws NoSuchAlgorithmException if there is an algorithm error | |
| 325 | * @throws InvalidKeySpecException if there is a key specification error | |
| 326 | */ | |
| 327 | public TeamStatus getTeamMembershipStatus( | |
| 328 | String githubLogin, Integer teamId, Course course, Integer orgId) | |
| 329 | throws JsonProcessingException, NoSuchAlgorithmException, InvalidKeySpecException { | |
| 330 |
1
1. getTeamMembershipStatus : negated conditional → KILLED |
if (githubLogin == null) { |
| 331 |
1
1. getTeamMembershipStatus : replaced return value with null for edu/ucsb/cs156/frontiers/services/GithubTeamService::getTeamMembershipStatus → KILLED |
return TeamStatus.NO_GITHUB_ID; |
| 332 | } | |
| 333 | ||
| 334 | String endpoint = | |
| 335 | "https://api.github.com/organizations/" | |
| 336 | + orgId | |
| 337 | + "/team/" | |
| 338 | + teamId | |
| 339 | + "/memberships/" | |
| 340 | + githubLogin; | |
| 341 | HttpHeaders headers = new HttpHeaders(); | |
| 342 | String token = jwtService.getInstallationToken(course); | |
| 343 |
1
1. getTeamMembershipStatus : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 344 |
1
1. getTeamMembershipStatus : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 345 |
1
1. getTeamMembershipStatus : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 346 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 347 | ||
| 348 | try { | |
| 349 | ResponseEntity<String> response = | |
| 350 | restTemplate.exchange(endpoint, HttpMethod.GET, entity, String.class); | |
| 351 | JsonNode responseJson = objectMapper.readTree(response.getBody()); | |
| 352 | String role = responseJson.get("role").asText(); | |
| 353 |
2
1. getTeamMembershipStatus : negated conditional → KILLED 2. getTeamMembershipStatus : replaced return value with null for edu/ucsb/cs156/frontiers/services/GithubTeamService::getTeamMembershipStatus → KILLED |
return "maintainer".equalsIgnoreCase(role) |
| 354 | ? TeamStatus.TEAM_MAINTAINER | |
| 355 | : TeamStatus.TEAM_MEMBER; | |
| 356 | } catch (HttpClientErrorException e) { | |
| 357 |
1
1. getTeamMembershipStatus : negated conditional → KILLED |
if (e.getStatusCode().value() == 404) { |
| 358 |
1
1. getTeamMembershipStatus : replaced return value with null for edu/ucsb/cs156/frontiers/services/GithubTeamService::getTeamMembershipStatus → KILLED |
return TeamStatus.NOT_ORG_MEMBER; // User is not a member of the team |
| 359 | } | |
| 360 | throw e; | |
| 361 | } | |
| 362 | } | |
| 363 | ||
| 364 | /** | |
| 365 | * Adds a member to a GitHub team. | |
| 366 | * | |
| 367 | * @param githubLogin The GitHub login of the user to add | |
| 368 | * @param teamId The GitHub team ID | |
| 369 | * @param role The role to assign ("member" or "maintainer") | |
| 370 | * @param course The course containing the organization | |
| 371 | * @return The resulting team status | |
| 372 | * @throws JsonProcessingException if there is an error processing JSON | |
| 373 | * @throws NoSuchAlgorithmException if there is an algorithm error | |
| 374 | * @throws InvalidKeySpecException if there is a key specification error | |
| 375 | */ | |
| 376 | public TeamStatus addMemberToGithubTeam( | |
| 377 | String githubLogin, Integer teamId, String role, Course course, Integer orgId) | |
| 378 | throws JsonProcessingException, NoSuchAlgorithmException, InvalidKeySpecException { | |
| 379 | String endpoint = | |
| 380 | "https://api.github.com/organizations/" | |
| 381 | + orgId | |
| 382 | + "/team/" | |
| 383 | + teamId | |
| 384 | + "/memberships/" | |
| 385 | + githubLogin; | |
| 386 | HttpHeaders headers = new HttpHeaders(); | |
| 387 | String token = jwtService.getInstallationToken(course); | |
| 388 |
1
1. addMemberToGithubTeam : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 389 |
1
1. addMemberToGithubTeam : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 390 |
1
1. addMemberToGithubTeam : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 391 | ||
| 392 | Map<String, Object> body = new HashMap<>(); | |
| 393 | body.put("role", role); | |
| 394 | String bodyAsJson = objectMapper.writeValueAsString(body); | |
| 395 | HttpEntity<String> entity = new HttpEntity<>(bodyAsJson, headers); | |
| 396 | ||
| 397 | ResponseEntity<String> response = | |
| 398 | restTemplate.exchange(endpoint, HttpMethod.PUT, entity, String.class); | |
| 399 | JsonNode responseJson = objectMapper.readTree(response.getBody()); | |
| 400 | String resultRole = responseJson.get("role").asText(); | |
| 401 | log.info("Added user '{}' to team ID {} with role '{}'", githubLogin, teamId, resultRole); | |
| 402 |
2
1. addMemberToGithubTeam : negated conditional → KILLED 2. addMemberToGithubTeam : replaced return value with null for edu/ucsb/cs156/frontiers/services/GithubTeamService::addMemberToGithubTeam → KILLED |
return "maintainer".equalsIgnoreCase(resultRole) |
| 403 | ? TeamStatus.TEAM_MAINTAINER | |
| 404 | : TeamStatus.TEAM_MEMBER; | |
| 405 | } | |
| 406 | ||
| 407 | /** | |
| 408 | * Removes a member from a GitHub team | |
| 409 | * | |
| 410 | * @param orgId | |
| 411 | * @param githubLogin | |
| 412 | * @param teamId | |
| 413 | * @param course | |
| 414 | * @throws NoSuchAlgorithmException | |
| 415 | * @throws InvalidKeySpecException | |
| 416 | * @throws JsonProcessingException | |
| 417 | */ | |
| 418 | public void removeMemberFromGithubTeam( | |
| 419 | Integer orgId, String githubLogin, Integer teamId, Course course) | |
| 420 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 421 | ||
| 422 | String endpoint = | |
| 423 | "https://api.github.com/organizations/" | |
| 424 | + orgId | |
| 425 | + "/team/" | |
| 426 | + teamId | |
| 427 | + "/memberships/" | |
| 428 | + githubLogin; | |
| 429 | HttpHeaders headers = new HttpHeaders(); | |
| 430 | String token = jwtService.getInstallationToken(course); | |
| 431 |
1
1. removeMemberFromGithubTeam : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 432 |
1
1. removeMemberFromGithubTeam : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 433 |
1
1. removeMemberFromGithubTeam : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 434 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 435 | ||
| 436 | restTemplate.exchange(endpoint, HttpMethod.DELETE, entity, String.class); | |
| 437 | log.info("Successfully removed member {} from team ID {}", githubLogin, teamId); | |
| 438 | } | |
| 439 | ||
| 440 | /** | |
| 441 | * Returns all team members for a GitHub team. | |
| 442 | * | |
| 443 | * @param teamSlug The GitHub team slug | |
| 444 | * @param course The course containing the organization | |
| 445 | * @return A map of github login to TeamStatus | |
| 446 | * @throws NoSuchAlgorithmException if there is an algorithm error | |
| 447 | * @throws InvalidKeySpecException if there is a key specification error | |
| 448 | * @throws JsonProcessingException if there is an error processing JSON | |
| 449 | */ | |
| 450 | public Map<String, TeamStatus> getTeamMemberships(String teamSlug, Course course) | |
| 451 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 452 |
2
1. getTeamMemberships : negated conditional → KILLED 2. getTeamMemberships : negated conditional → KILLED |
if (teamSlug == null || teamSlug.isBlank()) { |
| 453 | throw new IllegalArgumentException("teamSlug must be provided"); | |
| 454 | } | |
| 455 | HttpHeaders headers = new HttpHeaders(); | |
| 456 | String token = jwtService.getInstallationToken(course); | |
| 457 |
1
1. getTeamMemberships : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 458 |
1
1. getTeamMemberships : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 459 |
1
1. getTeamMemberships : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 460 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 461 | ||
| 462 | Map<String, TeamStatus> memberships = new HashMap<>(); | |
| 463 | String endpointPrefix = | |
| 464 | "https://api.github.com/orgs/" + course.getOrgName() + "/teams/" + teamSlug + "/members"; | |
| 465 |
1
1. getTeamMemberships : removed call to edu/ucsb/cs156/frontiers/services/GithubTeamService::addMembershipsByRole → KILLED |
addMembershipsByRole(endpointPrefix, "member", TeamStatus.TEAM_MEMBER, entity, memberships); |
| 466 |
1
1. getTeamMemberships : removed call to edu/ucsb/cs156/frontiers/services/GithubTeamService::addMembershipsByRole → KILLED |
addMembershipsByRole( |
| 467 | endpointPrefix, "maintainer", TeamStatus.TEAM_MAINTAINER, entity, memberships); | |
| 468 | ||
| 469 |
1
1. getTeamMemberships : replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/services/GithubTeamService::getTeamMemberships → KILLED |
return memberships; |
| 470 | } | |
| 471 | ||
| 472 | private void addMembershipsByRole( | |
| 473 | String endpointPrefix, | |
| 474 | String role, | |
| 475 | TeamStatus status, | |
| 476 | HttpEntity<String> entity, | |
| 477 | Map<String, TeamStatus> memberships) | |
| 478 | throws JsonProcessingException { | |
| 479 | String endpoint = endpointPrefix + "?per_page=100&role=" + role; | |
| 480 | Pattern pattern = Pattern.compile("(?<=<)([\\S]*)(?=>; rel=\"next\")"); | |
| 481 | ||
| 482 | ResponseEntity<String> response = | |
| 483 | restTemplate.exchange(endpoint, HttpMethod.GET, entity, String.class); | |
| 484 | List<String> responseLinks = response.getHeaders().getOrEmpty("link"); | |
| 485 | ||
| 486 |
2
1. addMembershipsByRole : negated conditional → KILLED 2. addMembershipsByRole : negated conditional → KILLED |
while (!responseLinks.isEmpty() && responseLinks.getFirst().contains("next")) { |
| 487 | for (GithubTeamMemberInfo member : | |
| 488 | objectMapper.convertValue( | |
| 489 | objectMapper.readTree(response.getBody()), | |
| 490 | new TypeReference<List<GithubTeamMemberInfo>>() {})) { | |
| 491 | memberships.put(member.login(), status); | |
| 492 | } | |
| 493 | ||
| 494 | Matcher matcher = pattern.matcher(responseLinks.getFirst()); | |
| 495 | matcher.find(); | |
| 496 | response = restTemplate.exchange(matcher.group(0), HttpMethod.GET, entity, String.class); | |
| 497 | responseLinks = response.getHeaders().getOrEmpty("link"); | |
| 498 | } | |
| 499 | ||
| 500 | for (GithubTeamMemberInfo member : | |
| 501 | objectMapper.convertValue( | |
| 502 | objectMapper.readTree(response.getBody()), | |
| 503 | new TypeReference<List<GithubTeamMemberInfo>>() {})) { | |
| 504 | memberships.put(member.login(), status); | |
| 505 | } | |
| 506 | } | |
| 507 | ||
| 508 | /** | |
| 509 | * Returns all teams for an organization, following pagination links when present. | |
| 510 | * | |
| 511 | * @param course The course containing the organization | |
| 512 | * @return A list of GitHub teams with id and name | |
| 513 | * @throws NoSuchAlgorithmException if there is an algorithm error | |
| 514 | * @throws InvalidKeySpecException if there is a key specification error | |
| 515 | * @throws JsonProcessingException if there is an error processing JSON | |
| 516 | */ | |
| 517 | public List<GithubTeamInfo> getAllTeams(Course course) | |
| 518 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 519 | String endpoint = "https://api.github.com/orgs/" + course.getOrgName() + "/teams?per_page=100"; | |
| 520 | Pattern pattern = Pattern.compile("(?<=<)([\\S]*)(?=>; rel=\"next\")"); | |
| 521 | ||
| 522 | HttpHeaders headers = new HttpHeaders(); | |
| 523 | String token = jwtService.getInstallationToken(course); | |
| 524 |
1
1. getAllTeams : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 525 |
1
1. getAllTeams : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 526 |
1
1. getAllTeams : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 527 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 528 | ||
| 529 | ResponseEntity<String> response = | |
| 530 | restTemplate.exchange(endpoint, HttpMethod.GET, entity, String.class); | |
| 531 | List<String> responseLinks = response.getHeaders().getOrEmpty("link"); | |
| 532 | List<GithubTeamInfo> teams = new ArrayList<>(); | |
| 533 | ||
| 534 |
2
1. getAllTeams : negated conditional → KILLED 2. getAllTeams : negated conditional → KILLED |
while (!responseLinks.isEmpty() && responseLinks.getFirst().contains("next")) { |
| 535 | teams.addAll( | |
| 536 | objectMapper.convertValue( | |
| 537 | objectMapper.readTree(response.getBody()), | |
| 538 | new TypeReference<List<GithubTeamInfo>>() {})); | |
| 539 | ||
| 540 | Matcher matcher = pattern.matcher(responseLinks.getFirst()); | |
| 541 | matcher.find(); | |
| 542 | response = restTemplate.exchange(matcher.group(0), HttpMethod.GET, entity, String.class); | |
| 543 | responseLinks = response.getHeaders().getOrEmpty("link"); | |
| 544 | } | |
| 545 | ||
| 546 | teams.addAll( | |
| 547 | objectMapper.convertValue( | |
| 548 | objectMapper.readTree(response.getBody()), | |
| 549 | new TypeReference<List<GithubTeamInfo>>() {})); | |
| 550 | ||
| 551 |
1
1. getAllTeams : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/services/GithubTeamService::getAllTeams → KILLED |
return teams; |
| 552 | } | |
| 553 | } | |
Mutations | ||
| 79 |
1.1 2.2 |
|
| 95 |
1.1 2.2 |
|
| 98 |
1.1 |
|
| 101 |
1.1 |
|
| 102 |
1.1 |
|
| 106 |
1.1 |
|
| 127 |
1.1 |
|
| 128 |
1.1 |
|
| 129 |
1.1 |
|
| 135 |
1.1 |
|
| 151 |
1.1 2.2 |
|
| 169 |
1.1 |
|
| 170 |
1.1 |
|
| 171 |
1.1 |
|
| 177 |
1.1 |
|
| 179 |
1.1 |
|
| 202 |
1.1 |
|
| 203 |
1.1 |
|
| 204 |
1.1 |
|
| 210 |
1.1 |
|
| 212 |
1.1 |
|
| 232 |
1.1 |
|
| 233 |
1.1 |
|
| 252 |
1.1 2.2 |
|
| 270 |
1.1 |
|
| 271 |
1.1 |
|
| 272 |
1.1 |
|
| 288 |
1.1 |
|
| 306 |
1.1 |
|
| 307 |
1.1 |
|
| 308 |
1.1 |
|
| 330 |
1.1 |
|
| 331 |
1.1 |
|
| 343 |
1.1 |
|
| 344 |
1.1 |
|
| 345 |
1.1 |
|
| 353 |
1.1 2.2 |
|
| 357 |
1.1 |
|
| 358 |
1.1 |
|
| 388 |
1.1 |
|
| 389 |
1.1 |
|
| 390 |
1.1 |
|
| 402 |
1.1 2.2 |
|
| 431 |
1.1 |
|
| 432 |
1.1 |
|
| 433 |
1.1 |
|
| 452 |
1.1 2.2 |
|
| 457 |
1.1 |
|
| 458 |
1.1 |
|
| 459 |
1.1 |
|
| 465 |
1.1 |
|
| 466 |
1.1 |
|
| 469 |
1.1 |
|
| 486 |
1.1 2.2 |
|
| 524 |
1.1 |
|
| 525 |
1.1 |
|
| 526 |
1.1 |
|
| 534 |
1.1 2.2 |
|
| 551 |
1.1 |