✨Feat: 接收解析 WebHook 信息
This commit is contained in:
@@ -17,4 +17,17 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.hezhaohui</groupId>
|
||||
<artifactId>lark-sender</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package cn.hezhaohui.webhook.constant;
|
||||
|
||||
|
||||
|
||||
public class Payload {
|
||||
public final static String SECRET = "dn293830";
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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<Commit> 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;
|
||||
}
|
||||
}
|
||||
@@ -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<String> 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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
server:
|
||||
port: 9000
|
||||
@@ -26,4 +26,19 @@
|
||||
<version>2.4.16</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>3.2.2</version>
|
||||
<configuration>
|
||||
<skip>
|
||||
true
|
||||
</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
Reference in New Issue
Block a user