| 1 | package edu.ucsb.cs156.frontiers.jobs; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.frontiers.entities.Course; | |
| 4 | import edu.ucsb.cs156.frontiers.entities.CourseStaff; | |
| 5 | import edu.ucsb.cs156.frontiers.entities.RosterStudent; | |
| 6 | import edu.ucsb.cs156.frontiers.entities.Team; | |
| 7 | import edu.ucsb.cs156.frontiers.entities.TeamMember; | |
| 8 | import edu.ucsb.cs156.frontiers.enums.TeamStatus; | |
| 9 | import edu.ucsb.cs156.frontiers.repositories.CourseRepository; | |
| 10 | import edu.ucsb.cs156.frontiers.repositories.TeamMemberRepository; | |
| 11 | import edu.ucsb.cs156.frontiers.repositories.TeamRepository; | |
| 12 | import edu.ucsb.cs156.frontiers.services.GithubTeamService; | |
| 13 | import edu.ucsb.cs156.frontiers.services.GithubTeamService.GithubTeamInfo; | |
| 14 | import edu.ucsb.cs156.jobs.services.JobContext; | |
| 15 | import edu.ucsb.cs156.jobs.services.JobContextConsumer; | |
| 16 | import java.util.HashMap; | |
| 17 | import java.util.List; | |
| 18 | import java.util.Map; | |
| 19 | import java.util.Optional; | |
| 20 | import java.util.concurrent.atomic.AtomicBoolean; | |
| 21 | import lombok.Builder; | |
| 22 | ||
| 23 | @Builder | |
| 24 | public class PullTeamsFromGithubJob implements JobContextConsumer { | |
| 25 | Long courseId; | |
| 26 | CourseRepository courseRepository; | |
| 27 | TeamRepository teamRepository; | |
| 28 | TeamMemberRepository teamMemberRepository; | |
| 29 | GithubTeamService githubTeamService; | |
| 30 | ||
| 31 | @Override | |
| 32 | public String getScopeType() { | |
| 33 |
1
1. getScopeType : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/PullTeamsFromGithubJob::getScopeType → KILLED |
return "course"; |
| 34 | } | |
| 35 | ||
| 36 | @Override | |
| 37 | public Long getScopeId() { | |
| 38 |
1
1. getScopeId : replaced Long return value with 0L for edu/ucsb/cs156/frontiers/jobs/PullTeamsFromGithubJob::getScopeId → KILLED |
return courseId; |
| 39 | } | |
| 40 | ||
| 41 | @Override | |
| 42 | public void accept(JobContext ctx) throws Exception { | |
| 43 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log(String.format("Starting pull teams from GitHub job for course ID: %s", courseId)); |
| 44 | ||
| 45 | Optional<Course> courseOpt = courseRepository.findById(courseId); | |
| 46 |
1
1. accept : negated conditional → KILLED |
if (courseOpt.isEmpty()) { |
| 47 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log(String.format("ERROR: Course with ID %s not found", courseId)); |
| 48 | return; | |
| 49 | } | |
| 50 | Course course = courseOpt.get(); | |
| 51 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 52 | String.format( | |
| 53 | "Processing course: %s (org: %s)", course.getCourseName(), course.getOrgName())); | |
| 54 | ||
| 55 |
2
1. accept : negated conditional → KILLED 2. accept : negated conditional → KILLED |
if (course.getOrgName() == null || course.getInstallationId() == null) { |
| 56 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("ERROR: Course has no linked GitHub organization"); |
| 57 | return; | |
| 58 | } | |
| 59 | ||
| 60 | List<GithubTeamInfo> githubTeams; | |
| 61 | try { | |
| 62 | githubTeams = githubTeamService.getAllTeams(course); | |
| 63 | } catch (Exception e) { | |
| 64 | ctx.log(String.format("ERROR: Failed to pull teams from GitHub: %s", e.getMessage())); | |
| 65 | return; | |
| 66 | } | |
| 67 | ||
| 68 | Map<Integer, Team> localByGithubId = new HashMap<>(); | |
| 69 | Map<String, Team> localByName = new HashMap<>(); | |
| 70 | Map<String, RosterStudent> localStudentsByGithubLogin = new HashMap<>(); | |
| 71 | for (Team localTeam : teamRepository.findByCourseId(courseId)) { | |
| 72 |
1
1. accept : negated conditional → KILLED |
if (localTeam.getGithubTeamId() != null) { |
| 73 | localByGithubId.put(localTeam.getGithubTeamId(), localTeam); | |
| 74 | } | |
| 75 | localByName.put(localTeam.getName(), localTeam); | |
| 76 | } | |
| 77 |
1
1. accept : negated conditional → KILLED |
if (course.getRosterStudents() != null) { |
| 78 | for (RosterStudent student : course.getRosterStudents()) { | |
| 79 |
1
1. accept : negated conditional → KILLED |
if (student.getGithubLogin() != null) { |
| 80 | localStudentsByGithubLogin.put(student.getGithubLogin(), student); | |
| 81 | } | |
| 82 | } | |
| 83 | } | |
| 84 | Map<String, CourseStaff> staffByGithubLogin = new HashMap<>(); | |
| 85 |
1
1. accept : negated conditional → KILLED |
if (course.getCourseStaff() != null) { |
| 86 | for (CourseStaff staff : course.getCourseStaff()) { | |
| 87 |
1
1. accept : negated conditional → KILLED |
if (staff.getGithubLogin() != null) { |
| 88 | staffByGithubLogin.put(staff.getGithubLogin(), staff); | |
| 89 | } | |
| 90 | } | |
| 91 | } | |
| 92 | ||
| 93 | int created = 0; | |
| 94 | int updated = 0; | |
| 95 | int unchanged = 0; | |
| 96 | ||
| 97 | for (GithubTeamInfo githubTeam : githubTeams) { | |
| 98 | // The common case on a re-sync is a team that's already up to date on every field and | |
| 99 | // has no membership changes either -- that iteration never calls ctx.log() at all. | |
| 100 | // checkCancellation() gives this loop its own checkpoint independent of how many teams | |
| 101 | // in a row turn out to be unchanged (this is the same failure mode that drove lib-jobs | |
| 102 | // v0.3.2: see SyncCourseWithPlRepoJob in proj-scaffold). | |
| 103 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::checkCancellation → KILLED |
ctx.checkCancellation(); |
| 104 | Team localTeam = localByGithubId.get(githubTeam.id()); | |
| 105 |
1
1. accept : negated conditional → KILLED |
if (localTeam == null) { |
| 106 | localTeam = localByName.get(githubTeam.name()); | |
| 107 | } | |
| 108 | ||
| 109 | // The members are needed before the team is created, to tell a staff team (every member | |
| 110 | // is on the course staff) from a regular one. Without any students or staff with GitHub | |
| 111 | // logins, nobody could be matched, so the call is skipped. | |
| 112 | Map<String, TeamStatus> githubMemberships = Map.of(); | |
| 113 |
2
1. accept : negated conditional → KILLED 2. accept : negated conditional → KILLED |
if (!localStudentsByGithubLogin.isEmpty() || !staffByGithubLogin.isEmpty()) { |
| 114 | githubMemberships = githubTeamService.getTeamMemberships(githubTeam.slug(), course); | |
| 115 | } | |
| 116 | boolean staffOnly = | |
| 117 |
1
1. accept : negated conditional → KILLED |
!githubMemberships.isEmpty() |
| 118 |
1
1. accept : negated conditional → KILLED |
&& staffByGithubLogin.keySet().containsAll(githubMemberships.keySet()); |
| 119 | ||
| 120 | boolean teamCreated = false; | |
| 121 |
1
1. accept : negated conditional → KILLED |
if (localTeam == null) { |
| 122 | Team newTeam = | |
| 123 | Team.builder() | |
| 124 | .name(githubTeam.name()) | |
| 125 | .course(course) | |
| 126 | .githubTeamId(githubTeam.id()) | |
| 127 | .githubTeamSlug(githubTeam.slug()) | |
| 128 | .isStaffTeam(staffOnly) | |
| 129 | .build(); | |
| 130 | teamRepository.save(newTeam); | |
| 131 | localByGithubId.put(githubTeam.id(), newTeam); | |
| 132 | localByName.put(githubTeam.name(), newTeam); | |
| 133 |
1
1. accept : Changed increment from 1 to -1 → KILLED |
created++; |
| 134 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 135 | String.format( | |
| 136 | "Created local team '%s' with GitHub team ID: %d%s", | |
| 137 | githubTeam.name(), | |
| 138 | githubTeam.id(), | |
| 139 |
1
1. accept : negated conditional → KILLED |
staffOnly ? " as a staff team (every member is on the course staff)" : "")); |
| 140 | localTeam = newTeam; | |
| 141 | teamCreated = true; | |
| 142 | } | |
| 143 | ||
| 144 | AtomicBoolean teamUnchanged = new AtomicBoolean(true); | |
| 145 |
1
1. accept : negated conditional → KILLED |
if (!teamCreated) { |
| 146 |
1
1. accept : negated conditional → KILLED |
if (!githubTeam.name().equals(localTeam.getName())) { |
| 147 | localByName.remove(localTeam.getName()); | |
| 148 |
1
1. accept : removed call to edu/ucsb/cs156/frontiers/entities/Team::setName → KILLED |
localTeam.setName(githubTeam.name()); |
| 149 |
1
1. accept : removed call to java/util/concurrent/atomic/AtomicBoolean::set → KILLED |
teamUnchanged.set(false); |
| 150 | } | |
| 151 |
1
1. accept : negated conditional → KILLED |
if (!githubTeam.id().equals(localTeam.getGithubTeamId())) { |
| 152 |
1
1. accept : negated conditional → KILLED |
if (localTeam.getGithubTeamId() != null) { |
| 153 | localByGithubId.remove(localTeam.getGithubTeamId()); | |
| 154 | } | |
| 155 |
1
1. accept : removed call to edu/ucsb/cs156/frontiers/entities/Team::setGithubTeamId → KILLED |
localTeam.setGithubTeamId(githubTeam.id()); |
| 156 |
1
1. accept : removed call to java/util/concurrent/atomic/AtomicBoolean::set → KILLED |
teamUnchanged.set(false); |
| 157 | } | |
| 158 |
1
1. accept : negated conditional → KILLED |
if (localTeam.getGithubTeamSlug() == null |
| 159 |
1
1. accept : negated conditional → KILLED |
|| !githubTeam.slug().equals(localTeam.getGithubTeamSlug())) { |
| 160 |
1
1. accept : removed call to edu/ucsb/cs156/frontiers/entities/Team::setGithubTeamSlug → KILLED |
localTeam.setGithubTeamSlug(githubTeam.slug()); |
| 161 |
1
1. accept : removed call to java/util/concurrent/atomic/AtomicBoolean::set → KILLED |
teamUnchanged.set(false); |
| 162 | } | |
| 163 | // A regular team with no members whose GitHub team has only staff on it becomes a staff | |
| 164 | // team. One that already has student members is left alone, since those members would | |
| 165 | // otherwise be hidden; the staff on its GitHub team are noted below instead. | |
| 166 |
1
1. accept : negated conditional → KILLED |
if (staffOnly |
| 167 |
1
1. accept : negated conditional → KILLED |
&& !Boolean.TRUE.equals(localTeam.getIsStaffTeam()) |
| 168 |
2
1. accept : negated conditional → KILLED 2. accept : negated conditional → KILLED |
&& (localTeam.getTeamMembers() == null || localTeam.getTeamMembers().isEmpty())) { |
| 169 |
1
1. accept : removed call to edu/ucsb/cs156/frontiers/entities/Team::setIsStaffTeam → KILLED |
localTeam.setIsStaffTeam(true); |
| 170 |
1
1. accept : removed call to java/util/concurrent/atomic/AtomicBoolean::set → KILLED |
teamUnchanged.set(false); |
| 171 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 172 | String.format( | |
| 173 | "Local team '%s' is now a staff team (every member of its GitHub team is on the course staff)", | |
| 174 | localTeam.getName())); | |
| 175 | } | |
| 176 | ||
| 177 |
1
1. accept : negated conditional → KILLED |
if (!teamUnchanged.get()) { |
| 178 | teamRepository.save(localTeam); | |
| 179 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 180 | String.format( | |
| 181 | "Updated local team '%s' with GitHub team ID: %d", | |
| 182 | localTeam.getName(), githubTeam.id())); | |
| 183 | } | |
| 184 | } | |
| 185 | ||
| 186 | localByGithubId.put(githubTeam.id(), localTeam); | |
| 187 | localByName.put(localTeam.getName(), localTeam); | |
| 188 | ||
| 189 | Team currentTeam = localTeam; | |
| 190 | boolean isStaffTeam = Boolean.TRUE.equals(currentTeam.getIsStaffTeam()); | |
| 191 |
1
1. accept : removed call to java/util/Map::forEach → KILLED |
githubMemberships.forEach( |
| 192 | (githubLogin, membershipStatus) -> { | |
| 193 | // Same reasoning as the outer team loop: a member with no status change never | |
| 194 | // logs. checkCancellation() throws JobCancelledException, which is unchecked -- | |
| 195 | // required here since BiConsumer's functional interface can't declare checked | |
| 196 | // exceptions. | |
| 197 |
1
1. lambda$accept$0 : removed call to edu/ucsb/cs156/jobs/services/JobContext::checkCancellation → KILLED |
ctx.checkCancellation(); |
| 198 |
1
1. lambda$accept$0 : negated conditional → KILLED |
if (isStaffTeam) { |
| 199 | // The members of a staff team are the course staff; nothing is stored per member. | |
| 200 |
1
1. lambda$accept$0 : negated conditional → KILLED |
if (!staffByGithubLogin.containsKey(githubLogin)) { |
| 201 |
1
1. lambda$accept$0 : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 202 | String.format( | |
| 203 | "Note: '%s' is on the GitHub team of staff team '%s' but is not on the course staff", | |
| 204 | githubLogin, currentTeam.getName())); | |
| 205 | } | |
| 206 | return; | |
| 207 | } | |
| 208 | RosterStudent student = localStudentsByGithubLogin.get(githubLogin); | |
| 209 |
1
1. lambda$accept$0 : negated conditional → KILLED |
if (student == null) { |
| 210 | // Only students become members of a regular team; anyone else is noted here. | |
| 211 |
1
1. lambda$accept$0 : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 212 | String.format( | |
| 213 | "Note: '%s' is on GitHub team '%s' but is %s, so was not added to the local team", | |
| 214 | githubLogin, | |
| 215 | currentTeam.getName(), | |
| 216 |
1
1. lambda$accept$0 : negated conditional → KILLED |
staffByGithubLogin.containsKey(githubLogin) |
| 217 | ? "on the course staff, not a student" | |
| 218 | : "not a student of this course")); | |
| 219 | return; | |
| 220 | } | |
| 221 | ||
| 222 | Optional<TeamMember> existingTeamMember = | |
| 223 | teamMemberRepository.findByTeamAndRosterStudent(currentTeam, student); | |
| 224 |
1
1. lambda$accept$0 : negated conditional → KILLED |
if (existingTeamMember.isPresent()) { |
| 225 | TeamMember teamMember = existingTeamMember.get(); | |
| 226 |
1
1. lambda$accept$0 : negated conditional → KILLED |
if (teamMember.getTeamStatus() != membershipStatus) { |
| 227 |
1
1. lambda$accept$0 : removed call to edu/ucsb/cs156/frontiers/entities/TeamMember::setTeamStatus → KILLED |
teamMember.setTeamStatus(membershipStatus); |
| 228 | teamMemberRepository.save(teamMember); | |
| 229 |
1
1. lambda$accept$0 : removed call to java/util/concurrent/atomic/AtomicBoolean::set → KILLED |
teamUnchanged.set(false); |
| 230 |
1
1. lambda$accept$0 : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 231 | String.format( | |
| 232 | "Updated team member '%s' in team '%s' with status %s", | |
| 233 | githubLogin, currentTeam.getName(), membershipStatus)); | |
| 234 | } | |
| 235 | } else { | |
| 236 | TeamMember newTeamMember = | |
| 237 | TeamMember.builder() | |
| 238 | .team(currentTeam) | |
| 239 | .rosterStudent(student) | |
| 240 | .teamStatus(membershipStatus) | |
| 241 | .build(); | |
| 242 | teamMemberRepository.save(newTeamMember); | |
| 243 |
1
1. lambda$accept$0 : removed call to java/util/concurrent/atomic/AtomicBoolean::set → KILLED |
teamUnchanged.set(false); |
| 244 |
1
1. lambda$accept$0 : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 245 | String.format( | |
| 246 | "Created team member '%s' in team '%s' with status %s", | |
| 247 | githubLogin, currentTeam.getName(), membershipStatus)); | |
| 248 | } | |
| 249 | }); | |
| 250 | ||
| 251 |
1
1. accept : negated conditional → KILLED |
if (!teamCreated) { |
| 252 |
1
1. accept : negated conditional → KILLED |
if (teamUnchanged.get()) { |
| 253 |
1
1. accept : Changed increment from 1 to -1 → KILLED |
unchanged++; |
| 254 | } else { | |
| 255 |
1
1. accept : Changed increment from 1 to -1 → KILLED |
updated++; |
| 256 | } | |
| 257 | } | |
| 258 | } | |
| 259 | ||
| 260 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 261 | String.format( | |
| 262 | "Completed pull teams from GitHub job for course ID: %s (GitHub teams: %d, created: %d, updated: %d, unchanged: %d)", | |
| 263 | courseId, githubTeams.size(), created, updated, unchanged)); | |
| 264 | } | |
| 265 | } | |
Mutations | ||
| 33 |
1.1 |
|
| 38 |
1.1 |
|
| 43 |
1.1 |
|
| 46 |
1.1 |
|
| 47 |
1.1 |
|
| 51 |
1.1 |
|
| 55 |
1.1 2.2 |
|
| 56 |
1.1 |
|
| 72 |
1.1 |
|
| 77 |
1.1 |
|
| 79 |
1.1 |
|
| 85 |
1.1 |
|
| 87 |
1.1 |
|
| 103 |
1.1 |
|
| 105 |
1.1 |
|
| 113 |
1.1 2.2 |
|
| 117 |
1.1 |
|
| 118 |
1.1 |
|
| 121 |
1.1 |
|
| 133 |
1.1 |
|
| 134 |
1.1 |
|
| 139 |
1.1 |
|
| 145 |
1.1 |
|
| 146 |
1.1 |
|
| 148 |
1.1 |
|
| 149 |
1.1 |
|
| 151 |
1.1 |
|
| 152 |
1.1 |
|
| 155 |
1.1 |
|
| 156 |
1.1 |
|
| 158 |
1.1 |
|
| 159 |
1.1 |
|
| 160 |
1.1 |
|
| 161 |
1.1 |
|
| 166 |
1.1 |
|
| 167 |
1.1 |
|
| 168 |
1.1 2.2 |
|
| 169 |
1.1 |
|
| 170 |
1.1 |
|
| 171 |
1.1 |
|
| 177 |
1.1 |
|
| 179 |
1.1 |
|
| 191 |
1.1 |
|
| 197 |
1.1 |
|
| 198 |
1.1 |
|
| 200 |
1.1 |
|
| 201 |
1.1 |
|
| 209 |
1.1 |
|
| 211 |
1.1 |
|
| 216 |
1.1 |
|
| 224 |
1.1 |
|
| 226 |
1.1 |
|
| 227 |
1.1 |
|
| 229 |
1.1 |
|
| 230 |
1.1 |
|
| 243 |
1.1 |
|
| 244 |
1.1 |
|
| 251 |
1.1 |
|
| 252 |
1.1 |
|
| 253 |
1.1 |
|
| 255 |
1.1 |
|
| 260 |
1.1 |