RepositoryService.java

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
    String endpoint = "https://api.github.com/orgs/" + course.getOrgName() + "/repos?per_page=100";
347
348
    HttpEntity<String> entity = new HttpEntity<>(githubHeaders(token));
349
    ResponseEntity<String> response =
350
        restTemplate.exchange(endpoint, HttpMethod.GET, entity, String.class);
351
352
    JsonNode repos = mapper.readTree(response.getBody());
353
    List<GithubRepository> matchingRepos = new ArrayList<>();
354
    for (JsonNode repo : repos) {
355
      String name = repo.path("name").asText();
356 1 1. getRepositoriesMatchingPrefix : negated conditional → KILLED
      if (name.startsWith(prefix)) {
357
        matchingRepos.add(new GithubRepository(name, repo.path("full_name").asText()));
358
      }
359
    }
360 1 1. getRepositoriesMatchingPrefix : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/services/RepositoryService::getRepositoriesMatchingPrefix → KILLED
    return matchingRepos;
361
  }
362
363
  /** Returns true when the repository has no commits. */
364
  public boolean isRepositoryEmpty(Course course, String repositoryName)
365
      throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException {
366
    String token = jwtService.getInstallationToken(course);
367
    String endpoint =
368
        "https://api.github.com/repos/"
369
            + course.getOrgName()
370
            + "/"
371
            + repositoryName
372
            + "/commits?per_page=1";
373
374
    HttpEntity<String> entity = new HttpEntity<>(githubHeaders(token));
375
    try {
376
      ResponseEntity<String> response =
377
          restTemplate.exchange(endpoint, HttpMethod.GET, entity, String.class);
378
      JsonNode commits = mapper.readTree(response.getBody());
379 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();
380
    } catch (HttpClientErrorException e) {
381 1 1. isRepositoryEmpty : negated conditional → KILLED
      if (e.getStatusCode().equals(HttpStatus.CONFLICT)) {
382 1 1. isRepositoryEmpty : replaced boolean return with false for edu/ucsb/cs156/frontiers/services/RepositoryService::isRepositoryEmpty → KILLED
        return true;
383
      }
384
      throw e;
385
    }
386
  }
387
388
  /** Deletes a repository only when it is empty. Returns true when a delete request was sent. */
389
  public boolean deleteRepositoryIfEmpty(Course course, String repositoryName)
390
      throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException {
391 1 1. deleteRepositoryIfEmpty : negated conditional → KILLED
    if (!isRepositoryEmpty(course, repositoryName)) {
392 1 1. deleteRepositoryIfEmpty : replaced boolean return with true for edu/ucsb/cs156/frontiers/services/RepositoryService::deleteRepositoryIfEmpty → KILLED
      return false;
393
    }
394
395
    String token = jwtService.getInstallationToken(course);
396
    String endpoint = "https://api.github.com/repos/" + course.getOrgName() + "/" + repositoryName;
397
398
    HttpEntity<String> entity = new HttpEntity<>(githubHeaders(token));
399
    restTemplate.exchange(endpoint, HttpMethod.DELETE, entity, String.class);
400 1 1. deleteRepositoryIfEmpty : replaced boolean return with false for edu/ucsb/cs156/frontiers/services/RepositoryService::deleteRepositoryIfEmpty → KILLED
    return true;
401
  }
402
403
  /**
404
   * Creates a GitHub repository for a team (student or staff), given only their team name.
405
   *
406
   * <ul>
407
   *   <li>Checks whether the repository already exists.
408
   *   <li>If not, creates a new repository under the course's organization; if so, makes it private
409
   *       or public as given, since that may have changed since it was created.
410
   *   <li>Adds all team members as collaborators with the given permission level.
411
   * </ul>
412
   *
413
   * @param course the course whose organization the repo belongs to
414
   * @param team the team for which the repo is being created
415
   * @param repoPrefix prefix for the repository name (repoPrefix-teamSlug)
416
   * @param isPrivate whether the created repository should be private
417
   * @param permissions collaborator permissions to grant the user
418
   * @param orgId GitHub organization ID used for team-based repo provisioning
419
   * @return a {@link RepositoryCreationResult} indicating the repository name and whether it was
420
   *     newly created, or {@link Optional#empty()} if the existence check failed unexpectedly
421
   * @throws NoSuchAlgorithmException if signing fails
422
   * @throws InvalidKeySpecException if signing fails
423
   * @throws JsonProcessingException if JSON serialization fails
424
   */
