| 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.errors.InvalidInstallationTypeException; | |
| 7 | import org.springframework.beans.factory.annotation.Autowired; | |
| 8 | import org.springframework.boot.web.client.RestTemplateBuilder; | |
| 9 | import org.springframework.http.HttpEntity; | |
| 10 | import org.springframework.http.HttpHeaders; | |
| 11 | import org.springframework.http.HttpMethod; | |
| 12 | import org.springframework.http.ResponseEntity; | |
| 13 | import org.springframework.stereotype.Service; | |
| 14 | import org.springframework.web.client.RestTemplate; | |
| 15 | ||
| 16 | import java.security.NoSuchAlgorithmException; | |
| 17 | import java.security.spec.InvalidKeySpecException; | |
| 18 | ||
| 19 | @Service | |
| 20 | public class OrganizationLinkerService { | |
| 21 |     private RestTemplate restTemplate; | |
| 22 | ||
| 23 |     @Autowired | |
| 24 |     JwtService jwtService; | |
| 25 | ||
| 26 |     @Autowired | |
| 27 |     ObjectMapper objectMapper; | |
| 28 | ||
| 29 |     public OrganizationLinkerService(RestTemplateBuilder restTemplateBuilder) { | |
| 30 |         restTemplate = restTemplateBuilder.build(); | |
| 31 |     } | |
| 32 | ||
| 33 |     /** | |
| 34 |      * Returns the URL for a redirect to install Frontiers | |
| 35 |      * @return URL to install Frontiers to an organization | |
| 36 |      */ | |
| 37 |     public String getRedirectUrl() throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 38 |         String token = jwtService.getJwt(); | |
| 39 |         HttpHeaders requestHeaders = new HttpHeaders(); | |
| 40 | 
1
1. getRedirectUrl : removed call to org/springframework/http/HttpHeaders::add → KILLED | 
        requestHeaders.add("Authorization", "Bearer " + token); | 
| 41 | 
1
1. getRedirectUrl : removed call to org/springframework/http/HttpHeaders::add → KILLED | 
        requestHeaders.add("Accept", "application/vnd.github+json"); | 
| 42 | 
1
1. getRedirectUrl : removed call to org/springframework/http/HttpHeaders::add → KILLED | 
        requestHeaders.add("X-GitHub-Api-Version", "2022-11-28"); | 
| 43 |         String ENDPOINT = "https://api.github.com/app"; | |
| 44 |         HttpEntity<String> newEntity = new HttpEntity<>(requestHeaders); | |
| 45 |         ResponseEntity<String> response = restTemplate.exchange(ENDPOINT, HttpMethod.GET,  newEntity, String.class); | |
| 46 | ||
| 47 |         JsonNode responseJson = objectMapper.readTree(response.getBody()); | |
| 48 | ||
| 49 |         String newUrl = responseJson.get("html_url").toString().replaceAll("\"", ""); | |
| 50 | 
1
1. getRedirectUrl : replaced return value with "" for edu/ucsb/cs156/frontiers/services/OrganizationLinkerService::getRedirectUrl → KILLED | 
        return newUrl; | 
| 51 |     } | |
| 52 | ||
| 53 |     /** | |
| 54 |      * Provides the name of the organization attached to a particular installation ID | |
| 55 |      * @param installation_id ID of the app installation | |
| 56 |      * @return name of the organization attached to the installation | |
| 57 |      */ | |
| 58 |     public String getOrgName(String installation_id) throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 59 |         String token = jwtService.getJwt(); | |
| 60 |         String ENDPOINT = "https://api.github.com/app/installations/" + installation_id; | |
| 61 | ||
| 62 |         HttpHeaders headers = new HttpHeaders(); | |
| 63 | 
1
1. getOrgName : removed call to org/springframework/http/HttpHeaders::add → KILLED | 
        headers.add("Authorization", "Bearer " + token); | 
| 64 | 
1
1. getOrgName : removed call to org/springframework/http/HttpHeaders::add → KILLED | 
        headers.add("Accept", "application/vnd.github+json"); | 
| 65 | 
1
1. getOrgName : removed call to org/springframework/http/HttpHeaders::add → KILLED | 
        headers.add("X-GitHub-Api-Version", "2022-11-28"); | 
| 66 |         HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 67 |         ResponseEntity<String> response = restTemplate.exchange(ENDPOINT, HttpMethod.GET, entity, String.class); | |
| 68 |         JsonNode responseJson = objectMapper.readTree(response.getBody()); | |
| 69 |         String type = responseJson.get("account").get("type").asText(); | |
| 70 | 
1
1. getOrgName : negated conditional → KILLED | 
        if(!type.equals("Organization")){ | 
| 71 |             throw new InvalidInstallationTypeException(type); | |
| 72 |         } | |
| 73 |         String orgName = responseJson.get("account").get("login").asText(); | |
| 74 | 
1
1. getOrgName : replaced return value with "" for edu/ucsb/cs156/frontiers/services/OrganizationLinkerService::getOrgName → KILLED | 
        return orgName; | 
| 75 |     } | |
| 76 | } | |
Mutations | ||
| 40 | 
 
 1.1  | 
|
| 41 | 
 
 1.1  | 
|
| 42 | 
 
 1.1  | 
|
| 50 | 
 
 1.1  | 
|
| 63 | 
 
 1.1  | 
|
| 64 | 
 
 1.1  | 
|
| 65 | 
 
 1.1  | 
|
| 70 | 
 
 1.1  | 
|
| 74 | 
 
 1.1  |