| 1 | package edu.ucsb.cs156.frontiers.services; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.databind.JsonNode; | |
| 5 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 6 | import edu.ucsb.cs156.frontiers.entities.Course; | |
| 7 | import edu.ucsb.cs156.frontiers.entities.CourseStaff; | |
| 8 | import edu.ucsb.cs156.frontiers.entities.RosterStudent; | |
| 9 | import edu.ucsb.cs156.frontiers.entities.Team; | |
| 10 | import edu.ucsb.cs156.frontiers.enums.PullRequestCreationPolicy; | |
| 11 | import edu.ucsb.cs156.frontiers.enums.RepositoryPermissions; | |
| 12 | import edu.ucsb.cs156.frontiers.repositories.TeamRepository; | |
| 13 | import java.security.NoSuchAlgorithmException; | |
| 14 | import java.security.spec.InvalidKeySpecException; | |
| 15 | import java.time.Duration; | |
| 16 | import java.util.ArrayList; | |
| 17 | import java.util.HashMap; | |
| 18 | import java.util.List; | |
| 19 | import java.util.Map; | |
| 20 | import java.util.Optional; | |
| 21 | import lombok.extern.slf4j.Slf4j; | |
| 22 | import org.springframework.boot.web.client.RestTemplateBuilder; | |
| 23 | import org.springframework.http.*; | |
| 24 | import org.springframework.stereotype.Service; | |
| 25 | import org.springframework.web.client.HttpClientErrorException; | |
| 26 | import org.springframework.web.client.HttpStatusCodeException; | |
| 27 | import org.springframework.web.client.RestTemplate; | |
| 28 | ||
| 29 | @Service | |
| 30 | @Slf4j | |
| 31 | public class RepositoryService { | |
| 32 | /* | |
| 33 | * A RestTemplate built with no timeout blocks its calling thread forever on a hung external | |
| 34 | * call. That's a real incident lib-jobs' single-threaded jobsExecutor hit on another app | |
| 35 | * (proj-scaffold): a job stuck this way permanently wedged the executor, with no way to recover | |
| 36 | * short of restarting the app (see lib-jobs DESIGN.md 9 -- cooperative job cancellation only | |
| 37 | * helps a job that reaches another checkpoint, which a truly hung thread never will). Generous | |
| 38 | * but finite: long enough to never trip on legitimate slowness, short enough to guarantee a job | |
| 39 | * can't hang forever. | |
| 40 | */ | |
| 41 | private static final Duration CONNECT_TIMEOUT = Duration.ofSeconds(10); | |
| 42 | private static final Duration READ_TIMEOUT = Duration.ofSeconds(60); | |
| 43 | ||
| 44 | private final JwtService jwtService; | |
| 45 | private final GithubTeamService githubTeamService; | |
| 46 | private final TeamRepository teamRepository; | |
| 47 | private final RestTemplate restTemplate; | |
| 48 | private final ObjectMapper mapper; | |
| 49 | ||
| 50 | public record GithubRepository(String name, String fullName) {} | |
| 51 | ||
| 52 | /** | |
| 53 | * Result of a create-repository call: the repository name and whether the repository was newly | |
| 54 | * created (true) or already existed and was potentially updated (false). | |
| 55 | */ | |
| 56 | public record RepositoryCreationResult(String repoName, boolean created) {} | |
| 57 | ||
| 58 | private HttpHeaders githubHeaders(String token) { | |
| 59 | HttpHeaders headers = new HttpHeaders(); | |
| 60 |
1
1. githubHeaders : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 61 |
1
1. githubHeaders : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 62 |
1
1. githubHeaders : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 63 |
1
1. githubHeaders : replaced return value with null for edu/ucsb/cs156/frontiers/services/RepositoryService::githubHeaders → KILLED |
return headers; |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * Creates a GitHub repository for a user (student or staff), given only their GitHub login. | |
| 68 | * | |
| 69 | * <p>This helper method contains the shared logic used by both {@link | |
| 70 | * #createStudentRepository(Course, RosterStudent, String, Boolean, RepositoryPermissions)} and | |
| 71 | * {@link #createStaffRepository(Course, CourseStaff, String, Boolean, RepositoryPermissions)}. | |
| 72 | * | |
| 73 | * <ul> | |
| 74 | * <li>Checks whether the repository already exists. | |
| 75 | * <li>If not, creates a new repository under the course's organization; if so, makes it private | |
| 76 | * or public as given, since that may have changed since it was created. | |
| 77 | * <li>Adds the user as a collaborator with the given permission level. | |
| 78 | * </ul> | |
| 79 | * | |
| 80 | * @param course the course whose organization the repo belongs to | |
| 81 | * @param githubLogin GitHub username of the student or staff member | |
| 82 | * @param repoPrefix prefix for the repository name (repoPrefix-githubLogin) | |
| 83 | * @param isPrivate whether the created repository should be private | |
| 84 | * @param permissions collaborator permissions to grant the user | |
| 85 | * @return a {@link RepositoryCreationResult} indicating the repository name and whether it was | |
| 86 | * newly created, or {@link Optional#empty()} if the existence check failed unexpectedly | |
| 87 | * @throws NoSuchAlgorithmException if signing fails | |
| 88 | * @throws InvalidKeySpecException if signing fails | |
| 89 | * @throws JsonProcessingException if JSON serialization fails | |
| 90 | */ | |
| 91 | private Optional<RepositoryCreationResult> createRepositoryForStudentOrStaff( | |
| 92 | Course course, | |
| 93 | String githubLogin, | |
| 94 | String repoPrefix, | |
| 95 | Boolean isPrivate, | |
| 96 | RepositoryPermissions permissions) | |
| 97 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 98 | ||
| 99 | String newRepoName = repoPrefix + "-" + githubLogin; | |
| 100 | String token = jwtService.getInstallationToken(course); | |
| 101 | ||
| 102 | String existenceEndpoint = | |
| 103 | "https://api.github.com/repos/" + course.getOrgName() + "/" + newRepoName; | |
| 104 | String createEndpoint = "https://api.github.com/orgs/" + course.getOrgName() + "/repos"; | |
| 105 | String provisionEndpoint = | |
| 106 | "https://api.github.com/repos/" | |
| 107 | + course.getOrgName() | |
| 108 | + "/" | |
| 109 | + newRepoName | |
| 110 | + "/collaborators/" | |
| 111 | + githubLogin; | |
| 112 | ||
| 113 | HttpHeaders existenceHeaders = githubHeaders(token); | |
| 114 | ||
| 115 | HttpEntity<String> existenceEntity = new HttpEntity<>(existenceHeaders); | |
| 116 | ||
| 117 | boolean created = false; | |
| 118 | try { | |
| 119 | restTemplate.exchange(existenceEndpoint, HttpMethod.GET, existenceEntity, String.class); | |
| 120 | } catch (HttpClientErrorException e) { | |
| 121 |
1
1. createRepositoryForStudentOrStaff : negated conditional → KILLED |
if (e.getStatusCode().equals(HttpStatus.NOT_FOUND)) { |
| 122 | HttpHeaders createHeaders = githubHeaders(token); | |
| 123 | ||
| 124 | Map<String, Object> body = new HashMap<>(); | |
| 125 | body.put("name", newRepoName); | |
| 126 | body.put("private", isPrivate); | |
| 127 | String bodyAsJson = mapper.writeValueAsString(body); | |
| 128 | ||
| 129 | HttpEntity<String> createEntity = new HttpEntity<>(bodyAsJson, createHeaders); | |
| 130 | ||
| 131 | restTemplate.exchange(createEndpoint, HttpMethod.POST, createEntity, String.class); | |
| 132 | created = true; | |
| 133 | } else { | |
| 134 | log.warn( | |
| 135 | "Unexpected response code {} when checking for existence of repository {}", | |
| 136 | e.getStatusCode(), | |
| 137 | newRepoName); | |
| 138 | return Optional.empty(); | |
| 139 | } | |
| 140 | } | |
| 141 |
1
1. createRepositoryForStudentOrStaff : negated conditional → KILLED |
if (!created) { |
| 142 |
1
1. createRepositoryForStudentOrStaff : removed call to edu/ucsb/cs156/frontiers/services/RepositoryService::setRepositoryVisibility → KILLED |
setRepositoryVisibility(course, newRepoName, isPrivate, token); |
| 143 | } | |
| 144 | ||
| 145 | try { | |
| 146 | Map<String, Object> provisionBody = new HashMap<>(); | |
| 147 | provisionBody.put("permission", permissions.getApiName()); | |
| 148 | String provisionAsJson = mapper.writeValueAsString(provisionBody); | |
| 149 | ||
| 150 | HttpEntity<String> provisionEntity = new HttpEntity<>(provisionAsJson, existenceHeaders); | |
| 151 | restTemplate.exchange(provisionEndpoint, HttpMethod.PUT, provisionEntity, String.class); | |
| 152 | } catch (HttpClientErrorException ignored) { | |
| 153 | // silently ignore if provisioning fails (same as before) | |
| 154 | } | |
| 155 |
1
1. createRepositoryForStudentOrStaff : replaced return value with Optional.empty for edu/ucsb/cs156/frontiers/services/RepositoryService::createRepositoryForStudentOrStaff → KILLED |
return Optional.of(new RepositoryCreationResult(newRepoName, created)); |
| 156 | } | |
| 157 | ||
| 158 | /** | |
| 159 | * Makes an existing repository private, or public, as the assignment is now set. Creating a | |
| 160 | * repository sets this once, but an assignment can be changed from private to public (or back) | |
| 161 | * after its repositories exist, and running its job again must apply the change to them. | |
| 162 | * | |
| 163 | * <p>The request is always sent: setting a repository to the visibility it already has is | |
| 164 | * harmless, and doing so keeps the repositories of an assignment in step with it. | |
| 165 | * | |
| 166 | * @throws HttpStatusCodeException if GitHub refuses the change, for example because the GitHub | |
| 167 | * App is not allowed to administer repositories | |
| 168 | */ | |
| 169 | private void setRepositoryVisibility( | |
| 170 | Course course, String repositoryName, Boolean isPrivate, String token) | |
| 171 | throws JsonProcessingException { | |
| 172 | String endpoint = "https://api.github.com/repos/" + course.getOrgName() + "/" + repositoryName; | |
| 173 | Map<String, Object> body = new HashMap<>(); | |
| 174 | body.put("private", isPrivate); | |
| 175 | HttpEntity<String> entity = | |
| 176 | new HttpEntity<>(mapper.writeValueAsString(body), githubHeaders(token)); | |
| 177 | restTemplate.exchange(endpoint, HttpMethod.PATCH, entity, String.class); | |
| 178 | } | |
| 179 | ||
| 180 | public RepositoryService( | |
| 181 | JwtService jwtService, | |
| 182 | GithubTeamService githubTeamService, | |
| 183 | TeamRepository teamRepository, | |
| 184 | RestTemplateBuilder restTemplateBuilder, | |
| 185 | ObjectMapper mapper) { | |
| 186 | this.jwtService = jwtService; | |
| 187 | this.githubTeamService = githubTeamService; | |
| 188 | this.teamRepository = teamRepository; | |
| 189 | this.restTemplate = | |
| 190 | restTemplateBuilder.connectTimeout(CONNECT_TIMEOUT).readTimeout(READ_TIMEOUT).build(); | |
| 191 | this.mapper = mapper; | |
| 192 | } | |
| 193 | ||
| 194 | /** | |
| 195 | * Creates a single student repository if it doesn't already exist, and provisions access to the | |
| 196 | * repository by that student | |
| 197 | * | |
| 198 | * @param course The Course in question | |
| 199 | * @param student RosterStudent of the student the repository should be created for | |
| 200 | * @param repoPrefix Name of the project or assignment. Used to title the repository, in the | |
| 201 | * format repoPrefix-githubLogin | |
| 202 | * @param isPrivate Whether the repository is private or not | |
| 203 | * @return a {@link RepositoryCreationResult} indicating the repository name and whether it was | |
| 204 | * newly created, or {@link Optional#empty()} if the existence check failed unexpectedly | |
| 205 | */ | |
| 206 | public Optional<RepositoryCreationResult> createStudentRepository( | |
| 207 | Course course, | |
| 208 | RosterStudent student, | |
| 209 | String repoPrefix, | |
| 210 | Boolean isPrivate, | |
| 211 | RepositoryPermissions permissions) | |
| 212 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 213 |
1
1. createStudentRepository : replaced return value with Optional.empty for edu/ucsb/cs156/frontiers/services/RepositoryService::createStudentRepository → KILLED |
return createRepositoryForStudentOrStaff( |
| 214 | course, student.getGithubLogin(), repoPrefix, isPrivate, permissions); | |
| 215 | } | |
| 216 | ||
| 217 | /** | |
| 218 | * Creates a single staff repository if it doesn't already exist, and provisions access to the | |
| 219 | * repository by that staff member | |
| 220 | * | |
| 221 | * @param course The Course in question | |
| 222 | * @param staff CourseStaff of the staff the repository should be created for | |
| 223 | * @param repoPrefix Name of the project or assignment. Used to title the repository, in the | |
| 224 | * format repoPrefix-githubLogin | |
| 225 | * @param isPrivate Whether the repository is private or not | |
| 226 | * @return a {@link RepositoryCreationResult} indicating the repository name and whether it was | |
| 227 | * newly created, or {@link Optional#empty()} if the existence check failed unexpectedly | |
| 228 | */ | |
| 229 | public Optional<RepositoryCreationResult> createStaffRepository( | |
| 230 | Course course, | |
| 231 | CourseStaff staff, | |
| 232 | String repoPrefix, | |
| 233 | Boolean isPrivate, | |
| 234 | RepositoryPermissions permissions) | |
| 235 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 236 | ||
| 237 |
1
1. createStaffRepository : replaced return value with Optional.empty for edu/ucsb/cs156/frontiers/services/RepositoryService::createStaffRepository → KILLED |
return createRepositoryForStudentOrStaff( |
| 238 | course, staff.getGithubLogin(), repoPrefix, isPrivate, permissions); | |
| 239 | } | |
| 240 | ||
| 241 | /** The name of the ruleset that requires signed commits on a repository. */ | |
| 242 | public static final String SIGNED_COMMITS_RULESET_NAME = "Require Signed Commits"; | |
| 243 | ||
| 244 | /** | |
| 245 | * Makes a repository require signed commits, or not, by giving it a ruleset named {@value | |
| 246 | * #SIGNED_COMMITS_RULESET_NAME}, or removing that ruleset. The ruleset requires signatures on | |
| 247 | * every branch except gh-pages, and is active. | |
| 248 | * | |
| 249 | * <ul> | |
| 250 | * <li>required, and the repository has no such ruleset: it is created. | |
| 251 | * <li>required, and the repository already has it: it is updated in place, so that it has the | |
| 252 | * settings above and the repository is never without it. | |
| 253 | * <li>not required, and the repository has it: it is deleted. | |
| 254 | * <li>not required, and it does not: nothing is changed. | |
| 255 | * </ul> | |
| 256 | * | |
| 257 | * Only rulesets of the repository itself are considered, not ones inherited from the | |
| 258 | * organization. | |
| 259 | * | |
| 260 | * @param course the course whose organization the repository belongs to | |
| 261 | * @param repositoryName the name of the repository | |
| 262 | * @param required whether commits must be signed | |
| 263 | * @throws HttpStatusCodeException if GitHub refuses a request, for example because the GitHub App | |
| 264 | * is not allowed to administer repositories | |
| 265 | */ | |
| 266 | public void setSignedCommitsRequired(Course course, String repositoryName, boolean required) | |
| 267 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 268 | String token = jwtService.getInstallationToken(course); | |
| 269 | String rulesetsEndpoint = | |
| 270 | "https://api.github.com/repos/" + course.getOrgName() + "/" + repositoryName + "/rulesets"; | |
| 271 | ||
| 272 | ResponseEntity<String> listResponse = | |
| 273 | restTemplate.exchange( | |
| 274 | rulesetsEndpoint + "?includes_parents=false&per_page=100", | |
| 275 | HttpMethod.GET, | |
| 276 | new HttpEntity<>(githubHeaders(token)), | |
| 277 | String.class); | |
| 278 | Long existingId = null; | |
| 279 | for (JsonNode ruleset : mapper.readTree(listResponse.getBody())) { | |
| 280 |
1
1. setSignedCommitsRequired : negated conditional → KILLED |
if (SIGNED_COMMITS_RULESET_NAME.equals(ruleset.path("name").asText())) { |
| 281 | existingId = ruleset.path("id").asLong(); | |
| 282 | break; | |
| 283 | } | |
| 284 | } | |
| 285 | ||
| 286 |
1
1. setSignedCommitsRequired : negated conditional → KILLED |
if (!required) { |
| 287 |
1
1. setSignedCommitsRequired : negated conditional → KILLED |
if (existingId != null) { |
| 288 | restTemplate.exchange( | |
| 289 | rulesetsEndpoint + "/" + existingId, | |
| 290 | HttpMethod.DELETE, | |
| 291 | new HttpEntity<>(githubHeaders(token)), | |
| 292 | String.class); | |
| 293 | } | |
| 294 | return; | |
| 295 | } | |
| 296 | ||
| 297 | Map<String, Object> body = new HashMap<>(); | |
| 298 | body.put("name", SIGNED_COMMITS_RULESET_NAME); | |
| 299 | body.put("target", "branch"); | |
| 300 | body.put("enforcement", "active"); | |
| 301 | body.put( | |
| 302 | "conditions", | |
| 303 | Map.of( | |
| 304 | "ref_name", | |
| 305 | Map.of("include", List.of("~ALL"), "exclude", List.of("refs/heads/gh-pages")))); | |
| 306 | body.put("rules", List.of(Map.of("type", "required_signatures"))); | |
| 307 | HttpEntity<String> entity = | |
| 308 | new HttpEntity<>(mapper.writeValueAsString(body), githubHeaders(token)); | |
| 309 |
1
1. setSignedCommitsRequired : negated conditional → KILLED |
if (existingId == null) { |
| 310 | restTemplate.exchange(rulesetsEndpoint, HttpMethod.POST, entity, String.class); | |
| 311 | } else { | |
| 312 | restTemplate.exchange( | |
| 313 | rulesetsEndpoint + "/" + existingId, HttpMethod.PUT, entity, String.class); | |
| 314 | } | |
| 315 | } | |
| 316 | ||
| 317 | /** | |
| 318 | * Sets who may open pull requests against a repository, using the repository setting {@code | |
| 319 | * pull_request_creation_policy}. This works for any repository in the course's organization, | |
| 320 | * whatever created it. | |
| 321 | * | |
| 322 | * @param course the course whose organization the repository belongs to | |
| 323 | * @param repositoryName the name of the repository | |
| 324 | * @param policy who may open pull requests | |
| 325 | * @throws HttpStatusCodeException if GitHub refuses the request, for example because the GitHub | |
| 326 | * App is not allowed to administer repositories | |
| 327 | */ | |
| 328 | public void setPullRequestCreationPolicy( | |
| 329 | Course course, String repositoryName, PullRequestCreationPolicy policy) | |
| 330 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 331 | String token = jwtService.getInstallationToken(course); | |
| 332 | String endpoint = "https://api.github.com/repos/" + course.getOrgName() + "/" + repositoryName; | |
| 333 | Map<String, Object> body = Map.of("pull_request_creation_policy", policy.getGithubValue()); | |
| 334 | HttpEntity<String> entity = | |
| 335 | new HttpEntity<>(mapper.writeValueAsString(body), githubHeaders(token)); | |
| 336 | restTemplate.exchange(endpoint, HttpMethod.PATCH, entity, String.class); | |
| 337 | } | |
| 338 | ||
| 339 | /** | |
| 340 | * Lists repositories in the course GitHub organization whose names start with the provided | |
| 341 | * prefix. | |
| 342 | */ | |
| 343 | public List<GithubRepository> getRepositoriesMatchingPrefix(Course course, String prefix) | |
| 344 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 345 | String token = jwtService.getInstallationToken(course); | |
| 346 | ||
| 347 | HttpEntity<String> entity = new HttpEntity<>(githubHeaders(token)); | |
| 348 | ||
| 349 | List<GithubRepository> matchingRepos = new ArrayList<>(); | |
| 350 | int page = 1; | |
| 351 | while (true) { | |
| 352 | String endpoint = | |
| 353 | "https://api.github.com/orgs/" | |
| 354 | + course.getOrgName() | |
| 355 | + "/repos?per_page=" | |
| 356 | + 100 | |
| 357 | + "&page=" | |
| 358 | + page; | |
| 359 | ||
| 360 | ResponseEntity<String> response = | |
| 361 | restTemplate.exchange(endpoint, HttpMethod.GET, entity, String.class); | |
| 362 | ||
| 363 | JsonNode repos = mapper.readTree(response.getBody()); | |
| 364 | for (JsonNode repo : repos) { | |
| 365 | String name = repo.path("name").asText(); | |
| 366 |
1
1. getRepositoriesMatchingPrefix : negated conditional → KILLED |
if (name.startsWith(prefix)) { |
| 367 | matchingRepos.add(new GithubRepository(name, repo.path("full_name").asText())); | |
| 368 | } | |
| 369 | } | |
| 370 | ||
| 371 |
2
1. getRepositoriesMatchingPrefix : changed conditional boundary → KILLED 2. getRepositoriesMatchingPrefix : negated conditional → KILLED |
if (repos.size() < 100) { |
| 372 | break; | |
| 373 | } | |
| 374 |
1
1. getRepositoriesMatchingPrefix : Changed increment from 1 to -1 → KILLED |
page++; |
| 375 | } | |
| 376 |
1
1. getRepositoriesMatchingPrefix : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/services/RepositoryService::getRepositoriesMatchingPrefix → KILLED |
return matchingRepos; |
| 377 | } | |
| 378 | ||
| 379 | /** Returns true when the repository has no commits. */ | |
| 380 | public boolean isRepositoryEmpty(Course course, String repositoryName) | |
| 381 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 382 | String token = jwtService.getInstallationToken(course); | |
| 383 | String endpoint = | |
| 384 | "https://api.github.com/repos/" | |
| 385 | + course.getOrgName() | |
| 386 | + "/" | |
| 387 | + repositoryName | |
| 388 | + "/commits?per_page=1"; | |
| 389 | ||
| 390 | HttpEntity<String> entity = new HttpEntity<>(githubHeaders(token)); | |
| 391 | try { | |
| 392 | ResponseEntity<String> response = | |
| 393 | restTemplate.exchange(endpoint, HttpMethod.GET, entity, String.class); | |
| 394 | JsonNode commits = mapper.readTree(response.getBody()); | |
| 395 |
3
1. isRepositoryEmpty : negated conditional → KILLED 2. isRepositoryEmpty : replaced boolean return with true for edu/ucsb/cs156/frontiers/services/RepositoryService::isRepositoryEmpty → KILLED 3. isRepositoryEmpty : negated conditional → KILLED |
return commits.isArray() && commits.isEmpty(); |
| 396 | } catch (HttpClientErrorException e) { | |
| 397 |
1
1. isRepositoryEmpty : negated conditional → KILLED |
if (e.getStatusCode().equals(HttpStatus.CONFLICT)) { |
| 398 |
1
1. isRepositoryEmpty : replaced boolean return with false for edu/ucsb/cs156/frontiers/services/RepositoryService::isRepositoryEmpty → KILLED |
return true; |
| 399 | } | |
| 400 | throw e; | |
| 401 | } | |
| 402 | } | |
| 403 | ||
| 404 | /** Deletes a repository only when it is empty. Returns true when a delete request was sent. */ | |
| 405 | public boolean deleteRepositoryIfEmpty(Course course, String repositoryName) | |
| 406 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 407 |
1
1. deleteRepositoryIfEmpty : negated conditional → KILLED |
if (!isRepositoryEmpty(course, repositoryName)) { |
| 408 |
1
1. deleteRepositoryIfEmpty : replaced boolean return with true for edu/ucsb/cs156/frontiers/services/RepositoryService::deleteRepositoryIfEmpty → KILLED |
return false; |
| 409 | } | |
| 410 | ||
| 411 | String token = jwtService.getInstallationToken(course); | |
| 412 | String endpoint = "https://api.github.com/repos/" + course.getOrgName() + "/" + repositoryName; | |
| 413 | ||
| 414 | HttpEntity<String> entity = new HttpEntity<>(githubHeaders(token)); | |
| 415 | restTemplate.exchange(endpoint, HttpMethod.DELETE, entity, String.class); | |
| 416 |
1
1. deleteRepositoryIfEmpty : replaced boolean return with false for edu/ucsb/cs156/frontiers/services/RepositoryService::deleteRepositoryIfEmpty → KILLED |
return true; |
| 417 | } | |
| 418 | ||
| 419 | /** | |
| 420 | * Creates a GitHub repository for a team (student or staff), given only their team name. | |
| 421 | * | |
| 422 | * <ul> | |
| 423 | * <li>Checks whether the repository already exists. | |
| 424 | * <li>If not, creates a new repository under the course's organization; if so, makes it private | |
| 425 | * or public as given, since that may have changed since it was created. | |
| 426 | * <li>Adds all team members as collaborators with the given permission level. | |
| 427 | * </ul> | |
| 428 | * | |
| 429 | * @param course the course whose organization the repo belongs to | |
| 430 | * @param team the team for which the repo is being created | |
| 431 | * @param repoPrefix prefix for the repository name (repoPrefix-teamSlug) | |
| 432 | * @param isPrivate whether the created repository should be private | |
| 433 | * @param permissions collaborator permissions to grant the user | |
| 434 | * @param orgId GitHub organization ID used for team-based repo provisioning | |
| 435 | * @return a {@link RepositoryCreationResult} indicating the repository name and whether it was | |
| 436 | * newly created, or {@link Optional#empty()} if the existence check failed unexpectedly | |
| 437 | * @throws NoSuchAlgorithmException if signing fails | |
| 438 | * @throws InvalidKeySpecException if signing fails | |
| 439 | * @throws JsonProcessingException if JSON serialization fails | |
| 440 | */ | |
| 441 | public Optional<RepositoryCreationResult> createTeamRepository( | |
| 442 | Course course, | |
| 443 | Team team, | |
| 444 | String repoPrefix, | |
| 445 | Boolean isPrivate, | |
| 446 | RepositoryPermissions permissions, | |
| 447 | Integer orgId) | |
| 448 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 449 | String teamSlug = getOrFetchTeamSlug(course, team, orgId); | |
| 450 | String newRepoName = repoPrefix + "-" + teamSlug; | |
| 451 | String token = jwtService.getInstallationToken(course); | |
| 452 | ||
| 453 | String existenceEndpoint = | |
| 454 | "https://api.github.com/repos/" + course.getOrgName() + "/" + newRepoName; | |
| 455 | String createEndpoint = "https://api.github.com/orgs/" + course.getOrgName() + "/repos"; | |
| 456 | String provisionEndpoint = | |
| 457 | "https://api.github.com/organizations/" | |
| 458 | + orgId | |
| 459 | + "/team/" | |
| 460 | + team.getGithubTeamId() | |
| 461 | + "/repos/" | |
| 462 | + course.getOrgName() | |
| 463 | + "/" | |
| 464 | + newRepoName; | |
| 465 | ||
| 466 | HttpHeaders existenceHeaders = githubHeaders(token); | |
| 467 | ||
| 468 | HttpEntity<String> existenceEntity = new HttpEntity<>(existenceHeaders); | |
| 469 | ||
| 470 | boolean created = false; | |
| 471 | try { | |
| 472 | restTemplate.exchange(existenceEndpoint, HttpMethod.GET, existenceEntity, String.class); | |
| 473 | } catch (HttpClientErrorException e) { | |
| 474 |
1
1. createTeamRepository : negated conditional → KILLED |
if (e.getStatusCode().equals(HttpStatus.NOT_FOUND)) { |
| 475 | HttpHeaders createHeaders = githubHeaders(token); | |
| 476 | ||
| 477 | Map<String, Object> body = new HashMap<>(); | |
| 478 | body.put("name", newRepoName); | |
| 479 | body.put("private", isPrivate); | |
| 480 | String bodyAsJson = mapper.writeValueAsString(body); | |
| 481 | ||
| 482 | HttpEntity<String> createEntity = new HttpEntity<>(bodyAsJson, createHeaders); | |
| 483 | ||
| 484 | restTemplate.exchange(createEndpoint, HttpMethod.POST, createEntity, String.class); | |
| 485 | created = true; | |
| 486 | } else { | |
| 487 | log.warn( | |
| 488 | "Unexpected response code {} when checking for existence of repository {}", | |
| 489 | e.getStatusCode(), | |
| 490 | newRepoName); | |
| 491 | return Optional.empty(); | |
| 492 | } | |
| 493 | } | |
| 494 |
1
1. createTeamRepository : negated conditional → KILLED |
if (!created) { |
| 495 |
1
1. createTeamRepository : removed call to edu/ucsb/cs156/frontiers/services/RepositoryService::setRepositoryVisibility → KILLED |
setRepositoryVisibility(course, newRepoName, isPrivate, token); |
| 496 | } | |
| 497 | try { | |
| 498 | Map<String, Object> provisionBody = new HashMap<>(); | |
| 499 | provisionBody.put("permission", permissions.getApiName()); | |
| 500 | String provisionAsJson = mapper.writeValueAsString(provisionBody); | |
| 501 | ||
| 502 | HttpEntity<String> provisionEntity = new HttpEntity<>(provisionAsJson, existenceHeaders); | |
| 503 | restTemplate.exchange(provisionEndpoint, HttpMethod.PUT, provisionEntity, String.class); | |
| 504 | } catch (HttpClientErrorException ignored) { | |
| 505 | // silently ignore if provisioning fails (same as before) | |
| 506 | } | |
| 507 |
1
1. createTeamRepository : replaced return value with Optional.empty for edu/ucsb/cs156/frontiers/services/RepositoryService::createTeamRepository → KILLED |
return Optional.of(new RepositoryCreationResult(newRepoName, created)); |
| 508 | } | |
| 509 | ||
| 510 | private String getOrFetchTeamSlug(Course course, Team team, Integer orgId) | |
| 511 | throws JsonProcessingException, NoSuchAlgorithmException, InvalidKeySpecException { | |
| 512 |
2
1. getOrFetchTeamSlug : negated conditional → KILLED 2. getOrFetchTeamSlug : negated conditional → KILLED |
if (team.getGithubTeamSlug() != null && !team.getGithubTeamSlug().isBlank()) { |
| 513 |
1
1. getOrFetchTeamSlug : replaced return value with "" for edu/ucsb/cs156/frontiers/services/RepositoryService::getOrFetchTeamSlug → KILLED |
return team.getGithubTeamSlug(); |
| 514 | } | |
| 515 | ||
| 516 |
1
1. getOrFetchTeamSlug : negated conditional → KILLED |
if (team.getGithubTeamId() == null) { |
| 517 | throw new IllegalStateException( | |
| 518 | "Cannot create team repository without a GitHub team ID for team '" | |
| 519 | + team.getName() | |
| 520 | + "'"); | |
| 521 | } | |
| 522 | ||
| 523 | GithubTeamService.GithubTeamInfo teamInfo = | |
| 524 | githubTeamService.getTeamInfoById(orgId, team.getGithubTeamId(), course); | |
| 525 | ||
| 526 |
3
1. getOrFetchTeamSlug : negated conditional → KILLED 2. getOrFetchTeamSlug : negated conditional → KILLED 3. getOrFetchTeamSlug : negated conditional → KILLED |
if (teamInfo == null || teamInfo.slug() == null || teamInfo.slug().isBlank()) { |
| 527 | throw new IllegalStateException( | |
| 528 | "Cannot determine GitHub team slug for team '" + team.getName() + "'"); | |
| 529 | } | |
| 530 | ||
| 531 |
1
1. getOrFetchTeamSlug : removed call to edu/ucsb/cs156/frontiers/entities/Team::setGithubTeamSlug → KILLED |
team.setGithubTeamSlug(teamInfo.slug()); |
| 532 | teamRepository.save(team); | |
| 533 |
1
1. getOrFetchTeamSlug : replaced return value with "" for edu/ucsb/cs156/frontiers/services/RepositoryService::getOrFetchTeamSlug → KILLED |
return teamInfo.slug(); |
| 534 | } | |
| 535 | } | |
Mutations | ||
| 60 |
1.1 |
|
| 61 |
1.1 |
|
| 62 |
1.1 |
|
| 63 |
1.1 |
|
| 121 |
1.1 |
|
| 141 |
1.1 |
|
| 142 |
1.1 |
|
| 155 |
1.1 |
|
| 213 |
1.1 |
|
| 237 |
1.1 |
|
| 280 |
1.1 |
|
| 286 |
1.1 |
|
| 287 |
1.1 |
|
| 309 |
1.1 |
|
| 366 |
1.1 |
|
| 371 |
1.1 2.2 |
|
| 374 |
1.1 |
|
| 376 |
1.1 |
|
| 395 |
1.1 2.2 3.3 |
|
| 397 |
1.1 |
|
| 398 |
1.1 |
|
| 407 |
1.1 |
|
| 408 |
1.1 |
|
| 416 |
1.1 |
|
| 474 |
1.1 |
|
| 494 |
1.1 |
|
| 495 |
1.1 |
|
| 507 |
1.1 |
|
| 512 |
1.1 2.2 |
|
| 513 |
1.1 |
|
| 516 |
1.1 |
|
| 526 |
1.1 2.2 3.3 |
|
| 531 |
1.1 |
|
| 533 |
1.1 |