Skip to content

Commit

Permalink
feat: GitHub connectors connect a project to watch - MEED-2195 - MEED…
Browse files Browse the repository at this point in the history
  • Loading branch information
AzmiTouil committed Jul 26, 2023
1 parent 838212b commit e394e53
Show file tree
Hide file tree
Showing 10 changed files with 510 additions and 399 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.meeds.gamification.service.ConnectorHookService;
import io.meeds.gamification.service.ConnectorService;
import org.exoplatform.commons.utils.CommonsUtils;
import org.exoplatform.container.PortalContainer;
import org.exoplatform.gamification.github.services.GithubHooksManagement;
import org.exoplatform.gamification.github.utils.Utils;
Expand Down Expand Up @@ -52,25 +52,29 @@ public class GithubWebHookRest implements ResourceContainer {

private static final Log LOG = ExoLogger.getLogger(GithubWebHookRest.class);

private GithubHooksManagement githubHooksManagement;
private final GithubHooksManagement githubHooksManagement;

public GithubWebHookRest(GithubHooksManagement githubHooksManagement) {
private final ConnectorHookService connectorHookService;

private final ConnectorService connectorService;

public GithubWebHookRest(GithubHooksManagement githubHooksManagement,
ConnectorHookService connectorHookService,
ConnectorService connectorService) {
this.githubHooksManagement = githubHooksManagement;
this.connectorHookService = connectorHookService;
this.connectorService = connectorService;
}

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Path("webhooks")
public Response githubEvent(// NOSONAR
@HeaderParam("x-github-event")
String event,
@HeaderParam("x-hub-signature")
String signature,
@HeaderParam("x-github-event") String event,
@HeaderParam("x-hub-signature") String signature,
String obj) {
if (Utils.verifySignature(obj, signature, githubHooksManagement.getSecret())) {

if (Utils.verifySignature(connectorHookService, obj, signature)) {
try {
ConnectorService connectorService = CommonsUtils.getService(ConnectorService.class);
ObjectMapper objectMapper = new ObjectMapper();
JsonNode infoNode = objectMapper.readTree(obj);
String ruleTitle = "";
Expand Down Expand Up @@ -171,5 +175,4 @@ public Identity getUserSocialId(String userName) {
IdentityManager identityManager = PortalContainer.getInstance().getComponentInstanceOfType(IdentityManager.class);
return identityManager.getOrCreateUserIdentity(userName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,122 +19,76 @@
import javax.ws.rs.*;
import javax.ws.rs.core.*;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.apache.commons.lang.StringUtils;

import org.exoplatform.gamification.github.entity.GitHubHookEntity;
import org.exoplatform.commons.ObjectAlreadyExistsException;
import org.exoplatform.commons.exception.ObjectNotFoundException;
import org.exoplatform.gamification.github.services.GithubHooksManagement;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.services.rest.resource.ResourceContainer;
import org.exoplatform.social.core.identity.model.Identity;
import org.exoplatform.social.service.rest.Util;
import org.exoplatform.services.security.ConversationState;

@Path("/gamification/connectors/github/hooksmanagement")
@Path("/gamification/connectors/github/hooks")
public class HooksManagementRest implements ResourceContainer {

private static final String PORTAL_CONTAINER_NAME = "portal";

private static final Log LOG = ExoLogger.getLogger(HooksManagementRest.class);

private GithubHooksManagement githubHooksManagement;
private final GithubHooksManagement githubHooksManagement;

public HooksManagementRest(GithubHooksManagement githubHooksManagement) {
this.githubHooksManagement = githubHooksManagement;
}

@GET
@RolesAllowed("administrators")
@Produces(MediaType.APPLICATION_JSON)
@Path("hooks")
public Response getHooks(
@Context
UriInfo uriInfo) {
Identity sourceIdentity = Util.getAuthenticatedUserIdentity(PORTAL_CONTAINER_NAME);
if (sourceIdentity == null
|| githubHooksManagement.getSecret() == null
|| githubHooksManagement.getToken() == null
|| githubHooksManagement.getExoEnvironment() == null) {
return Response.status(Response.Status.UNAUTHORIZED).build();
}
return Response.ok(githubHooksManagement.getAllHooks()).build();
}

@POST
@RolesAllowed("administrators")
@Consumes(MediaType.APPLICATION_JSON)
@Path("hooks")
public Response createHook(
@Context
UriInfo uriInfo,
GitHubHookEntity hook) {
Identity sourceIdentity = Util.getAuthenticatedUserIdentity(PORTAL_CONTAINER_NAME);
if (sourceIdentity == null || githubHooksManagement.getSecret() == null || githubHooksManagement.getToken() == null
|| githubHooksManagement.getExoEnvironment() == null) {
return Response.status(Response.Status.UNAUTHORIZED).build();
}
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@RolesAllowed("users")
@Operation(summary = "Create a organization webhook for Remote GitHub connector.", description = "Create a organization webhook for Remote GitHub connector.", method = "POST")
@ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Request fulfilled"),
@ApiResponse(responseCode = "400", description = "Invalid query input"),
@ApiResponse(responseCode = "401", description = "Unauthorized operation"),
@ApiResponse(responseCode = "500", description = "Internal server error") })
public Response createGitHubHook(@Parameter(description = "GitHub organization name", required = true) @FormParam("hookName") String hookName,
@Parameter(description = "Hook secret", required = true) @FormParam("hookSecret") String hookSecret) {

try {
String baseUri = uriInfo.getBaseUri().toString();
String serverDomain = baseUri.split(PORTAL_CONTAINER_NAME)[0];
hook.setWebhook(githubHooksManagement.getWebhookUrl());
String fullPath = serverDomain + githubHooksManagement.getWebhookUrl();
Long id = githubHooksManagement.addHook(fullPath, hook.getOrganization(), hook.getRepo(), hook.getEnabled());
githubHooksManagement.createHook(id, hook, true);
LOG.info("New webhook added by {}", sourceIdentity.getRemoteId());
return Response.status(Response.Status.CREATED).build();
} catch (Exception e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
if (StringUtils.isBlank(hookName)) {
return Response.status(Response.Status.BAD_REQUEST).entity("'hookName' parameter is mandatory").build();
}
}

@PUT
@RolesAllowed("administrators")
@Consumes(MediaType.APPLICATION_JSON)
@Path("hooks/{id}")
public Response editHook(
@Context
UriInfo uriInfo,
@PathParam("id")
Long id,
GitHubHookEntity hook) {
Identity sourceIdentity = Util.getAuthenticatedUserIdentity(PORTAL_CONTAINER_NAME);
if (sourceIdentity == null || githubHooksManagement.getSecret() == null || githubHooksManagement.getToken() == null
|| githubHooksManagement.getExoEnvironment() == null) {
return Response.status(Response.Status.UNAUTHORIZED).build();
if (StringUtils.isBlank(hookSecret)) {
return Response.status(Response.Status.BAD_REQUEST).entity("'hookSecret' parameter is mandatory").build();
}
String currentUser = ConversationState.getCurrent().getIdentity().getUserId();
try {
String baseUri = uriInfo.getBaseUri().toString();
String serverDomain = baseUri.split(PORTAL_CONTAINER_NAME)[0];
hook.setWebhook(githubHooksManagement.getWebhookUrl());
String fullPath = serverDomain + githubHooksManagement.getWebhookUrl();
githubHooksManagement.updateHook(hook, fullPath);
LOG.info("Webhook {} edited by {}", id, sourceIdentity.getRemoteId());
return Response.noContent().build();
} catch (Exception e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
githubHooksManagement.addHook(hookName, hookSecret, currentUser);
return Response.status(Response.Status.CREATED).build();
} catch (IllegalAccessException e) {
return Response.status(Response.Status.UNAUTHORIZED).entity(e.getMessage()).build();
} catch (ObjectAlreadyExistsException e) {
return Response.status(Response.Status.CONFLICT).entity(e.getMessage()).build();
}
}

@DELETE
@RolesAllowed("administrators")
@Path("hooks/{id}")
public Response deleteHook(
@Context
UriInfo uriInfo,
@PathParam("id")
Long id) {
Identity sourceIdentity = Util.getAuthenticatedUserIdentity(PORTAL_CONTAINER_NAME);
if (sourceIdentity == null || githubHooksManagement.getSecret() == null || githubHooksManagement.getToken() == null
|| githubHooksManagement.getExoEnvironment() == null) {
return Response.status(Response.Status.UNAUTHORIZED).build();
@Path("{hookName}")
@RolesAllowed("users")
@Operation(summary = "Deletes gitHub connector hook", description = "Deletes gitHub connector hook", method = "DELETE")
@ApiResponses(value = {
@ApiResponse(responseCode = "204", description = "Request fulfilled"),
@ApiResponse(responseCode = "400", description = "Bad request"),
@ApiResponse(responseCode = "401", description = "Unauthorized operation"),
@ApiResponse(responseCode = "500", description = "Internal server error"), })
public Response deleteGitHubHook(@Parameter(description = "GitHub organization name", required = true) @PathParam("hookName") String hookName) {
if (StringUtils.isBlank(hookName)) {
return Response.status(Response.Status.BAD_REQUEST).entity("'hookName' parameter is mandatory").build();
}
String currentUser = ConversationState.getCurrent().getIdentity().getUserId();
try {
GitHubHookEntity hook = githubHooksManagement.getHookEntityById(id);
githubHooksManagement.deleteHook(hook);
LOG.info("Webhook {} deleted by {}", id, sourceIdentity.getRemoteId());
githubHooksManagement.deleteHook(hookName, currentUser);
return Response.noContent().build();
} catch (Exception e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
} catch (IllegalAccessException e) {
return Response.status(Response.Status.UNAUTHORIZED).entity(e.getMessage()).type(MediaType.TEXT_PLAIN).build();
} catch (ObjectNotFoundException e) {
return Response.status(Response.Status.NOT_FOUND).entity("The GitHub hook doesn't exit").build();
}
}
}
Loading

0 comments on commit e394e53

Please sign in to comment.