Skip to content

Commit

Permalink
Add GitHub webhook receiver #123
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-cd committed Aug 9, 2022
1 parent 5182bd2 commit 97a5a61
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
import i5.las2peer.restMapper.annotations.ServicePath;
import i5.las2peer.security.BotAgent;
import i5.las2peer.services.socialBotManagerService.chat.*;
import i5.las2peer.services.socialBotManagerService.chat.github.GitHubWebhookReceiver;
import i5.las2peer.services.socialBotManagerService.chat.xAPI.ChatStatement;
import i5.las2peer.services.socialBotManagerService.database.SQLDatabase;
import i5.las2peer.services.socialBotManagerService.database.SQLDatabaseType;
Expand Down Expand Up @@ -240,6 +241,7 @@ protected void initResources() {
getResourceConfig().register(BotModelResource.class);
getResourceConfig().register(TrainingResource.class);
getResourceConfig().register(this);
getResourceConfig().register(GitHubWebhookReceiver.class);
}

@POST
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package i5.las2peer.services.socialBotManagerService.chat.github;

import i5.las2peer.api.Context;
import i5.las2peer.services.socialBotManagerService.SocialBotManagerService;
import i5.las2peer.services.socialBotManagerService.chat.ChatService;
import i5.las2peer.services.socialBotManagerService.model.Bot;
import i5.las2peer.services.socialBotManagerService.model.Messenger;
import i5.las2peer.services.socialBotManagerService.model.VLE;
import io.swagger.annotations.Api;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;

import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
import java.util.Collection;

@Api(value = "GitHub Webhook Receiver Resource")
@Path("/github")
public class GitHubWebhookReceiver {

/**
* Receives incoming webhook events from a GitHub app and sends them to related GitHub chat mediators.
*
* @param body Event
* @param eventName Name of event
* @param gitHubAppId Id of GitHub app
* @return 200
*/
@POST
@Path("/webhook/{gitHubAppId}")
public Response receiveWebhookEvent(String body, @HeaderParam("X-GitHub-Event") String eventName,
@PathParam("gitHubAppId") int gitHubAppId) {
JSONObject payload = (JSONObject) JSONValue.parse(body);

// put name of event and payload into one JSONObject
JSONObject eventObj = new JSONObject();
eventObj.put("event", eventName);
eventObj.put("payload", payload);

SocialBotManagerService service = (SocialBotManagerService) Context.get().getService();

// need to find bot(s) that use this GitHub app id
Collection<VLE> vles = service.getConfig().getVLEs().values();
for (VLE vle : vles) {
for (Bot bot : vle.getBots().values()) {
Messenger messenger = bot.getMessenger(ChatService.GITHUB_ISSUES);
if (messenger != null) {
GitHubIssueMediator mediator = (GitHubIssueMediator) messenger.getChatMediator();
if (mediator.getGitHubAppId() == gitHubAppId) {
mediator.handleEvent(eventObj);
}
}

messenger = bot.getMessenger(ChatService.GITHUB_PR);
if (messenger != null) {
GitHubPRMediator mediator = (GitHubPRMediator) messenger.getChatMediator();
if (mediator.getGitHubAppId() == gitHubAppId) {
mediator.handleEvent(eventObj);
}
}
}
}

return Response.status(200).build();
}
}

0 comments on commit 97a5a61

Please sign in to comment.