From adad851991185109f1fed1958e3091af3136dbe0 Mon Sep 17 00:00:00 2001 From: Wonder Date: Mon, 13 Oct 2025 13:49:39 +0800 Subject: [PATCH] =?UTF-8?q?=20=E2=9C=A8Feat:=20=E6=8E=A5=E6=94=B6=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=20WebHook=20=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gitea-webhook/pom.xml | 13 +++++ .../cn/hezhaohui/webhook/Application.java | 11 +++++ .../hezhaohui/webhook/constant/Payload.java | 7 +++ .../webhook/controller/HookController.java | 32 +++++++++++++ .../webhook/entity/GiteaPushEvent.java | 48 +++++++++++++++++++ .../webhook/service/HookService.java | 34 +++++++++++++ .../src/main/resources/application.yml | 2 + lark-sender/pom.xml | 15 ++++++ pom.xml | 9 ++++ 9 files changed, 171 insertions(+) create mode 100644 gitea-webhook/src/main/java/cn/hezhaohui/webhook/Application.java create mode 100644 gitea-webhook/src/main/java/cn/hezhaohui/webhook/constant/Payload.java create mode 100644 gitea-webhook/src/main/java/cn/hezhaohui/webhook/controller/HookController.java create mode 100644 gitea-webhook/src/main/java/cn/hezhaohui/webhook/entity/GiteaPushEvent.java create mode 100644 gitea-webhook/src/main/java/cn/hezhaohui/webhook/service/HookService.java create mode 100644 gitea-webhook/src/main/resources/application.yml diff --git a/gitea-webhook/pom.xml b/gitea-webhook/pom.xml index ed49ed0..60c83aa 100644 --- a/gitea-webhook/pom.xml +++ b/gitea-webhook/pom.xml @@ -17,4 +17,17 @@ UTF-8 + + + cn.hezhaohui + lark-sender + 1.0-SNAPSHOT + + + + org.springframework.boot + spring-boot-starter-web + + + \ No newline at end of file diff --git a/gitea-webhook/src/main/java/cn/hezhaohui/webhook/Application.java b/gitea-webhook/src/main/java/cn/hezhaohui/webhook/Application.java new file mode 100644 index 0000000..2b0baf3 --- /dev/null +++ b/gitea-webhook/src/main/java/cn/hezhaohui/webhook/Application.java @@ -0,0 +1,11 @@ +package cn.hezhaohui.webhook; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/gitea-webhook/src/main/java/cn/hezhaohui/webhook/constant/Payload.java b/gitea-webhook/src/main/java/cn/hezhaohui/webhook/constant/Payload.java new file mode 100644 index 0000000..497a1eb --- /dev/null +++ b/gitea-webhook/src/main/java/cn/hezhaohui/webhook/constant/Payload.java @@ -0,0 +1,7 @@ +package cn.hezhaohui.webhook.constant; + + + +public class Payload { + public final static String SECRET = "dn293830"; +} diff --git a/gitea-webhook/src/main/java/cn/hezhaohui/webhook/controller/HookController.java b/gitea-webhook/src/main/java/cn/hezhaohui/webhook/controller/HookController.java new file mode 100644 index 0000000..ef48d33 --- /dev/null +++ b/gitea-webhook/src/main/java/cn/hezhaohui/webhook/controller/HookController.java @@ -0,0 +1,32 @@ +package cn.hezhaohui.webhook.controller; + +import cn.hezhaohui.webhook.entity.GiteaPushEvent; +import cn.hezhaohui.webhook.service.HookService; +import jakarta.annotation.Resource; +import lombok.extern.slf4j.Slf4j; +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; + +import static cn.hezhaohui.webhook.constant.Payload.SECRET; + +@RestController +@RequestMapping("/hook") +@Slf4j +public class HookController { + + @Resource + private HookService hookService; + + @PostMapping + public void handlePush(@RequestBody GiteaPushEvent event) { + log.info(event.toString()); + + if(!SECRET.equals(event.getSecret())) { + return; + } + + hookService.handlePush(event); + } +} diff --git a/gitea-webhook/src/main/java/cn/hezhaohui/webhook/entity/GiteaPushEvent.java b/gitea-webhook/src/main/java/cn/hezhaohui/webhook/entity/GiteaPushEvent.java new file mode 100644 index 0000000..7fd11ec --- /dev/null +++ b/gitea-webhook/src/main/java/cn/hezhaohui/webhook/entity/GiteaPushEvent.java @@ -0,0 +1,48 @@ +package cn.hezhaohui.webhook.entity; + +import lombok.Data; + +import java.util.List; + +@Data +public class GiteaPushEvent { + private String secret; + private String ref; + private String before; + private String after; + private String compare_url; + private List commits; + private Repository repository; + private User pusher; + private User sender; + + @Data + public static class Repository { + private Integer id; + private User owner; + private String name; + private String full_name; + private String description; + private String html_url; + private String clone_url; + } + + @Data + public static class Commit { + private String id; + private String message; + private String url; + private User author; + private User committer; + private String timestamp; + } + + @Data + public static class User { + private Integer id; + private String login; + private String username; + private String email; + private String avatar_url; + } +} 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 new file mode 100644 index 0000000..d7c27c3 --- /dev/null +++ b/gitea-webhook/src/main/java/cn/hezhaohui/webhook/service/HookService.java @@ -0,0 +1,34 @@ +package cn.hezhaohui.webhook.service; + +import cn.hezhaohui.lark.entity.CardData; +import cn.hezhaohui.lark.util.Lark; +import cn.hezhaohui.webhook.entity.GiteaPushEvent; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class HookService { + + public void handlePush(GiteaPushEvent event) { + List commitMsgList = event.getCommits().stream() + .map(GiteaPushEvent.Commit::getMessage) + .toList(); + CardData cardData = CardData.builder() + .title("[Gitea] 仓库状态变化") + .repo(event.getRepository().getName()) + .link(event.getRepository().getHtml_url()) + .msg(String.join("\n", commitMsgList)) + .tag("PUSH") + .subtitle("Ciallo~(∠・ω< )⌒★") + .ai("待开发") + .build(); + + try { + Lark.sendCardMessage(cardData); + } catch (Exception e) { + throw new RuntimeException(e); + } + + } +} diff --git a/gitea-webhook/src/main/resources/application.yml b/gitea-webhook/src/main/resources/application.yml new file mode 100644 index 0000000..e346b3d --- /dev/null +++ b/gitea-webhook/src/main/resources/application.yml @@ -0,0 +1,2 @@ +server: + port: 9000 \ No newline at end of file diff --git a/lark-sender/pom.xml b/lark-sender/pom.xml index b432e89..468079c 100644 --- a/lark-sender/pom.xml +++ b/lark-sender/pom.xml @@ -26,4 +26,19 @@ 2.4.16 + + + + + org.springframework.boot + spring-boot-maven-plugin + 3.2.2 + + + true + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 39030a7..2cdf747 100644 --- a/pom.xml +++ b/pom.xml @@ -39,4 +39,13 @@ + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file