425
  public Optional<RepositoryCreationResult> createTeamRepository(
426
      Course course,
427
      Team team,
428
      String repoPrefix,
429
      Boolean isPrivate,
430
      RepositoryPermissions permissions,
431
      Integer orgId)
432
      throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException {
433
    String teamSlug = getOrFetchTeamSlug(course, team, orgId);
434
    String newRepoName = repoPrefix + "-" + teamSlug;
435
    String token = jwtService.getInstallationToken(course);
436
437
    String existenceEndpoint =
438
        "https://api.github.com/repos/" + course.getOrgName() + "/" + newRepoName;
439
    String createEndpoint = "https://api.github.com/orgs/" + course.getOrgName() + "/repos";
440
    String provisionEndpoint =
441
        "https://api.github.com/organizations/"
442
            + orgId
443
            + "/team/"
444
            + team.getGithubTeamId()
445
            + "/repos/"
446
            + course.getOrgName()
447
            + "/"
448
            + newRepoName;
449
450
    HttpHeaders existenceHeaders = githubHeaders(token);
451
452
    HttpEntity<String> existenceEntity = new HttpEntity<>(existenceHeaders);
453
454
    boolean created = false;
455
    try {
456
      restTemplate.exchange(existenceEndpoint, HttpMethod.GET, existenceEntity, String.class);
457
    } catch (HttpClientErrorException e) {
458 1 1. createTeamRepository : negated conditional → KILLED
      if (e.getStatusCode().equals(HttpStatus.NOT_FOUND)) {
459
        HttpHeaders createHeaders = githubHeaders(token);
460
461
        Map<String, Object> body = new HashMap<>();
462
        body.put("name", newRepoName);
463
        body.put("private", isPrivate);
464
        String bodyAsJson = mapper.writeValueAsString(body);
465
466
        HttpEntity<String> createEntity = new HttpEntity<>(bodyAsJson, createHeaders);
467
468
        restTemplate.exchange(createEndpoint, HttpMethod.POST, createEntity, String.class);
469
        created = true;
470
      } else {
471
        log.warn(
472
            "Unexpected response code {} when checking for existence of repository {}",
473
            e.getStatusCode(),
474
            newRepoName);
475
        return Optional.empty();
476
      }
477
    }
478 1 1. createTeamRepository : negated conditional → KILLED
    if (!created) {
479 1 1. createTeamRepository : removed call to edu/ucsb/cs156/frontiers/services/RepositoryService::setRepositoryVisibility → KILLED
      setRepositoryVisibility(course, newRepoName, isPrivate, token);
480
    }
481
    try {
482
      Map<String, Object> provisionBody = new HashMap<>();
483
      provisionBody.put("permission", permissions.getApiName());
484
      String provisionAsJson = mapper.writeValueAsString(provisionBody);
485
486
      HttpEntity<String> provisionEntity = new HttpEntity<>(provisionAsJson, existenceHeaders);
487
      restTemplate.exchange(provisionEndpoint, HttpMethod.PUT, provisionEntity, String.class);
488
    } catch (HttpClientErrorException ignored) {
489
      // silently ignore if provisioning fails (same as before)
490
    }
491 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));
492
  }
493
494
  private String getOrFetchTeamSlug(Course course, Team team, Integer orgId)
495
      throws JsonProcessingException, NoSuchAlgorithmException, InvalidKeySpecException {
496 2 1. getOrFetchTeamSlug : negated conditional → KILLED
2. getOrFetchTeamSlug : negated conditional → KILLED
    if (team.getGithubTeamSlug() != null && !team.getGithubTeamSlug().isBlank()) {
497 1 1. getOrFetchTeamSlug : replaced return value with "" for edu/ucsb/cs156/frontiers/services/RepositoryService::getOrFetchTeamSlug → KILLED
      return team.getGithubTeamSlug();
498
    }
499
500 1 1. getOrFetchTeamSlug : negated conditional → KILLED
    if (team.getGithubTeamId() == null) {
501
      throw new IllegalStateException(
502
          "Cannot create team repository without a GitHub team ID for team '"
503
              + team.getName()
504
              + "'");
505
    }
506
507
    GithubTeamService.GithubTeamInfo teamInfo =
508
        githubTeamService.getTeamInfoById(orgId, team.getGithubTeamId(), course);
509
510 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()) {
511
      throw new IllegalStateException(
512
          "Cannot determine GitHub team slug for team '" + team.getName() + "'");
513
    }
