diff --git a/gitea-webhook/src/main/java/cn/hezhaohui/webhook/constant/AIConstant.java b/gitea-webhook/src/main/java/cn/hezhaohui/webhook/constant/AIConstant.java index e3b8473..bcce605 100644 --- a/gitea-webhook/src/main/java/cn/hezhaohui/webhook/constant/AIConstant.java +++ b/gitea-webhook/src/main/java/cn/hezhaohui/webhook/constant/AIConstant.java @@ -14,4 +14,6 @@ public class AIConstant { "[Modified Files]: ...\n" + "【输出格式】" + "[总结]: ..."; + + public static final String DEFAULT_ERROR_RESPONSE = "*变化文件数量过多,请在仓库中查看"; } diff --git a/gitea-webhook/src/main/java/cn/hezhaohui/webhook/service/HookService.java b/gitea-webhook/src/main/java/cn/hezhaohui/webhook/service/HookService.java index a9be9ee..4371017 100644 --- a/gitea-webhook/src/main/java/cn/hezhaohui/webhook/service/HookService.java +++ b/gitea-webhook/src/main/java/cn/hezhaohui/webhook/service/HookService.java @@ -2,7 +2,9 @@ package cn.hezhaohui.webhook.service; import cn.hezhaohui.lark.entity.CardData; import cn.hezhaohui.lark.util.Lark; +import cn.hezhaohui.webhook.constant.AIConstant; import cn.hezhaohui.webhook.entity.GiteaPushEvent; +import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -13,6 +15,12 @@ import java.util.stream.Stream; @Slf4j public class HookService { + @Resource + private AIService aiService; + + @Resource + private FileService fileService; + public void handlePush(GiteaPushEvent event) { List commitMsgList = event.getCommits().stream() .map(GiteaPushEvent.Commit::getMessage) @@ -20,22 +28,48 @@ public class HookService { List addedFiles = event.getCommits().stream() .flatMap(commit -> commit.getAdded().stream()) + .distinct() .toList(); List removedFiles = event.getCommits().stream() .flatMap(commit -> commit.getRemoved().stream()) + .distinct() .toList(); List modifiedFiles = event.getCommits().stream() .flatMap(commit -> commit.getModified().stream()) + .distinct() .toList(); + if (addedFiles.isEmpty() && removedFiles.isEmpty() && modifiedFiles.isEmpty()) { + return; + } + + String ai = AIConstant.DEFAULT_ERROR_RESPONSE; + + + if (addedFiles.size() + removedFiles.size() + modifiedFiles.size() <= 20) { + String addedFilesContent = fileService.getAllFiles(event.getRepository().getHtml_url(), addedFiles); + String removedFilesContent = fileService.getAllFiles(event.getRepository().getHtml_url(), removedFiles); + String modifiedFilesContent = fileService.getAllFiles(event.getRepository().getHtml_url(), modifiedFiles); + + String allFilesContentString = "[Added Files]: \n" + + addedFilesContent + + "\n[Removed Files]: \n" + + removedFilesContent + + "\n[Modified Files]: \n" + + modifiedFilesContent; + String input = "[Commit Messages]: \n" + + String.join("\n", commitMsgList) + + "\n" + allFilesContentString; + ai = aiService.getAIResponse(input); + } + log.info("commitMsgList: {}\n", commitMsgList); log.info("addedFiles: {}\n", addedFiles); log.info("removedFiles: {}\n", removedFiles); log.info("modifiedFiles: {}\n", modifiedFiles); - // TODO: 调用 AI 模块 CardData cardData = CardData.builder() .title("[Gitea] 仓库状态变化") @@ -44,7 +78,7 @@ public class HookService { .msg(String.join("\n", commitMsgList)) .tag("PUSH") .subtitle("Ciallo~(∠・ω< )⌒★") - .ai("待开发") + .ai(ai) .build(); try {