Skip to content

Commit

Permalink
Release
Browse files Browse the repository at this point in the history
  • Loading branch information
MouamleH committed Sep 22, 2019
1 parent 59626da commit 1bec86d
Show file tree
Hide file tree
Showing 27 changed files with 3,209 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/main/java/mouamle/tggh/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package mouamle.tggh;

import mouamle.tggh.common.ServiceInterface;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {
Expand All @@ -10,4 +14,12 @@ public static void main(String[] args) {
SpringApplication.run(Application.class);
}

@Bean
ApplicationRunner init(@Qualifier("githubInterface") ServiceInterface githubInterface,
@Qualifier("telegramInterface") ServiceInterface telegramInterface) {
return args -> {
githubInterface.init();
telegramInterface.init();
};
}
}
5 changes: 5 additions & 0 deletions src/main/java/mouamle/tggh/common/ServiceInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package mouamle.tggh.common;

public interface ServiceInterface {
void init();
}
33 changes: 33 additions & 0 deletions src/main/java/mouamle/tggh/common/Settings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package mouamle.tggh.common;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Settings {

private final String botUsername;
private final int creatorId;
private final long groupId;

public Settings(@Value("${app.telegram-username}") String botUsername,
@Value("${app.telegram-creatorId}") int creatorId,
@Value("${app.telegram-groupId}") long groupId) {
this.botUsername = botUsername;
this.creatorId = creatorId;
this.groupId = groupId;
}

public String getBotUsername() {
return botUsername;
}

public int getCreatorId() {
return creatorId;
}

public long getGroupId() {
return groupId;
}

}
27 changes: 27 additions & 0 deletions src/main/java/mouamle/tggh/common/Tokens.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package mouamle.tggh.common;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Tokens {

private final String telegramToken;
private final String githubToken;

public Tokens(@Value("${app.telegram-token}") String telegramToken,
@Value("${app.github-token}") String githubToken) {
this.telegramToken = telegramToken;
this.githubToken = githubToken;
}


public String getTelegramToken() {
return telegramToken;
}

public String getGithubToken() {
return githubToken;
}

}
12 changes: 12 additions & 0 deletions src/main/java/mouamle/tggh/github/GithubInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mouamle.tggh.github;

import mouamle.tggh.common.ServiceInterface;
import org.springframework.stereotype.Service;

@Service
public class GithubInterface implements ServiceInterface {

@Override
public void init() { /*TODO*/ }

}
30 changes: 30 additions & 0 deletions src/main/java/mouamle/tggh/github/service/GithubService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package mouamle.tggh.github.service;

import mouamle.tggh.common.Tokens;
import org.eclipse.egit.github.core.User;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.service.UserService;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class GithubService {

private GitHubClient client;

public GithubService(Tokens tokens) {
client = new GitHubClient();
client.setOAuth2Token(tokens.getGithubToken());
}

public GitHubClient getClient() {
return client;
}

public User getUser(String name) throws IOException {
UserService service = new UserService(client);
return service.getUser(name);
}

}
52 changes: 52 additions & 0 deletions src/main/java/mouamle/tggh/github/web/IssuesEndpoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package mouamle.tggh.github.web;

import mouamle.tggh.github.web.model.IssueEvent;
import mouamle.tggh.github.web.service.PayloadRegistry;
import mouamle.tggh.util.bus.EventBus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class IssuesEndpoint {

private final EventBus<IssueEvent> bus;
private final PayloadRegistry payloadRegistry;

@Autowired
public IssuesEndpoint(EventBus<IssueEvent> bus, PayloadRegistry payloadRegistry) {
this.payloadRegistry = payloadRegistry;
this.bus = bus;
}

@PostMapping("hook")
public void hook(@RequestBody String payload,
@RequestHeader("X-Hub-Signature") String signature,
@RequestHeader("X-Github-Event") String event) throws IOException {

validateSignature(signature);
payloadRegistry.process(event, payload, bus::publish);
}

private void validateSignature(String signature) {
if (!signature.trim().isEmpty()) {
String[] split = signature.split("=");
if (split.length == 2) {
String hash = split[1];
if (isValidSHA1(hash)) {
return;
}
}
}
throw new RuntimeException("Nope!");
}

private boolean isValidSHA1(String s) {
return s.matches("^[a-fA-F0-9]{40}$");
}

}
Loading

0 comments on commit 1bec86d

Please sign in to comment.