Skip to content

Commit

Permalink
Merge pull request #179 from CHZZK-Study/dev
Browse files Browse the repository at this point in the history
[fix/release] 오늘의 질문 사라진 코드 복구
  • Loading branch information
HongYeseul authored Sep 20, 2024
2 parents fbfe0bd + 721345e commit 4b70c7e
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 69 deletions.
122 changes: 61 additions & 61 deletions .github/workflows/code-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,77 +11,77 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Check out repository
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Check out repository
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Set Server Info
run: |
echo "REVIEW_API_URL=${{ secrets.REVIEW_API_URL }}" >> $GITHUB_ENV
- name: Set Server Info
run: |
echo "REVIEW_API_URL=${{ secrets.REVIEW_API_URL }}" >> $GITHUB_ENV
# - name: Debug REVIEW_API_URL
# run: echo "REVIEW_API_URL=$REVIEW_API_URL"

# - name: API calling for CodeReview
# id: api_call
# run: |
# if [ -z "$REVIEW_API_URL" ]; then
# echo "REVIEW_API_URL is not set"
# exit 1
# fi
# REVIEW_RESPONSE=$(curl -X POST $REVIEW_API_URL)
# echo "Review Response: $REVIEW_RESPONSE"
# echo "REVIEW_RESPONSE=$REVIEW_RESPONSE" >> $GITHUB_ENV
# - name: Debug REVIEW_API_URL
# run: echo "REVIEW_API_URL=$REVIEW_API_URL"

- name: Get Git Diff
id: git_diff
run: |
git fetch origin
git diff --unified=3 ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} > diff.txt
DIFF_CONTENT=$(cat diff.txt)
echo "Diff content length: $(wc -c < diff.txt)"
echo "DIFF_CONTENT<<EOF" >> $GITHUB_ENV
echo "$DIFF_CONTENT" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
echo "Diff added to GITHUB_ENV"
# - name: API calling for CodeReview
# id: api_call
# run: |
# if [ -z "$REVIEW_API_URL" ]; then
# echo "REVIEW_API_URL is not set"
# exit 1
# fi
# REVIEW_RESPONSE=$(curl -X POST $REVIEW_API_URL)
# echo "Review Response: $REVIEW_RESPONSE"
# echo "REVIEW_RESPONSE=$REVIEW_RESPONSE" >> $GITHUB_ENV

- name: API calling for CodeReview
id: api_call
run: |
ESCAPED_DIFF=$(echo "$DIFF_CONTENT" | jq -sRr @json)
REQUEST_BODY=$(jq -n \
--arg model "${{ secrets.REVIEW_MODEL_NAME }}" \
--arg prompt "Review the following code changes and provide feedback:$ESCAPED_DIFF" \
'{model: $model, prompt: $prompt, stream: false}')
FULL_RESPONSE=$(curl -X POST ${REVIEW_API_URL} \
-H "Content-Type: application/json" \
-d "$REQUEST_BODY")
REVIEW_RESPONSE=$(echo $FULL_RESPONSE | jq -r '.response')
echo "Review Response: $REVIEW_RESPONSE"
echo "REVIEW_RESPONSE<<EOF" >> $GITHUB_ENV
echo "$REVIEW_RESPONSE" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- name: Get Git Diff
id: git_diff
run: |
git fetch origin
git diff --unified=3 ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} > diff.txt
DIFF_CONTENT=$(cat diff.txt)
echo "Diff content length: $(wc -c < diff.txt)"
echo "DIFF_CONTENT<<EOF" >> $GITHUB_ENV
echo "$DIFF_CONTENT" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
echo "Diff added to GITHUB_ENV"
- name: Debug output
run: |
echo "DIFF_CONTENT:"
echo "$DIFF_CONTENT"
echo "REVIEW_RESPONSE:"
echo "$REVIEW_RESPONSE"
- name: API calling for CodeReview
id: api_call
run: |
ESCAPED_DIFF=$(echo "$DIFF_CONTENT" | jq -sRr @json)
REQUEST_BODY=$(jq -n \
--arg model "${{ secrets.REVIEW_MODEL_NAME }}" \
--arg prompt "Review the following code changes and provide feedback:$ESCAPED_DIFF" \
'{model: $model, prompt: $prompt, stream: false}')
FULL_RESPONSE=$(curl -X POST ${REVIEW_API_URL} \
-H "Content-Type: application/json" \
-d "$REQUEST_BODY")
REVIEW_RESPONSE=$(echo $FULL_RESPONSE | jq -r '.response')
echo "Review Response: $REVIEW_RESPONSE"
echo "REVIEW_RESPONSE<<EOF" >> $GITHUB_ENV
echo "$REVIEW_RESPONSE" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- name: Add review to pull request
uses: actions/github-script@v6
env:
DIFF_CONTENT: ${{ env.DIFF_CONTENT }}
with:
github-token: ${{ secrets.REVIEW_BOT_TOKEN }}
script: |
- name: Debug output
run: |
echo "DIFF_CONTENT:"
echo "$DIFF_CONTENT"
echo "REVIEW_RESPONSE:"
echo "$REVIEW_RESPONSE"
- name: Add review to pull request
uses: actions/github-script@v6
env:
DIFF_CONTENT: ${{ env.DIFF_CONTENT }}
with:
github-token: ${{ secrets.REVIEW_BOT_TOKEN }}
script: |
const diff = process.env.DIFF_CONTENT;
const response = process.env.REVIEW_RESPONSE;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `${response}`
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import org.springframework.data.jpa.repository.Query;

