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