| 1 | package edu.ucsb.cs156.frontiers.services; | |
| 2 | ||
| 3 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 4 | import com.fasterxml.jackson.core.type.TypeReference; | |
| 5 | import com.fasterxml.jackson.databind.DeserializationFeature; | |
| 6 | import com.fasterxml.jackson.databind.JsonNode; | |
| 7 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 8 | import edu.ucsb.cs156.frontiers.entities.Course; | |
| 9 | import edu.ucsb.cs156.frontiers.entities.CourseStaff; | |
| 10 | import edu.ucsb.cs156.frontiers.entities.RosterStudent; | |
| 11 | import edu.ucsb.cs156.frontiers.enums.OrgStatus; | |
| 12 | import edu.ucsb.cs156.frontiers.models.OrgMember; | |
| 13 | import edu.ucsb.cs156.frontiers.repositories.RosterStudentRepository; | |
| 14 | import java.security.NoSuchAlgorithmException; | |
| 15 | import java.security.spec.InvalidKeySpecException; | |
| 16 | import java.time.Duration; | |
| 17 | import java.util.ArrayList; | |
| 18 | import java.util.HashMap; | |
| 19 | import java.util.List; | |
| 20 | import java.util.Map; | |
| 21 | import java.util.regex.Matcher; | |
| 22 | import java.util.regex.Pattern; | |
| 23 | import lombok.extern.slf4j.Slf4j; | |
| 24 | import org.springframework.boot.web.client.RestTemplateBuilder; | |
| 25 | import org.springframework.http.HttpEntity; | |
| 26 | import org.springframework.http.HttpHeaders; | |
| 27 | import org.springframework.http.HttpMethod; | |
| 28 | import org.springframework.http.ResponseEntity; | |
| 29 | import org.springframework.stereotype.Service; | |
| 30 | import org.springframework.web.client.HttpClientErrorException; | |
| 31 | import org.springframework.web.client.RestTemplate; | |
| 32 | ||
| 33 | @Slf4j | |
| 34 | @Service | |
| 35 | public class OrganizationMemberService { | |
| 36 | ||
| 37 | /* | |
| 38 | * A RestTemplate built with no timeout blocks its calling thread forever on a hung external | |
| 39 | * call. That's a real incident lib-jobs' single-threaded jobsExecutor hit on another app | |
| 40 | * (proj-scaffold): a job stuck this way permanently wedged the executor, with no way to recover | |
| 41 | * short of restarting the app (see lib-jobs DESIGN.md 9 -- cooperative job cancellation only | |
| 42 | * helps a job that reaches another checkpoint, which a truly hung thread never will). Generous | |
| 43 | * but finite: long enough to never trip on legitimate slowness, short enough to guarantee a job | |
| 44 | * can't hang forever. | |
| 45 | */ | |
| 46 | private static final Duration CONNECT_TIMEOUT = Duration.ofSeconds(10); | |
| 47 | private static final Duration READ_TIMEOUT = Duration.ofSeconds(60); | |
| 48 | ||
| 49 | private final JwtService jwtService; | |
| 50 | private final ObjectMapper objectMapper; | |
| 51 | private final RestTemplate restTemplate; | |
| 52 | private final RosterStudentRepository rosterStudentRepository; | |
| 53 | ||
| 54 | public OrganizationMemberService( | |
| 55 | JwtService jwtService, | |
| 56 | ObjectMapper objectMapper, | |
| 57 | RestTemplateBuilder builder, | |
| 58 | RosterStudentRepository rosterStudentRepository) { | |
| 59 | this.jwtService = jwtService; | |
| 60 | this.objectMapper = objectMapper; | |
| 61 | this.rosterStudentRepository = rosterStudentRepository; | |
| 62 | this.objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | |
| 63 | this.restTemplate = builder.connectTimeout(CONNECT_TIMEOUT).readTimeout(READ_TIMEOUT).build(); | |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * This endpoint returns the list of **members**, not admins for the organization. This is so that | |
| 68 | * the roles are known for the return values. | |
| 69 | */ | |
| 70 | public Iterable<OrgMember> getOrganizationMembers(Course course) | |
| 71 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 72 | String ENDPOINT = "https://api.github.com/orgs/" + course.getOrgName() + "/members?role=member"; | |
| 73 |
1
1. getOrganizationMembers : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/services/OrganizationMemberService::getOrganizationMembers → KILLED |
return getOrganizationMembersWithRole(course, ENDPOINT); |
| 74 | } | |
| 75 | ||
| 76 | /** | |
| 77 | * This endpoint returns the list of **admins** for the organization. This is so that the roles | |
| 78 | * are known for the return values. | |
| 79 | */ | |
| 80 | public Iterable<OrgMember> getOrganizationAdmins(Course course) | |
| 81 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 82 | String ENDPOINT = "https://api.github.com/orgs/" + course.getOrgName() + "/members?role=admin"; | |
| 83 |
1
1. getOrganizationAdmins : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/services/OrganizationMemberService::getOrganizationAdmins → KILLED |
return getOrganizationMembersWithRole(course, ENDPOINT); |
| 84 | } | |
| 85 | ||
| 86 | /** | |
| 87 | * This endpoint returns the list of users who have been **invited** to the organization but have | |
| 88 | * not yet accepted. | |
| 89 | */ | |
| 90 | public Iterable<OrgMember> getOrganizationInvitees(Course course) | |
| 91 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 92 | String ENDPOINT = "https://api.github.com/orgs/" + course.getOrgName() + "/invitations"; | |
| 93 |
1
1. getOrganizationInvitees : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/services/OrganizationMemberService::getOrganizationInvitees → KILLED |
return getOrganizationMembersWithRole(course, ENDPOINT); |
| 94 | } | |
| 95 | ||
| 96 | private Iterable<OrgMember> getOrganizationMembersWithRole(Course course, String ENDPOINT) | |
| 97 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 98 | // happily stolen directly from GitHub: | |
| 99 | // https://docs.github.com/en/rest/using-the-rest-api/using-pagination-in-the-rest-api?apiVersion=2022-11-28 | |
| 100 | Pattern pattern = Pattern.compile("(?<=<)([\\S]*)(?=>; rel=\"next\")"); | |
| 101 | String token = jwtService.getInstallationToken(course); | |
| 102 | HttpHeaders headers = new HttpHeaders(); | |
| 103 |
1
1. getOrganizationMembersWithRole : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 104 |
1
1. getOrganizationMembersWithRole : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 105 |
1
1. getOrganizationMembersWithRole : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 106 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 107 | ResponseEntity<String> response = | |
| 108 | restTemplate.exchange(ENDPOINT, HttpMethod.GET, entity, String.class); | |
| 109 | List<String> responseLinks = response.getHeaders().getOrEmpty("link"); | |
| 110 | List<OrgMember> orgMembers = new ArrayList<>(); | |
| 111 |
2
1. getOrganizationMembersWithRole : negated conditional → KILLED 2. getOrganizationMembersWithRole : negated conditional → KILLED |
while (!responseLinks.isEmpty() && responseLinks.getFirst().contains("next")) { |
| 112 | orgMembers.addAll( | |
| 113 | objectMapper.convertValue( | |
| 114 | objectMapper.readTree(response.getBody()), new TypeReference<List<OrgMember>>() {})); | |
| 115 | Matcher matcher = pattern.matcher(responseLinks.getFirst()); | |
| 116 | matcher.find(); | |
| 117 | response = restTemplate.exchange(matcher.group(0), HttpMethod.GET, entity, String.class); | |
| 118 | responseLinks = response.getHeaders().getOrEmpty("link"); | |
| 119 | } | |
| 120 | orgMembers.addAll( | |
| 121 | objectMapper.convertValue( | |
| 122 | objectMapper.readTree(response.getBody()), new TypeReference<List<OrgMember>>() {})); | |
| 123 |
1
1. getOrganizationMembersWithRole : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/services/OrganizationMemberService::getOrganizationMembersWithRole → KILLED |
return orgMembers; |
| 124 | } | |
| 125 | ||
| 126 | public OrgStatus inviteOrganizationMember(RosterStudent student) | |
| 127 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 128 | Course course = student.getCourse(); | |
| 129 |
1
1. inviteOrganizationMember : replaced return value with null for edu/ucsb/cs156/frontiers/services/OrganizationMemberService::inviteOrganizationMember → KILLED |
return inviteMember(student.getGithubId(), course, "direct_member", student.getGithubLogin()); |
| 130 | } | |
| 131 | ||
| 132 | public OrgStatus inviteOrganizationOwner(CourseStaff staff) | |
| 133 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 134 | Course course = staff.getCourse(); | |
| 135 |
1
1. inviteOrganizationOwner : replaced return value with null for edu/ucsb/cs156/frontiers/services/OrganizationMemberService::inviteOrganizationOwner → KILLED |
return inviteMember(staff.getGithubId(), course, "admin", staff.getGithubLogin()); |
| 136 | } | |
| 137 | ||
| 138 | private OrgStatus inviteMember(int githubId, Course course, String role, String githubLogin) | |
| 139 | throws JsonProcessingException, NoSuchAlgorithmException, InvalidKeySpecException { | |
| 140 | String ENDPOINT = "https://api.github.com/orgs/" + course.getOrgName() + "/invitations"; | |
| 141 | HttpHeaders headers = new HttpHeaders(); | |
| 142 | String token = jwtService.getInstallationToken(course); | |
| 143 |
1
1. inviteMember : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 144 |
1
1. inviteMember : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 145 |
1
1. inviteMember : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 146 | Map<String, Object> body = new HashMap<>(); | |
| 147 | body.put("invitee_id", githubId); | |
| 148 | body.put("role", role); | |
| 149 | String bodyAsJson = objectMapper.writeValueAsString(body); | |
| 150 | HttpEntity<String> entity = new HttpEntity<>(bodyAsJson, headers); | |
| 151 | try { | |
| 152 | restTemplate.exchange(ENDPOINT, HttpMethod.POST, entity, String.class); | |
| 153 | } catch (HttpClientErrorException e) { | |
| 154 |
1
1. inviteMember : replaced return value with null for edu/ucsb/cs156/frontiers/services/OrganizationMemberService::inviteMember → KILLED |
return getMemberStatus(githubLogin, course); |
| 155 | } | |
| 156 |
1
1. inviteMember : replaced return value with null for edu/ucsb/cs156/frontiers/services/OrganizationMemberService::inviteMember → KILLED |
return OrgStatus.INVITED; |
| 157 | } | |
| 158 | ||
| 159 | private OrgStatus getMemberStatus(String githubLogin, Course course) | |
| 160 | throws JsonProcessingException, NoSuchAlgorithmException, InvalidKeySpecException { | |
| 161 | String ENDPOINT = | |
| 162 | "https://api.github.com/orgs/" + course.getOrgName() + "/memberships/" + githubLogin; | |
| 163 | HttpHeaders headers = new HttpHeaders(); | |
| 164 | String token = jwtService.getInstallationToken(course); | |
| 165 |
1
1. getMemberStatus : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 166 |
1
1. getMemberStatus : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 167 |
1
1. getMemberStatus : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 168 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 169 | try { | |
| 170 | ResponseEntity<String> response = | |
| 171 | restTemplate.exchange(ENDPOINT, HttpMethod.GET, entity, String.class); | |
| 172 | JsonNode responseJson = objectMapper.readTree(response.getBody()); | |
| 173 |
1
1. getMemberStatus : negated conditional → KILLED |
if (responseJson.get("role").asText().equalsIgnoreCase("admin")) { |
| 174 |
1
1. getMemberStatus : replaced return value with null for edu/ucsb/cs156/frontiers/services/OrganizationMemberService::getMemberStatus → KILLED |
return OrgStatus.OWNER; |
| 175 |
1
1. getMemberStatus : negated conditional → KILLED |
} else if (responseJson.get("role").asText().equalsIgnoreCase("member")) { |
| 176 |
1
1. getMemberStatus : replaced return value with null for edu/ucsb/cs156/frontiers/services/OrganizationMemberService::getMemberStatus → KILLED |
return OrgStatus.MEMBER; |
| 177 | } else { | |
| 178 | log.warn( | |
| 179 | "Unexpected role {} used in course {}", | |
| 180 | responseJson.get("role").asText(), | |
| 181 | course.getCourseName()); | |
| 182 |
1
1. getMemberStatus : replaced return value with null for edu/ucsb/cs156/frontiers/services/OrganizationMemberService::getMemberStatus → KILLED |
return OrgStatus.JOINCOURSE; |
| 183 | } | |
| 184 | } catch (HttpClientErrorException e) { | |
| 185 | log.warn("Error while trying to get member status: {}", e.getMessage()); | |
| 186 |
1
1. getMemberStatus : replaced return value with null for edu/ucsb/cs156/frontiers/services/OrganizationMemberService::getMemberStatus → KILLED |
return OrgStatus.JOINCOURSE; |
| 187 | } | |
| 188 | } | |
| 189 | ||
| 190 | /** | |
| 191 | * Removes a member from an organization. | |
| 192 | * | |
| 193 | * @param student The roster student to remove from the organization | |
| 194 | * @throws NoSuchAlgorithmException if there is an algorithm error | |
| 195 | * @throws InvalidKeySpecException if there is a key specification error | |
| 196 | * @throws JsonProcessingException if there is an error processing JSON | |
| 197 | * @throws IllegalArgumentException if student has no GitHub login or course has no linked | |
| 198 | * organization | |
| 199 | * @throws Exception if there is an error removing the student from the organization | |
| 200 | */ | |
| 201 | public void removeOrganizationMember(RosterStudent student) | |
| 202 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 203 |
1
1. removeOrganizationMember : negated conditional → KILLED |
if (student.getGithubLogin() == null) { |
| 204 | throw new IllegalArgumentException( | |
| 205 | "Cannot remove student from organization: GitHub login is null"); | |
| 206 | } | |
| 207 | ||
| 208 | Course course = student.getCourse(); | |
| 209 |
2
1. removeOrganizationMember : negated conditional → KILLED 2. removeOrganizationMember : negated conditional → KILLED |
if (course.getOrgName() == null || course.getInstallationId() == null) { |
| 210 | throw new IllegalArgumentException( | |
| 211 | "Cannot remove student from organization: Course has no linked organization"); | |
| 212 | } | |
| 213 |
1
1. removeOrganizationMember : removed call to edu/ucsb/cs156/frontiers/services/OrganizationMemberService::removeOrganizationMember → KILLED |
removeOrganizationMember( |
| 214 | course.getOrgName(), student.getGithubLogin(), jwtService.getInstallationToken(course)); | |
| 215 | } | |
| 216 | ||
| 217 | /** | |
| 218 | * Removes a member from an organization. | |
| 219 | * | |
| 220 | * @param staffMember The staff member to remove from the organization | |
| 221 | * @throws NoSuchAlgorithmException if there is an algorithm error | |
| 222 | * @throws InvalidKeySpecException if there is a key specification error | |
| 223 | * @throws JsonProcessingException if there is an error processing JSON | |
| 224 | * @throws IllegalArgumentException if student has no GitHub login or course has no linked | |
| 225 | * organization | |
| 226 | * @throws Exception if there is an error removing the student from the organization | |
| 227 | */ | |
| 228 | public void removeOrganizationMember(CourseStaff staffMember) | |
| 229 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 230 |
1
1. removeOrganizationMember : negated conditional → KILLED |
if (staffMember.getGithubLogin() == null) { |
| 231 | throw new IllegalArgumentException( | |
| 232 | "Cannot remove staff member from organization: GitHub login is null"); | |
| 233 | } | |
| 234 | ||
| 235 | Course course = staffMember.getCourse(); | |
| 236 |
2
1. removeOrganizationMember : negated conditional → KILLED 2. removeOrganizationMember : negated conditional → KILLED |
if (course.getOrgName() == null || course.getInstallationId() == null) { |
| 237 | throw new IllegalArgumentException( | |
| 238 | "Cannot remove staff member from organization: Course has no linked organization"); | |
| 239 | } | |
| 240 |
1
1. removeOrganizationMember : removed call to edu/ucsb/cs156/frontiers/services/OrganizationMemberService::removeOrganizationMember → KILLED |
removeOrganizationMember( |
| 241 | course.getOrgName(), staffMember.getGithubLogin(), jwtService.getInstallationToken(course)); | |
| 242 | } | |
| 243 | ||
| 244 | /** | |
| 245 | * Remove member from organization | |
| 246 | * | |
| 247 | * @param orgName | |
| 248 | * @param githubLogin | |
| 249 | * @param token | |
| 250 | * @throws NoSuchAlgorithmException | |
| 251 | * @throws InvalidKeySpecException | |
| 252 | * @throws JsonProcessingException | |
| 253 | */ | |
| 254 | public void removeOrganizationMember(String orgName, String githubLogin, String token) | |
| 255 | throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException { | |
| 256 | ||
| 257 | String ENDPOINT = "https://api.github.com/orgs/" + orgName + "/members/" + githubLogin; | |
| 258 | HttpHeaders headers = new HttpHeaders(); | |
| 259 | ||
| 260 |
1
1. removeOrganizationMember : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Authorization", "Bearer " + token); |
| 261 |
1
1. removeOrganizationMember : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("Accept", "application/vnd.github+json"); |
| 262 |
1
1. removeOrganizationMember : removed call to org/springframework/http/HttpHeaders::add → KILLED |
headers.add("X-GitHub-Api-Version", "2022-11-28"); |
| 263 | HttpEntity<String> entity = new HttpEntity<>(headers); | |
| 264 | ||
| 265 | restTemplate.exchange(ENDPOINT, HttpMethod.DELETE, entity, String.class); | |
| 266 | log.info("Successfully removed student {} from organization {}", githubLogin, orgName); | |
| 267 | } | |
| 268 | } | |
Mutations | ||
| 73 |
1.1 |
|
| 83 |
1.1 |
|
| 93 |
1.1 |
|
| 103 |
1.1 |
|
| 104 |
1.1 |
|
| 105 |
1.1 |
|
| 111 |
1.1 2.2 |
|
| 123 |
1.1 |
|
| 129 |
1.1 |
|
| 135 |
1.1 |
|
| 143 |
1.1 |
|
| 144 |
1.1 |
|
| 145 |
1.1 |
|
| 154 |
1.1 |
|
| 156 |
1.1 |
|
| 165 |
1.1 |
|
| 166 |
1.1 |
|
| 167 |
1.1 |
|
| 173 |
1.1 |
|
| 174 |
1.1 |
|
| 175 |
1.1 |
|
| 176 |
1.1 |
|
| 182 |
1.1 |
|
| 186 |
1.1 |
|
| 203 |
1.1 |
|
| 209 |
1.1 2.2 |
|
| 213 |
1.1 |
|
| 230 |
1.1 |
|
| 236 |
1.1 2.2 |
|
| 240 |
1.1 |
|
| 260 |
1.1 |
|
| 261 |
1.1 |
|
| 262 |
1.1 |