| 1 | package edu.ucsb.cs156.frontiers.services; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 5 | import edu.ucsb.cs156.frontiers.entities.Course; | |
| 6 | import edu.ucsb.cs156.frontiers.entities.RosterStudent; | |
| 7 | import org.springframework.boot.web.client.RestTemplateBuilder; | |
| 8 | import org.springframework.http.*; | |
| 9 | import org.springframework.stereotype.Service; | |
| 10 | import org.springframework.web.client.HttpClientErrorException; | |
| 11 | import org.springframework.web.client.RestTemplate; | |
| 12 | ||
| 13 | import java.security.NoSuchAlgorithmException; | |
| 14 | import java.security.spec.InvalidKeySpecException; | |
| 15 | import java.util.HashMap; | |
| 16 | import java.util.Map; | |
| 17 | ||
| 18 | @Service | |
| 19 | public class RepositoryService { | |
| 20 | private final JwtService jwtService; | |
| 21 | private final RestTemplate restTemplate; | |
| 22 | private final ObjectMapper mapper; | |
| 23 | ||
| 24 | public RepositoryService(JwtService jwtService, RestTemplateBuilder restTemplateBuilder, ObjectMapper mapper) { | |
| 25 | this.jwtService = jwtService; | |
| 26 | this.restTemplate = restTemplateBuilder.build(); | |
| 27 | this.mapper = mapper; | |
| 28 | } | |
| 29 | ||
| 30 | /** | |
| 31 | * Creates a single student repository if it doesn't already exist, and provisions access to the repository by that student | |
| 32 | * @param course The Course in question | |
| 33 | * @param student RosterStudent of the student the repository should be created for | |
| 34 | * @param repoPrefix Name of the project or assignment. Used to title the repository, in the format repoPrefix-githubLogin | |
| 35 | * @param isPrivate Whether the repository is private or not | |
| 36 | */ | |
| 37 | public void createStudentRepository(Course course, RosterStudent student, String repoPrefix, Boolean isPrivate) throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 38 | String newRepoName = repoPrefix+"-"+student.getGithubLogin(); | |
| 39 | String token = jwtService.getInstallationToken(course); | |
| 40 | String existenceEndpoint = "https://api.github.com/repos/"+course.getOrgName()+"/"+newRepoName; | |
| 41 | String createEndpoint = "https://api.github.com/orgs/"+course.getOrgName()+"/repos"; | |
| 42 | String provisionEndpoint = "https://api.github.com/repos/"+course.getOrgName()+"/"+newRepoName+"/collaborators/"+student.getGithubLogin(); | |
| 43 | HttpHeaders existenceHeaders = new HttpHeaders(); | |
| 44 |
1
1. createStudentRepository : removed call to org/springframework/http/HttpHeaders::add → KILLED |
existenceHeaders.add("Authorization", "Bearer " + token); |
| 45 |
1
1. createStudentRepository : removed call to org/springframework/http/HttpHeaders::add → KILLED |
existenceHeaders.add("Accept", "application/vnd.github+json"); |
| 46 |
1
1. createStudentRepository : removed call to org/springframework/http/HttpHeaders::add → KILLED |
existenceHeaders.add("X-GitHub-Api-Version", "2022-11-28"); |
| 47 | ||
| 48 | HttpEntity<String> existenceEntity = new HttpEntity<>(existenceHeaders); | |
| 49 | ||
| 50 | try { | |
| 51 | restTemplate.exchange(existenceEndpoint, HttpMethod.GET, existenceEntity, String.class); | |
| 52 | } catch(HttpClientErrorException e){ | |
| 53 |
1
1. createStudentRepository : negated conditional → KILLED |
if(e.getStatusCode().equals(HttpStatus.NOT_FOUND)){ |
| 54 | HttpHeaders createHeaders = new HttpHeaders(); | |
| 55 |
1
1. createStudentRepository : removed call to org/springframework/http/HttpHeaders::add → KILLED |
createHeaders.add("Authorization", "Bearer " + token); |
| 56 |
1
1. createStudentRepository : removed call to org/springframework/http/HttpHeaders::add → KILLED |
createHeaders.add("Accept", "application/vnd.github+json"); |
| 57 |
1
1. createStudentRepository : removed call to org/springframework/http/HttpHeaders::add → KILLED |
createHeaders.add("X-GitHub-Api-Version", "2022-11-28"); |
| 58 | ||
| 59 | Map<String, Object> body = new HashMap<>(); | |
| 60 | body.put("name", newRepoName); | |
| 61 | body.put("private", isPrivate); | |
| 62 | String bodyAsJson = mapper.writeValueAsString(body); | |
| 63 | ||
| 64 | HttpEntity<String> createEntity = new HttpEntity<>(bodyAsJson, createHeaders); | |
| 65 | ||
| 66 | restTemplate.exchange(createEndpoint, HttpMethod.POST, createEntity, String.class); | |
| 67 | Map<String, Object> provisionBody = new HashMap<>(); | |
| 68 | provisionBody.put("permission", "admin"); | |
| 69 | String provisionAsJson = mapper.writeValueAsString(provisionBody); | |
| 70 | ||
| 71 | HttpEntity<String> provisionEntity = new HttpEntity<>(provisionAsJson, createHeaders); | |
| 72 | restTemplate.exchange(provisionEndpoint, HttpMethod.PUT, provisionEntity, String.class); | |
| 73 | } | |
| 74 | } | |
| 75 | ||
| 76 | } | |
| 77 | } | |
Mutations | ||
| 44 |
1.1 |
|
| 45 |
1.1 |
|
| 46 |
1.1 |
|
| 53 |
1.1 |
|
| 55 |
1.1 |
|
| 56 |
1.1 |
|
| 57 |
1.1 |