From 0571923d608dcdd4a6fba196faf90a8b3280036f Mon Sep 17 00:00:00 2001 From: 5jisoo <56earls@gmail.com> Date: Thu, 22 Feb 2024 14:53:08 +0900 Subject: [PATCH 1/2] =?UTF-8?q?#12=20Feat:=20=EA=B9=83=ED=97=88=EB=B8=8C?= =?UTF-8?q?=20=EC=95=84=EC=9D=B4=EB=94=94=20=EB=93=B1=EB=A1=9D=20=EB=AA=85?= =?UTF-8?q?=EB=A0=B9=EC=96=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../slasckbot/service/SlackbotService.java | 29 ++++++++++++--- .../team/controller/TeamController.java | 1 - .../issuetree/team/service/TeamService.java | 6 +-- .../user/controller/UserController.java | 29 +++++++++++++++ .../com/wltt/issuetree/user/domain/User.java | 37 +++++++++++++++++++ .../domain/repository/UserRepository.java | 7 ++++ .../issuetree/user/parser/UserParser.java | 16 ++++++++ .../issuetree/user/service/UserService.java | 36 ++++++++++++++++++ 8 files changed, 151 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/wltt/issuetree/user/controller/UserController.java create mode 100644 src/main/java/com/wltt/issuetree/user/domain/User.java create mode 100644 src/main/java/com/wltt/issuetree/user/domain/repository/UserRepository.java create mode 100644 src/main/java/com/wltt/issuetree/user/parser/UserParser.java create mode 100644 src/main/java/com/wltt/issuetree/user/service/UserService.java diff --git a/src/main/java/com/wltt/issuetree/global/slasckbot/service/SlackbotService.java b/src/main/java/com/wltt/issuetree/global/slasckbot/service/SlackbotService.java index 49b832b..5e9052e 100644 --- a/src/main/java/com/wltt/issuetree/global/slasckbot/service/SlackbotService.java +++ b/src/main/java/com/wltt/issuetree/global/slasckbot/service/SlackbotService.java @@ -9,15 +9,11 @@ import com.wltt.issuetree.global.apipayload.exception.GeneralException; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import java.io.IOException; import java.io.IOException; import java.util.List; import static com.slack.api.model.block.Blocks.*; -import static com.slack.api.model.block.Blocks.section; import static com.slack.api.model.block.composition.BlockCompositions.markdownText; import static com.slack.api.model.block.composition.BlockCompositions.plainText; @@ -25,7 +21,7 @@ @RequiredArgsConstructor public class SlackbotService { private final MethodsClient methodsClient; - + public void chatMessage( final String text, final String headerContent, @@ -48,4 +44,27 @@ public void chatMessage( throw new GeneralException(ErrorStatus._INTERNAL_SERVER_ERROR); } } + + public void chatMessage( + final List textObjectList, + final String headerContent, + final String channelNameOrId + ) { + ChatPostMessageRequest messageRequest = ChatPostMessageRequest.builder() + .channel(channelNameOrId) + .blocks(asBlocks( + header( + header -> header.text(plainText(headerContent)) + ), + divider(), + section(section -> section.fields(textObjectList)) + ) + ) + .build(); + try { + methodsClient.chatPostMessage(messageRequest); + } catch (SlackApiException | IOException e) { + throw new GeneralException(ErrorStatus._INTERNAL_SERVER_ERROR); + } + } } diff --git a/src/main/java/com/wltt/issuetree/team/controller/TeamController.java b/src/main/java/com/wltt/issuetree/team/controller/TeamController.java index 4a97872..c9aba74 100644 --- a/src/main/java/com/wltt/issuetree/team/controller/TeamController.java +++ b/src/main/java/com/wltt/issuetree/team/controller/TeamController.java @@ -24,7 +24,6 @@ public class TeamController { public void addTeams( @RequestBody final MultiValueMap data ) { - System.out.println(data); teamService.addTeam(new SlackUrlEncodedForm(data)); } } diff --git a/src/main/java/com/wltt/issuetree/team/service/TeamService.java b/src/main/java/com/wltt/issuetree/team/service/TeamService.java index 45a857e..d1d2466 100644 --- a/src/main/java/com/wltt/issuetree/team/service/TeamService.java +++ b/src/main/java/com/wltt/issuetree/team/service/TeamService.java @@ -1,6 +1,5 @@ package com.wltt.issuetree.team.service; -import com.slack.api.methods.MethodsClient; import com.slack.api.model.block.composition.TextObject; import com.wltt.issuetree.global.SlackUrlEncodedForm; import com.wltt.issuetree.global.slasckbot.service.SlackbotService; @@ -19,7 +18,6 @@ @RequiredArgsConstructor public class TeamService { private final TeamRepository teamRepository; - private final MethodsClient methodsClient; private final SlackbotService slackbotService; public void addTeam(SlackUrlEncodedForm request) { @@ -47,7 +45,7 @@ private void chatSuccessMessage( String header = "정상적으로 역할이 지정되었습니다."; slackbotService.chatMessage( - textObjects.toString(), + textObjects, header, request.getChannelName() ); @@ -55,7 +53,7 @@ private void chatSuccessMessage( header = "새로운 역할이 지정되었습니다."; slackbotService.chatMessage( - textObjects.toString(), + textObjects, header, team.getChannelId() ); diff --git a/src/main/java/com/wltt/issuetree/user/controller/UserController.java b/src/main/java/com/wltt/issuetree/user/controller/UserController.java new file mode 100644 index 0000000..319d9a8 --- /dev/null +++ b/src/main/java/com/wltt/issuetree/user/controller/UserController.java @@ -0,0 +1,29 @@ +package com.wltt.issuetree.user.controller; + +import com.wltt.issuetree.global.SlackUrlEncodedForm; +import com.wltt.issuetree.user.service.UserService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.MediaType; +import org.springframework.util.MultiValueMap; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/v1/users") +public class UserController { + private final UserService userService; + + @PostMapping( + value = "", + consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, + produces = MediaType.APPLICATION_JSON_VALUE + ) + public void addUser( + @RequestBody final MultiValueMap data + ) { + userService.addUser(new SlackUrlEncodedForm(data)); + } +} diff --git a/src/main/java/com/wltt/issuetree/user/domain/User.java b/src/main/java/com/wltt/issuetree/user/domain/User.java new file mode 100644 index 0000000..1114524 --- /dev/null +++ b/src/main/java/com/wltt/issuetree/user/domain/User.java @@ -0,0 +1,37 @@ +package com.wltt.issuetree.user.domain; + +import com.wltt.issuetree.global.BaseEntity; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.springframework.data.annotation.Id; +import org.springframework.data.elasticsearch.annotations.Document; +import org.springframework.data.elasticsearch.annotations.Field; +import org.springframework.data.elasticsearch.annotations.FieldType; + +import static lombok.AccessLevel.PROTECTED; + +@Document(indexName = "users") +@NoArgsConstructor(access = PROTECTED) +@Getter +public class User extends BaseEntity { + @Id + private String id; + + @Field(type = FieldType.Keyword) + private String githubId; + + @Field(type = FieldType.Keyword) + private String slackId; + + @Override + public boolean isNew() { + return id == null || (getCreatedDate() == null); + } + + @Builder(builderMethodName = "of") + public User(String githubId, String slackId) { + this.githubId = githubId; + this.slackId = slackId; + } +} diff --git a/src/main/java/com/wltt/issuetree/user/domain/repository/UserRepository.java b/src/main/java/com/wltt/issuetree/user/domain/repository/UserRepository.java new file mode 100644 index 0000000..5968d75 --- /dev/null +++ b/src/main/java/com/wltt/issuetree/user/domain/repository/UserRepository.java @@ -0,0 +1,7 @@ +package com.wltt.issuetree.user.domain.repository; + +import com.wltt.issuetree.global.elasticsearch.repository.ElasticsearchRepository; +import com.wltt.issuetree.user.domain.User; + +public interface UserRepository extends ElasticsearchRepository { +} diff --git a/src/main/java/com/wltt/issuetree/user/parser/UserParser.java b/src/main/java/com/wltt/issuetree/user/parser/UserParser.java new file mode 100644 index 0000000..951f259 --- /dev/null +++ b/src/main/java/com/wltt/issuetree/user/parser/UserParser.java @@ -0,0 +1,16 @@ +package com.wltt.issuetree.user.parser; + +import com.wltt.issuetree.global.SlackUrlEncodedForm; +import com.wltt.issuetree.user.domain.User; + +public class UserParser { + public static User parseToUser(final SlackUrlEncodedForm request) { + final String userId = request.getUserId(); + final String githubId = request.getText(); + + return User.of() + .slackId(userId) + .githubId(githubId) + .build(); + } +} diff --git a/src/main/java/com/wltt/issuetree/user/service/UserService.java b/src/main/java/com/wltt/issuetree/user/service/UserService.java new file mode 100644 index 0000000..3cd3be9 --- /dev/null +++ b/src/main/java/com/wltt/issuetree/user/service/UserService.java @@ -0,0 +1,36 @@ +package com.wltt.issuetree.user.service; + +import com.wltt.issuetree.global.SlackUrlEncodedForm; +import com.wltt.issuetree.global.slasckbot.service.SlackbotService; +import com.wltt.issuetree.user.domain.User; +import com.wltt.issuetree.user.domain.repository.UserRepository; +import com.wltt.issuetree.user.parser.UserParser; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +public class UserService { + private final UserRepository userRepository; + private final SlackbotService slackbotService; + + @Transactional + public void addUser(SlackUrlEncodedForm slackUrlEncodedForm) { + final User user = UserParser.parseToUser(slackUrlEncodedForm); + userRepository.save(user); + + final String header = "정상적으로 깃허브 아이디가 등록되었습니다."; + String content + = "*요청자:*\n" + + ">" + "<@" + slackUrlEncodedForm.getUserId() + ">\n" + + "*저장된 깃허브 아이디:*\n" + + ">" + slackUrlEncodedForm.getText() + "\n"; + + slackbotService.chatMessage( + content, + header, + slackUrlEncodedForm.getChannelId() + ); + } +} From 54beb45a01d744ed76579113809d24ae450b05af Mon Sep 17 00:00:00 2001 From: 5jisoo <56earls@gmail.com> Date: Thu, 22 Feb 2024 16:01:16 +0900 Subject: [PATCH 2/2] =?UTF-8?q?#12=20Feat:=20=EB=A6=AC=ED=8F=AC=ED=8A=B8?= =?UTF-8?q?=20=EC=A0=80=EC=9E=A5=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/slasckbot/service/SlackbotService.java | 13 +++++++++---- .../report/request/ReportCreationRequest.java | 1 + .../issuetree/report/service/ReportService.java | 13 +++++++++++-- .../user/domain/repository/UserRepository.java | 3 +++ .../wltt/issuetree/user/service/UserService.java | 7 +++---- 5 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/wltt/issuetree/global/slasckbot/service/SlackbotService.java b/src/main/java/com/wltt/issuetree/global/slasckbot/service/SlackbotService.java index 5e9052e..a82a3cb 100644 --- a/src/main/java/com/wltt/issuetree/global/slasckbot/service/SlackbotService.java +++ b/src/main/java/com/wltt/issuetree/global/slasckbot/service/SlackbotService.java @@ -8,7 +8,9 @@ import com.wltt.issuetree.global.apipayload.code.status.ErrorStatus; import com.wltt.issuetree.global.apipayload.exception.GeneralException; import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; import java.io.IOException; import java.util.List; @@ -22,13 +24,16 @@ public class SlackbotService { private final MethodsClient methodsClient; + @Value("${slack.token}") + private String slackToken; + public void chatMessage( final String text, final String headerContent, - final String channelNameOrId + final String channelNameOrIdOrUserId ) { ChatPostMessageRequest messageRequest = ChatPostMessageRequest.builder() - .channel(channelNameOrId) + .channel(channelNameOrIdOrUserId) .blocks(asBlocks( header( header -> header.text(plainText(headerContent)) @@ -48,10 +53,10 @@ public void chatMessage( public void chatMessage( final List textObjectList, final String headerContent, - final String channelNameOrId + final String channelNameOrIdOrUserId ) { ChatPostMessageRequest messageRequest = ChatPostMessageRequest.builder() - .channel(channelNameOrId) + .channel(channelNameOrIdOrUserId) .blocks(asBlocks( header( header -> header.text(plainText(headerContent)) diff --git a/src/main/java/com/wltt/issuetree/report/request/ReportCreationRequest.java b/src/main/java/com/wltt/issuetree/report/request/ReportCreationRequest.java index 49936fd..bb5b39c 100644 --- a/src/main/java/com/wltt/issuetree/report/request/ReportCreationRequest.java +++ b/src/main/java/com/wltt/issuetree/report/request/ReportCreationRequest.java @@ -8,6 +8,7 @@ public class ReportCreationRequest { private String reporterName; private String packageName; private String errorMessage; + private String managerGithubId; private String comment; private String stack; private String version; diff --git a/src/main/java/com/wltt/issuetree/report/service/ReportService.java b/src/main/java/com/wltt/issuetree/report/service/ReportService.java index 6cdf8aa..9e10bdb 100644 --- a/src/main/java/com/wltt/issuetree/report/service/ReportService.java +++ b/src/main/java/com/wltt/issuetree/report/service/ReportService.java @@ -8,6 +8,8 @@ import com.wltt.issuetree.report.request.ReportCreationRequest; import com.wltt.issuetree.team.domain.Team; import com.wltt.issuetree.team.domain.repository.TeamRepository; +import com.wltt.issuetree.user.domain.User; +import com.wltt.issuetree.user.domain.repository.UserRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -16,19 +18,25 @@ public class ReportService { private final ReportRepository reportRepository; private final TeamRepository teamRepository; + private final UserRepository userRepository; private final SlackbotService slackbotService; public void reportIssue(ReportCreationRequest request) { final Report report = request.toEntity(); reportRepository.save(report); + final User user = userRepository.findByGithubId(request.getManagerGithubId()).orElse(null); final Team team = teamRepository.findByPackageListIsContaining(request.getPackageName()) .orElseThrow( () -> new NotFoundTeamException(ErrorStatus.FAIL_SEARCH_TEAM) ); - sendReportMessage(team, request); + sendReportMessage(user, team, request); } - private void sendReportMessage(Team team, ReportCreationRequest request) { + private void sendReportMessage( + final User user, + final Team team, + final ReportCreationRequest request + ) { String text = "*요청자:*\n" + ">" + request.getReporterName() + "\n\n" + @@ -42,5 +50,6 @@ private void sendReportMessage(Team team, ReportCreationRequest request) { String header = "새로운 오류 해결 요청이 들어왔습니다."; slackbotService.chatMessage(text, header, team.getChannelId()); + slackbotService.chatMessage(text, header, user.getSlackId()); } } diff --git a/src/main/java/com/wltt/issuetree/user/domain/repository/UserRepository.java b/src/main/java/com/wltt/issuetree/user/domain/repository/UserRepository.java index 5968d75..220e80f 100644 --- a/src/main/java/com/wltt/issuetree/user/domain/repository/UserRepository.java +++ b/src/main/java/com/wltt/issuetree/user/domain/repository/UserRepository.java @@ -3,5 +3,8 @@ import com.wltt.issuetree.global.elasticsearch.repository.ElasticsearchRepository; import com.wltt.issuetree.user.domain.User; +import java.util.Optional; + public interface UserRepository extends ElasticsearchRepository { + Optional findByGithubId(String githubId); } diff --git a/src/main/java/com/wltt/issuetree/user/service/UserService.java b/src/main/java/com/wltt/issuetree/user/service/UserService.java index 3cd3be9..4fa80f1 100644 --- a/src/main/java/com/wltt/issuetree/user/service/UserService.java +++ b/src/main/java/com/wltt/issuetree/user/service/UserService.java @@ -22,15 +22,14 @@ public void addUser(SlackUrlEncodedForm slackUrlEncodedForm) { final String header = "정상적으로 깃허브 아이디가 등록되었습니다."; String content - = "*요청자:*\n" + - ">" + "<@" + slackUrlEncodedForm.getUserId() + ">\n" + - "*저장된 깃허브 아이디:*\n" + + = + "*깃허브 아이디:*\n" + ">" + slackUrlEncodedForm.getText() + "\n"; slackbotService.chatMessage( content, header, - slackUrlEncodedForm.getChannelId() + slackUrlEncodedForm.getUserId() ); } }