Compare commits
3 Commits
Author | SHA1 | Date |
---|---|---|
zhengyi | 5df61c5300 | |
zhengyi | 3a34ea3236 | |
zhengyi | ffc2a847a5 |
|
@ -0,0 +1,36 @@
|
|||
package top.mczhengyi.gitsyncbot.api;
|
||||
|
||||
import top.mczhengyi.gitsyncbot.bean.event.EventInfo;
|
||||
import top.mczhengyi.gitsyncbot.bean.event.EventType;
|
||||
import top.mczhengyi.gitsyncbot.bean.object.RepositoryInfo;
|
||||
|
||||
/**
|
||||
* API 请求样例
|
||||
* @author zhengyi59
|
||||
*/
|
||||
public abstract class BaseApi {
|
||||
/**
|
||||
* 发布Issue
|
||||
* @param info event信息
|
||||
* @param repo 目标仓库
|
||||
* @return 成功
|
||||
*/
|
||||
abstract public Boolean pushIssue(EventInfo info, RepositoryInfo repo);
|
||||
|
||||
/**
|
||||
* 发布Issue评论
|
||||
* @param info event信息
|
||||
* @param repo 目标仓库
|
||||
* @return 成功
|
||||
*/
|
||||
abstract public Boolean pushIssueComment(EventInfo info, RepositoryInfo repo);
|
||||
|
||||
|
||||
/**
|
||||
* 发布Releases
|
||||
* @param info event信息
|
||||
* @param repo 目标仓库
|
||||
* @return 成功
|
||||
*/
|
||||
abstract public Boolean pushReleases(EventInfo info, RepositoryInfo repo);
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package top.mczhengyi.gitsyncbot.api;
|
||||
|
||||
import top.mczhengyi.gitsyncbot.bean.event.EventInfo;
|
||||
import top.mczhengyi.gitsyncbot.bean.object.RepositoryInfo;
|
||||
|
||||
public class GithubApi extends BaseApi {
|
||||
@Override
|
||||
public Boolean pushIssue(EventInfo info, RepositoryInfo repo) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean pushIssueComment(EventInfo info, RepositoryInfo repo) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean pushReleases(EventInfo info, RepositoryInfo repo) {
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package top.mczhengyi.gitsyncbot.bean.event;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
public enum EventAction {
|
||||
// Issue、Comment、PR创建
|
||||
CREATED(new String[]{"created"}, "created"),
|
||||
// PR被接受
|
||||
APPROVE(new String[]{""}, "approved"),
|
||||
// Issue、Comment、PR被编辑
|
||||
EDITED(new String[]{""}, "edited");
|
||||
|
||||
private final List<String> sign;
|
||||
private final String name;
|
||||
|
||||
EventAction(String[] sign, String name) {
|
||||
this.sign = List.of(sign);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public static EventAction getAction(String sign) {
|
||||
for (EventAction action : EventAction.values()) {
|
||||
if (action.getSign().contains(sign)) return action;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package top.mczhengyi.gitsyncbot.bean.event;
|
||||
|
||||
import lombok.Data;
|
||||
import top.mczhengyi.gitsyncbot.bean.hook.Hook;
|
||||
|
||||
@Data
|
||||
public class EventInfo {
|
||||
private Platform platform;
|
||||
private EventType event;
|
||||
private EventAction action;
|
||||
private Issue issue;
|
||||
private Repository repository;
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package top.mczhengyi.gitsyncbot.bean.event;
|
||||
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
public enum EventType {
|
||||
ISSUE(new String[]{"issue"}, "issue"),
|
||||
ISSUE_COMMENT(new String[]{"issue_comment"}, "issue_comment");
|
||||
|
||||
private final List<String> sign;
|
||||
private final String name;
|
||||
EventType(String[] sign, String name) {
|
||||
this.sign = List.of(sign);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
public static EventType getEvent(String event) {
|
||||
for (EventType eventType : EventType.values()) {
|
||||
if (eventType.sign.contains(event)) return eventType;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package top.mczhengyi.gitsyncbot.bean.event;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Issue {
|
||||
private String title;
|
||||
private String body;
|
||||
private Integer number;
|
||||
private User author;
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package top.mczhengyi.gitsyncbot.bean.event;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public enum Platform {
|
||||
GITHUB("github", "https://github.com");
|
||||
|
||||
private String name;
|
||||
private String defaultUrl;
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package top.mczhengyi.gitsyncbot.bean.event;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Repository {
|
||||
private String name;
|
||||
private String url;
|
||||
private Integer id;
|
||||
private User owner;
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package top.mczhengyi.gitsyncbot.bean.event;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class User {
|
||||
private String username;
|
||||
private String avatar;
|
||||
private String url;
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package top.mczhengyi.gitsyncbot.bean.hook;
|
||||
|
||||
import top.mczhengyi.gitsyncbot.bean.event.EventInfo;
|
||||
|
||||
public class Hook {
|
||||
public EventInfo getHookInfo() {
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package top.mczhengyi.gitsyncbot.bean.hook.github;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import top.mczhengyi.gitsyncbot.bean.event.EventAction;
|
||||
import top.mczhengyi.gitsyncbot.bean.event.EventType;
|
||||
import top.mczhengyi.gitsyncbot.bean.event.EventInfo;
|
||||
import top.mczhengyi.gitsyncbot.bean.event.Platform;
|
||||
import top.mczhengyi.gitsyncbot.bean.hook.Hook;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class GithubHook extends Hook {
|
||||
private String event;
|
||||
private String action;
|
||||
private GithubIssue issue;
|
||||
private GithubRepo repository;
|
||||
|
||||
public EventInfo getHookInfo() {
|
||||
EventInfo info = new EventInfo();
|
||||
info.setEvent(EventType.getEvent(this.event));
|
||||
info.setAction(EventAction.getAction(this.action));
|
||||
info.setIssue(this.issue.getIssue());
|
||||
info.setPlatform(Platform.GITHUB);
|
||||
info.setRepository(repository.getRepository());
|
||||
return info;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package top.mczhengyi.gitsyncbot.bean.hook.github;
|
||||
|
||||
import lombok.Data;
|
||||
import top.mczhengyi.gitsyncbot.bean.event.Issue;
|
||||
|
||||
@Data
|
||||
public class GithubIssue {
|
||||
private Integer id;
|
||||
private String title;
|
||||
private String body;
|
||||
private Integer number;
|
||||
private GithubUser user;
|
||||
|
||||
public Issue getIssue() {
|
||||
Issue i = new Issue();
|
||||
i.setNumber(this.number);
|
||||
i.setTitle(this.title);
|
||||
i.setBody(this.body);
|
||||
i.setAuthor(user.getUser());
|
||||
return i;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package top.mczhengyi.gitsyncbot.bean.hook.github;
|
||||
|
||||
import lombok.Data;
|
||||
import top.mczhengyi.gitsyncbot.bean.event.Repository;
|
||||
|
||||
@Data
|
||||
public class GithubRepo {
|
||||
private Integer id;
|
||||
private String name;
|
||||
private GithubUser owner;
|
||||
private String html_url;
|
||||
|
||||
public Repository getRepository() {
|
||||
Repository repo = new Repository();
|
||||
repo.setId(id);
|
||||
repo.setName(name);
|
||||
repo.setOwner(owner.getUser());
|
||||
repo.setUrl(html_url);
|
||||
return repo;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package top.mczhengyi.gitsyncbot.bean.hook.github;
|
||||
|
||||
import lombok.Data;
|
||||
import top.mczhengyi.gitsyncbot.bean.event.User;
|
||||
|
||||
@Data
|
||||
public class GithubUser {
|
||||
private String login;
|
||||
private String avatar_url;
|
||||
private String html_url;
|
||||
|
||||
public User getUser() {
|
||||
User user = new User();
|
||||
user.setUsername(login);
|
||||
user.setAvatar(avatar_url);
|
||||
user.setUrl(html_url);
|
||||
return user;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package top.mczhengyi.gitsyncbot.bean.object;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class RepositoryInfo {
|
||||
private ServerInfo server;
|
||||
private String name;
|
||||
private String owner;
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package top.mczhengyi.gitsyncbot.bean.object;
|
||||
|
||||
import lombok.Data;
|
||||
import top.mczhengyi.gitsyncbot.bean.event.Platform;
|
||||
|
||||
@Data
|
||||
public class ServerInfo {
|
||||
private Platform platform;
|
||||
private String url;
|
||||
private String token;
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package top.mczhengyi.gitsyncbot.consumer;
|
||||
|
||||
/**
|
||||
* Issue创建消费者
|
||||
*/
|
||||
public class IssueCreatedConsumer {
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package top.mczhengyi.gitsyncbot.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import top.mczhengyi.gitsyncbot.bean.event.EventInfo;
|
||||
import top.mczhengyi.gitsyncbot.bean.hook.github.GithubHook;
|
||||
import top.mczhengyi.gitsyncbot.services.IHookServices;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("hook")
|
||||
public class HookController {
|
||||
private final IHookServices hookServices;
|
||||
|
||||
public HookController(IHookServices hookServices) {
|
||||
this.hookServices = hookServices;
|
||||
}
|
||||
|
||||
@PostMapping("github")
|
||||
public EventInfo platform(
|
||||
@RequestHeader("x-github-event") String event,
|
||||
@RequestBody GithubHook githubHook
|
||||
) {
|
||||
githubHook.setEvent(event);
|
||||
hookServices.hookProcess(githubHook);
|
||||
return githubHook.getHookInfo();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package top.mczhengyi.gitsyncbot.event;
|
||||
|
||||
import io.lettuce.core.protocol.Command;
|
||||
import jdk.jfr.Event;
|
||||
import top.mczhengyi.gitsyncbot.bean.event.EventInfo;
|
||||
import top.mczhengyi.gitsyncbot.bean.event.EventType;
|
||||
import top.mczhengyi.gitsyncbot.event.provider.CommandProvider;
|
||||
import top.mczhengyi.gitsyncbot.event.provider.SyncProvider;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class EventCenter {
|
||||
private Map<EventType, List<IEventProvider>> eventProviderMap;
|
||||
private static EventCenter eventCenter;
|
||||
|
||||
public static EventCenter getEventCenter() {
|
||||
if (eventCenter == null) {
|
||||
eventCenter = new EventCenter();
|
||||
eventCenter.on(EventType.ISSUE_COMMENT, new CommandProvider());
|
||||
SyncProvider syncProvider = new SyncProvider();
|
||||
eventCenter.on(EventType.ISSUE, syncProvider);
|
||||
eventCenter.on(EventType.ISSUE_COMMENT, syncProvider);
|
||||
}
|
||||
return eventCenter;
|
||||
}
|
||||
|
||||
EventCenter() {
|
||||
this.eventProviderMap = new HashMap<>();
|
||||
}
|
||||
|
||||
public void on(EventType type, IEventProvider provider) {
|
||||
if (eventProviderMap.containsKey(type)) {
|
||||
eventProviderMap.get(type).add(provider);
|
||||
} else {
|
||||
List<IEventProvider> providerList = new ArrayList<>();
|
||||
providerList.add(provider);
|
||||
eventProviderMap.put(type, providerList);
|
||||
}
|
||||
}
|
||||
|
||||
public void emit(EventInfo info) {
|
||||
List<IEventProvider> providerList = eventProviderMap.get(info.getEvent());
|
||||
if (providerList != null) {
|
||||
providerList.forEach(provider -> {
|
||||
provider.process(info);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package top.mczhengyi.gitsyncbot.event;
|
||||
|
||||
import top.mczhengyi.gitsyncbot.bean.event.EventInfo;
|
||||
import top.mczhengyi.gitsyncbot.bean.event.EventType;
|
||||
|
||||
public interface IEventProvider {
|
||||
void process(EventInfo info);
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package top.mczhengyi.gitsyncbot.event.provider;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import top.mczhengyi.gitsyncbot.bean.event.EventInfo;
|
||||
import top.mczhengyi.gitsyncbot.event.IEventProvider;
|
||||
|
||||
@Slf4j
|
||||
public class CommandProvider implements IEventProvider {
|
||||
@Override
|
||||
public void process(EventInfo info) {
|
||||
log.info("Process Command");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package top.mczhengyi.gitsyncbot.event.provider;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import top.mczhengyi.gitsyncbot.bean.event.EventInfo;
|
||||
import top.mczhengyi.gitsyncbot.bean.event.EventType;
|
||||
import top.mczhengyi.gitsyncbot.event.IEventProvider;
|
||||
import top.mczhengyi.gitsyncbot.utils.EventUtil;
|
||||
|
||||
/**
|
||||
* 同步处理
|
||||
* @author zhengyi59
|
||||
*/
|
||||
@Slf4j
|
||||
public class SyncProvider implements IEventProvider {
|
||||
|
||||
@Override
|
||||
public void process(EventInfo info) {
|
||||
log.info(EventUtil.getIdentify(info.getPlatform(), info.getRepository().getOwner().getUsername(),
|
||||
info.getRepository().getName()));
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package top.mczhengyi.gitsyncbot.services;
|
||||
|
||||
import top.mczhengyi.gitsyncbot.bean.hook.Hook;
|
||||
|
||||
public interface IHookServices {
|
||||
void hookProcess(Hook hook);
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package top.mczhengyi.gitsyncbot.services.impl;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import top.mczhengyi.gitsyncbot.bean.event.EventInfo;
|
||||
import top.mczhengyi.gitsyncbot.bean.hook.Hook;
|
||||
import top.mczhengyi.gitsyncbot.event.EventCenter;
|
||||
import top.mczhengyi.gitsyncbot.services.IHookServices;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class HookServices implements IHookServices {
|
||||
@Override
|
||||
public void hookProcess(Hook hook) {
|
||||
EventInfo info = hook.getHookInfo();
|
||||
EventCenter.getEventCenter().emit(info);
|
||||
// Topic: event.action
|
||||
// 生产者发布
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package top.mczhengyi.gitsyncbot.utils;
|
||||
|
||||
import top.mczhengyi.gitsyncbot.bean.event.EventType;
|
||||
import top.mczhengyi.gitsyncbot.bean.event.Platform;
|
||||
|
||||
public class EventUtil {
|
||||
public static String getIdentify(Platform platform, String owner, String repo) {
|
||||
// Server@Platform/Owner/Repo
|
||||
return "%s@%s/%s/%s".formatted("", platform.getName(), owner, repo);
|
||||
}
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
|
|
@ -0,0 +1,8 @@
|
|||
server:
|
||||
port: 8080
|
||||
spring:
|
||||
datasource:
|
||||
username: root
|
||||
password: justice
|
||||
url: jdbc:mysql://localhost:3306/git-bot
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
|
@ -8,6 +8,7 @@ class GitSyncBotApplicationTests {
|
|||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package top.mczhengyi.gitsyncbot.bean.event;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import top.mczhengyi.gitsyncbot.bean.hook.Hook;
|
||||
import top.mczhengyi.gitsyncbot.bean.hook.github.GithubHook;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class EventInfoTest {
|
||||
|
||||
}
|
Loading…
Reference in New Issue