| 1 | package edu.ucsb.cs156.frontiers.controllers; | |
| 2 | ||
| 3 | import static org.springframework.data.domain.Sort.by; | |
| 4 | ||
| 5 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 6 | import com.fasterxml.jackson.databind.ObjectMapper; | |
| 7 | import edu.ucsb.cs156.frontiers.entities.Job; | |
| 8 | import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException; | |
| 9 | import edu.ucsb.cs156.frontiers.jobs.AddTeamMemberToGithubJob; | |
| 10 | import edu.ucsb.cs156.frontiers.jobs.DeleteTeamMemberFromGithubJob; | |
| 11 | import edu.ucsb.cs156.frontiers.jobs.MembershipAuditJob; | |
| 12 | import edu.ucsb.cs156.frontiers.jobs.PushTeamsToGithubJob; | |
| 13 | import edu.ucsb.cs156.frontiers.jobs.UpdateAllJob; | |
| 14 | import edu.ucsb.cs156.frontiers.repositories.CourseRepository; | |
| 15 | import edu.ucsb.cs156.frontiers.repositories.CourseStaffRepository; | |
| 16 | import edu.ucsb.cs156.frontiers.repositories.JobsRepository; | |
| 17 | import edu.ucsb.cs156.frontiers.repositories.RosterStudentRepository; | |
| 18 | import edu.ucsb.cs156.frontiers.repositories.TeamMemberRepository; | |
| 19 | import edu.ucsb.cs156.frontiers.repositories.TeamRepository; | |
| 20 | import edu.ucsb.cs156.frontiers.services.GithubTeamService; | |
| 21 | import edu.ucsb.cs156.frontiers.services.OrganizationMemberService; | |
| 22 | import edu.ucsb.cs156.frontiers.services.UpdateUserService; | |
| 23 | import edu.ucsb.cs156.frontiers.services.jobs.JobService; | |
| 24 | import io.swagger.v3.oas.annotations.Operation; | |
| 25 | import io.swagger.v3.oas.annotations.Parameter; | |
| 26 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 27 | import java.util.Map; | |
| 28 | import lombok.extern.slf4j.Slf4j; | |
| 29 | import org.springframework.beans.factory.annotation.Autowired; | |
| 30 | import org.springframework.data.domain.Sort; | |
| 31 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 32 | import org.springframework.web.bind.annotation.DeleteMapping; | |
| 33 | import org.springframework.web.bind.annotation.GetMapping; | |
| 34 | import org.springframework.web.bind.annotation.PathVariable; | |
| 35 | import org.springframework.web.bind.annotation.PostMapping; | |
| 36 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 37 | import org.springframework.web.bind.annotation.RequestParam; | |
| 38 | import org.springframework.web.bind.annotation.RestController; | |
| 39 | ||
| 40 | @Tag(name = "Jobs") | |
| 41 | @RequestMapping("/api/jobs") | |
| 42 | @RestController | |
| 43 | @Slf4j | |
| 44 | public class JobsController extends ApiController { | |
| 45 | @Autowired private JobsRepository jobsRepository; | |
| 46 | ||
| 47 | @Autowired private JobService jobService; | |
| 48 | ||
| 49 | @Autowired private UpdateUserService updateUserService; | |
| 50 | ||
| 51 | @Autowired ObjectMapper mapper; | |
| 52 | @Autowired private RosterStudentRepository rosterStudentRepository; | |
| 53 | @Autowired private CourseRepository courseRepository; | |
| 54 | @Autowired private OrganizationMemberService organizationMemberService; | |
| 55 | @Autowired private CourseStaffRepository courseStaffRepository; | |
| 56 | @Autowired private TeamRepository teamRepository; | |
| 57 | @Autowired private TeamMemberRepository teamMemberRepository; | |
| 58 | @Autowired private GithubTeamService githubTeamService; | |
| 59 | ||
| 60 | @Operation(summary = "List all jobs") | |
| 61 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 62 | @GetMapping("/all") | |
| 63 | public Iterable<Job> allJobs() { | |
| 64 | Iterable<Job> jobs = jobsRepository.findAll(by(Sort.Direction.DESC, "createdAt")); | |
| 65 |
1
1. allJobs : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/JobsController::allJobs → KILLED |
return jobs; |
| 66 | } | |
| 67 | ||
| 68 | @Operation(summary = "Delete all job records") | |
| 69 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 70 | @DeleteMapping("/all") | |
| 71 | public Map<String, String> deleteAllJobs() { | |
| 72 |
1
1. deleteAllJobs : removed call to edu/ucsb/cs156/frontiers/repositories/JobsRepository::deleteAll → KILLED |
jobsRepository.deleteAll(); |
| 73 |
1
1. deleteAllJobs : replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/controllers/JobsController::deleteAllJobs → KILLED |
return Map.of("message", "All jobs deleted"); |
| 74 | } | |
| 75 | ||
| 76 | @Operation(summary = "Get a specific Job Log by ID if it is in the database") | |
| 77 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 78 | @GetMapping("") | |
| 79 | public Job getJobLogById( | |
| 80 | @Parameter(name = "id", description = "ID of the job") @RequestParam Long id) | |
| 81 | throws JsonProcessingException { | |
| 82 | ||
| 83 | Job job = | |
| 84 |
1
1. lambda$getJobLogById$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/JobsController::lambda$getJobLogById$0 → KILLED |
jobsRepository.findById(id).orElseThrow(() -> new EntityNotFoundException(Job.class, id)); |
| 85 | ||
| 86 |
1
1. getJobLogById : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/JobsController::getJobLogById → KILLED |
return job; |
| 87 | } | |
| 88 | ||
| 89 | @Operation(summary = "Delete specific job record") | |
| 90 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 91 | @DeleteMapping("") | |
| 92 | public Map<String, String> deleteAllJobs(@Parameter(name = "id") @RequestParam Long id) { | |
| 93 |
1
1. deleteAllJobs : negated conditional → KILLED |
if (!jobsRepository.existsById(id)) { |
| 94 |
1
1. deleteAllJobs : replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/controllers/JobsController::deleteAllJobs → KILLED |
return Map.of("message", String.format("Job with id %d not found", id)); |
| 95 | } | |
| 96 |
1
1. deleteAllJobs : removed call to edu/ucsb/cs156/frontiers/repositories/JobsRepository::deleteById → KILLED |
jobsRepository.deleteById(id); |
| 97 |
1
1. deleteAllJobs : replaced return value with Collections.emptyMap for edu/ucsb/cs156/frontiers/controllers/JobsController::deleteAllJobs → KILLED |
return Map.of("message", String.format("Job with id %d deleted", id)); |
| 98 | } | |
| 99 | ||
| 100 | @Operation(summary = "Get long job logs") | |
| 101 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 102 | @GetMapping("/logs/{id}") | |
| 103 | public String getJobLogs(@Parameter(name = "id", description = "Job ID") @PathVariable Long id) { | |
| 104 | ||
| 105 |
1
1. getJobLogs : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/JobsController::getJobLogs → KILLED |
return jobService.getJobLogs(id); |
| 106 | } | |
| 107 | ||
| 108 | @Operation(summary = "Launch UpdateAll job") | |
| 109 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 110 | @PostMapping("/launch/updateAll") | |
| 111 | public Job launchUpdateAllJob() { | |
| 112 | ||
| 113 | UpdateAllJob job = UpdateAllJob.builder().updateUserService(updateUserService).build(); | |
| 114 |
1
1. launchUpdateAllJob : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/JobsController::launchUpdateAllJob → KILLED |
return jobService.runAsJob(job); |
| 115 | } | |
| 116 | ||
| 117 | @Operation(summary = "Launch Audit All Courses Job") | |
| 118 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 119 | @PostMapping("/launch/auditAllCourses") | |
| 120 | public Job launchAuditAllCoursesJob() { | |
| 121 | ||
| 122 | MembershipAuditJob job = | |
| 123 | MembershipAuditJob.builder() | |
| 124 | .rosterStudentRepository(rosterStudentRepository) | |
| 125 | .courseRepository(courseRepository) | |
| 126 | .organizationMemberService(organizationMemberService) | |
| 127 | .courseStaffRepository(courseStaffRepository) | |
| 128 | .build(); | |
| 129 |
1
1. launchAuditAllCoursesJob : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/JobsController::launchAuditAllCoursesJob → KILLED |
return jobService.runAsJob(job); |
| 130 | } | |
| 131 | ||
| 132 | @Operation(summary = "Launch Push Teams to GitHub Job") | |
| 133 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 134 | @PostMapping("/launch/pushTeamsToGithub") | |
| 135 | public Job launchPushTeamsToGithubJob(@Parameter(name = "courseId") @RequestParam Long courseId) { | |
| 136 | ||
| 137 | PushTeamsToGithubJob job = | |
| 138 | PushTeamsToGithubJob.builder() | |
| 139 | .courseId(courseId) | |
| 140 | .courseRepository(courseRepository) | |
| 141 | .teamRepository(teamRepository) | |
| 142 | .teamMemberRepository(teamMemberRepository) | |
| 143 | .githubTeamService(githubTeamService) | |
| 144 | .build(); | |
| 145 |
1
1. launchPushTeamsToGithubJob : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/JobsController::launchPushTeamsToGithubJob → KILLED |
return jobService.runAsJob(job); |
| 146 | } | |
| 147 | ||
| 148 | @Operation(summary = "Launch Delete Team Member From GitHub Job") | |
| 149 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 150 | @PostMapping("/launch/deleteTeamMemberFromGithub") | |
| 151 | public Job launchDeleteTeamMemberFromGithubJob( | |
| 152 | @Parameter(name = "memberGithubLogin") @RequestParam String memberGithubLogin, | |
| 153 | @Parameter(name = "githubTeamId") @RequestParam Integer githubTeamId, | |
| 154 | @Parameter(name = "courseId") @RequestParam Long courseId) { | |
| 155 | ||
| 156 | DeleteTeamMemberFromGithubJob job = | |
| 157 | DeleteTeamMemberFromGithubJob.builder() | |
| 158 | .memberGithubLogin(memberGithubLogin) | |
| 159 | .githubTeamId(githubTeamId) | |
| 160 | .course(courseRepository.findById(courseId).get()) | |
| 161 | .githubTeamService(githubTeamService) | |
| 162 | .build(); | |
| 163 |
1
1. launchDeleteTeamMemberFromGithubJob : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/JobsController::launchDeleteTeamMemberFromGithubJob → KILLED |
return jobService.runAsJob(job); |
| 164 | } | |
| 165 | ||
| 166 | @Operation(summary = "Launch Add Team Member To GitHub Job") | |
| 167 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 168 | @PostMapping("/launch/addTeamMemberToGithub") | |
| 169 | public Job launchAddTeamMemberToGithubJob( | |
| 170 | @Parameter(name = "memberGithubLogin") @RequestParam String memberGithubLogin, | |
| 171 | @Parameter(name = "githubTeamId") @RequestParam Integer githubTeamId, | |
| 172 | @Parameter(name = "teamMemberId") @RequestParam Long teamMemberId, | |
| 173 | @Parameter(name = "courseId") @RequestParam Long courseId) { | |
| 174 | ||
| 175 | AddTeamMemberToGithubJob job = | |
| 176 | AddTeamMemberToGithubJob.builder() | |
| 177 | .memberGithubLogin(memberGithubLogin) | |
| 178 | .githubTeamId(githubTeamId) | |
| 179 | .teamMemberId(teamMemberId) | |
| 180 | .course(courseRepository.findById(courseId).get()) | |
| 181 | .githubTeamService(githubTeamService) | |
| 182 | .teamMemberRepository(teamMemberRepository) | |
| 183 | .build(); | |
| 184 |
1
1. launchAddTeamMemberToGithubJob : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/JobsController::launchAddTeamMemberToGithubJob → KILLED |
return jobService.runAsJob(job); |
| 185 | } | |
| 186 | } | |
Mutations | ||
| 65 |
1.1 |
|
| 72 |
1.1 |
|
| 73 |
1.1 |
|
| 84 |
1.1 |
|
| 86 |
1.1 |
|
| 93 |
1.1 |
|
| 94 |
1.1 |
|
| 96 |
1.1 |
|
| 97 |
1.1 |
|
| 105 |
1.1 |
|
| 114 |
1.1 |
|
| 129 |
1.1 |
|
| 145 |
1.1 |
|
| 163 |
1.1 |
|
| 184 |
1.1 |