| 1 | package edu.ucsb.cs156.frontiers.controllers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.frontiers.errors.EntityNotFoundException; | |
| 4 | import edu.ucsb.cs156.frontiers.jobs.AddTeamMemberToGithubJob; | |
| 5 | import edu.ucsb.cs156.frontiers.jobs.AddTeamToGithubJob; | |
| 6 | import edu.ucsb.cs156.frontiers.jobs.DeleteTeamFromGithubJob; | |
| 7 | import edu.ucsb.cs156.frontiers.jobs.DeleteTeamMemberFromGithubJob; | |
| 8 | import edu.ucsb.cs156.frontiers.jobs.MembershipAuditJob; | |
| 9 | import edu.ucsb.cs156.frontiers.jobs.PullTeamsFromGithubJob; | |
| 10 | import edu.ucsb.cs156.frontiers.jobs.PushTeamsToGithubJob; | |
| 11 | import edu.ucsb.cs156.frontiers.jobs.UpdateAllJob; | |
| 12 | import edu.ucsb.cs156.frontiers.models.JobLogTail; | |
| 13 | import edu.ucsb.cs156.frontiers.repositories.CourseRepository; | |
| 14 | import edu.ucsb.cs156.frontiers.repositories.CourseStaffRepository; | |
| 15 | import edu.ucsb.cs156.frontiers.repositories.RosterStudentRepository; | |
| 16 | import edu.ucsb.cs156.frontiers.repositories.TeamMemberRepository; | |
| 17 | import edu.ucsb.cs156.frontiers.repositories.TeamRepository; | |
| 18 | import edu.ucsb.cs156.frontiers.services.GithubTeamService; | |
| 19 | import edu.ucsb.cs156.frontiers.services.OrganizationMemberService; | |
| 20 | import edu.ucsb.cs156.frontiers.services.UpdateUserService; | |
| 21 | import edu.ucsb.cs156.jobs.entities.Job; | |
| 22 | import edu.ucsb.cs156.jobs.repositories.JobsRepository; | |
| 23 | import edu.ucsb.cs156.jobs.services.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 lombok.extern.slf4j.Slf4j; | |
| 28 | import org.springframework.beans.factory.annotation.Autowired; | |
| 29 | import org.springframework.security.access.prepost.PreAuthorize; | |
| 30 | import org.springframework.web.bind.annotation.GetMapping; | |
| 31 | import org.springframework.web.bind.annotation.PostMapping; | |
| 32 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 33 | import org.springframework.web.bind.annotation.RequestParam; | |
| 34 | import org.springframework.web.bind.annotation.RestController; | |
| 35 | ||
| 36 | /** | |
| 37 | * App-level job endpoints: launching this app's concrete jobs, plus the course-scoped jobs listing | |
| 38 | * and course-scoped full-log endpoint (which use this app's CourseSecurity rule). The generic admin | |
| 39 | * endpoints (list all / paginated / logs / delete) come from the lib-jobs library's own controller. | |
| 40 | */ | |
| 41 | @Tag(name = "Jobs") | |
| 42 | @RequestMapping("/api/jobs") | |
| 43 | @RestController | |
| 44 | @Slf4j | |
| 45 | public class JobsController extends ApiController { | |
| 46 | @Autowired private JobsRepository jobsRepository; | |
| 47 | ||
| 48 | @Autowired private JobService jobService; | |
| 49 | ||
| 50 | @Autowired private UpdateUserService updateUserService; | |
| 51 | ||
| 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 = "Launch UpdateAll job") | |
| 61 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 62 | @PostMapping("/launch/updateAll") | |
| 63 | public Job launchUpdateAllJob() { | |
| 64 | ||
| 65 | UpdateAllJob job = UpdateAllJob.builder().updateUserService(updateUserService).build(); | |
| 66 |
1
1. launchUpdateAllJob : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/JobsController::launchUpdateAllJob → KILLED |
return jobService.runAsJob(job); |
| 67 | } | |
| 68 | ||
| 69 | @Operation(summary = "Launch Audit All Courses Job") | |
| 70 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 71 | @PostMapping("/launch/auditAllCourses") | |
| 72 | public Job launchAuditAllCoursesJob() { | |
| 73 | ||
| 74 | MembershipAuditJob job = | |
| 75 | MembershipAuditJob.builder() | |
| 76 | .rosterStudentRepository(rosterStudentRepository) | |
| 77 | .courseRepository(courseRepository) | |
| 78 | .organizationMemberService(organizationMemberService) | |
| 79 | .courseStaffRepository(courseStaffRepository) | |
| 80 | .build(); | |
| 81 |
1
1. launchAuditAllCoursesJob : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/JobsController::launchAuditAllCoursesJob → KILLED |
return jobService.runAsJob(job); |
| 82 | } | |
| 83 | ||
| 84 | @Operation(summary = "Launch Push Teams to GitHub Job") | |
| 85 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 86 | @PostMapping("/launch/pushTeamsToGithub") | |
| 87 | public Job launchPushTeamsToGithubJob(@Parameter(name = "courseId") @RequestParam Long courseId) { | |
| 88 | ||
| 89 | PushTeamsToGithubJob job = | |
| 90 | PushTeamsToGithubJob.builder() | |
| 91 | .courseId(courseId) | |
| 92 | .courseRepository(courseRepository) | |
| 93 | .teamRepository(teamRepository) | |
| 94 | .teamMemberRepository(teamMemberRepository) | |
| 95 | .githubTeamService(githubTeamService) | |
| 96 | .build(); | |
| 97 |
1
1. launchPushTeamsToGithubJob : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/JobsController::launchPushTeamsToGithubJob → KILLED |
return jobService.runAsJob(job); |
| 98 | } | |
| 99 | ||
| 100 | @Operation(summary = "Launch Pull Teams from GitHub Job") | |
| 101 | @PreAuthorize("hasRole('ROLE_ADMIN')") | |
| 102 | @PostMapping("/launch/pullTeamsFromGithub") | |
| 103 | public Job launchPullTeamsFromGithubJob( | |
| 104 | @Parameter(name = "courseId") @RequestParam Long courseId) { | |
| 105 | ||
| 106 | PullTeamsFromGithubJob job = | |
| 107 | PullTeamsFromGithubJob.builder() | |
| 108 | .courseId(courseId) | |
| 109 | .courseRepository(courseRepository) | |
| 110 | .teamRepository(teamRepository) | |
| 111 | .teamMemberRepository(teamMemberRepository) | |
| 112 | .githubTeamService(githubTeamService) | |
| 113 | .build(); | |
| 114 |
1
1. launchPullTeamsFromGithubJob : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/JobsController::launchPullTeamsFromGithubJob → KILLED |
return jobService.runAsJob(job); |
| 115 | } | |
| 116 | ||
| 117 | @Operation(summary = "Launch Delete Team Member From GitHub Job") | |
| 118 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 119 | @PostMapping("/launch/deleteTeamMemberFromGithub") | |
| 120 | public Job launchDeleteTeamMemberFromGithubJob( | |
| 121 | @Parameter(name = "memberGithubLogin") @RequestParam String memberGithubLogin, | |
| 122 | @Parameter(name = "githubTeamId") @RequestParam Integer githubTeamId, | |
| 123 | @Parameter(name = "courseId") @RequestParam Long courseId) { | |
| 124 | ||
| 125 | DeleteTeamMemberFromGithubJob job = | |
| 126 | DeleteTeamMemberFromGithubJob.builder() | |
| 127 | .memberGithubLogin(memberGithubLogin) | |
| 128 | .githubTeamId(githubTeamId) | |
| 129 | .course(courseRepository.findById(courseId).get()) | |
| 130 | .githubTeamService(githubTeamService) | |
| 131 | .build(); | |
| 132 |
1
1. launchDeleteTeamMemberFromGithubJob : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/JobsController::launchDeleteTeamMemberFromGithubJob → KILLED |
return jobService.runAsJob(job); |
| 133 | } | |
| 134 | ||
| 135 | @Operation(summary = "Launch Add Team Member To GitHub Job") | |
| 136 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 137 | @PostMapping("/launch/addTeamMemberToGithub") | |
| 138 | public Job launchAddTeamMemberToGithubJob( | |
| 139 | @Parameter(name = "memberGithubLogin") @RequestParam String memberGithubLogin, | |
| 140 | @Parameter(name = "githubTeamId") @RequestParam Integer githubTeamId, | |
| 141 | @Parameter(name = "teamMemberId") @RequestParam Long teamMemberId, | |
| 142 | @Parameter(name = "courseId") @RequestParam Long courseId) { | |
| 143 | ||
| 144 | AddTeamMemberToGithubJob job = | |
| 145 | AddTeamMemberToGithubJob.builder() | |
| 146 | .memberGithubLogin(memberGithubLogin) | |
| 147 | .githubTeamId(githubTeamId) | |
| 148 | .teamMemberId(teamMemberId) | |
| 149 | .course(courseRepository.findById(courseId).get()) | |
| 150 | .githubTeamService(githubTeamService) | |
| 151 | .teamMemberRepository(teamMemberRepository) | |
| 152 | .build(); | |
| 153 |
1
1. launchAddTeamMemberToGithubJob : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/JobsController::launchAddTeamMemberToGithubJob → KILLED |
return jobService.runAsJob(job); |
| 154 | } | |
| 155 | ||
| 156 | @Operation(summary = "Launch Delete Team From GitHub Job") | |
| 157 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 158 | @PostMapping("/launch/deleteTeamFromGithub") | |
| 159 | public Job launchDeleteTeamFromGithubJob( | |
| 160 | @Parameter(name = "githubTeamId") @RequestParam Integer githubTeamId, | |
| 161 | @Parameter(name = "courseId") @RequestParam Long courseId) { | |
| 162 | ||
| 163 | DeleteTeamFromGithubJob job = | |
| 164 | DeleteTeamFromGithubJob.builder() | |
| 165 | .githubTeamId(githubTeamId) | |
| 166 | .course(courseRepository.findById(courseId).get()) | |
| 167 | .githubTeamService(githubTeamService) | |
| 168 | .build(); | |
| 169 |
1
1. launchDeleteTeamFromGithubJob : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/JobsController::launchDeleteTeamFromGithubJob → KILLED |
return jobService.runAsJob(job); |
| 170 | } | |
| 171 | ||
| 172 | @Operation(summary = "Launch Add Team To GitHub Job") | |
| 173 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 174 | @PostMapping("/launch/addTeamToGithub") | |
| 175 | public Job launchAddTeamToGithubJob( | |
| 176 | @Parameter(name = "teamName") @RequestParam String teamName, | |
| 177 | @Parameter(name = "courseId") @RequestParam Long courseId) { | |
| 178 | ||
| 179 | AddTeamToGithubJob job = | |
| 180 | AddTeamToGithubJob.builder() | |
| 181 | .teamName(teamName) | |
| 182 | .course(courseRepository.findById(courseId).get()) | |
| 183 | .teamRepository(teamRepository) | |
| 184 | .githubTeamService(githubTeamService) | |
| 185 | .build(); | |
| 186 |
1
1. launchAddTeamToGithubJob : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/JobsController::launchAddTeamToGithubJob → KILLED |
return jobService.runAsJob(job); |
| 187 | } | |
| 188 | ||
| 189 | @Operation(summary = "List jobs by courseId") | |
| 190 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 191 | @GetMapping("/course") | |
| 192 | public Iterable<Job> jobsByCourse(@Parameter(name = "courseId") @RequestParam Long courseId) { | |
| 193 | Iterable<Job> jobs = jobsRepository.findByScopeTypeAndScopeIdOrderByIdDesc("course", courseId); | |
| 194 | /* | |
| 195 | * Since lib-jobs v0.2.0, Job.log is @Transient (see job_logs) and is no | |
| 196 | * longer populated automatically by JPA; callers that return Job entities | |
| 197 | * directly must set it explicitly, same as the library's own controller | |
| 198 | * does for /all and /paginated. | |
| 199 | */ | |
| 200 |
2
1. lambda$jobsByCourse$0 : removed call to edu/ucsb/cs156/jobs/entities/Job::setLog → KILLED 2. jobsByCourse : removed call to java/lang/Iterable::forEach → KILLED |
jobs.forEach(job -> job.setLog(jobService.getJobLogPreview(job.getId()))); |
| 201 |
1
1. jobsByCourse : replaced return value with Collections.emptyList for edu/ucsb/cs156/frontiers/controllers/JobsController::jobsByCourse → KILLED |
return jobs; |
| 202 | } | |
| 203 | ||
| 204 | /** | |
| 205 | * Returns the full log of one job that belongs to the given course. | |
| 206 | * | |
| 207 | * <p>Why this app-owned endpoint exists: the library's own {@code GET /api/jobs/logs/{id}} | |
| 208 | * returns the full log but is restricted to {@code ROLE_ADMIN}. The per-course jobs tab is used | |
| 209 | * by course instructors and staff who are not necessarily admins, and the course-scoped listing | |
| 210 | * above only populates {@code Job.log} with a short tail preview ({@code | |
| 211 | * JobService.getJobLogPreview}). This endpoint gives those users the same full log, guarded by | |
| 212 | * the same course-permission rule as the listing. | |
| 213 | * | |
| 214 | * <p>A job that exists but is not scoped to the given course is deliberately reported as not | |
| 215 | * found (404) rather than forbidden (403), so that job ids do not leak across courses. | |
| 216 | * | |
| 217 | * @param courseId the id of the course the job must belong to | |
| 218 | * @param jobId the id of the job | |
| 219 | * @return the full job log, oldest line first | |
| 220 | */ | |
| 221 | @Operation(summary = "Get the full log of one job belonging to a course") | |
| 222 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 223 | @GetMapping("/course/logs") | |
| 224 | public String jobLogsByCourse( | |
| 225 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
| 226 | @Parameter(name = "jobId") @RequestParam Long jobId) { | |
| 227 | findJobInCourse(courseId, jobId); | |
| 228 |
1
1. jobLogsByCourse : replaced return value with "" for edu/ucsb/cs156/frontiers/controllers/JobsController::jobLogsByCourse → KILLED |
return jobService.getJobLogs(jobId); |
| 229 | } | |
| 230 | ||
| 231 | /** | |
| 232 | * Returns the log lines of one job that belong to the given course that were written after the | |
| 233 | * line with id {@code afterId}, together with the job's status: one poll of a "tail -f" style | |
| 234 | * view. The client keeps the id of the last line it has received and passes it as {@code afterId} | |
| 235 | * on its next poll, and stops polling once the status is one of a job that has finished. | |
| 236 | * | |
| 237 | * <p>This is the course-scoped counterpart of the library's admin-only {@code GET | |
| 238 | * /api/jobs/logs/{id}/tail}, for the same reason as {@link #jobLogsByCourse}. Like that endpoint | |
| 239 | * it reports a job of another course as not found. | |
| 240 | * | |
| 241 | * <p>The status is read <em>before</em> the lines. If the job has already finished when its | |
| 242 | * status is read, all of its lines have been written by the time they are read, so a client that | |
| 243 | * stops polling on a finished status has not missed any. Reading them the other way round could | |
| 244 | * miss the last lines of a job that finishes in between. | |
| 245 | * | |
| 246 | * @param courseId the id of the course the job must belong to | |
| 247 | * @param jobId the id of the job | |
| 248 | * @param afterId only lines with an id greater than this are returned; 0 for the whole log | |
| 249 | * @return the status of the job and its new log lines, oldest first | |
| 250 | */ | |
| 251 | @Operation(summary = "Get the new log lines and the status of one job belonging to a course") | |
| 252 | @PreAuthorize("@CourseSecurity.hasManagePermissions(#root, #courseId)") | |
| 253 | @GetMapping("/course/logs/tail") | |
| 254 | public JobLogTail jobLogTailByCourse( | |
| 255 | @Parameter(name = "courseId") @RequestParam Long courseId, | |
| 256 | @Parameter(name = "jobId") @RequestParam Long jobId, | |
| 257 | @Parameter(name = "afterId", description = "only return lines with id greater than this") | |
| 258 | @RequestParam(defaultValue = "0") | |
| 259 | Long afterId) { | |
| 260 | String status = findJobInCourse(courseId, jobId).getStatus(); | |
| 261 |
1
1. jobLogTailByCourse : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/JobsController::jobLogTailByCourse → KILLED |
return new JobLogTail(status, jobService.getJobLogTail(jobId, afterId)); |
| 262 | } | |
| 263 | ||
| 264 | /** | |
| 265 | * Finds a job, which must be a course-scoped job of the given course. | |
| 266 | * | |
| 267 | * @throws EntityNotFoundException if there is no such job, or it belongs to something else | |
| 268 | */ | |
| 269 | private Job findJobInCourse(Long courseId, Long jobId) { | |
| 270 | Job job = | |
| 271 | jobsRepository | |
| 272 | .findById(jobId) | |
| 273 |
1
1. lambda$findJobInCourse$0 : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/JobsController::lambda$findJobInCourse$0 → KILLED |
.orElseThrow(() -> new EntityNotFoundException(Job.class, jobId)); |
| 274 |
2
1. findJobInCourse : negated conditional → KILLED 2. findJobInCourse : negated conditional → KILLED |
if (!"course".equals(job.getScopeType()) || !courseId.equals(job.getScopeId())) { |
| 275 | throw new EntityNotFoundException(Job.class, jobId); | |
| 276 | } | |
| 277 |
1
1. findJobInCourse : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/JobsController::findJobInCourse → KILLED |
return job; |
| 278 | } | |
| 279 | } | |
Mutations | ||
| 66 |
1.1 |
|
| 81 |
1.1 |
|
| 97 |
1.1 |
|
| 114 |
1.1 |
|
| 132 |
1.1 |
|
| 153 |
1.1 |
|
| 169 |
1.1 |
|
| 186 |
1.1 |
|
| 200 |
1.1 2.2 |
|
| 201 |
1.1 |
|
| 228 |
1.1 |
|
| 261 |
1.1 |
|
| 273 |
1.1 |
|
| 274 |
1.1 2.2 |
|
| 277 |
1.1 |