| 1 | package edu.ucsb.cs156.frontiers.startup; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.frontiers.entities.Admin; | |
| 4 | import edu.ucsb.cs156.frontiers.entities.Course; | |
| 5 | import edu.ucsb.cs156.frontiers.entities.CourseStaff; | |
| 6 | import edu.ucsb.cs156.frontiers.entities.User; | |
| 7 | import edu.ucsb.cs156.frontiers.enums.OrgStatus; | |
| 8 | import edu.ucsb.cs156.frontiers.repositories.AdminRepository; | |
| 9 | import edu.ucsb.cs156.frontiers.repositories.CourseRepository; | |
| 10 | import edu.ucsb.cs156.frontiers.repositories.CourseStaffRepository; | |
| 11 | import edu.ucsb.cs156.frontiers.repositories.UserRepository; | |
| 12 | import java.util.List; | |
| 13 | import java.util.Optional; | |
| 14 | import lombok.extern.slf4j.Slf4j; | |
| 15 | import org.springframework.beans.factory.annotation.Autowired; | |
| 16 | import org.springframework.beans.factory.annotation.Value; | |
| 17 | import org.springframework.stereotype.Component; | |
| 18 | ||
| 19 | /** This class contains a `run` method that is called once at application startup time. */ | |
| 20 | @Slf4j | |
| 21 | @Component | |
| 22 | public class FrontiersStartup { | |
| 23 | ||
| 24 | @Value("#{'${app.admin.emails}'.split(',')}") | |
| 25 | List<String> adminEmails; | |
| 26 | ||
| 27 | @Value("${app.webhook.secret}") | |
| 28 | String webhookSecret; | |
| 29 | ||
| 30 | @Autowired AdminRepository adminRepository; | |
| 31 | ||
| 32 | @Autowired CourseRepository courseRepository; | |
| 33 | ||
| 34 | @Autowired CourseStaffRepository courseStaffRepository; | |
| 35 | ||
| 36 | @Autowired UserRepository userRepository; | |
| 37 | ||
| 38 | /** | |
| 39 | * Called once at application startup time . Put code here if you want it to run once each time | |
| 40 | * the Spring Boot application starts up in all environments. | |
| 41 | */ | |
| 42 | public void alwaysRunOnStartup() { | |
| 43 | log.info("alwaysRunOnStartup called"); | |
| 44 | ||
| 45 |
1
1. alwaysRunOnStartup : removed call to edu/ucsb/cs156/frontiers/startup/FrontiersStartup::validateWebhookSecret → KILLED |
validateWebhookSecret(); |
| 46 | ||
| 47 | try { | |
| 48 |
1
1. alwaysRunOnStartup : removed call to java/util/List::forEach → KILLED |
adminEmails.forEach( |
| 49 | (email) -> { | |
| 50 | Admin admin = new Admin(email); | |
| 51 | adminRepository.save(admin); | |
| 52 | }); | |
| 53 | } catch (Exception e) { | |
| 54 | log.error("Error in loading all ADMIN_EMAILS:", e); | |
| 55 | } | |
| 56 | ||
| 57 |
1
1. alwaysRunOnStartup : removed call to edu/ucsb/cs156/frontiers/startup/FrontiersStartup::addMissingInstructorsAsStaff → KILLED |
addMissingInstructorsAsStaff(); |
| 58 | } | |
| 59 | ||
| 60 | void addMissingInstructorsAsStaff() { | |
| 61 | try { | |
| 62 | for (Course course : courseRepository.findAll()) { | |
| 63 | String instructorEmail = course.getInstructorEmail(); | |
| 64 |
2
1. addMissingInstructorsAsStaff : negated conditional → KILLED 2. addMissingInstructorsAsStaff : negated conditional → KILLED |
if (instructorEmail == null || instructorEmail.isBlank()) { |
| 65 | continue; | |
| 66 | } | |
| 67 |
1
1. addMissingInstructorsAsStaff : negated conditional → KILLED |
if (courseStaffRepository.findByEmailAndCourse(instructorEmail, course).isPresent()) { |
| 68 | continue; | |
| 69 | } | |
| 70 | ||
| 71 | CourseStaff courseStaff = | |
| 72 | CourseStaff.builder() | |
| 73 | .firstName("") | |
| 74 | .lastName("") | |
| 75 | .email(instructorEmail) | |
| 76 | .course(course) | |
| 77 | .orgStatus( | |
| 78 |
1
1. addMissingInstructorsAsStaff : negated conditional → KILLED |
course.getInstallationId() != null ? OrgStatus.JOINCOURSE : OrgStatus.PENDING) |
| 79 | .build(); | |
| 80 | ||
| 81 | Optional<User> optionalUser = userRepository.findByEmail(instructorEmail); | |
| 82 |
1
1. addMissingInstructorsAsStaff : negated conditional → KILLED |
if (optionalUser.isPresent()) { |
| 83 | User user = optionalUser.get(); | |
| 84 |
1
1. addMissingInstructorsAsStaff : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setUser → KILLED |
courseStaff.setUser(user); |
| 85 |
1
1. addMissingInstructorsAsStaff : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setFirstName → KILLED |
courseStaff.setFirstName(user.getGivenName()); |
| 86 |
1
1. addMissingInstructorsAsStaff : removed call to edu/ucsb/cs156/frontiers/entities/CourseStaff::setLastName → KILLED |
courseStaff.setLastName(user.getFamilyName()); |
| 87 | } | |
| 88 | ||
| 89 | courseStaffRepository.save(courseStaff); | |
| 90 | log.info( | |
| 91 | "Added instructor {} as staff member of course {}", instructorEmail, course.getId()); | |
| 92 | } | |
| 93 | } catch (Exception e) { | |
| 94 | log.error("Error in adding missing instructors as course staff:", e); | |
| 95 | } | |
| 96 | } | |
| 97 | ||
| 98 | private void validateWebhookSecret() { | |
| 99 |
3
1. validateWebhookSecret : negated conditional → KILLED 2. validateWebhookSecret : changed conditional boundary → KILLED 3. validateWebhookSecret : negated conditional → KILLED |
if (webhookSecret == null || webhookSecret.length() < 10) { |
| 100 | String error = | |
| 101 | String.format( | |
| 102 | "WEBHOOK_SECRET must be at least 10 characters long. Current length: %d", | |
| 103 |
1
1. validateWebhookSecret : negated conditional → KILLED |
webhookSecret == null ? 0 : webhookSecret.length()); |
| 104 | log.error(error); | |
| 105 | throw new IllegalStateException(error); | |
| 106 | } | |
| 107 | log.info("Webhook secret validation passed"); | |
| 108 | } | |
| 109 | } | |
Mutations | ||
| 45 |
1.1 |
|
| 48 |
1.1 |
|
| 57 |
1.1 |
|
| 64 |
1.1 2.2 |
|
| 67 |
1.1 |
|
| 78 |
1.1 |
|
| 82 |
1.1 |
|
| 84 |
1.1 |
|
| 85 |
1.1 |
|
| 86 |
1.1 |
|
| 99 |
1.1 2.2 3.3 |
|
| 103 |
1.1 |