import java.time.LocalDateTime;
import java.util.Optional;

public interface TodayQuestionDAO extends JpaRepository<TodayQuestion, Long> {
@Query("SELECT t FROM TodayQuestion t WHERE t.createdAt >= :startOfDay AND t.createdAt <= :endOfDay")
TodayQuestion findByCreatedAtBetween(LocalDateTime startOfDay, LocalDateTime endOfDay);
@Query("SELECT t FROM TodayQuestion t WHERE t.createdAt >= :startOfDay AND t.createdAt < :endOfDay ORDER BY t.createdAt DESC LIMIT 1")
Optional<TodayQuestion> findLatestByCreatedAtBetween(LocalDateTime startOfDay, LocalDateTime endOfDay);

boolean existsByCreatedAtBetween(LocalDateTime startOfDay, LocalDateTime endOfDay);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,58 @@
import chzzk.grassdiary.domain.diary.entity.question.TodayQuestion;
import chzzk.grassdiary.domain.diary.entity.question.TodayQuestionDAO;
import chzzk.grassdiary.domain.diary.dto.TodayQuestionDTO;
import chzzk.grassdiary.global.common.error.exception.SystemException;
import chzzk.grassdiary.global.common.response.ServerErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

import static chzzk.grassdiary.domain.diary.entity.question.QuestionPrompt.getRandomQuestion;

@RequiredArgsConstructor
@Service
@Slf4j
public class TodayQuestionService {

private final TodayQuestionDAO todayQuestionDAO;

@Scheduled(cron = "0 0 0 * * *")
@CacheEvict(value = "todayQuestion", allEntries = true) // 캐시 삭제: 모든 캐시 엔트리 삭제
@Transactional
public void makeTodayQuestions() {
todayQuestionDAO.save(new TodayQuestion(getRandomQuestion()));
LocalDate today = LocalDate.now();
LocalDateTime startOfDay = today.atStartOfDay();
LocalDateTime endOfDay = today.plusDays(1).atStartOfDay();

// 오늘 날짜의 질문이 이미 존재하는지 확인
if (todayQuestionDAO.existsByCreatedAtBetween(startOfDay, endOfDay)) {
log.info("오늘의 질문이 이미 존재합니다. 새로운 질문을 생성하지 않습니다.");
return;
}

// 오늘의 질문이 없는 경우에만 새로운 질문 생성
TodayQuestion newQuestion = new TodayQuestion(getRandomQuestion());
todayQuestionDAO.save(newQuestion);
log.info("새로운 오늘의 질문이 생성되었습니다: {}", newQuestion.getQuestionPrompt().getQuestion());
}

@Cacheable(value = "todayQuestion", key = "#root.method.name") // 캐시 저장: 메서드 이름을 키로 사용
@Transactional(readOnly = true)
public TodayQuestionDTO getTodayQuestion() {
LocalDate today = LocalDate.now();

LocalDateTime startOfDay = today.atStartOfDay();
LocalDateTime endOfDay = today.atTime(LocalTime.MAX);

TodayQuestion todayQuestion = todayQuestionDAO.findByCreatedAtBetween(startOfDay, endOfDay);
LocalDateTime endOfDay = today.plusDays(1).atStartOfDay();

return new TodayQuestionDTO(todayQuestion.getQuestionPrompt().getQuestion());
return todayQuestionDAO.findLatestByCreatedAtBetween(startOfDay, endOfDay)
.map(question -> new TodayQuestionDTO(question.getQuestionPrompt().getQuestion()))
.orElseThrow(() -> new SystemException(ServerErrorCode.QUESTION_UNAVAILABLE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public enum ServerErrorCode implements ErrorCodeModel {
OUT_OF_MEMORY(500, "OUT_OF_MEMORY", "서버에 메모리 부족이 발생했습니다. 잠시 후 다시 시도해주세요."),
NOT_IMPLEMENTED(501, "NOT_IMPLEMENTED", "현재 지원되지 않는 기능입니다."),
SERVICE_UNAVAILABLE(503, "SERVICE_UNAVAILABLE", "현재 서비스가 사용 불가합니다. 나중에 다시 시도해주세요."),
QUESTION_UNAVAILABLE(503, "QUESTION_UNAVAILABLE", "현재 '오늘의 일기' 서비스가 사용 불가합니다. 나중에 다시 시도해주세요."),
IMAGE_UPLOAD_FAILED(500, "IMAGE_UPLOAD_FAILED", "이미지 업로드에 실패했습니다."),
REWARD_HISTORY_SAVE_FAILED(500, "REWARD_HISTORY_SAVE_FAILED", "일기 히스토리 저장에 실패했습니다.");

Expand Down

0 comments on commit 4b70c7e

Please sign in to comment.