| 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.errors.InvalidInstallationTypeException; | |
| 8 | import edu.ucsb.cs156.frontiers.models.CourseWarning; | |
| 9 | import java.security.NoSuchAlgorithmException; | |
| 10 | import java.security.spec.InvalidKeySpecException; | |
| 11 | import java.time.ZonedDateTime; | |
| 12 | import org.springframework.beans.factory.annotation.Autowired; | |
| 13 | import org.springframework.boot.web.client.RestTemplateBuilder; | |
| 14 | import org.springframework.data.auditing.DateTimeProvider; | |
| 15 | import org.springframework.http.HttpEntity; | |
| 16 | import org.springframework.http.HttpHeaders; | |
| 17 | import org.springframework.http.HttpMethod; | |
| 18 | import org.springframework.http.ResponseEntity; | |
| 19 | import org.springframework.stereotype.Service; | |
| 20 | import org.springframework.web.client.HttpClientErrorException; | |
| 21 | import org.springframework.web.client.RestTemplate; | |
| 22 | ||
| 23 | @Service | |
| 24 | public class OrganizationLinkerService { | |
| 25 | private RestTemplate restTemplate; | |
| 26 | ||
| 27 | @Autowired JwtService jwtService; | |
| 28 | ||
| 29 | @Autowired ObjectMapper objectMapper; | |
| 30 | ||
| 31 | @Autowired DateTimeProvider provider; | |
| 32 | ||
| 33 | public OrganizationLinkerService(RestTemplateBuilder restTemplateBuilder) { | |
| 34 | restTemplate = restTemplateBuilder.build(); | |
| 35 | } | |
| 36 | ||
| 37 | /** | |
| 38 | * Returns the URL for a redirect to install Frontiers | |
| 39 | * | |
| 40 | * @return URL to install Frontiers to an organization | |
| 41 | */ | |
| 42 | public String getRedirectUrl() | |
| 43 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 44 | String token = jwtService.getJwt(); | |
| 45 | HttpHeaders requestHeaders = new HttpHeaders(); | |
| 46 |
1
1. getRedirectUrl : removed call to org/springframework/http/HttpHeaders::add → KILLED |
requestHeaders.add("Authorization", "Bearer " + token); |
| 47 |
1
1. getRedirectUrl : removed call to org/springframework/http/HttpHeaders::add → KILLED |
requestHeaders.add("Accept", "application/vnd.github+json"); |
| 48 |
1
1. getRedirectUrl : removed call to org/springframework/http/HttpHeaders::add → KILLED |
requestHeaders.add("X-GitHub-Api-Version", "2022-11-28"); |
| 49 | String ENDPOINT = "https://api.github.com/app"; | |
| 50 | HttpEntity<String> newEntity = new HttpEntity<>(requestHeaders); | |
| 51 | ResponseEntity<String> response = | |
| 52 | restTemplate.exchange(ENDPOINT, HttpMethod.GET, newEntity, String.class); | |
| 53 | ||
| 54 | JsonNode responseJson = objectMapper.readTree(response.getBody()); | |
| 55 | ||
| 56 | String newUrl = responseJson.get("html_url").toString().replaceAll("\"", ""); | |
| 57 |
1
1. getRedirectUrl : replaced return value with "" for edu/ucsb/cs156/frontiers/services/OrganizationLinkerService::getRedirectUrl → KILLED |
return newUrl; |
| 58 | } | |
| 59 | ||
| 60 | /** | |
| 61 | * Provides the name of the organization attached to a particular installation ID | |
| 62 | * | |
| 63 | * @param installation_id ID of the app installation | |
| 64 | * @return name of the organization attached to the installation | |
| 65 | */ | |
| 66 | public String getOrgName(String installation_id) | |
| 67 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 68 | String token = jwtService.getJwt(); | |
| 69 | String ENDPOINT = "https://api.github.com/app/installations/" + installation_id; | |
| 70 | ||
| 71 | HttpHeaders headers = new HttpHeaders(); | |
| 72 |
1
1. getOrgName : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 73 |
1
1. getOrgName : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 74 |
1
1. getOrgName : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 75 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 76 | ResponseEntity<String> response = | |
| 77 | restTemplate.exchange(ENDPOINT, HttpMethod.GET, entity, String.class); | |
| 78 | JsonNode responseJson = objectMapper.readTree(response.getBody()); | |
| 79 | String type = responseJson.get("account").get("type").asText(); | |
| 80 |
1
1. getOrgName : negated conditional → KILLED |
if (!type.equals("Organization")) { |
| 81 | throw new InvalidInstallationTypeException(type); | |
| 82 | } | |
| 83 | String orgName = responseJson.get("account").get("login").asText(); | |
| 84 |
1
1. getOrgName : replaced return value with "" for edu/ucsb/cs156/frontiers/services/OrganizationLinkerService::getOrgName → KILLED |
return orgName; |
| 85 | } | |
| 86 | ||
| 87 | public CourseWarning checkCourseWarnings(Course course) | |
| 88 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 89 | ||
| 90 |
2
1. checkCourseWarnings : negated conditional → KILLED 2. checkCourseWarnings : negated conditional → KILLED |
if (course.getOrgName() == null || course.getInstallationId() == null) { |
| 91 |
1
1. checkCourseWarnings : replaced return value with null for edu/ucsb/cs156/frontiers/services/OrganizationLinkerService::checkCourseWarnings → KILLED |
return new CourseWarning(false); |
| 92 | } | |
| 93 | ||
| 94 | String ENDPOINT = "https://api.github.com/orgs/" + course.getOrgName(); | |
| 95 | HttpHeaders headers = new HttpHeaders(); | |
| 96 | String token = jwtService.getInstallationToken(course); | |
| 97 |
1
1. checkCourseWarnings : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 98 |
1
1. checkCourseWarnings : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 99 |
1
1. checkCourseWarnings : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 100 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 101 | ResponseEntity<JsonNode> response = | |
| 102 | restTemplate.exchange(ENDPOINT, HttpMethod.GET, entity, JsonNode.class); | |
| 103 | JsonNode responseJson = response.getBody(); | |
| 104 | ZonedDateTime creationDate = ZonedDateTime.parse(responseJson.get("created_at").asText()); | |
| 105 | ZonedDateTime now = ZonedDateTime.from(provider.getNow().get()); | |
| 106 |
1
1. checkCourseWarnings : replaced return value with null for edu/ucsb/cs156/frontiers/services/OrganizationLinkerService::checkCourseWarnings → KILLED |
return new CourseWarning(creationDate.isAfter(now.minusMonths(1))); |
| 107 | } | |
| 108 | ||
| 109 | /** | |
| 110 | * Removes the Frontiers installation from the linked GitHub org | |
| 111 | * | |
| 112 | * @param course The entity for the course about to be deleted | |
| 113 | */ | |
| 114 | public void unenrollOrganization(Course course) | |
| 115 | throws NoSuchAlgorithmException, InvalidKeySpecException { | |
| 116 |
2
1. unenrollOrganization : negated conditional → KILLED 2. unenrollOrganization : negated conditional → KILLED |
if (course.getOrgName() == null || course.getInstallationId() == null) { |
| 117 | return; | |
| 118 | } | |
| 119 | String token = jwtService.getJwt(); | |
| 120 | String ENDPOINT = "https://api.github.com/app/installations/" + course.getInstallationId(); | |
| 121 | HttpHeaders headers = new HttpHeaders(); | |
| 122 |
1
1. unenrollOrganization : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 123 |
1
1. unenrollOrganization : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 124 |
1
1. unenrollOrganization : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 125 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 126 | try { | |
| 127 | ResponseEntity<String> response = | |
| 128 | restTemplate.exchange(ENDPOINT, HttpMethod.DELETE, entity, String.class); | |
| 129 | } catch (HttpClientErrorException ignored) { | |
| 130 | ||
| 131 | } | |
| 132 | } | |
| 133 | } | |
Mutations | ||
| 46 |
1.1 |
|
| 47 |
1.1 |
|
| 48 |
1.1 |
|
| 57 |
1.1 |
|
| 72 |
1.1 |
|
| 73 |
1.1 |
|
| 74 |
1.1 |
|
| 80 |
1.1 |
|
| 84 |
1.1 |
|
| 90 |
1.1 2.2 |
|
| 91 |
1.1 |
|
| 97 |
1.1 |
|
| 98 |
1.1 |
|
| 99 |
1.1 |
|
| 106 |
1.1 |
|
| 116 |
1.1 2.2 |
|
| 122 |
1.1 |
|
| 123 |
1.1 |
|
| 124 |
1.1 |