-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
37f2c7a
commit a0e7844
Showing
7 changed files
with
106 additions
and
3 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
back/src/main/kotlin/knu/dong/onedayonebaek/github/controller/GithubWebhookController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package knu.dong.onedayonebaek.github.controller | ||
|
||
import knu.dong.onedayonebaek.common.exception.NotFoundException | ||
import knu.dong.onedayonebaek.github.dto.CommitWebhookRequest | ||
import knu.dong.onedayonebaek.problem.service.ProblemService | ||
import knu.dong.onedayonebaek.user.service.UserService | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.* | ||
|
||
@RestController | ||
@RequestMapping("/github/webhook") | ||
class GithubWebhookController( | ||
private val userService: UserService, | ||
private val problemService: ProblemService | ||
) { | ||
|
||
@PostMapping("/push") | ||
fun push( | ||
@RequestHeader(value = "X-GitHub-Event") githubEvent: String, | ||
@RequestBody request: CommitWebhookRequest | ||
): ResponseEntity<String> { | ||
if (githubEvent == "ping") { | ||
return ResponseEntity.ok().body("valid webhook") | ||
} else if (githubEvent != "push") { | ||
return ResponseEntity.badRequest().body("Only allowed if 'X-Github-Event' is 'push'.") | ||
} | ||
|
||
val loginId: String = request.pusher.name | ||
|
||
val commits: List<CommitWebhookRequest.Commit> = request.commits | ||
val user = userService.findUserByLoginId(loginId).orElseThrow { NotFoundException() } | ||
|
||
problemService.addProblems(user, commits) | ||
|
||
return ResponseEntity.ok().build() | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
back/src/main/kotlin/knu/dong/onedayonebaek/github/dto/CommitWebhookRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package knu.dong.onedayonebaek.github.dto | ||
|
||
import java.time.LocalDateTime | ||
|
||
data class CommitWebhookRequest( | ||
val pusher: Pusher, | ||
val commits: List<Commit> = ArrayList() | ||
) { | ||
data class Commit (val url: String, val message: String, val timestamp: LocalDateTime) | ||
data class Pusher (val name: String, val email: String) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
back/src/main/kotlin/knu/dong/onedayonebaek/user/service/UserService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package knu.dong.onedayonebaek.user.service | ||
|
||
import knu.dong.onedayonebaek.user.domain.User | ||
import knu.dong.onedayonebaek.user.repository.UserRepository | ||
import org.springframework.stereotype.Service | ||
import java.util.* | ||
|
||
@Service | ||
class UserService( | ||
private val userRepository: UserRepository | ||
) { | ||
|
||
fun findUserByLoginId(loginId: String): Optional<User> = userRepository.findByLoginId(loginId) | ||
} |