From 481ccb964e46d6ec58af1039f5ad59e84dc316f9 Mon Sep 17 00:00:00 2001 From: hysong4u Date: Thu, 5 Dec 2024 23:25:07 +0900 Subject: [PATCH 1/4] =?UTF-8?q?#33=20[chore]=20=ED=9A=8C=EC=9B=90=EA=B0=80?= =?UTF-8?q?=EC=9E=85=20=EC=95=8C=EB=A6=BC=20=EC=9B=B9=ED=9B=85=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../user/service/CustomOAuth2UserService.java | 3 ++ .../server/global/utils/WebhookService.java | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/main/java/org/autorepo/server/global/utils/WebhookService.java diff --git a/src/main/java/org/autorepo/server/domain/user/service/CustomOAuth2UserService.java b/src/main/java/org/autorepo/server/domain/user/service/CustomOAuth2UserService.java index d836c86..7310713 100644 --- a/src/main/java/org/autorepo/server/domain/user/service/CustomOAuth2UserService.java +++ b/src/main/java/org/autorepo/server/domain/user/service/CustomOAuth2UserService.java @@ -4,6 +4,7 @@ import org.autorepo.server.domain.user.entity.User; import org.autorepo.server.domain.user.entity.UserRole; import org.autorepo.server.domain.user.repository.UserRepository; +import org.autorepo.server.global.utils.WebhookService; import org.springframework.security.oauth2.core.user.DefaultOAuth2User; import org.springframework.security.oauth2.core.user.OAuth2User; import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService; @@ -15,6 +16,7 @@ public class CustomOAuth2UserService extends DefaultOAuth2UserService { private final UserRepository userRepository; + private final WebhookService webhookService; @Override public OAuth2User loadUser(OAuth2UserRequest userRequest) { @@ -39,6 +41,7 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) { .githubToken(accessToken) .userRole(UserRole.USER) .build(); + webhookService.sendDiscordNotification(); return userRepository.save(newUser); }); diff --git a/src/main/java/org/autorepo/server/global/utils/WebhookService.java b/src/main/java/org/autorepo/server/global/utils/WebhookService.java new file mode 100644 index 0000000..797f151 --- /dev/null +++ b/src/main/java/org/autorepo/server/global/utils/WebhookService.java @@ -0,0 +1,45 @@ +package org.autorepo.server.global.utils; + +import lombok.RequiredArgsConstructor; +import org.autorepo.server.domain.repo.repository.RepoRepository; +import org.autorepo.server.domain.user.repository.UserRepository; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +import java.util.HashMap; +import java.util.Map; + +@Service +@RequiredArgsConstructor +public class WebhookService { + private final UserRepository userRepository; + private final RepoRepository repoRepository; + + @Value("${discord.webhook.url}") + private String discordWebhookUrl; + + // 회원가입 웹훅 + public String sendDiscordNotification() { + RestTemplate restTemplate = new RestTemplate(); + Long totalMembers = userRepository.count(); + Long totalRepos = repoRepository.count(); + + String message = totalMembers + "번째 AutoRepoCat이 등장했어요!\n(현재 등록 레포 수 : " + totalRepos + "개)"; + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + + Map body = new HashMap<>(); + body.put("content", message); + + HttpEntity> requestEntity = new HttpEntity<>(body, headers); + + restTemplate.postForEntity(discordWebhookUrl, requestEntity, String.class); + return null; + } + +} From e61885a797623a346e3ea8958556fd9162ed4d34 Mon Sep 17 00:00:00 2001 From: hysong4u Date: Thu, 5 Dec 2024 23:58:02 +0900 Subject: [PATCH 2/4] =?UTF-8?q?#33=20[chore]=20=ED=9A=8C=EC=9B=90=EA=B0=80?= =?UTF-8?q?=EC=9E=85=20=EC=95=8C=EB=A6=BC=20=EC=9B=B9=ED=9B=85=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/user/service/CustomOAuth2UserService.java | 6 +++--- .../org/autorepo/server/global/utils/WebhookService.java | 8 +++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/autorepo/server/domain/user/service/CustomOAuth2UserService.java b/src/main/java/org/autorepo/server/domain/user/service/CustomOAuth2UserService.java index 7310713..3c3bcc4 100644 --- a/src/main/java/org/autorepo/server/domain/user/service/CustomOAuth2UserService.java +++ b/src/main/java/org/autorepo/server/domain/user/service/CustomOAuth2UserService.java @@ -36,13 +36,13 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) { return userRepository.save(existingUser); }).orElseGet(() -> { // 새로운 사용자일 경우 생성 - User newUser = User.builder() + User newUser = userRepository.save(User.builder() .githubId(githubEmail) .githubToken(accessToken) .userRole(UserRole.USER) - .build(); + .build()); webhookService.sendDiscordNotification(); - return userRepository.save(newUser); + return newUser; }); return new DefaultOAuth2User(oAuth2User.getAuthorities(), oAuth2User.getAttributes(), "id"); diff --git a/src/main/java/org/autorepo/server/global/utils/WebhookService.java b/src/main/java/org/autorepo/server/global/utils/WebhookService.java index 797f151..f0adf0d 100644 --- a/src/main/java/org/autorepo/server/global/utils/WebhookService.java +++ b/src/main/java/org/autorepo/server/global/utils/WebhookService.java @@ -38,7 +38,13 @@ public String sendDiscordNotification() { HttpEntity> requestEntity = new HttpEntity<>(body, headers); - restTemplate.postForEntity(discordWebhookUrl, requestEntity, String.class); + try { + restTemplate.postForEntity(discordWebhookUrl, requestEntity, String.class); + System.out.println("Discord Webhook 호출 성공"); + } catch (Exception e) { + System.err.println("Discord Webhook 호출 실패: " + e.getMessage()); + e.printStackTrace(); + } return null; } From b0be5bb1ea37208cbce353e80945cf1677a72cbf Mon Sep 17 00:00:00 2001 From: hysong4u Date: Fri, 6 Dec 2024 00:14:29 +0900 Subject: [PATCH 3/4] =?UTF-8?q?#33=20[chore]=20=EB=A1=9C=EA=B7=B8=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/domain/user/service/CustomOAuth2UserService.java | 4 +++- .../java/org/autorepo/server/global/utils/WebhookService.java | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/autorepo/server/domain/user/service/CustomOAuth2UserService.java b/src/main/java/org/autorepo/server/domain/user/service/CustomOAuth2UserService.java index 3c3bcc4..51db8e3 100644 --- a/src/main/java/org/autorepo/server/domain/user/service/CustomOAuth2UserService.java +++ b/src/main/java/org/autorepo/server/domain/user/service/CustomOAuth2UserService.java @@ -35,13 +35,15 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) { existingUser.setGithubToken(accessToken); return userRepository.save(existingUser); }).orElseGet(() -> { - // 새로운 사용자일 경우 생성 + // 새로운 사용자일 경우 생성중 User newUser = userRepository.save(User.builder() .githubId(githubEmail) .githubToken(accessToken) .userRole(UserRole.USER) .build()); + System.out.println("새로운 사용자 등록 완료"); webhookService.sendDiscordNotification(); + System.out.println("웹훅 호출 완료"); return newUser; }); diff --git a/src/main/java/org/autorepo/server/global/utils/WebhookService.java b/src/main/java/org/autorepo/server/global/utils/WebhookService.java index f0adf0d..e7da7e0 100644 --- a/src/main/java/org/autorepo/server/global/utils/WebhookService.java +++ b/src/main/java/org/autorepo/server/global/utils/WebhookService.java @@ -24,6 +24,7 @@ public class WebhookService { // 회원가입 웹훅 public String sendDiscordNotification() { + System.out.println("Discord Webhook 호출중"); RestTemplate restTemplate = new RestTemplate(); Long totalMembers = userRepository.count(); Long totalRepos = repoRepository.count(); From 29ab0ad2419523301f522956526e4b5fd506446f Mon Sep 17 00:00:00 2001 From: hysong4u Date: Fri, 6 Dec 2024 02:07:58 +0900 Subject: [PATCH 4/4] =?UTF-8?q?#33=20[chore]=20=EB=A1=9C=EA=B7=B8=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/domain/user/service/CustomOAuth2UserService.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/org/autorepo/server/domain/user/service/CustomOAuth2UserService.java b/src/main/java/org/autorepo/server/domain/user/service/CustomOAuth2UserService.java index 51db8e3..c4df744 100644 --- a/src/main/java/org/autorepo/server/domain/user/service/CustomOAuth2UserService.java +++ b/src/main/java/org/autorepo/server/domain/user/service/CustomOAuth2UserService.java @@ -21,6 +21,7 @@ public class CustomOAuth2UserService extends DefaultOAuth2UserService { @Override public OAuth2User loadUser(OAuth2UserRequest userRequest) { OAuth2User oAuth2User = super.loadUser(userRequest); + System.out.println("CustomOAuth2UserService - loadUser 호출됨"); // GitHub의 email 필드를 가져오되, null이면 login으로 대체 String email = oAuth2User.getAttribute("email"); @@ -31,11 +32,13 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) { // 사용자 찾기 또는 생성 및 업데이트 User user = userRepository.findByGithubId(githubEmail).map(existingUser -> { + System.out.println("기존 사용자 발견: " + existingUser.getGithubId()); // 기존 사용자일 경우 토큰 업데이트 existingUser.setGithubToken(accessToken); return userRepository.save(existingUser); }).orElseGet(() -> { // 새로운 사용자일 경우 생성중 + System.out.println("새로운 사용자 발견: " + githubEmail); User newUser = userRepository.save(User.builder() .githubId(githubEmail) .githubToken(accessToken)