| 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.errors.NoLinkedOrganizationException; | |
| 9 | import edu.ucsb.cs156.frontiers.models.CourseWarning; | |
| 10 | import java.security.NoSuchAlgorithmException; | |
| 11 | import java.security.spec.InvalidKeySpecException; | |
| 12 | import java.time.Duration; | |
| 13 | import java.time.ZonedDateTime; | |
| 14 | import org.springframework.beans.factory.annotation.Autowired; | |
| 15 | import org.springframework.boot.web.client.RestTemplateBuilder; | |
| 16 | import org.springframework.data.auditing.DateTimeProvider; | |
| 17 | import org.springframework.http.HttpEntity; | |
| 18 | import org.springframework.http.HttpHeaders; | |
| 19 | import org.springframework.http.HttpMethod; | |
| 20 | import org.springframework.http.ResponseEntity; | |
| 21 | import org.springframework.stereotype.Service; | |
| 22 | import org.springframework.web.client.HttpClientErrorException; | |
| 23 | import org.springframework.web.client.RestTemplate; | |
| 24 | ||
| 25 | @Service | |
| 26 | public class OrganizationLinkerService { | |
| 27 | /* | |
| 28 | * A RestTemplate built with no timeout blocks its calling thread forever on a hung external | |
| 29 | * call. That's a real incident lib-jobs' single-threaded jobsExecutor hit on another app | |
| 30 | * (proj-scaffold): a job stuck this way permanently wedged the executor, with no way to recover | |
| 31 | * short of restarting the app (see lib-jobs DESIGN.md 9 -- cooperative job cancellation only | |
| 32 | * helps a job that reaches another checkpoint, which a truly hung thread never will). Generous | |
| 33 | * but finite: long enough to never trip on legitimate slowness, short enough to guarantee a job | |
| 34 | * can't hang forever. | |
| 35 | */ | |
| 36 | private static final Duration CONNECT_TIMEOUT = Duration.ofSeconds(10); | |
| 37 | private static final Duration READ_TIMEOUT = Duration.ofSeconds(60); | |
| 38 | ||
| 39 | private RestTemplate restTemplate; | |
| 40 | ||
| 41 | @Autowired JwtService jwtService; | |
| 42 | ||
| 43 | @Autowired ObjectMapper objectMapper; | |
| 44 | ||
| 45 | @Autowired DateTimeProvider provider; | |
| 46 | ||
| 47 | public OrganizationLinkerService(RestTemplateBuilder restTemplateBuilder) { | |
| 48 | restTemplate = | |
| 49 | restTemplateBuilder.connectTimeout(CONNECT_TIMEOUT).readTimeout(READ_TIMEOUT).build(); | |
| 50 | } | |
| 51 | ||
| 52 | /** | |
| 53 | * Returns the URL for a redirect to install Frontiers | |
| 54 | * | |
| 55 | * @return URL to install Frontiers to an organization | |
| 56 | */ | |
| 57 | public String getRedirectUrl() | |
| 58 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 59 | String token = jwtService.getJwt(); | |
| 60 | HttpHeaders requestHeaders = new HttpHeaders(); | |
| 61 |
1
1. getRedirectUrl : removed call to org/springframework/http/HttpHeaders::add → KILLED |
requestHeaders.add("Authorization", "Bearer " + token); |
| 62 |
1
1. getRedirectUrl : removed call to org/springframework/http/HttpHeaders::add → KILLED |
requestHeaders.add("Accept", "application/vnd.github+json"); |
| 63 |
1
1. getRedirectUrl : removed call to org/springframework/http/HttpHeaders::add → KILLED |
requestHeaders.add("X-GitHub-Api-Version", "2022-11-28"); |
| 64 | String ENDPOINT = "https://api.github.com/app"; | |
| 65 | HttpEntity<String> newEntity = new HttpEntity<>(requestHeaders); | |
| 66 | ResponseEntity<String> response = | |
| 67 | restTemplate.exchange(ENDPOINT, HttpMethod.GET, newEntity, String.class); | |
| 68 | ||
| 69 | JsonNode responseJson = objectMapper.readTree(response.getBody()); | |
| 70 | ||
| 71 | String newUrl = responseJson.get("html_url").toString().replaceAll("\"", ""); | |
| 72 |
1
1. getRedirectUrl : replaced return value with "" for edu/ucsb/cs156/frontiers/services/OrganizationLinkerService::getRedirectUrl → KILLED |
return newUrl; |
| 73 | } | |
| 74 | ||
| 75 | /** | |
| 76 | * Provides the name of the organization attached to a particular installation ID | |
| 77 | * | |
| 78 | * @param installation_id ID of the app installation | |
| 79 | * @return name of the organization attached to the installation | |
| 80 | */ | |
| 81 | public String getOrgName(String installation_id) | |
| 82 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 83 | String token = jwtService.getJwt(); | |
| 84 | String ENDPOINT = "https://api.github.com/app/installations/" + installation_id; | |
| 85 | ||
| 86 | HttpHeaders headers = new HttpHeaders(); | |
| 87 |
1
1. getOrgName : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 88 |
1
1. getOrgName : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 89 |
1
1. getOrgName : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 90 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 91 | ResponseEntity<String> response = | |
| 92 | restTemplate.exchange(ENDPOINT, HttpMethod.GET, entity, String.class); | |
| 93 | JsonNode responseJson = objectMapper.readTree(response.getBody()); | |
| 94 | String type = responseJson.get("account").get("type").asText(); | |
| 95 |
1
1. getOrgName : negated conditional → KILLED |
if (!type.equals("Organization")) { |
| 96 | throw new InvalidInstallationTypeException(type); | |
| 97 | } | |
| 98 | String orgName = responseJson.get("account").get("login").asText(); | |
| 99 |
1
1. getOrgName : replaced return value with "" for edu/ucsb/cs156/frontiers/services/OrganizationLinkerService::getOrgName → KILLED |
return orgName; |
| 100 | } | |
| 101 | ||
| 102 | public CourseWarning checkCourseWarnings(Course course) | |
| 103 | throws NoSuchAlgorithmException, | |
| 104 | InvalidKeySpecException, | |
| 105 | JsonProcessingException, | |
| 106 | NoLinkedOrganizationException { | |
| 107 | ||
| 108 |
2
1. checkCourseWarnings : negated conditional → KILLED 2. checkCourseWarnings : negated conditional → KILLED |
if (course.getOrgName() == null || course.getInstallationId() == null) { |
| 109 |
1
1. checkCourseWarnings : replaced return value with null for edu/ucsb/cs156/frontiers/services/OrganizationLinkerService::checkCourseWarnings → KILLED |
return new CourseWarning(false, false, false); |
| 110 | } | |
| 111 | ||
| 112 | String ENDPOINT = "https://api.github.com/orgs/" + course.getOrgName(); | |
| 113 | HttpHeaders headers = new HttpHeaders(); | |
| 114 | String token = jwtService.getInstallationToken(course); | |
| 115 |
1
1. checkCourseWarnings : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 116 |
1
1. checkCourseWarnings : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 117 |
1
1. checkCourseWarnings : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 118 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 119 | ResponseEntity<JsonNode> response = | |
| 120 | restTemplate.exchange(ENDPOINT, HttpMethod.GET, entity, JsonNode.class); | |
| 121 | JsonNode responseJson = response.getBody(); | |
| 122 | ZonedDateTime creationDate = ZonedDateTime.parse(responseJson.get("created_at").asText()); | |
| 123 | ZonedDateTime now = ZonedDateTime.from(provider.getNow().get()); | |
| 124 | boolean showOrganizationAgeWarning = creationDate.isAfter(now.minusMonths(1)); | |
| 125 | String defaultRepositoryPermission = getDefaultRepositoryPermission(course); | |
| 126 |
1
1. checkCourseWarnings : negated conditional → KILLED |
boolean showDefaultBasePermissions = !"none".equals(defaultRepositoryPermission); |
| 127 | boolean showFreePlanWarning = FREE_PLAN_NAME.equals(getPlanName(responseJson)); | |
| 128 |
1
1. checkCourseWarnings : replaced return value with null for edu/ucsb/cs156/frontiers/services/OrganizationLinkerService::checkCourseWarnings → KILLED |
return new CourseWarning( |
| 129 | showOrganizationAgeWarning, showDefaultBasePermissions, showFreePlanWarning); | |
| 130 | } | |
| 131 | ||
| 132 | /** The {@code plan.name} GitHub reports for an organization on its Free plan. */ | |
| 133 | public static final String FREE_PLAN_NAME = "free"; | |
| 134 | ||
| 135 | /** | |
| 136 | * Extracts the organization's plan name (e.g. {@code free}, {@code team}, {@code enterprise}) | |
| 137 | * from a GET /orgs/{org} response. GitHub only includes the {@code plan} object when the GitHub | |
| 138 | * App has the organization "Plan" (read) permission; without it, the plan is unknown and the | |
| 139 | * empty string is returned, so no plan-based warning is shown. | |
| 140 | * | |
| 141 | * @param orgJson the response body of GET /orgs/{org} | |
| 142 | * @return the plan name, or the empty string if it is not present | |
| 143 | */ | |
| 144 | public static String getPlanName(JsonNode orgJson) { | |
| 145 |
1
1. getPlanName : replaced return value with "" for edu/ucsb/cs156/frontiers/services/OrganizationLinkerService::getPlanName → KILLED |
return orgJson.path("plan").path("name").asText(""); |
| 146 | } | |
| 147 | ||
| 148 | /** | |
| 149 | * Retrieves the default repository permission for the GitHub organization linked to a course. | |
| 150 | * | |
| 151 | * @param course The course whose linked organization is queried. | |
| 152 | * @return The organization's default repository permission (e.g. {@code none}, {@code read}). | |
| 153 | */ | |
| 154 | public String getDefaultRepositoryPermission(Course course) | |
| 155 | throws NoSuchAlgorithmException, | |
| 156 | InvalidKeySpecException, | |
| 157 | JsonProcessingException, | |
| 158 | NoLinkedOrganizationException { | |
| 159 | String ENDPOINT = "https://api.github.com/orgs/" + course.getOrgName(); | |
| 160 | HttpHeaders headers = new HttpHeaders(); | |
| 161 | String token = jwtService.getInstallationToken(course); | |
| 162 |
1
1. getDefaultRepositoryPermission : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 163 |
1
1. getDefaultRepositoryPermission : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 164 |
1
1. getDefaultRepositoryPermission : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 165 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 166 | ResponseEntity<JsonNode> response = | |
| 167 | restTemplate.exchange(ENDPOINT, HttpMethod.GET, entity, JsonNode.class); | |
| 168 |
1
1. getDefaultRepositoryPermission : replaced return value with "" for edu/ucsb/cs156/frontiers/services/OrganizationLinkerService::getDefaultRepositoryPermission → KILLED |
return response.getBody().get("default_repository_permission").asText(); |
| 169 | } | |
| 170 | ||
| 171 | /** | |
| 172 | * Removes the Frontiers installation from the linked GitHub org | |
| 173 | * | |
| 174 | * @param course The entity for the course about to be deleted | |
| 175 | */ | |
| 176 | public void unenrollOrganization(Course course) | |
| 177 | throws NoSuchAlgorithmException, InvalidKeySpecException { | |
| 178 |
2
1. unenrollOrganization : negated conditional → KILLED 2. unenrollOrganization : negated conditional → KILLED |
if (course.getOrgName() == null || course.getInstallationId() == null) { |
| 179 | return; | |
| 180 | } | |
| 181 | String token = jwtService.getJwt(); | |
| 182 | String ENDPOINT = "https://api.github.com/app/installations/" + course.getInstallationId(); | |
| 183 | HttpHeaders headers = new HttpHeaders(); | |
| 184 |
1
1. unenrollOrganization : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 185 |
1
1. unenrollOrganization : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 186 |
1
1. unenrollOrganization : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 187 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 188 | try { | |
| 189 | ResponseEntity<String> response = | |
| 190 | restTemplate.exchange(ENDPOINT, HttpMethod.DELETE, entity, String.class); | |
| 191 | } catch (HttpClientErrorException ignored) { | |
| 192 | ||
| 193 | } | |
| 194 | } | |
| 195 | } | |
Mutations | ||
| 61 |
1.1 |
|
| 62 |
1.1 |
|
| 63 |
1.1 |
|
| 72 |
1.1 |
|
| 87 |
1.1 |
|
| 88 |
1.1 |
|
| 89 |
1.1 |
|
| 95 |
1.1 |
|
| 99 |
1.1 |
|
| 108 |
1.1 2.2 |
|
| 109 |
1.1 |
|
| 115 |
1.1 |
|
| 116 |
1.1 |
|
| 117 |
1.1 |
|
| 126 |
1.1 |
|
| 128 |
1.1 |
|
| 145 |
1.1 |
|
| 162 |
1.1 |
|
| 163 |
1.1 |
|
| 164 |
1.1 |
|
| 168 |
1.1 |
|
| 178 |
1.1 2.2 |
|
| 184 |
1.1 |
|
| 185 |
1.1 |
|
| 186 |
1.1 |