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..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 @@ -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,10 +16,12 @@ public class CustomOAuth2UserService extends DefaultOAuth2UserService { private final UserRepository userRepository; + private final WebhookService webhookService; @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"); @@ -29,17 +32,22 @@ 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(() -> { - // 새로운 사용자일 경우 생성 - User newUser = User.builder() + // 새로운 사용자일 경우 생성중 + System.out.println("새로운 사용자 발견: " + githubEmail); + User newUser = userRepository.save(User.builder() .githubId(githubEmail) .githubToken(accessToken) .userRole(UserRole.USER) - .build(); - return userRepository.save(newUser); + .build()); + System.out.println("새로운 사용자 등록 완료"); + webhookService.sendDiscordNotification(); + System.out.println("웹훅 호출 완료"); + 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 new file mode 100644 index 0000000..e7da7e0 --- /dev/null +++ b/src/main/java/org/autorepo/server/global/utils/WebhookService.java @@ -0,0 +1,52 @@ +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() { + System.out.println("Discord Webhook 호출중"); + 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); + + 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; + } + +}