| 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.Section; | |
| 7 | import edu.ucsb.cs156.frontiers.enums.RosterStatus; | |
| 8 | import edu.ucsb.cs156.frontiers.errors.SlackApiException; | |
| 9 | import edu.ucsb.cs156.frontiers.models.SlackChannel; | |
| 10 | import edu.ucsb.cs156.frontiers.models.SlackUser; | |
| 11 | import edu.ucsb.cs156.frontiers.repositories.CourseRepository; | |
| 12 | import edu.ucsb.cs156.frontiers.repositories.CourseStaffRepository; | |
| 13 | import edu.ucsb.cs156.frontiers.repositories.RosterStudentRepository; | |
| 14 | import edu.ucsb.cs156.frontiers.repositories.SectionRepository; | |
| 15 | import edu.ucsb.cs156.frontiers.services.CanvasApiTokenSecurityService; | |
| 16 | import edu.ucsb.cs156.frontiers.services.SlackService; | |
| 17 | import edu.ucsb.cs156.frontiers.utilities.CanonicalFormConverter; | |
| 18 | import edu.ucsb.cs156.jobs.services.JobContext; | |
| 19 | import edu.ucsb.cs156.jobs.services.JobContextConsumer; | |
| 20 | import java.util.ArrayList; | |
| 21 | import java.util.HashMap; | |
| 22 | import java.util.HashSet; | |
| 23 | import java.util.LinkedHashMap; | |
| 24 | import java.util.List; | |
| 25 | import java.util.Locale; | |
| 26 | import java.util.Map; | |
| 27 | import java.util.Set; | |
| 28 | import java.util.stream.StreamSupport; | |
| 29 | import lombok.Builder; | |
| 30 | ||
| 31 | /** | |
| 32 | * Makes the public Slack channels of a course's sections match the roster. | |
| 33 | * | |
| 34 | * <ol> | |
| 35 | * <li>For each row of the sections table that has a Slack channel name, creates a public channel | |
| 36 | * with that name, unless it already exists. (Several sections may share a channel.) | |
| 37 | * <li>Adds each roster student (status ROSTER or MANUAL) to the channel of their section, unless | |
| 38 | * they are already in it. Students are matched to Slack users by email; students without an | |
| 39 | * active Slack account cannot be added, and are only counted, by reason (invited but not | |
| 40 | * accepted yet, account deactivated, or not in the workspace). | |
| 41 | * <li>Removes from each of those channels every person who is neither a roster student of one of | |
| 42 | * the channel's sections, nor a member of the course staff, nor the instructor. Bots | |
| 43 | * (including the bot this job acts as) and members that are not users of the workspace are | |
| 44 | * never removed. Whether the bot may remove members from public channels is a workspace | |
| 45 | * setting; if Slack refuses (restricted_action), the log says how to change the setting and | |
| 46 | * no further removals are attempted in that run. | |
| 47 | * </ol> | |
| 48 | * | |
| 49 | * A problem with one channel or one person is logged, and the job carries on with the rest. | |
| 50 | */ | |
| 51 | @Builder | |
| 52 | public class SetupSectionSlackChannelsJob implements JobContextConsumer { | |
| 53 | ||
| 54 | /** Only these roster students are considered: in particular, not dropped students. */ | |
| 55 | public static final List<RosterStatus> ENROLLED_STATUSES = | |
| 56 | List.of(RosterStatus.ROSTER, RosterStatus.MANUAL); | |
| 57 | ||
| 58 | Course course; | |
| 59 | CourseRepository courseRepository; | |
| 60 | SectionRepository sectionRepository; | |
| 61 | RosterStudentRepository rosterStudentRepository; | |
| 62 | CourseStaffRepository courseStaffRepository; | |
| 63 | SlackService slackService; | |
| 64 | CanvasApiTokenSecurityService tokenSecurityService; | |
| 65 | ||
| 66 | @Override | |
| 67 | public String getScopeType() { | |
| 68 |
1
1. getScopeType : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/SetupSectionSlackChannelsJob::getScopeType → KILLED |
return "course"; |
| 69 | } | |
| 70 | ||
| 71 | @Override | |
| 72 | public Long getScopeId() { | |
| 73 |
1
1. getScopeId : replaced Long return value with 0L for edu/ucsb/cs156/frontiers/jobs/SetupSectionSlackChannelsJob::getScopeId → KILLED |
return course.getId(); |
| 74 | } | |
| 75 | ||
| 76 | @Override | |
| 77 | public void accept(JobContext ctx) throws Exception { | |
| 78 | Course currentCourse = courseRepository.findById(course.getId()).orElseThrow(); | |
| 79 | String token = tokenSecurityService.decrypt(currentCourse.getSlackBotToken()); | |
| 80 |
2
1. accept : negated conditional → KILLED 2. accept : negated conditional → KILLED |
if (token == null || token.isEmpty()) { |
| 81 | throw new IllegalStateException( | |
| 82 | "No Slack token has been set for this course; enter one on the Settings tab."); | |
| 83 | } | |
| 84 | ||
| 85 | List<SlackUser> slackUsers = slackService.listUsers(token); | |
| 86 | Map<String, SlackUser> slackUsersById = new HashMap<>(); | |
| 87 | Map<String, String> activeSlackIdByEmail = new HashMap<>(); | |
| 88 | // For people whose account is not active: true if they have been invited but have not accepted | |
| 89 | // yet, false if their account has been deactivated. As on the Slack tab, an invitation wins | |
| 90 | // over a deactivated account with the same email. | |
| 91 | Map<String, Boolean> invitedByEmail = new HashMap<>(); | |
| 92 | for (SlackUser user : slackUsers) { | |
| 93 | slackUsersById.put(user.getId(), user); | |
| 94 |
2
1. accept : negated conditional → KILLED 2. accept : negated conditional → KILLED |
if (user.isActivePerson() && user.email() != null) { |
| 95 | activeSlackIdByEmail.put(canonical(user.email()), user.getId()); | |
| 96 |
2
1. accept : negated conditional → KILLED 2. accept : negated conditional → KILLED |
} else if (user.isPerson() && user.email() != null) { |
| 97 |
1
1. accept : negated conditional → KILLED |
invitedByEmail.merge(canonical(user.email()), !user.getDeleted(), Boolean::logicalOr); |
| 98 | } | |
| 99 | } | |
| 100 |
1
1. accept : negated conditional → KILLED |
if (activeSlackIdByEmail.isEmpty()) { |
| 101 | // Without emails nobody can be matched, and everybody would be removed from the channels. | |
| 102 | throw new IllegalStateException( | |
| 103 | "Slack did not provide the email of any user. Add the users:read.email scope to the Slack app, reinstall it to the workspace, and save the new token on the Settings tab."); | |
| 104 | } | |
| 105 | ||
| 106 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Creating Section Channels"); |
| 107 | Map<String, Set<String>> sectionsByChannelName = sectionsByChannelName(currentCourse.getId()); | |
| 108 | Map<String, List<String>> channelNamesBySection = channelNamesBySection(sectionsByChannelName); | |
| 109 | Map<String, String> channelIdByName = | |
| 110 | createChannels(ctx, token, sectionsByChannelName.keySet()); | |
| 111 | ||
| 112 | List<RosterStudent> students = enrolledStudents(currentCourse.getId()); | |
| 113 | Set<String> staffSlackIds = staffSlackIds(currentCourse, activeSlackIdByEmail); | |
| 114 | ||
| 115 | // Members of each channel before this job changes anything; null if they could not be listed | |
| 116 | Map<String, List<String>> membersByChannelName = new HashMap<>(); | |
| 117 | // Slack ids of the students that belong in each channel | |
| 118 | Map<String, Set<String>> studentSlackIdsByChannelName = new HashMap<>(); | |
| 119 | ||
| 120 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Adding Students to Channel"); |
| 121 | int studentsInvited = 0; | |
| 122 | int studentsDeactivated = 0; | |
| 123 | int studentsNotInWorkspace = 0; | |
| 124 | for (RosterStudent student : students) { | |
| 125 | List<String> matchingChannels = | |
| 126 | channelNamesBySection.getOrDefault(student.getSection(), List.of()); | |
| 127 |
1
1. accept : negated conditional → KILLED |
if (matchingChannels.isEmpty()) { |
| 128 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 129 | "Student %s is in untranslated roster section %s" | |
| 130 | .formatted(describe(student), describeSection(student.getSection()))); | |
| 131 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 132 | "Could not add %s to a section Slack channel because no configured channel matches untranslated roster section %s." | |
| 133 | .formatted(describe(student), describeSection(student.getSection()))); | |
| 134 | } else { | |
| 135 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 136 | "Student %s is in untranslated roster section %s and maps to %s" | |
| 137 | .formatted( | |
| 138 | describe(student), | |
| 139 | describeSection(student.getSection()), | |
| 140 | describeChannelNames(matchingChannels))); | |
| 141 | } | |
| 142 | } | |
| 143 | for (Map.Entry<String, String> channel : channelIdByName.entrySet()) { | |
| 144 | String channelName = channel.getKey(); | |
| 145 | String channelId = channel.getValue(); | |
| 146 | List<String> members; | |
| 147 | try { | |
| 148 | members = slackService.listChannelMembers(token, channelId); | |
| 149 | } catch (SlackApiException e) { | |
| 150 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Error listing members of #%s: %s".formatted(channelName, e.getMessage())); |
| 151 | continue; | |
| 152 | } | |
| 153 | membersByChannelName.put(channelName, members); | |
| 154 | ||
| 155 | Set<String> studentSlackIds = new HashSet<>(); | |
| 156 | List<RosterStudent> toAdd = new ArrayList<>(); | |
| 157 | for (RosterStudent student : students) { | |
| 158 |
1
1. accept : negated conditional → KILLED |
if (sectionsByChannelName.get(channelName).contains(student.getSection())) { |
| 159 | String slackId = | |
| 160 |
1
1. accept : negated conditional → KILLED |
student.getEmail() == null |
| 161 | ? null | |
| 162 | : activeSlackIdByEmail.get(canonical(student.getEmail())); | |
| 163 |
1
1. accept : negated conditional → KILLED |
if (slackId == null) { |
| 164 | Boolean invited = | |
| 165 |
1
1. accept : negated conditional → KILLED |
student.getEmail() == null |
| 166 | ? null | |
| 167 | : invitedByEmail.get(canonical(student.getEmail())); | |
| 168 |
1
1. accept : negated conditional → KILLED |
if (invited == null) { |
| 169 |
1
1. accept : Changed increment from 1 to -1 → KILLED |
studentsNotInWorkspace++; |
| 170 |
1
1. accept : negated conditional → KILLED |
} else if (invited) { |
| 171 |
1
1. accept : Changed increment from 1 to -1 → KILLED |
studentsInvited++; |
| 172 | } else { | |
| 173 |
1
1. accept : Changed increment from 1 to -1 → KILLED |
studentsDeactivated++; |
| 174 | } | |
| 175 | } else { | |
| 176 | studentSlackIds.add(slackId); | |
| 177 |
1
1. accept : negated conditional → KILLED |
if (!members.contains(slackId)) { |
| 178 | toAdd.add(student); | |
| 179 | } | |
| 180 | } | |
| 181 | } | |
| 182 | } | |
| 183 | studentSlackIdsByChannelName.put(channelName, studentSlackIds); | |
| 184 |
1
1. accept : removed call to edu/ucsb/cs156/frontiers/jobs/SetupSectionSlackChannelsJob::addStudents → KILLED |
addStudents(ctx, token, channelName, channelId, toAdd, activeSlackIdByEmail); |
| 185 | } | |
| 186 | // Students who cannot be added are not logged one by one (the Slack tab lists them), but the | |
| 187 | // log does say why: most often, they have not accepted their invitation yet. | |
| 188 | List<String> reasons = new ArrayList<>(); | |
| 189 |
2
1. accept : negated conditional → KILLED 2. accept : changed conditional boundary → KILLED |
if (studentsInvited > 0) { |
| 190 | reasons.add( | |
| 191 | "%d invited to the Slack workspace but not accepted yet".formatted(studentsInvited)); | |
| 192 | } | |
| 193 |
2
1. accept : changed conditional boundary → KILLED 2. accept : negated conditional → KILLED |
if (studentsDeactivated > 0) { |
| 194 | reasons.add("%d with a deactivated Slack account".formatted(studentsDeactivated)); | |
| 195 | } | |
| 196 |
2
1. accept : changed conditional boundary → KILLED 2. accept : negated conditional → KILLED |
if (studentsNotInWorkspace > 0) { |
| 197 | reasons.add("%d not in the Slack workspace".formatted(studentsNotInWorkspace)); | |
| 198 | } | |
| 199 |
1
1. accept : negated conditional → KILLED |
if (!reasons.isEmpty()) { |
| 200 |
3
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED 2. accept : Replaced integer addition with subtraction → KILLED 3. accept : Replaced integer addition with subtraction → KILLED |
ctx.log( |
| 201 | "%d student(s) in these sections could not be added: %s. See the Slack tab. Run this job again once they have joined." | |
| 202 | .formatted( | |
| 203 | studentsInvited + studentsDeactivated + studentsNotInWorkspace, | |
| 204 | String.join(", ", reasons))); | |
| 205 | } | |
| 206 | ||
| 207 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Removing Channel Members Who Are Not In The Section"); |
| 208 | channels: | |
| 209 | for (Map.Entry<String, String> channel : channelIdByName.entrySet()) { | |
| 210 | String channelName = channel.getKey(); | |
| 211 | List<String> members = membersByChannelName.get(channelName); | |
| 212 |
1
1. accept : negated conditional → KILLED |
if (members == null) { |
| 213 | continue; | |
| 214 | } | |
| 215 | for (String memberId : members) { | |
| 216 | SlackUser member = slackUsersById.get(memberId); | |
| 217 | boolean belongs = | |
| 218 |
1
1. accept : negated conditional → KILLED |
staffSlackIds.contains(memberId) |
| 219 |
1
1. accept : negated conditional → KILLED |
|| studentSlackIdsByChannelName.get(channelName).contains(memberId); |
| 220 | // Bots (including this one) and members who are not users of the workspace are left alone | |
| 221 |
3
1. accept : negated conditional → KILLED 2. accept : negated conditional → KILLED 3. accept : negated conditional → KILLED |
if (!belongs && member != null && member.isPerson()) { |
| 222 | try { | |
| 223 |
1
1. accept : removed call to edu/ucsb/cs156/frontiers/services/SlackService::removeFromChannel → KILLED |
slackService.removeFromChannel(token, channel.getValue(), memberId); |
| 224 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Removed %s from #%s".formatted(describe(member), channelName)); |
| 225 | } catch (SlackApiException e) { | |
| 226 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 227 | "Error removing %s from #%s: %s" | |
| 228 | .formatted(describe(member), channelName, e.getMessage())); | |
| 229 |
1
1. accept : negated conditional → KILLED |
if (SlackService.RESTRICTED_ACTION.equals(e.getMessage())) { |
| 230 | // A workspace setting forbids it, so every other removal would fail the same way | |
| 231 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log(SlackService.REMOVAL_RESTRICTED_ADVICE); |
| 232 | break channels; | |
| 233 | } | |
| 234 | } | |
| 235 | } | |
| 236 | } | |
| 237 | } | |
| 238 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Done"); |
| 239 | } | |
| 240 | ||
| 241 | /** | |
| 242 | * @return for each (normalized) channel name, in order of section, the sections that use it | |
| 243 | */ | |
| 244 | private Map<String, Set<String>> sectionsByChannelName(Long courseId) { | |
| 245 | Map<String, Set<String>> result = new LinkedHashMap<>(); | |
| 246 | for (Section section : sectionRepository.findByCourseIdOrderBySectionAsc(courseId)) { | |
| 247 | String channelName = normalizeChannelName(section.getSlackChannelName()); | |
| 248 |
1
1. sectionsByChannelName : negated conditional → KILLED |
if (!channelName.isEmpty()) { |
| 249 |
1
1. lambda$sectionsByChannelName$0 : replaced return value with Collections.emptySet for edu/ucsb/cs156/frontiers/jobs/SetupSectionSlackChannelsJob::lambda$sectionsByChannelName$0 → KILLED |
result.computeIfAbsent(channelName, name -> new HashSet<>()).add(section.getSection()); |
| 250 | } | |
| 251 | } | |
| 252 |
1
1. sectionsByChannelName : replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/jobs/SetupSectionSlackChannelsJob::sectionsByChannelName → KILLED |
return result; |
| 253 | } | |
| 254 | ||
| 255 | private Map<String, List<String>> channelNamesBySection( | |
| 256 | Map<String, Set<String>> sectionsByChannelName) { | |
| 257 | Map<String, List<String>> result = new HashMap<>(); | |
| 258 | for (Map.Entry<String, Set<String>> entry : sectionsByChannelName.entrySet()) { | |
| 259 | for (String section : entry.getValue()) { | |
| 260 |
1
1. lambda$channelNamesBySection$0 : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/jobs/SetupSectionSlackChannelsJob::lambda$channelNamesBySection$0 → KILLED |
result.computeIfAbsent(section, unused -> new ArrayList<>()).add(entry.getKey()); |
| 261 | } | |
| 262 | } | |
| 263 |
1
1. channelNamesBySection : replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/jobs/SetupSectionSlackChannelsJob::channelNamesBySection → KILLED |
return result; |
| 264 | } | |
| 265 | ||
| 266 | private List<RosterStudent> enrolledStudents(Long courseId) { | |
| 267 |
1
1. enrolledStudents : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/jobs/SetupSectionSlackChannelsJob::enrolledStudents → KILLED |
return StreamSupport.stream( |
| 268 | rosterStudentRepository | |
| 269 | .findByCourseIdOrderByFirstNameAscLastNameAscIgnoreCase(courseId) | |
| 270 | .spliterator(), | |
| 271 | false) | |
| 272 |
2
1. lambda$enrolledStudents$0 : replaced boolean return with true for edu/ucsb/cs156/frontiers/jobs/SetupSectionSlackChannelsJob::lambda$enrolledStudents$0 → KILLED 2. lambda$enrolledStudents$0 : replaced boolean return with false for edu/ucsb/cs156/frontiers/jobs/SetupSectionSlackChannelsJob::lambda$enrolledStudents$0 → KILLED |
.filter(student -> ENROLLED_STATUSES.contains(student.getRosterStatus())) |
| 273 | .toList(); | |
| 274 | } | |
| 275 | ||
| 276 | /** | |
| 277 | * Slack channel names are lowercase, and are written with a leading # that is not part of the | |
| 278 | * name. | |
| 279 | * | |
| 280 | * @param name the name as entered for the section; may be null | |
| 281 | * @return the name to use with the Slack API, or an empty string if there is none | |
| 282 | */ | |
| 283 | public static String normalizeChannelName(String name) { | |
| 284 |
1
1. normalizeChannelName : negated conditional → KILLED |
if (name == null) { |
| 285 | return ""; | |
| 286 | } | |
| 287 | String stripped = name.strip(); | |
| 288 |
1
1. normalizeChannelName : negated conditional → KILLED |
if (stripped.startsWith("#")) { |
| 289 | stripped = stripped.substring(1).strip(); | |
| 290 | } | |
| 291 |
1
1. normalizeChannelName : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/SetupSectionSlackChannelsJob::normalizeChannelName → KILLED |
return stripped.toLowerCase(Locale.ROOT); |
| 292 | } | |
| 293 | ||
| 294 | /** | |
| 295 | * Creates the channels that do not exist yet, and makes the bot a member of the ones that do. | |
| 296 | * | |
| 297 | * @return the id of each channel that is ready to be used, by name | |
| 298 | */ | |
| 299 | private Map<String, String> createChannels( | |
| 300 | JobContext ctx, String token, Set<String> channelNames) { | |
| 301 | Map<String, SlackChannel> existingByName = new HashMap<>(); | |
| 302 | for (SlackChannel existing : slackService.listPublicChannels(token)) { | |
| 303 | existingByName.put(existing.getName(), existing); | |
| 304 | } | |
| 305 | ||
| 306 | Map<String, String> channelIdByName = new LinkedHashMap<>(); | |
| 307 | for (String channelName : channelNames) { | |
| 308 | SlackChannel existing = existingByName.get(channelName); | |
| 309 | try { | |
| 310 |
1
1. createChannels : negated conditional → KILLED |
if (existing == null) { |
| 311 | SlackChannel created = slackService.createPublicChannel(token, channelName); | |
| 312 | channelIdByName.put(channelName, created.getId()); | |
| 313 |
1
1. createChannels : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Created channel #%s".formatted(channelName)); |
| 314 |
1
1. createChannels : negated conditional → KILLED |
} else if (existing.getArchived()) { |
| 315 |
1
1. createChannels : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 316 | "Channel #%s already exists, but is archived; unarchive it in Slack, then run this job again. Skipping it." | |
| 317 | .formatted(channelName)); | |
| 318 | } else { | |
| 319 |
1
1. createChannels : removed call to edu/ucsb/cs156/frontiers/services/SlackService::joinChannel → KILLED |
slackService.joinChannel(token, existing.getId()); |
| 320 | channelIdByName.put(channelName, existing.getId()); | |
| 321 |
1
1. createChannels : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Channel #%s already exists".formatted(channelName)); |
| 322 | } | |
| 323 | } catch (SlackApiException e) { | |
| 324 |
1
1. createChannels : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 325 | "Error setting up channel #%s: %s. Skipping it." | |
| 326 | .formatted(channelName, e.getMessage())); | |
| 327 | } | |
| 328 | } | |
| 329 |
1
1. createChannels : replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/jobs/SetupSectionSlackChannelsJob::createChannels → KILLED |
return channelIdByName; |
| 330 | } | |
| 331 | ||
| 332 | /** | |
| 333 | * Adds the students with one call to Slack. Slack adds all or none, so if that fails, they are | |
| 334 | * added one at a time, so that one problem student does not keep the others out. | |
| 335 | */ | |
| 336 | private void addStudents( | |
| 337 | JobContext ctx, | |
| 338 | String token, | |
| 339 | String channelName, | |
| 340 | String channelId, | |
| 341 | List<RosterStudent> toAdd, | |
| 342 | Map<String, String> activeSlackIdByEmail) { | |
| 343 |
1
1. addStudents : negated conditional → KILLED |
if (toAdd.isEmpty()) { |
| 344 | return; | |
| 345 | } | |
| 346 | List<String> slackIds = new ArrayList<>(); | |
| 347 | for (RosterStudent student : toAdd) { | |
| 348 | slackIds.add(activeSlackIdByEmail.get(canonical(student.getEmail()))); | |
| 349 | } | |
| 350 | try { | |
| 351 |
1
1. addStudents : removed call to edu/ucsb/cs156/frontiers/services/SlackService::inviteToChannel → KILLED |
slackService.inviteToChannel(token, channelId, slackIds); |
| 352 | for (RosterStudent student : toAdd) { | |
| 353 |
1
1. addStudents : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Added %s to #%s".formatted(describe(student), channelName)); |
| 354 | } | |
| 355 | return; | |
| 356 | } catch (SlackApiException e) { | |
| 357 |
1
1. addStudents : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 358 | "Could not add %d student(s) to #%s in one step (%s); adding them one at a time." | |
| 359 | .formatted(toAdd.size(), channelName, e.getMessage())); | |
| 360 | } | |
| 361 |
2
1. addStudents : changed conditional boundary → KILLED 2. addStudents : negated conditional → KILLED |
for (int i = 0; i < toAdd.size(); i++) { |
| 362 | try { | |
| 363 |
1
1. addStudents : removed call to edu/ucsb/cs156/frontiers/services/SlackService::inviteToChannel → KILLED |
slackService.inviteToChannel(token, channelId, List.of(slackIds.get(i))); |
| 364 |
1
1. addStudents : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Added %s to #%s".formatted(describe(toAdd.get(i)), channelName)); |
| 365 | } catch (SlackApiException e) { | |
| 366 |
1
1. addStudents : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 367 | "Error adding %s to #%s: %s" | |
| 368 | .formatted(describe(toAdd.get(i)), channelName, e.getMessage())); | |
| 369 | } | |
| 370 | } | |
| 371 | } | |
| 372 | ||
| 373 | /** Slack ids of the staff of the course and of its instructor, who belong in every channel. */ | |
| 374 | private Set<String> staffSlackIds(Course currentCourse, Map<String, String> slackIdByEmail) { | |
| 375 | Set<String> result = new HashSet<>(); | |
| 376 | for (CourseStaff staff : courseStaffRepository.findByCourseId(currentCourse.getId())) { | |
| 377 |
1
1. staffSlackIds : negated conditional → KILLED |
if (staff.getEmail() != null) { |
| 378 | // null (staff member not in Slack) is harmless in this set: no channel member has id null | |
| 379 | result.add(slackIdByEmail.get(canonical(staff.getEmail()))); | |
| 380 | } | |
| 381 | } | |
| 382 |
1
1. staffSlackIds : negated conditional → KILLED |
if (currentCourse.getInstructorEmail() != null) { |
| 383 | result.add(slackIdByEmail.get(canonical(currentCourse.getInstructorEmail()))); | |
| 384 | } | |
| 385 |
1
1. staffSlackIds : replaced return value with Collections.emptySet for edu/ucsb/cs156/frontiers/jobs/SetupSectionSlackChannelsJob::staffSlackIds → KILLED |
return result; |
| 386 | } | |
| 387 | ||
| 388 | private static String describe(RosterStudent student) { | |
| 389 |
1
1. describe : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/SetupSectionSlackChannelsJob::describe → KILLED |
return "%s %s (%s)" |
| 390 | .formatted(student.getFirstName(), student.getLastName(), student.getEmail()); | |
| 391 | } | |
| 392 | ||
| 393 | private static String describe(SlackUser user) { | |
| 394 |
1
1. describe : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/SetupSectionSlackChannelsJob::describe → KILLED |
return "%s (%s)".formatted(user.getRealName(), user.email()); |
| 395 | } | |
| 396 | ||
| 397 | private static String describeSection(String section) { | |
| 398 |
2
1. describeSection : negated conditional → KILLED 2. describeSection : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/SetupSectionSlackChannelsJob::describeSection → KILLED |
return section == null ? "(blank)" : section; |
| 399 | } | |
| 400 | ||
| 401 | private static String describeChannelNames(List<String> channelNames) { | |
| 402 |
1
1. describeChannelNames : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/SetupSectionSlackChannelsJob::describeChannelNames → KILLED |
return "#" + String.join(", #", channelNames); |
| 403 | } | |
| 404 | ||
| 405 | private static String canonical(String email) { | |
| 406 |
1
1. canonical : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/SetupSectionSlackChannelsJob::canonical → KILLED |
return CanonicalFormConverter.convertToValidEmail(email.strip()); |
| 407 | } | |
| 408 | } | |
Mutations | ||
| 68 |
1.1 |
|
| 73 |
1.1 |
|
| 80 |
1.1 2.2 |
|
| 94 |
1.1 2.2 |
|
| 96 |
1.1 2.2 |
|
| 97 |
1.1 |
|
| 100 |
1.1 |
|
| 106 |
1.1 |
|
| 120 |
1.1 |
|
| 127 |
1.1 |
|
| 128 |
1.1 |
|
| 131 |
1.1 |
|
| 135 |
1.1 |
|
| 150 |
1.1 |
|
| 158 |
1.1 |
|
| 160 |
1.1 |
|
| 163 |
1.1 |
|
| 165 |
1.1 |
|
| 168 |
1.1 |
|
| 169 |
1.1 |
|
| 170 |
1.1 |
|
| 171 |
1.1 |
|
| 173 |
1.1 |
|
| 177 |
1.1 |
|
| 184 |
1.1 |
|
| 189 |
1.1 2.2 |
|
| 193 |
1.1 2.2 |
|
| 196 |
1.1 2.2 |
|
| 199 |
1.1 |
|
| 200 |
1.1 2.2 3.3 |
|
| 207 |
1.1 |
|
| 212 |
1.1 |
|
| 218 |
1.1 |
|
| 219 |
1.1 |
|
| 221 |
1.1 2.2 3.3 |
|
| 223 |
1.1 |
|
| 224 |
1.1 |
|
| 226 |
1.1 |
|
| 229 |
1.1 |
|
| 231 |
1.1 |
|
| 238 |
1.1 |
|
| 248 |
1.1 |
|
| 249 |
1.1 |
|
| 252 |
1.1 |
|
| 260 |
1.1 |
|
| 263 |
1.1 |
|
| 267 |
1.1 |
|
| 272 |
1.1 2.2 |
|
| 284 |
1.1 |
|
| 288 |
1.1 |
|
| 291 |
1.1 |
|
| 310 |
1.1 |
|
| 313 |
1.1 |
|
| 314 |
1.1 |
|
| 315 |
1.1 |
|
| 319 |
1.1 |
|
| 321 |
1.1 |
|
| 324 |
1.1 |
|
| 329 |
1.1 |
|
| 343 |
1.1 |
|
| 351 |
1.1 |
|
| 353 |
1.1 |
|
| 357 |
1.1 |
|
| 361 |
1.1 2.2 |
|
| 363 |
1.1 |
|
| 364 |
1.1 |
|
| 366 |
1.1 |
|
| 377 |
1.1 |
|
| 382 |
1.1 |
|
| 385 |
1.1 |
|
| 389 |
1.1 |
|
| 394 |
1.1 |
|
| 398 |
1.1 2.2 |
|
| 402 |
1.1 |
|
| 406 |
1.1 |