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