Skip to content

Commit

Permalink
feat: GitHub connectors List events available in a project - MEED-2301
Browse files Browse the repository at this point in the history
…- Meeds-io/MIPs#64 (#83)

Allow rewarding users to enable/disable project events
  • Loading branch information
AzmiTouil committed Sep 20, 2023
1 parent 7c5a6ff commit 8a04613
Show file tree
Hide file tree
Showing 23 changed files with 494 additions and 121 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2023 Meeds Association [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.exoplatform.gamification.github.model;


import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class Event {

private String name;

private boolean enabled;

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class WebHook implements Cloneable {

private long organizationId;

private List<String> event;
private List<String> triggers;

private Boolean enabled;

Expand All @@ -53,7 +53,7 @@ public WebHook clone() { // NOSONAR
return new WebHook(id,
webhookId,
organizationId,
event,
triggers,
enabled,
watchedDate,
watchedBy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package org.exoplatform.gamification.github.plugin;

import java.util.Map;
import java.util.*;

import static org.exoplatform.gamification.github.utils.Utils.*;

Expand All @@ -42,4 +42,9 @@ public String parseGithubObject(Map<String, Object> payload) {
public String getEventName(Map<String, Object> payload) {
return COMMENT_PULL_REQUEST_EVENT_NAME;
}

@Override
public List<String> getEvents() {
return Collections.singletonList(COMMENT_PULL_REQUEST_EVENT_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.exoplatform.container.component.BaseComponentPlugin;
import org.exoplatform.gamification.github.services.WebhookService;

import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -52,4 +53,11 @@ public abstract class GithubTriggerPlugin extends BaseComponentPlugin {
* @return the event name
*/
public abstract String getEventName(Map<String, Object> payload);

/**
* Gets events that can be reached form trigger
**
* @return the List of events
*/
public abstract List<String> getEvents();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package org.exoplatform.gamification.github.plugin;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static org.exoplatform.gamification.github.utils.Utils.*;
Expand Down Expand Up @@ -55,4 +57,9 @@ public String getEventName(Map<String, Object> payload) {
}
return null;
}

@Override
public List<String> getEvents() {
return Arrays.asList(REVIEW_PULL_REQUEST_EVENT_NAME, PULL_REQUEST_VALIDATED_EVENT_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
*/
package org.exoplatform.gamification.github.plugin;

import java.util.Map;
import java.util.Objects;
import java.util.*;

import static org.exoplatform.gamification.github.utils.Utils.*;
import static org.exoplatform.gamification.github.utils.Utils.extractSubItem;
Expand Down Expand Up @@ -47,4 +46,9 @@ public String getEventName(Map<String, Object> payload) {
}
return null;
}

@Override
public List<String> getEvents() {
return Collections.singletonList(CREATE_PULL_REQUEST_EVENT_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
package org.exoplatform.gamification.github.plugin;

import java.util.Map;
import java.util.*;

import static org.exoplatform.gamification.github.utils.Utils.*;
import static org.exoplatform.gamification.github.utils.Utils.extractSubItem;
Expand All @@ -43,4 +43,9 @@ public String parseGithubObject(Map<String, Object> payload) {
public String getEventName(Map<String, Object> payload) {
return PUSH_CODE_EVENT_NAME;
}

@Override
public List<String> getEvents() {
return Collections.singletonList(PUSH_CODE_EVENT_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.exoplatform.gamification.github.rest.model.RepositoryList;
import org.exoplatform.gamification.github.rest.model.WebHookList;
import org.exoplatform.gamification.github.rest.model.WebHookRestEntity;
import org.exoplatform.gamification.github.services.GithubTriggerService;
import org.exoplatform.gamification.github.services.WebhookService;
import org.exoplatform.services.rest.http.PATCH;
import org.exoplatform.services.rest.resource.ResourceContainer;
Expand All @@ -47,12 +48,15 @@
@Path("/gamification/connectors/github/hooks")
public class HooksManagementRest implements ResourceContainer {

public static final String GITHUB_HOOK_NOT_FOUND = "The GitHub hook doesn't exit";
public static final String GITHUB_HOOK_NOT_FOUND = "The GitHub hook doesn't exit";

private final WebhookService webhookService;
private final WebhookService webhookService;

public HooksManagementRest(WebhookService webhookService) {
private final GithubTriggerService githubTriggerService;

public HooksManagementRest(WebhookService webhookService, GithubTriggerService githubTriggerService) {
this.webhookService = webhookService;
this.githubTriggerService = githubTriggerService;
}

@GET
Expand Down Expand Up @@ -203,10 +207,11 @@ public Response getWebHookRepos(@Parameter(description = "GitHub organization id
@POST
@RolesAllowed("users")
@Operation(summary = "enables/disables webhook repository.", description = "enables/disables webhook repository", method = "POST")
@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"), })
@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 updateWebHookRepoStatus(@Parameter(description = "GitHub organization remote Id", required = true) @FormParam("organizationId") long organizationId,
@Parameter(description = "Organization repository remote Id", required = true) @FormParam("repositoryId") long repositoryId,
@Parameter(description = "Organization repository status enabled/disabled. possible values: true for enabled, else false", required = true) @FormParam("enabled") boolean enabled) {
Expand All @@ -224,7 +229,8 @@ public Response updateWebHookRepoStatus(@Parameter(description = "GitHub organiz
@POST
@RolesAllowed("users")
@Operation(summary = "Limit webhook watch scope or not", description = "Limit webhook watch scope or not", method = "POST")
@ApiResponses(value = { @ApiResponse(responseCode = "204", description = "Request fulfilled"),
@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"), })
Expand All @@ -240,8 +246,30 @@ public Response updateWebHookWatchScope(@Parameter(description = "GitHub organiz
}
}

@Path("event/status")
@POST
@RolesAllowed("users")
@Operation(summary = "enables/disables webhook event.", description = "enables/disables webhook event", method = "POST")
@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 updateWebHookEventStatus(@Parameter(description = "GitHub organization remote Id", required = true) @FormParam("organizationId") long organizationId,
@Parameter(description = "Event name", required = true) @FormParam("event") String event,
@Parameter(description = "Event status enabled/disabled. possible values: true for enabled, else false", required = true) @FormParam("enabled") boolean enabled) {

String currentUser = getCurrentUser();
try {
githubTriggerService.setEventEnabled(organizationId, event, enabled, currentUser);
return Response.noContent().build();
} catch (IllegalAccessException e) {
return Response.status(Response.Status.UNAUTHORIZED).entity(e.getMessage()).type(MediaType.TEXT_PLAIN).build();
}
}

private List<WebHookRestEntity> getWebHookRestEntities(String username) throws IllegalAccessException {
Collection<WebHook> webHooks = webhookService.getWebhooks(username, 0, 20, false);
return WebHookBuilder.toRestEntities(webhookService, webHooks);
return WebHookBuilder.toRestEntities(webhookService, githubTriggerService, webHooks);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import java.util.Collection;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.exoplatform.gamification.github.model.RemoteOrganization;
import org.exoplatform.gamification.github.model.WebHook;
import org.exoplatform.gamification.github.rest.model.WebHookRestEntity;
import org.exoplatform.gamification.github.services.GithubTriggerService;
import org.exoplatform.gamification.github.services.WebhookService;

public class WebHookBuilder {
Expand All @@ -31,18 +33,19 @@ private WebHookBuilder() {
// Class with static methods
}

public static WebHookRestEntity toRestEntity(WebhookService webhookService, WebHook webHook) {
public static WebHookRestEntity toRestEntity(WebhookService webhookService, GithubTriggerService githubTriggerService, WebHook webHook) {
if (webHook == null) {
return null;
}

RemoteOrganization remoteOrganization = webhookService.retrieveRemoteOrganization(webHook.getOrganizationId(),
webHook.getToken());


return new WebHookRestEntity(webHook.getId(),
webHook.getWebhookId(),
webHook.getOrganizationId(),
webHook.getEvent(),
githubTriggerService.getEvents(webHook),
webHook.getEnabled(),
webHook.getWatchedDate(),
webHook.getWatchedBy(),
Expand All @@ -55,7 +58,7 @@ public static WebHookRestEntity toRestEntity(WebhookService webhookService, WebH
webhookService.isWebHookWatchLimitEnabled(webHook.getOrganizationId()));
}

public static List<WebHookRestEntity> toRestEntities(WebhookService webhookService, Collection<WebHook> webHooks) {
return webHooks.stream().map(webHook -> toRestEntity(webhookService, webHook)).toList();
public static List<WebHookRestEntity> toRestEntities(WebhookService webhookService, GithubTriggerService githubTriggerService, Collection<WebHook> webHooks) {
return webHooks.stream().map(webHook -> toRestEntity(webhookService, githubTriggerService, webHook)).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.exoplatform.gamification.github.rest.model;

import lombok.*;
import org.exoplatform.gamification.github.model.Event;

import java.util.List;

@AllArgsConstructor
Expand All @@ -30,7 +32,7 @@ public class WebHookRestEntity {

private long organizationId;

private List<String> event;
private List<Event> events;

private Boolean enabled;

Expand All @@ -55,7 +57,7 @@ public class WebHookRestEntity {
public WebHookRestEntity(long id, // NOSONAR
long webhookId,
long organizationId,
List<String> event,
List<Event> event,
boolean enabled,
String watchedDate,
String watchedBy,
Expand All @@ -70,7 +72,7 @@ public WebHookRestEntity(long id, // NOSONAR
this.id = id;
this.webhookId = webhookId;
this.organizationId = organizationId;
this.event = event;
this.events = event;
this.enabled = enabled;
this.name = name;
this.watchedDate = watchedDate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
*/
package org.exoplatform.gamification.github.services;

import org.exoplatform.gamification.github.model.WebHook;
import org.exoplatform.gamification.github.plugin.GithubTriggerPlugin;
import org.exoplatform.gamification.github.model.Event;
import java.util.List;

public interface GithubTriggerService {

Expand Down Expand Up @@ -49,4 +52,33 @@ public interface GithubTriggerService {
* @return list of configured github triggers
*/
String[] getTriggers();

/**
* Gets events that can be reached form all triggers
*
* @param webHook webHook
* @return {@link List} of {@link Event} of events
*/
List<Event> getEvents(WebHook webHook);

/**
* Check if webhook event is enabled
*
* @param organizationId github remote organization id
* @param event event name
* @return true if the webhook event is enabled, else false.
*/
boolean isEventEnabled(long organizationId, String event);

/**
* Enables/disables webhook event
*
* @param organizationId github remote organization id
* @param event event name
* @param enabled true to enabled, else false
* @param currentUser user name attempting to enables/disables event.
* @throws IllegalAccessException when user is not authorized enables/disables
* webhook event
*/
void setEventEnabled(long organizationId, String event, boolean enabled, String currentUser) throws IllegalAccessException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ void updateWebHookAccessToken(long webHookId, String accessToken, String current
* @param repositoryRemoteId gitHub repository remote Id
* @param enabled true to enabled, else false
* @param currentUser user name attempting to enables/disables repository.
* @throws IllegalAccessException when user is not authorized enables/disables
* repository
*/
void setWebHookRepositoryEnabled(long organizationRemoteId,
long repositoryRemoteId,
Expand All @@ -156,6 +158,8 @@ void setWebHookRepositoryEnabled(long organizationRemoteId,
* @param enabled true to enabled, else false
* @param currentUser user name attempting to enables/disables webHook watch
* limit.
* @throws IllegalAccessException when user is not authorized Limit webhook
* watch scope
*/
void setWebHookWatchLimitEnabled(long organizationRemoteId, boolean enabled, String currentUser) throws IllegalAccessException;

Expand Down
Loading

0 comments on commit 8a04613

Please sign in to comment.