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