Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#328 [fix] slack util deprecated api 수정 #329

Merged
merged 3 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ dependencies {
implementation group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.2'

// Slack Webhook
implementation 'com.slack.api:slack-api-client:1.28.0'
implementation 'com.slack.api:slack-api-client:1.30.0'
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
implementation 'com.slack.api:slack-app-backend:1.28.0'
Expand Down
42 changes: 14 additions & 28 deletions src/main/java/com/asap/server/infra/slack/SlackUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.slack.api.model.block.Blocks;
import com.slack.api.model.block.LayoutBlock;
import com.slack.api.model.block.composition.BlockCompositions;
import com.slack.api.webhook.Payload;
import com.slack.api.webhook.WebhookPayloads;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -25,74 +26,59 @@ public class SlackUtil {

@Value("${slack.webhook.url}")
private String webhookUrl;
private StringBuilder sb= new StringBuilder();
private final StringBuilder sb= new StringBuilder();

private String NEW_LINE = "\n";
private String DOUBLE_NEW_LINE = "\n\n";
private final String NEW_LINE = "\n";
private final String DOUBLE_NEW_LINE = "\n\n";

//slack으로 알림보내기
public void sendAlert(Exception error, HttpServletRequest request) throws IOException{
// 메시지 내용인 LayoutBlock List 생성
List layoutBlocks= generateLayoutBlock(error,request);
List<LayoutBlock> layoutBlocks= generateLayoutBlock(error,request);

// 슬랙의 send API과 webhookURL을 통해 생성한 메시지 내용 전송
Slack.getInstance().send(webhookUrl, WebhookPayloads
.payload(p->
// 메시지 전송 유저명
p.username("Exception is detected")
// 메시지 전송 유저 아이콘 이미지 URL
.iconUrl("https://yt3.googleusercontent.com/ytc/AGIKgqMVUzRrhoo1gDQcqvPo0PxaJz7e0gqDXT0D78R5VQ=s900-c-k-c0x00ffffff-no-rj")
// 메시지 내용
.blocks(layoutBlocks)));
.payload(p -> p.blocks(layoutBlocks)));
}

// 전체 메시지가 담긴 LayoutBlock 생성
private List generateLayoutBlock(Exception error, HttpServletRequest request) {
private List<LayoutBlock> generateLayoutBlock(Exception error, HttpServletRequest request) {
return Blocks.asBlocks(
getHeader("Internal Server Error Detected"),
Blocks.divider(),
getSection(generateErrorMessage(error)),
Blocks.divider(),
getSection(generateErrorPointMessage(request)),
Blocks.divider(),
// 이슈 생성을 위해 프로젝트의 Issue URL을 입력하여 바로가기 링크를 생성
getSection("<https://github.com/ASAP-as-soon-as-posiible/ASAP_Server/issues | Go To Make Issue >")
);
}


// 예외 정보 메시지 생성
private String generateErrorMessage(Exception error) {
sb.setLength(0);
sb.append("*[Exception]*" + NEW_LINE + error.toString() + DOUBLE_NEW_LINE);
sb.append("*[From]*" + NEW_LINE + readRootStackTrace(error) + DOUBLE_NEW_LINE);
sb.append("*[Exception]*").append(NEW_LINE).append(error.toString()).append(DOUBLE_NEW_LINE);
sb.append("*[From]*").append(NEW_LINE).append(readRootStackTrace(error)).append(DOUBLE_NEW_LINE);

return sb.toString();
}

// HttpServletRequest를 사용하여 예외발생 요청에 대한 정보 메시지 생성
private String generateErrorPointMessage(HttpServletRequest request) {
sb.setLength(0);
sb.append("*[Details]*" + NEW_LINE);
sb.append("Request URL : " + request.getRequestURL().toString() + NEW_LINE);
sb.append("Request Method : " + request.getMethod() + NEW_LINE);
sb.append("Request Time : " + new Date() + NEW_LINE);
sb.append("*[Details]*").append(NEW_LINE);
sb.append("Request URL : ").append(request.getRequestURL().toString()).append(NEW_LINE);
sb.append("Request Method : ").append(request.getMethod()).append(NEW_LINE);
sb.append("Request Time : ").append(new Date()).append(NEW_LINE);

return sb.toString();
}

// 예외발생 클래스 정보 return
private String readRootStackTrace(Exception error) {
return error.getStackTrace()[0].toString();
}
// 에러 로그 메시지의 제목 return

private LayoutBlock getHeader(String text) {
return Blocks.header(h->h.text(
plainText(pt->pt.emoji(true)
.text(text))));
}

// 에러 로그 메시지 내용 return
private LayoutBlock getSection(String message) {
return Blocks.section(s->
s.text(BlockCompositions.markdownText(message)));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.asap.server.persistence.domain.time;

import com.asap.server.persistence.domain.AuditingTimeEntity;
import com.asap.server.persistence.domain.enums.TimeSlot;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand All @@ -18,7 +19,7 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class UserMeetingSchedule {
public class UserMeetingSchedule extends AuditingTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand Down
Loading