Skip to content

Commit

Permalink
Merge pull request #13 from White-Long-tailed-Tit/feature/12-register…
Browse files Browse the repository at this point in the history
…-github

[feature/12-register-github] 깃허브 등록
  • Loading branch information
5jisoo authored Feb 22, 2024
2 parents 40c6a03 + 54beb45 commit c377eca
Show file tree
Hide file tree
Showing 10 changed files with 172 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,32 @@
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.transaction.annotation.Transactional;

import java.io.IOException;
import org.springframework.web.client.RestTemplate;

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;

@Service
@RequiredArgsConstructor
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))
Expand All @@ -48,4 +49,27 @@ public void chatMessage(
throw new GeneralException(ErrorStatus._INTERNAL_SERVER_ERROR);
}
}

public void chatMessage(
final List<TextObject> textObjectList,
final String headerContent,
final String channelNameOrIdOrUserId
) {
ChatPostMessageRequest messageRequest = ChatPostMessageRequest.builder()
.channel(channelNameOrIdOrUserId)
.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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/wltt/issuetree/report/service/ReportService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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" +
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class TeamController {
public void addTeams(
@RequestBody final MultiValueMap<String, Object> data
) {
System.out.println(data);
teamService.addTeam(new SlackUrlEncodedForm(data));
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -47,15 +45,15 @@ private void chatSuccessMessage(
String header = "정상적으로 역할이 지정되었습니다.";

slackbotService.chatMessage(
textObjects.toString(),
textObjects,
header,
request.getChannelName()
);

header = "새로운 역할이 지정되었습니다.";

slackbotService.chatMessage(
textObjects.toString(),
textObjects,
header,
team.getChannelId()
);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> data
) {
userService.addUser(new SlackUrlEncodedForm(data));
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/wltt/issuetree/user/domain/User.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.wltt.issuetree.user.domain.repository;

import com.wltt.issuetree.global.elasticsearch.repository.ElasticsearchRepository;
import com.wltt.issuetree.user.domain.User;

import java.util.Optional;

public interface UserRepository extends ElasticsearchRepository<User, String> {
Optional<User> findByGithubId(String githubId);
}
16 changes: 16 additions & 0 deletions src/main/java/com/wltt/issuetree/user/parser/UserParser.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/wltt/issuetree/user/service/UserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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.getText() + "\n";

slackbotService.chatMessage(
content,
header,
slackUrlEncodedForm.getUserId()
);
}
}

0 comments on commit c377eca

Please sign in to comment.