514
515 1 1. getOrFetchTeamSlug : removed call to edu/ucsb/cs156/frontiers/entities/Team::setGithubTeamSlug → KILLED
    team.setGithubTeamSlug(teamInfo.slug());
516
    teamRepository.save(team);
517 1 1. getOrFetchTeamSlug : replaced return value with "" for edu/ucsb/cs156/frontiers/services/RepositoryService::getOrFetchTeamSlug → KILLED
    return teamInfo.slug();
518
  }
519
}

Mutations

60

1.1
Location : githubHeaders
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:setPullRequestCreationPolicy_COLLABORATORS_ONLY_patches_the_repo()]
removed call to org/springframework/http/HttpHeaders::add → KILLED

61

1.1
Location : githubHeaders
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:setPullRequestCreationPolicy_COLLABORATORS_ONLY_patches_the_repo()]
removed call to org/springframework/http/HttpHeaders::add → KILLED

62

1.1
Location : githubHeaders
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:setPullRequestCreationPolicy_COLLABORATORS_ONLY_patches_the_repo()]
removed call to org/springframework/http/HttpHeaders::add → KILLED

63

1.1
Location : githubHeaders
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:setPullRequestCreationPolicy_COLLABORATORS_ONLY_patches_the_repo()]
replaced return value with null for edu/ucsb/cs156/frontiers/services/RepositoryService::githubHeaders → KILLED

121

1.1
Location : createRepositoryForStudentOrStaff
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:exits_if_not_not_found()]
negated conditional → KILLED

141

1.1
Location : createRepositoryForStudentOrStaff
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:refused_visibility_change_of_existing_repo_propagates()]
negated conditional → KILLED

142

1.1
Location : createRepositoryForStudentOrStaff
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:refused_visibility_change_of_existing_repo_propagates()]
removed call to edu/ucsb/cs156/frontiers/services/RepositoryService::setRepositoryVisibility → KILLED

155

1.1
Location : createRepositoryForStudentOrStaff
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:existing_staff_repo_is_made_private_when_assignment_is_private()]
replaced return value with Optional.empty for edu/ucsb/cs156/frontiers/services/RepositoryService::createRepositoryForStudentOrStaff → KILLED

213

1.1
Location : createStudentRepository
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:existing_repo_is_made_private_when_assignment_is_private()]
replaced return value with Optional.empty for edu/ucsb/cs156/frontiers/services/RepositoryService::createStudentRepository → KILLED

237

1.1
Location : createStaffRepository
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:existing_staff_repo_is_made_private_when_assignment_is_private()]
replaced return value with Optional.empty for edu/ucsb/cs156/frontiers/services/RepositoryService::createStaffRepository → KILLED

280

1.1
Location : setSignedCommitsRequired
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:setSignedCommitsRequired_propagates_a_refusal_to_delete_the_ruleset()]
negated conditional → KILLED

286

1.1
Location : setSignedCommitsRequired
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:setSignedCommitsRequired_propagates_a_refusal_to_delete_the_ruleset()]
negated conditional → KILLED

287

1.1
Location : setSignedCommitsRequired
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:setSignedCommitsRequired_propagates_a_refusal_to_delete_the_ruleset()]
negated conditional → KILLED

309

1.1
Location : setSignedCommitsRequired
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:setSignedCommitsRequired_true_ignores_rulesets_with_other_names()]
negated conditional → KILLED

356

1.1
Location : getRepositoriesMatchingPrefix
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:getRepositoriesMatchingPrefix_returns_empty_list_when_no_repositories_match()]
negated conditional → KILLED

360

1.1
Location : getRepositoriesMatchingPrefix
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:getRepositoriesMatchingPrefix_filters_matching_repositories()]
replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/services/RepositoryService::getRepositoriesMatchingPrefix → KILLED

379

