| 1 | package edu.ucsb.cs156.frontiers.controllers; | |
| 2 | ||
| 3 | ||
| 4 | import com.fasterxml.jackson.core.JsonProcessingException; | |
| 5 | import com.fasterxml.jackson.databind.JsonNode; | |
| 6 | import edu.ucsb.cs156.frontiers.entities.Course; | |
| 7 | import edu.ucsb.cs156.frontiers.entities.RosterStudent; | |
| 8 | import edu.ucsb.cs156.frontiers.entities.User; | |
| 9 | import edu.ucsb.cs156.frontiers.enums.OrgStatus; | |
| 10 | import edu.ucsb.cs156.frontiers.repositories.CourseRepository; | |
| 11 | import edu.ucsb.cs156.frontiers.repositories.RosterStudentRepository; | |
| 12 | import edu.ucsb.cs156.frontiers.repositories.UserRepository; | |
| 13 | import io.swagger.v3.oas.annotations.tags.Tag; | |
| 14 | import org.springframework.http.ResponseEntity; | |
| 15 | import org.springframework.web.bind.annotation.PostMapping; | |
| 16 | import org.springframework.web.bind.annotation.RequestBody; | |
| 17 | import org.springframework.web.bind.annotation.RequestMapping; | |
| 18 | import org.springframework.web.bind.annotation.RestController; | |
| 19 | ||
| 20 | import java.util.Optional; | |
| 21 | ||
| 22 | @Tag(name = "Webhooks Controller") | |
| 23 | @RestController | |
| 24 | @RequestMapping("/api/webhooks") | |
| 25 | public class WebhookController { | |
| 26 | ||
| 27 | ||
| 28 |     private final CourseRepository courseRepository; | |
| 29 |     private final RosterStudentRepository rosterStudentRepository; | |
| 30 | ||
| 31 |     public WebhookController(CourseRepository courseRepository, RosterStudentRepository rosterStudentRepository) { | |
| 32 |         this.courseRepository = courseRepository; | |
| 33 |         this.rosterStudentRepository = rosterStudentRepository; | |
| 34 |     } | |
| 35 | ||
| 36 |     /** | |
| 37 |     * Accepts webhooks from GitHub, currently to update the membership status of a RosterStudent. | |
| 38 |     * @param jsonBody body of the webhook. The description of the currently used webhook is available in docs/webhooks.md | |
| 39 |     * | |
| 40 |     * @return either the word success so GitHub will not flag the webhook as a failure, or the updated RosterStudent | |
| 41 |     */ | |
| 42 |     @PostMapping("/github") | |
| 43 |     public ResponseEntity<String> createGitHubWebhook(@RequestBody JsonNode jsonBody) throws JsonProcessingException { | |
| 44 | ||
| 45 | 
1
1. createGitHubWebhook : negated conditional → KILLED | 
        if(jsonBody.has("action")){ | 
| 46 | 
1
1. createGitHubWebhook : negated conditional → KILLED | 
            if(jsonBody.get("action").asText().equals("member_added")){ | 
| 47 |                 String githubLogin = jsonBody.get("membership").get("user").get("login").asText(); | |
| 48 |                 String installationId = jsonBody.get("installation").get("id").asText(); | |
| 49 |                 Optional<Course> course = courseRepository.findByInstallationId(installationId); | |
| 50 | 
1
1. createGitHubWebhook : negated conditional → KILLED | 
                if(course.isPresent()){ | 
| 51 |                     Optional<RosterStudent> student = rosterStudentRepository.findByCourseAndGithubLogin(course.get(), githubLogin); | |
| 52 | 
1
1. createGitHubWebhook : negated conditional → KILLED | 
                    if(student.isPresent()){ | 
| 53 |                         RosterStudent updatedStudent = student.get(); | |
| 54 | 
1
1. createGitHubWebhook : removed call to edu/ucsb/cs156/frontiers/entities/RosterStudent::setOrgStatus → KILLED | 
                        updatedStudent.setOrgStatus(OrgStatus.MEMBER); | 
| 55 |                         rosterStudentRepository.save(updatedStudent); | |
| 56 | 
1
1. createGitHubWebhook : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/WebhookController::createGitHubWebhook → KILLED | 
                        return ResponseEntity.ok(updatedStudent.toString()); | 
| 57 |                     } | |
| 58 |                 } | |
| 59 |             } | |
| 60 |         } | |
| 61 | 
1
1. createGitHubWebhook : replaced return value with null for edu/ucsb/cs156/frontiers/controllers/WebhookController::createGitHubWebhook → KILLED | 
        return  ResponseEntity.ok().body("success"); | 
| 62 |     } | |
| 63 | } | |
Mutations | ||
| 45 | 
 
 1.1  | 
|
| 46 | 
 
 1.1  | 
|
| 50 | 
 
 1.1  | 
|
| 52 | 
 
 1.1  | 
|
| 54 | 
 
 1.1  | 
|
| 56 | 
 
 1.1  | 
|
| 61 | 
 
 1.1  |