| 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.enums.RosterStatus; | |
| 7 | import edu.ucsb.cs156.frontiers.errors.SlackApiException; | |
| 8 | import edu.ucsb.cs156.frontiers.models.SlackAuthTestResponse; | |
| 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.services.CanvasApiTokenSecurityService; | |
| 15 | import edu.ucsb.cs156.frontiers.services.SlackService; | |
| 16 | import edu.ucsb.cs156.frontiers.utilities.CanonicalFormConverter; | |
| 17 | import edu.ucsb.cs156.jobs.services.JobContext; | |
| 18 | import edu.ucsb.cs156.jobs.services.JobContextConsumer; | |
| 19 | import java.text.Normalizer; | |
| 20 | import java.util.ArrayList; | |
| 21 | import java.util.HashMap; | |
| 22 | import java.util.LinkedHashMap; | |
| 23 | import java.util.List; | |
| 24 | import java.util.Locale; | |
| 25 | import java.util.Map; | |
| 26 | import java.util.Set; | |
| 27 | import java.util.stream.Stream; | |
| 28 | import lombok.Builder; | |
| 29 | ||
| 30 | /** | |
| 31 | * Gives each student of a course a private Slack channel shared with the staff of the course. | |
| 32 | * | |
| 33 | * <ol> | |
| 34 | * <li>For each roster student (status ROSTER or MANUAL) with an active Slack account, works out a | |
| 35 | * channel name: "private-first-last" (see {@link #channelNameFor}). The first and last name | |
| 36 | * come from the student's Slack profile when it has them, and otherwise from the roster; only | |
| 37 | * the first word of the first name is used. Students who would get the same name also get the | |
| 38 | * part of their email before the @ at the end. | |
| 39 | * <li>Finds the channel each student already has. A channel is a student's if it is a private | |
| 40 | * channel that this bot created, and that student is the only one of the students among its | |
| 41 | * members. Channels are never recognized by name, so a channel is found again even after the | |
| 42 | * name of the student has changed. | |
| 43 | * <li>Creates the private channel of each student who does not have one, and renames the channel | |
| 44 | * of each student whose channel does not have the name worked out in the first step. | |
| 45 | * <li>Adds the student, the instructor and the staff of the course to the channel, unless they | |
| 46 | * are already in it. People are matched to Slack users by email; those without an active | |
| 47 | * Slack account cannot be added, and are only counted. | |
| 48 | * <li>Removes from each student's channel every person who is neither the student, nor the | |
| 49 | * instructor, nor a member of the course staff: for example somebody who has been deleted | |
| 50 | * from the staff of the course. Bots (including the bot this job acts as) and members that | |
| 51 | * are not users of the workspace are never removed. If Slack refuses because of a workspace | |
| 52 | * setting (restricted_action), the log says how to change the setting and no further removals | |
| 53 | * are attempted in that run. | |
| 54 | * </ol> | |
| 55 | * | |
| 56 | * A problem with one channel or one person is logged, and the job carries on with the rest; but if | |
| 57 | * the members of an existing channel cannot be listed, the job stops before changing anything, | |
| 58 | * since it could not tell whose channel that is and might create a duplicate. The last line of the | |
| 59 | * log is a summary. | |
| 60 | */ | |
| 61 | @Builder | |
| 62 | public class SetupPrivateSlackChannelsJob implements JobContextConsumer { | |
| 63 | ||
| 64 | public static final String CHANNEL_PREFIX = "private"; | |
| 65 | ||
| 66 | /** Slack does not allow longer channel names. */ | |
| 67 | public static final int MAX_CHANNEL_NAME_LENGTH = 80; | |
| 68 | ||
| 69 | /** Slack's error code when a channel (public or private, archived or not) has the name. */ | |
| 70 | public static final String NAME_TAKEN = "name_taken"; | |
| 71 | ||
| 72 | /** What to tell the instructor when creating a channel fails with {@link #NAME_TAKEN}. */ | |
| 73 | public static final String NAME_TAKEN_ADVICE = | |
| 74 | "Slack already has a channel named #%s, which this job does not recognize as the private channel of this student: it may be a public channel, a private channel that this bot did not create, or a private channel that does not have exactly one student of the course among its members. Rename that channel in Slack, or fix its members, then run this job again."; | |
| 75 | ||
| 76 | /** | |
| 77 | * What to tell the instructor when removing a member fails with {@link | |
| 78 | * SlackService#RESTRICTED_ACTION}. | |
| 79 | */ | |
| 80 | public static final String REMOVAL_RESTRICTED_ADVICE = | |
| 81 | "Slack does not allow this bot to remove members from private channels, so no more members will be removed in this run. A Workspace Owner can change this in Slack under Workspace settings, Roles & permissions (on older workspaces: Permissions, Channel Management): set \"People who can remove members from private channels\" to \"Everyone, except guests\". Then run this job again."; | |
| 82 | ||
| 83 | /** Only these roster students are considered: in particular, not dropped students. */ | |
| 84 | public static final List<RosterStatus> ENROLLED_STATUSES = | |
| 85 | List.of(RosterStatus.ROSTER, RosterStatus.MANUAL); | |
| 86 | ||
| 87 | Course course; | |
| 88 | CourseRepository courseRepository; | |
| 89 | RosterStudentRepository rosterStudentRepository; | |
| 90 | CourseStaffRepository courseStaffRepository; | |
| 91 | SlackService slackService; | |
| 92 | CanvasApiTokenSecurityService tokenSecurityService; | |
| 93 | ||
| 94 | @Override | |
| 95 | public String getScopeType() { | |
| 96 |
1
1. getScopeType : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/SetupPrivateSlackChannelsJob::getScopeType → KILLED |
return "course"; |
| 97 | } | |
| 98 | ||
| 99 | @Override | |
| 100 | public Long getScopeId() { | |
| 101 |
1
1. getScopeId : replaced Long return value with 0L for edu/ucsb/cs156/frontiers/jobs/SetupPrivateSlackChannelsJob::getScopeId → KILLED |
return course.getId(); |
| 102 | } | |
| 103 | ||
| 104 | /** | |
| 105 | * Turns part of a name into the form Slack requires of channel names: accents are dropped, the | |
| 106 | * rest is lowercased, and each run of characters other than letters, numbers and underscores is | |
| 107 | * replaced by one hyphen (none at the start or the end). | |
| 108 | * | |
| 109 | * @param namePart for example a last name; may be null | |
| 110 | * @return the sanitized name part; empty if nothing is left | |
| 111 | */ | |
| 112 | public static String sanitize(String namePart) { | |
| 113 |
1
1. sanitize : negated conditional → KILLED |
if (namePart == null) { |
| 114 | return ""; | |
| 115 | } | |
| 116 |
1
1. sanitize : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/SetupPrivateSlackChannelsJob::sanitize → KILLED |
return Normalizer.normalize(namePart, Normalizer.Form.NFD) |
| 117 | .replaceAll("\\p{M}", "") | |
| 118 | .toLowerCase(Locale.ROOT) | |
| 119 | .replaceAll("[^a-z0-9_]+", "-") | |
| 120 | .replaceAll("^-|-$", ""); | |
| 121 | } | |
| 122 | ||
| 123 | /** | |
| 124 | * @param firstName a first name, possibly followed by middle names; may be null | |
| 125 | * @return the first name up to the first space | |
| 126 | */ | |
| 127 | public static String firstWord(String firstName) { | |
| 128 |
1
1. firstWord : negated conditional → KILLED |
if (firstName == null) { |
| 129 | return ""; | |
| 130 | } | |
| 131 |
1
1. firstWord : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/SetupPrivateSlackChannelsJob::firstWord → KILLED |
return firstName.strip().split("\\s+")[0]; |
| 132 | } | |
| 133 | ||
| 134 | /** | |
| 135 | * @param firstName the first name; only the part up to the first space is used | |
| 136 | * @param lastName the last name | |
| 137 | * @param suffix added at the end, to tell apart students with the same name; may be empty | |
| 138 | * @return the name of the student's private Slack channel, for example private-chris-gaucho | |
| 139 | */ | |
| 140 | public static String channelNameFor(String firstName, String lastName, String suffix) { | |
| 141 | String name = | |
| 142 | String.join( | |
| 143 | "-", | |
| 144 | Stream.of(CHANNEL_PREFIX, sanitize(firstWord(firstName)), sanitize(lastName), suffix) | |
| 145 |
2
1. lambda$channelNameFor$0 : negated conditional → KILLED 2. lambda$channelNameFor$0 : replaced boolean return with true for edu/ucsb/cs156/frontiers/jobs/SetupPrivateSlackChannelsJob::lambda$channelNameFor$0 → KILLED |
.filter(part -> !part.isEmpty()) |
| 146 | .toList()); | |
| 147 |
1
1. channelNameFor : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/SetupPrivateSlackChannelsJob::channelNameFor → KILLED |
return name.substring(0, Math.min(name.length(), MAX_CHANNEL_NAME_LENGTH)); |
| 148 | } | |
| 149 | ||
| 150 | /** A student who has an active Slack account, and so gets a channel. */ | |
| 151 | private static class StudentChannel { | |
| 152 | RosterStudent student; | |
| 153 | String firstName; | |
| 154 | String lastName; | |
| 155 | String channelName; | |
| 156 | // the channel the student already has, if any, and its members | |
| 157 | SlackChannel existing; | |
| 158 | List<String> members = List.of(); | |
| 159 | } | |
| 160 | ||
| 161 | /** The counts reported at the end of the log. */ | |
| 162 | private static class Summary { | |
| 163 | int channelsCreated; | |
| 164 | int channelsExisting; | |
| 165 | int channelsRenamed; | |
| 166 | int membersAdded; | |
| 167 | int membersAlreadyPresent; | |
| 168 | int membersRemoved; | |
| 169 | // set once Slack has refused a removal because of a workspace setting | |
| 170 | boolean removalsRestricted; | |
| 171 | } | |
| 172 | ||
| 173 | @Override | |
| 174 | public void accept(JobContext ctx) throws Exception { | |
| 175 | Course currentCourse = courseRepository.findById(course.getId()).orElseThrow(); | |
| 176 | String token = tokenSecurityService.decrypt(currentCourse.getSlackBotToken()); | |
| 177 |
2
1. accept : negated conditional → KILLED 2. accept : negated conditional → KILLED |
if (token == null || token.isEmpty()) { |
| 178 | throw new IllegalStateException( | |
| 179 | "No Slack token has been set for this course; enter one on the Settings tab."); | |
| 180 | } | |
| 181 | ||
| 182 | Map<String, SlackUser> slackUsersById = new HashMap<>(); | |
| 183 | Map<String, SlackUser> activeSlackUserByEmail = new HashMap<>(); | |
| 184 | for (SlackUser user : slackService.listUsers(token)) { | |
| 185 | slackUsersById.put(user.getId(), user); | |
| 186 |
2
1. accept : negated conditional → KILLED 2. accept : negated conditional → KILLED |
if (user.isActivePerson() && user.email() != null) { |
| 187 | activeSlackUserByEmail.put(canonical(user.email()), user); | |
| 188 | } | |
| 189 | } | |
| 190 |
1
1. accept : negated conditional → KILLED |
if (activeSlackUserByEmail.isEmpty()) { |
| 191 | // Without emails nobody can be matched to a Slack user. | |
| 192 | throw new IllegalStateException( | |
| 193 | "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."); | |
| 194 | } | |
| 195 | ||
| 196 | // The instructor and the staff, who belong in every channel: Slack id, and a label for the log | |
| 197 | Map<String, String> staff = new LinkedHashMap<>(); | |
| 198 | int staffNotInSlack = 0; | |
| 199 |
1
1. accept : negated conditional → KILLED |
if (currentCourse.getInstructorEmail() != null) { |
| 200 | SlackUser instructor = | |
| 201 | activeSlackUserByEmail.get(canonical(currentCourse.getInstructorEmail())); | |
| 202 |
1
1. accept : negated conditional → KILLED |
if (instructor == null) { |
| 203 |
1
1. accept : Changed increment from 1 to -1 → KILLED |
staffNotInSlack++; |
| 204 | } else { | |
| 205 | staff.put(instructor.getId(), "instructor " + currentCourse.getInstructorEmail()); | |
| 206 | } | |
| 207 | } | |
| 208 | for (CourseStaff staffMember : courseStaffRepository.findByCourseId(currentCourse.getId())) { | |
| 209 | SlackUser slackUser = | |
| 210 |
1
1. accept : negated conditional → KILLED |
staffMember.getEmail() == null |
| 211 | ? null | |
| 212 | : activeSlackUserByEmail.get(canonical(staffMember.getEmail())); | |
| 213 |
1
1. accept : negated conditional → KILLED |
if (slackUser == null) { |
| 214 |
1
1. accept : Changed increment from 1 to -1 → KILLED |
staffNotInSlack++; |
| 215 | } else { | |
| 216 | staff.putIfAbsent(slackUser.getId(), "staff member " + staffMember.getEmail()); | |
| 217 | } | |
| 218 | } | |
| 219 | ||
| 220 | // The students who get a channel, by Slack id | |
| 221 | Map<String, StudentChannel> students = new LinkedHashMap<>(); | |
| 222 | int studentsNotInSlack = 0; | |
| 223 | List<String> studentsOnStaff = new ArrayList<>(); | |
| 224 | for (RosterStudent student : | |
| 225 | rosterStudentRepository | |
| 226 | .findByCourseIdAndRosterStatusInOrderByFirstNameAscLastNameAscIgnoreCase( | |
| 227 | currentCourse.getId(), ENROLLED_STATUSES)) { | |
| 228 | SlackUser slackUser = | |
| 229 |
1
1. accept : negated conditional → KILLED |
student.getEmail() == null |
| 230 | ? null | |
| 231 | : activeSlackUserByEmail.get(canonical(student.getEmail())); | |
| 232 |
1
1. accept : negated conditional → KILLED |
if (slackUser == null) { |
| 233 |
1
1. accept : Changed increment from 1 to -1 → KILLED |
studentsNotInSlack++; |
| 234 |
1
1. accept : negated conditional → KILLED |
} else if (staff.containsKey(slackUser.getId())) { |
| 235 | // They are in every channel, so no channel could be recognized as theirs | |
| 236 | studentsOnStaff.add(describe(student)); | |
| 237 | } else { | |
| 238 | StudentChannel studentChannel = new StudentChannel(); | |
| 239 | studentChannel.student = student; | |
| 240 | studentChannel.firstName = firstNonBlank(slackUser.firstName(), student.getFirstName()); | |
| 241 | studentChannel.lastName = firstNonBlank(slackUser.lastName(), student.getLastName()); | |
| 242 | students.putIfAbsent(slackUser.getId(), studentChannel); | |
| 243 | } | |
| 244 | } | |
| 245 |
1
1. accept : removed call to edu/ucsb/cs156/frontiers/jobs/SetupPrivateSlackChannelsJob::chooseChannelNames → KILLED |
chooseChannelNames(students); |
| 246 | ||
| 247 | // Fails, before anything is changed, if the members of a channel cannot be listed | |
| 248 |
1
1. accept : removed call to edu/ucsb/cs156/frontiers/jobs/SetupPrivateSlackChannelsJob::findExistingChannels → KILLED |
findExistingChannels(token, students); |
| 249 | ||
| 250 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 251 | "Creating Private Channels (%d student(s), channel names start with %s-)" | |
| 252 | .formatted(students.size(), CHANNEL_PREFIX)); | |
| 253 | for (String student : studentsOnStaff) { | |
| 254 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 255 | "%s is also the instructor or a staff member of the course, so does not get a private channel." | |
| 256 | .formatted(student)); | |
| 257 | } | |
| 258 | Summary summary = new Summary(); | |
| 259 | for (Map.Entry<String, StudentChannel> entry : students.entrySet()) { | |
| 260 | StudentChannel studentChannel = entry.getValue(); | |
| 261 | String channelId = setUpChannel(ctx, token, studentChannel, summary); | |
| 262 |
1
1. accept : negated conditional → KILLED |
if (channelId == null) { |
| 263 | continue; | |
| 264 | } | |
| 265 | ||
| 266 | // who belongs, with a label for the log; the student first | |
| 267 | Map<String, String> belonging = new LinkedHashMap<>(); | |
| 268 | belonging.put(entry.getKey(), describe(studentChannel.student)); | |
| 269 |
1
1. accept : removed call to java/util/Map::putAll → KILLED |
belonging.putAll(staff); |
| 270 | List<Map.Entry<String, String>> toAdd = new ArrayList<>(); | |
| 271 | for (Map.Entry<String, String> person : belonging.entrySet()) { | |
| 272 |
1
1. accept : negated conditional → KILLED |
if (studentChannel.members.contains(person.getKey())) { |
| 273 |
1
1. accept : Replaced integer addition with subtraction → KILLED |
summary.membersAlreadyPresent++; |
| 274 | } else { | |
| 275 | toAdd.add(person); | |
| 276 | } | |
| 277 | } | |
| 278 |
1
1. accept : removed call to edu/ucsb/cs156/frontiers/jobs/SetupPrivateSlackChannelsJob::addMembers → KILLED |
addMembers(ctx, token, studentChannel.channelName, channelId, toAdd, summary); |
| 279 |
1
1. accept : removed call to edu/ucsb/cs156/frontiers/jobs/SetupPrivateSlackChannelsJob::removeOthers → KILLED |
removeOthers( |
| 280 | ctx, token, studentChannel, channelId, belonging.keySet(), slackUsersById, summary); | |
| 281 | } | |
| 282 | ||
| 283 |
2
1. accept : changed conditional boundary → KILLED 2. accept : negated conditional → KILLED |
if (studentsNotInSlack > 0) { |
| 284 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 285 | "%d student(s) did not get a private channel, because they do not have an active account in the Slack workspace; see the Slack tab. Run this job again once they have joined." | |
| 286 | .formatted(studentsNotInSlack)); | |
| 287 | } | |
| 288 |
2
1. accept : negated conditional → KILLED 2. accept : changed conditional boundary → KILLED |
if (staffNotInSlack > 0) { |
| 289 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 290 | "%d staff member(s) (counting the instructor) could not be added to the channels, because they do not have an active account in the Slack workspace; see the Slack tab. Run this job again once they have joined." | |
| 291 | .formatted(staffNotInSlack)); | |
| 292 | } | |
| 293 |
1
1. accept : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 294 | "Done. Channels created: %d, already existed: %d (of which renamed: %d). Members added: %d, already present: %d, removed: %d." | |
| 295 | .formatted( | |
| 296 | summary.channelsCreated, | |
| 297 | summary.channelsExisting, | |
| 298 | summary.channelsRenamed, | |
| 299 | summary.membersAdded, | |
| 300 | summary.membersAlreadyPresent, | |
| 301 | summary.membersRemoved)); | |
| 302 | } | |
| 303 | ||
| 304 | /** | |
| 305 | * Works out the name of each student's channel. Students who would get the same name are told | |
| 306 | * apart by the part of their email before the @. | |
| 307 | */ | |
| 308 | private static void chooseChannelNames(Map<String, StudentChannel> students) { | |
| 309 | Map<String, Integer> timesUsed = new HashMap<>(); | |
| 310 | for (StudentChannel studentChannel : students.values()) { | |
| 311 | studentChannel.channelName = | |
| 312 | channelNameFor(studentChannel.firstName, studentChannel.lastName, ""); | |
| 313 | timesUsed.merge(studentChannel.channelName, 1, Integer::sum); | |
| 314 | } | |
| 315 | for (StudentChannel studentChannel : students.values()) { | |
| 316 |
2
1. chooseChannelNames : changed conditional boundary → KILLED 2. chooseChannelNames : negated conditional → KILLED |
if (timesUsed.get(studentChannel.channelName) > 1) { |
| 317 | studentChannel.channelName = | |
| 318 | channelNameFor( | |
| 319 | studentChannel.firstName, | |
| 320 | studentChannel.lastName, | |
| 321 | sanitize(studentChannel.student.getEmail().split("@")[0])); | |
| 322 | } | |
| 323 | } | |
| 324 | } | |
| 325 | ||
| 326 | /** | |
| 327 | * Finds the channel each student already has: a private channel that this bot created, in which | |
| 328 | * that student is the only one of the students. | |
| 329 | * | |
| 330 | * @throws IllegalStateException if the members of such a channel cannot be listed | |
| 331 | */ | |
| 332 | private void findExistingChannels(String token, Map<String, StudentChannel> students) { | |
| 333 | SlackAuthTestResponse authTest = slackService.authTest(token); | |
| 334 |
1
1. findExistingChannels : negated conditional → KILLED |
if (!authTest.getOk()) { |
| 335 | throw new SlackApiException(String.valueOf(authTest.getError())); | |
| 336 | } | |
| 337 | for (SlackChannel channel : slackService.listPrivateChannels(token)) { | |
| 338 | // Only channels made by this job are considered, so that no other private channel that the | |
| 339 | // bot happens to have been added to is ever renamed, or opened up to the whole staff | |
| 340 |
2
1. findExistingChannels : negated conditional → KILLED 2. findExistingChannels : negated conditional → KILLED |
if (authTest.getUserId() == null || !authTest.getUserId().equals(channel.getCreator())) { |
| 341 | continue; | |
| 342 | } | |
| 343 | List<String> members; | |
| 344 | try { | |
| 345 | members = slackService.listChannelMembers(token, channel.getId()); | |
| 346 | } catch (SlackApiException e) { | |
| 347 | throw new IllegalStateException( | |
| 348 | "Could not list the members of the private channel #%s (%s), so cannot tell which student it belongs to. Nothing was changed in Slack." | |
| 349 | .formatted(channel.getName(), e.getMessage())); | |
| 350 | } | |
| 351 | List<String> studentMembers = members.stream().filter(students::containsKey).toList(); | |
| 352 |
1
1. findExistingChannels : negated conditional → KILLED |
if (studentMembers.size() == 1) { |
| 353 | StudentChannel studentChannel = students.get(studentMembers.get(0)); | |
| 354 | // If a student somehow has several, the one that is in use rather than archived wins | |
| 355 |
2
1. findExistingChannels : negated conditional → KILLED 2. findExistingChannels : negated conditional → KILLED |
if (studentChannel.existing == null || studentChannel.existing.getArchived()) { |
| 356 | studentChannel.existing = channel; | |
| 357 | studentChannel.members = members; | |
| 358 | } | |
| 359 | } | |
| 360 | } | |
| 361 | } | |
| 362 | ||
| 363 | /** | |
| 364 | * Creates the student's channel, or renames the one they have if its name is not the right one. | |
| 365 | * | |
| 366 | * @return the id of the channel, or null if it is not ready to be used | |
| 367 | */ | |
| 368 | private String setUpChannel( | |
| 369 | JobContext ctx, String token, StudentChannel studentChannel, Summary summary) { | |
| 370 | String channelName = studentChannel.channelName; | |
| 371 | String student = describe(studentChannel.student); | |
| 372 | SlackChannel existing = studentChannel.existing; | |
| 373 |
1
1. setUpChannel : negated conditional → KILLED |
if (existing == null) { |
| 374 | try { | |
| 375 | SlackChannel created = slackService.createPrivateChannel(token, channelName); | |
| 376 |
1
1. setUpChannel : Replaced integer addition with subtraction → KILLED |
summary.channelsCreated++; |
| 377 |
1
1. setUpChannel : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Created private channel #%s for %s".formatted(channelName, student)); |
| 378 |
1
1. setUpChannel : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/SetupPrivateSlackChannelsJob::setUpChannel → KILLED |
return created.getId(); |
| 379 | } catch (SlackApiException e) { | |
| 380 |
1
1. setUpChannel : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 381 | "Error creating private channel #%s for %s: %s. Skipping it." | |
| 382 | .formatted(channelName, student, e.getMessage())); | |
| 383 |
1
1. setUpChannel : negated conditional → KILLED |
if (NAME_TAKEN.equals(e.getMessage())) { |
| 384 |
1
1. setUpChannel : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log(NAME_TAKEN_ADVICE.formatted(channelName)); |
| 385 | } | |
| 386 |
1
1. setUpChannel : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/SetupPrivateSlackChannelsJob::setUpChannel → KILLED |
return null; |
| 387 | } | |
| 388 | } | |
| 389 |
1
1. setUpChannel : negated conditional → KILLED |
if (existing.getArchived()) { |
| 390 |
1
1. setUpChannel : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 391 | "Private channel #%s for %s already exists, but is archived; unarchive it in Slack, then run this job again. Skipping it." | |
| 392 | .formatted(existing.getName(), student)); | |
| 393 |
1
1. setUpChannel : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/SetupPrivateSlackChannelsJob::setUpChannel → KILLED |
return null; |
| 394 | } | |
| 395 |
1
1. setUpChannel : Replaced integer addition with subtraction → KILLED |
summary.channelsExisting++; |
| 396 |
1
1. setUpChannel : negated conditional → KILLED |
if (channelName.equals(existing.getName())) { |
| 397 |
1
1. setUpChannel : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Private channel #%s for %s already exists".formatted(channelName, student)); |
| 398 |
1
1. setUpChannel : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/SetupPrivateSlackChannelsJob::setUpChannel → KILLED |
return existing.getId(); |
| 399 | } | |
| 400 | try { | |
| 401 |
1
1. setUpChannel : removed call to edu/ucsb/cs156/frontiers/services/SlackService::renameChannel → KILLED |
slackService.renameChannel(token, existing.getId(), channelName); |
| 402 |
1
1. setUpChannel : Replaced integer addition with subtraction → KILLED |
summary.channelsRenamed++; |
| 403 |
1
1. setUpChannel : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 404 | "Renamed private channel #%s to #%s for %s" | |
| 405 | .formatted(existing.getName(), channelName, student)); | |
| 406 | } catch (SlackApiException e) { | |
| 407 |
1
1. setUpChannel : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 408 | "Error renaming private channel #%s to #%s for %s: %s. It keeps its name." | |
| 409 | .formatted(existing.getName(), channelName, student, e.getMessage())); | |
| 410 | studentChannel.channelName = existing.getName(); | |
| 411 | } | |
| 412 |
1
1. setUpChannel : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/SetupPrivateSlackChannelsJob::setUpChannel → KILLED |
return existing.getId(); |
| 413 | } | |
| 414 | ||
| 415 | /** | |
| 416 | * Adds the people with one call to Slack. Slack adds all or none, so if that fails, they are | |
| 417 | * added one at a time, so that one problem person does not keep the others out. | |
| 418 | * | |
| 419 | * @param toAdd Slack id and description of each person to add | |
| 420 | */ | |
| 421 | private void addMembers( | |
| 422 | JobContext ctx, | |
| 423 | String token, | |
| 424 | String channelName, | |
| 425 | String channelId, | |
| 426 | List<Map.Entry<String, String>> toAdd, | |
| 427 | Summary summary) { | |
| 428 |
1
1. addMembers : negated conditional → KILLED |
if (toAdd.isEmpty()) { |
| 429 | return; | |
| 430 | } | |
| 431 | List<String> slackIds = toAdd.stream().map(Map.Entry::getKey).toList(); | |
| 432 | try { | |
| 433 |
1
1. addMembers : removed call to edu/ucsb/cs156/frontiers/services/SlackService::inviteToChannel → KILLED |
slackService.inviteToChannel(token, channelId, slackIds); |
| 434 | for (Map.Entry<String, String> person : toAdd) { | |
| 435 |
1
1. addMembers : Replaced integer addition with subtraction → KILLED |
summary.membersAdded++; |
| 436 |
1
1. addMembers : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Added %s to #%s".formatted(person.getValue(), channelName)); |
| 437 | } | |
| 438 | return; | |
| 439 | } catch (SlackApiException e) { | |
| 440 |
1
1. addMembers : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 441 | "Could not add %d member(s) to #%s in one step (%s); adding them one at a time." | |
| 442 | .formatted(toAdd.size(), channelName, e.getMessage())); | |
| 443 | } | |
| 444 | for (Map.Entry<String, String> person : toAdd) { | |
| 445 | try { | |
| 446 |
1
1. addMembers : removed call to edu/ucsb/cs156/frontiers/services/SlackService::inviteToChannel → KILLED |
slackService.inviteToChannel(token, channelId, List.of(person.getKey())); |
| 447 |
1
1. addMembers : Replaced integer addition with subtraction → KILLED |
summary.membersAdded++; |
| 448 |
1
1. addMembers : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Added %s to #%s".formatted(person.getValue(), channelName)); |
| 449 | } catch (SlackApiException e) { | |
| 450 |
1
1. addMembers : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 451 | "Error adding %s to #%s: %s".formatted(person.getValue(), channelName, e.getMessage())); | |
| 452 | } | |
| 453 | } | |
| 454 | } | |
| 455 | ||
| 456 | /** | |
| 457 | * Removes from the channel everybody who does not belong in it, such as somebody who is no longer | |
| 458 | * on the staff of the course. Bots (including this one) and members who are not users of the | |
| 459 | * workspace are left alone. | |
| 460 | * | |
| 461 | * @param belonging Slack ids of the student, the instructor and the staff | |
| 462 | */ | |
| 463 | private void removeOthers( | |
| 464 | JobContext ctx, | |
| 465 | String token, | |
| 466 | StudentChannel studentChannel, | |
| 467 | String channelId, | |
| 468 | Set<String> belonging, | |
| 469 | Map<String, SlackUser> slackUsersById, | |
| 470 | Summary summary) { | |
| 471 | String channelName = studentChannel.channelName; | |
| 472 | for (String memberId : studentChannel.members) { | |
| 473 | SlackUser member = slackUsersById.get(memberId); | |
| 474 |
1
1. removeOthers : negated conditional → KILLED |
if (summary.removalsRestricted |
| 475 |
2
1. removeOthers : negated conditional → KILLED 2. removeOthers : negated conditional → KILLED |
|| belonging.contains(memberId) |
| 476 | || member == null | |
| 477 |
1
1. removeOthers : negated conditional → KILLED |
|| !member.isPerson()) { |
| 478 | continue; | |
| 479 | } | |
| 480 | try { | |
| 481 |
1
1. removeOthers : removed call to edu/ucsb/cs156/frontiers/services/SlackService::removeFromChannel → KILLED |
slackService.removeFromChannel(token, channelId, memberId); |
| 482 |
1
1. removeOthers : Replaced integer addition with subtraction → KILLED |
summary.membersRemoved++; |
| 483 |
1
1. removeOthers : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log("Removed %s from #%s".formatted(describe(member), channelName)); |
| 484 | } catch (SlackApiException e) { | |
| 485 |
1
1. removeOthers : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log( |
| 486 | "Error removing %s from #%s: %s" | |
| 487 | .formatted(describe(member), channelName, e.getMessage())); | |
| 488 |
1
1. removeOthers : negated conditional → KILLED |
if (SlackService.RESTRICTED_ACTION.equals(e.getMessage())) { |
| 489 | // A workspace setting forbids it, so every other removal would fail the same way | |
| 490 |
1
1. removeOthers : removed call to edu/ucsb/cs156/jobs/services/JobContext::log → KILLED |
ctx.log(REMOVAL_RESTRICTED_ADVICE); |
| 491 | summary.removalsRestricted = true; | |
| 492 | } | |
| 493 | } | |
| 494 | } | |
| 495 | } | |
| 496 | ||
| 497 | private static String firstNonBlank(String preferred, String fallback) { | |
| 498 |
3
1. firstNonBlank : negated conditional → KILLED 2. firstNonBlank : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/SetupPrivateSlackChannelsJob::firstNonBlank → KILLED 3. firstNonBlank : negated conditional → KILLED |
return preferred == null || preferred.isBlank() ? fallback : preferred; |
| 499 | } | |
| 500 | ||
| 501 | private static String describe(RosterStudent student) { | |
| 502 |
1
1. describe : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/SetupPrivateSlackChannelsJob::describe → KILLED |
return "%s %s (%s)" |
| 503 | .formatted(student.getFirstName(), student.getLastName(), student.getEmail()); | |
| 504 | } | |
| 505 | ||
| 506 | private static String describe(SlackUser user) { | |
| 507 |
1
1. describe : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/SetupPrivateSlackChannelsJob::describe → KILLED |
return "%s (%s)".formatted(user.getRealName(), user.email()); |
| 508 | } | |
| 509 | ||
| 510 | private static String canonical(String email) { | |
| 511 |
1
1. canonical : replaced return value with "" for edu/ucsb/cs156/frontiers/jobs/SetupPrivateSlackChannelsJob::canonical → KILLED |
return CanonicalFormConverter.convertToValidEmail(email.strip()); |
| 512 | } | |
| 513 | } | |
Mutations | ||
| 96 |
1.1 |
|
| 101 |
1.1 |
|
| 113 |
1.1 |
|
| 116 |
1.1 |
|
| 128 |
1.1 |
|
| 131 |
1.1 |
|
| 145 |
1.1 2.2 |
|
| 147 |
1.1 |
|
| 177 |
1.1 2.2 |
|
| 186 |
1.1 2.2 |
|
| 190 |
1.1 |
|
| 199 |
1.1 |
|
| 202 |
1.1 |
|
| 203 |
1.1 |
|
| 210 |
1.1 |
|
| 213 |
1.1 |
|
| 214 |
1.1 |
|
| 229 |
1.1 |
|
| 232 |
1.1 |
|
| 233 |
1.1 |
|
| 234 |
1.1 |
|
| 245 |
1.1 |
|
| 248 |
1.1 |
|
| 250 |
1.1 |
|
| 254 |
1.1 |
|
| 262 |
1.1 |
|
| 269 |
1.1 |
|
| 272 |
1.1 |
|
| 273 |
1.1 |
|
| 278 |
1.1 |
|
| 279 |
1.1 |
|
| 283 |
1.1 2.2 |
|
| 284 |
1.1 |
|
| 288 |
1.1 2.2 |
|
| 289 |
1.1 |
|
| 293 |
1.1 |
|
| 316 |
1.1 2.2 |
|
| 334 |
1.1 |
|
| 340 |
1.1 2.2 |
|
| 352 |
1.1 |
|
| 355 |
1.1 2.2 |
|
| 373 |
1.1 |
|
| 376 |
1.1 |
|
| 377 |
1.1 |
|
| 378 |
1.1 |
|
| 380 |
1.1 |
|
| 383 |
1.1 |
|
| 384 |
1.1 |
|
| 386 |
1.1 |
|
| 389 |
1.1 |
|
| 390 |
1.1 |
|
| 393 |
1.1 |
|
| 395 |
1.1 |
|
| 396 |
1.1 |
|
| 397 |
1.1 |
|
| 398 |
1.1 |
|
| 401 |
1.1 |
|
| 402 |
1.1 |
|
| 403 |
1.1 |
|
| 407 |
1.1 |
|
| 412 |
1.1 |
|
| 428 |
1.1 |
|
| 433 |
1.1 |
|
| 435 |
1.1 |
|
| 436 |
1.1 |
|
| 440 |
1.1 |
|
| 446 |
1.1 |
|
| 447 |
1.1 |
|
| 448 |
1.1 |
|
| 450 |
1.1 |
|
| 474 |
1.1 |
|
| 475 |
1.1 2.2 |
|
| 477 |
1.1 |
|
| 481 |
1.1 |
|
| 482 |
1.1 |
|
| 483 |
1.1 |
|
| 485 |
1.1 |
|
| 488 |
1.1 |
|
| 490 |
1.1 |
|
| 498 |
1.1 2.2 3.3 |
|
| 502 |
1.1 |
|
| 507 |
1.1 |
|
| 511 |
1.1 |