✨ Complete basic structure
This commit is contained in:
parent
39522fa1f5
commit
ffc2a847a5
|
@ -0,0 +1,4 @@
|
|||
package top.mczhengyi.gitsyncbot.api;
|
||||
|
||||
public class GithubApi {
|
||||
}
|
|
@ -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 String action;
|
||||
private Issue issue;
|
||||
private Repository repository;
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package top.mczhengyi.gitsyncbot.bean.event;
|
||||
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
public enum EventType {
|
||||
ISSUE(new String[]{"issue"}),
|
||||
ISSUE_COMMENT(new String[]{"issue_comment"});
|
||||
|
||||
private final List<String> typeName;
|
||||
EventType(String[] typeName) {
|
||||
this.typeName = List.of(typeName);
|
||||
}
|
||||
|
||||
|
||||
public static EventType getEvent(String event) {
|
||||
for (EventType eventType : EventType.values()) {
|
||||
if (eventType.typeName.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,27 @@
|
|||
package top.mczhengyi.gitsyncbot.bean.hook.github;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
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(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,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,18 @@
|
|||
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);
|
||||
}
|
||||
}
|
|
@ -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