1.1
Location : isRepositoryEmpty
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:isRepositoryEmpty_returns_true_when_commits_response_is_empty_array()]
negated conditional → KILLED

2.2
Location : isRepositoryEmpty
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:isRepositoryEmpty_returns_false_when_commits_response_is_not_an_array()]
replaced boolean return with true for edu/ucsb/cs156/frontiers/services/RepositoryService::isRepositoryEmpty → KILLED

3.3
Location : isRepositoryEmpty
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:isRepositoryEmpty_returns_true_when_commits_response_is_empty_array()]
negated conditional → KILLED

381

1.1
Location : isRepositoryEmpty
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:isRepositoryEmpty_returns_true_when_github_reports_empty_repository()]
negated conditional → KILLED

382

1.1
Location : isRepositoryEmpty
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:isRepositoryEmpty_returns_true_when_github_reports_empty_repository()]
replaced boolean return with false for edu/ucsb/cs156/frontiers/services/RepositoryService::isRepositoryEmpty → KILLED

391

1.1
Location : deleteRepositoryIfEmpty
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:deleteRepositoryIfEmpty_does_not_delete_non_empty_repository()]
negated conditional → KILLED

392

1.1
Location : deleteRepositoryIfEmpty
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:deleteRepositoryIfEmpty_does_not_delete_non_empty_repository()]
replaced boolean return with true for edu/ucsb/cs156/frontiers/services/RepositoryService::deleteRepositoryIfEmpty → KILLED

400

1.1
Location : deleteRepositoryIfEmpty
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:deleteRepositoryIfEmpty_deletes_empty_repository()]
replaced boolean return with false for edu/ucsb/cs156/frontiers/services/RepositoryService::deleteRepositoryIfEmpty → KILLED

458

1.1
Location : createTeamRepository
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:exits_if_team_repo_not_not_found()]
negated conditional → KILLED

478

1.1
Location : createTeamRepository
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:refused_visibility_change_of_existing_team_repo_propagates()]
negated conditional → KILLED

479

1.1
Location : createTeamRepository
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:refused_visibility_change_of_existing_team_repo_propagates()]
removed call to edu/ucsb/cs156/frontiers/services/RepositoryService::setRepositoryVisibility → KILLED

491

1.1
Location : createTeamRepository
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:existing_team_repo_is_made_private_when_assignment_is_private()]
replaced return value with Optional.empty for edu/ucsb/cs156/frontiers/services/RepositoryService::createTeamRepository → KILLED

496

1.1
Location : getOrFetchTeamSlug
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:team_repo_throws_when_slug_missing_and_team_id_missing()]
negated conditional → KILLED

2.2
Location : getOrFetchTeamSlug
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:refused_visibility_change_of_existing_team_repo_propagates()]
negated conditional → KILLED

497

1.1
Location : getOrFetchTeamSlug
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:refused_visibility_change_of_existing_team_repo_propagates()]
replaced return value with "" for edu/ucsb/cs156/frontiers/services/RepositoryService::getOrFetchTeamSlug → KILLED

500

1.1
Location : getOrFetchTeamSlug
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:team_repo_throws_when_slug_missing_and_team_id_missing()]
negated conditional → KILLED

510

1.1
Location : getOrFetchTeamSlug
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:team_repo_throws_when_fetched_team_slug_is_blank()]
negated conditional → KILLED

2.2
Location : getOrFetchTeamSlug
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:team_repo_throws_when_fetched_team_slug_is_null()]
negated conditional → KILLED

3.3
Location : getOrFetchTeamSlug
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:team_repo_throws_when_fetched_team_info_is_null()]
negated conditional → KILLED

515

1.1
Location : getOrFetchTeamSlug
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:team_repo_fetches_and_persists_slug_when_missing()]
removed call to edu/ucsb/cs156/frontiers/entities/Team::setGithubTeamSlug → KILLED

517

1.1
Location : getOrFetchTeamSlug
Killed by : edu.ucsb.cs156.frontiers.services.RepositoryServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.RepositoryServiceTests]/[method:team_repo_fetches_and_persists_slug_when_missing()]
replaced return value with "" for edu/ucsb/cs156/frontiers/services/RepositoryService::getOrFetchTeamSlug → KILLED

Active mutators

Tests examined


Report generated by PIT 1.30.0 support