-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
3,209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package mouamle.tggh.common; | ||
|
||
public interface ServiceInterface { | ||
void init(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
src/main/java/mouamle/tggh/github/service/GithubService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}$"); | ||
} | ||
|
||
} |
Oops, something went wrong.