Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
bbabbi committed Dec 6, 2024
2 parents 07805ae + 29ab0ad commit 19f6f9d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -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");
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/org/autorepo/server/global/utils/WebhookService.java
Original file line number Diff line number Diff line change
@@ -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<String, String> body = new HashMap<>();
body.put("content", message);

HttpEntity<Map<String, String>> 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;
}

}

0 comments on commit 19f6f9d

Please sign in to comment.