From 54a897c285124edce89124a5912d854df5eb227f Mon Sep 17 00:00:00 2001 From: Azmi TOUIL <42934070+AzmiTouil@users.noreply.github.com> Date: Wed, 29 Nov 2023 14:25:51 +0100 Subject: [PATCH 1/2] feat: Implement second step for automatic action - Application + event - MEED-2962 - Meeds-io/MIPs#105 (#132) --- .../github/rest/HooksManagementRest.java | 24 --------- .../github/services/GithubTriggerService.java | 7 --- .../github/services/WebhookService.java | 15 ------ .../impl/GithubTriggerServiceImpl.java | 35 ++++--------- .../services/impl/WebhookServiceImpl.java | 44 ++-------------- ...ication-github-connector-configuration.xml | 20 ++++---- .../GithubAdminConnectorEventItem.vue | 34 +++++-------- .../GithubAdminConnectorEventList.vue | 51 +++++++++---------- .../js/GithubConnectorService.js | 20 -------- 9 files changed, 61 insertions(+), 189 deletions(-) diff --git a/gamification-github-services/src/main/java/org/exoplatform/gamification/github/rest/HooksManagementRest.java b/gamification-github-services/src/main/java/org/exoplatform/gamification/github/rest/HooksManagementRest.java index d9b09f38..0a81380e 100644 --- a/gamification-github-services/src/main/java/org/exoplatform/gamification/github/rest/HooksManagementRest.java +++ b/gamification-github-services/src/main/java/org/exoplatform/gamification/github/rest/HooksManagementRest.java @@ -251,30 +251,6 @@ public Response updateWebHookRepoStatus(@Parameter(description = "GitHub organiz } } - @Path("events/status") - @POST - @RolesAllowed("users") - @Operation(summary = "enables/disables event for gitHub organization.", description = "enables/disables event for gitHub organization", 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 = "Event Id", required = true) @FormParam("eventId") long eventId, - @Parameter(description = "Organization remote Id", required = true) @FormParam("organizationId") long organizationId, - @Parameter(description = "Event status enabled/disabled. possible values: true for enabled, else false", required = true) @FormParam("enabled") boolean enabled) { - - String currentUser = getCurrentUser(); - try { - webhookService.setEventEnabledForOrganization(eventId, organizationId, enabled, currentUser); - return Response.noContent().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("Event not found").build(); - } - } - @Path("watchScope/status") @POST @RolesAllowed("users") diff --git a/gamification-github-services/src/main/java/org/exoplatform/gamification/github/services/GithubTriggerService.java b/gamification-github-services/src/main/java/org/exoplatform/gamification/github/services/GithubTriggerService.java index 0a563e4c..7f85b3cd 100644 --- a/gamification-github-services/src/main/java/org/exoplatform/gamification/github/services/GithubTriggerService.java +++ b/gamification-github-services/src/main/java/org/exoplatform/gamification/github/services/GithubTriggerService.java @@ -52,11 +52,4 @@ public interface GithubTriggerService { * @param payload payload The raw payload of the webhook request. */ void handleTrigger(String trigger, String signature, String payload); - - /** - * Gets list of configured github triggers - * - * @return list of configured github triggers - */ - String[] getTriggers(); } diff --git a/gamification-github-services/src/main/java/org/exoplatform/gamification/github/services/WebhookService.java b/gamification-github-services/src/main/java/org/exoplatform/gamification/github/services/WebhookService.java index d3375e85..4d347e84 100644 --- a/gamification-github-services/src/main/java/org/exoplatform/gamification/github/services/WebhookService.java +++ b/gamification-github-services/src/main/java/org/exoplatform/gamification/github/services/WebhookService.java @@ -197,19 +197,4 @@ List retrieveOrganizationRepos(long organizationRemoteId, int perPage, String keyword) throws IllegalAccessException, ObjectNotFoundException; - /** - * Enables/disables organization event - * - * @param eventId event Id - * @param organizationId organization remote id - * @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 - * organization event - */ - void setEventEnabledForOrganization(long eventId, - long organizationId, - boolean enabled, - String currentUser) throws IllegalAccessException, ObjectNotFoundException; - } diff --git a/gamification-github-services/src/main/java/org/exoplatform/gamification/github/services/impl/GithubTriggerServiceImpl.java b/gamification-github-services/src/main/java/org/exoplatform/gamification/github/services/impl/GithubTriggerServiceImpl.java index d565661d..a7c1d943 100644 --- a/gamification-github-services/src/main/java/org/exoplatform/gamification/github/services/impl/GithubTriggerServiceImpl.java +++ b/gamification-github-services/src/main/java/org/exoplatform/gamification/github/services/impl/GithubTriggerServiceImpl.java @@ -21,6 +21,7 @@ import io.meeds.gamification.model.filter.EventFilter; import io.meeds.gamification.service.ConnectorService; import io.meeds.gamification.service.EventService; +import io.meeds.gamification.service.TriggerService; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.eclipse.jetty.util.thread.QueuedThreadPool; @@ -40,7 +41,6 @@ import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.stream.Collectors; import static org.exoplatform.gamification.github.utils.Utils.*; @@ -54,6 +54,8 @@ public class GithubTriggerServiceImpl implements GithubTriggerService, Startable private final EventService eventService; + private final TriggerService triggerService; + private WebhookService webhookService; private final IdentityManager identityManager; @@ -65,11 +67,13 @@ public class GithubTriggerServiceImpl implements GithubTriggerService, Startable public GithubTriggerServiceImpl(ListenerService listenerService, ConnectorService connectorService, IdentityManager identityManager, - EventService eventService) { + EventService eventService, + TriggerService triggerService) { this.listenerService = listenerService; this.connectorService = connectorService; this.identityManager = identityManager; this.eventService = eventService; + this.triggerService = triggerService; } @Override @@ -118,28 +122,15 @@ public void handleTrigger(String trigger, String signature, String payload) { if (triggerPlugin != null) { events = triggerPlugin.getEvents(payloadMap); } - processEvents(events, trigger, organizationId); + processEvents(events, organizationId); } - private void processEvents(List events, String trigger, String organizationId) { - events.stream().filter(event -> isEventEnabled(event.getName(), trigger, organizationId)).forEach(this::processEvent); + private void processEvents(List events, String organizationId) { + events.stream().filter(event -> isTriggerEnabled(event.getName(), organizationId)).forEach(this::processEvent); } - private boolean isEventEnabled(String eventName, String trigger, String organizationId) { - EventDTO eventDTO = eventService.getEventByTitleAndTrigger(eventName, trigger); - if (eventDTO != null) { - return isOrganizationEventEnabled(eventDTO, organizationId); - } - return true; - } - - private boolean isOrganizationEventEnabled(EventDTO eventDTO, String organizationId) { - String organizationPropertyKey = organizationId + ".enabled"; - Map properties = eventDTO.getProperties(); - if (properties != null && !properties.isEmpty()) { - return Boolean.parseBoolean(properties.get(organizationPropertyKey)); - } - return true; + private boolean isTriggerEnabled(String trigger, String organizationId) { + return triggerService.isTriggerEnabledForAccount(trigger, Long.parseLong(organizationId)); } private void processEvent(Event event) { @@ -158,10 +149,6 @@ private void processEvent(Event event) { } } - public String[] getTriggers() { - return triggerPlugins.values().stream().map(GithubTriggerPlugin::getName).toArray(String[]::new); - } - private void broadcastGithubEvent(String ruleTitle, String senderId, String receiverId, String objectId, String objectType) { try { Map gam = new HashMap<>(); diff --git a/gamification-github-services/src/main/java/org/exoplatform/gamification/github/services/impl/WebhookServiceImpl.java b/gamification-github-services/src/main/java/org/exoplatform/gamification/github/services/impl/WebhookServiceImpl.java index c8c658b0..686a67c5 100644 --- a/gamification-github-services/src/main/java/org/exoplatform/gamification/github/services/impl/WebhookServiceImpl.java +++ b/gamification-github-services/src/main/java/org/exoplatform/gamification/github/services/impl/WebhookServiceImpl.java @@ -36,7 +36,6 @@ import org.exoplatform.gamification.github.model.WebHook; import org.exoplatform.gamification.github.model.RemoteOrganization; import org.exoplatform.gamification.github.services.GithubConsumerService; -import org.exoplatform.gamification.github.services.GithubTriggerService; import org.exoplatform.gamification.github.services.WebhookService; import org.exoplatform.gamification.github.storage.WebHookStorage; import org.json.JSONObject; @@ -51,28 +50,21 @@ public class WebhookServiceImpl implements WebhookService { private static final Scope DISABLED_REPOS_SCOPE = Scope.APPLICATION.id("disabledRepos"); - public static final String ENABLED = ".enabled"; + private static final String[] GITHUB_TRIGGERS = new String[] { "pull_request", "issue_comment", + "pull_request_review_comment", "pull_request_review", "issues", "push" }; private final SettingService settingService; private final WebHookStorage webHookStorage; - private final GithubTriggerService githubTriggerService; - private final GithubConsumerService githubServiceConsumer; - private final EventService eventService; - public WebhookServiceImpl(SettingService settingService, - GithubTriggerService githubTriggerService, WebHookStorage webHookStorage, - GithubConsumerService githubServiceConsumer, - EventService eventService) { + GithubConsumerService githubServiceConsumer) { this.settingService = settingService; - this.githubTriggerService = githubTriggerService; this.webHookStorage = webHookStorage; this.githubServiceConsumer = githubServiceConsumer; - this.eventService = eventService; } public List getWebhooks(String currentUser, int offset, int limit, boolean forceUpdate) throws IllegalAccessException { @@ -142,7 +134,7 @@ public void createWebhook(String organizationName, String accessToken, String cu if (existsWebHook != null) { throw new ObjectAlreadyExistsException(existsWebHook); } - WebHook webHook = githubServiceConsumer.createWebhook(organizationName, githubTriggerService.getTriggers(), accessToken); + WebHook webHook = githubServiceConsumer.createWebhook(organizationName, GITHUB_TRIGGERS, accessToken); if (webHook != null) { webHook.setOrganizationId(remoteOrganization.getId()); @@ -293,34 +285,6 @@ public List retrieveOrganizationRepos(long organizationRemoteI return remoteRepositories; } - @Override - public void setEventEnabledForOrganization(long eventId, - long organizationId, - boolean enabled, - String currentUser) throws IllegalAccessException, ObjectNotFoundException { - if (!Utils.isRewardingManager(currentUser)) { - throw new IllegalAccessException("The user is not authorized to enable/disable event for organization"); - } - EventDTO eventDTO = eventService.getEvent(eventId); - if (eventDTO == null) { - throw new ObjectNotFoundException("event not found"); - } - Map eventProperties = eventDTO.getProperties(); - if (eventProperties != null && !eventProperties.isEmpty()) { - String eventProjectStatus = eventProperties.get(organizationId + ENABLED); - if (StringUtils.isNotBlank(eventProjectStatus)) { - eventProperties.remove(organizationId + ENABLED); - eventProperties.put(organizationId + ENABLED, String.valueOf(enabled)); - eventDTO.setProperties(eventProperties); - } - } else { - Map properties = new HashMap<>(); - properties.put(organizationId + ENABLED, String.valueOf(enabled)); - eventDTO.setProperties(properties); - } - eventService.updateEvent(eventDTO); - } - @SuppressWarnings("unchecked") private void forceUpdateWebhook(WebHook webHook) { TokenStatus tokenStatus = githubServiceConsumer.checkGitHubTokenStatus(webHook.getToken()); diff --git a/gamification-github-services/src/main/resources/conf/portal/gamification-github-connector-configuration.xml b/gamification-github-services/src/main/resources/conf/portal/gamification-github-connector-configuration.xml index 3ccdf995..0a65a404 100644 --- a/gamification-github-services/src/main/resources/conf/portal/gamification-github-connector-configuration.xml +++ b/gamification-github-services/src/main/resources/conf/portal/gamification-github-connector-configuration.xml @@ -37,7 +37,7 @@ github - push + pushCode @@ -58,7 +58,7 @@ github - pull_request + creatPullRequest @@ -86,7 +86,7 @@ github - pull_request + requestReviewForPullRequest @@ -114,7 +114,7 @@ github - pull_request_review + reviewPullRequest @@ -135,7 +135,7 @@ github - pull_request_review + pullRequestValidated @@ -156,7 +156,7 @@ github - issue_comment + commentPullRequest @@ -184,7 +184,7 @@ github - issues + addIssueLabel @@ -212,7 +212,7 @@ github - pull_request_review + validatePullRequest @@ -233,7 +233,7 @@ github - issue_comment + commentIssue @@ -261,7 +261,7 @@ github - issues + createIssue diff --git a/gamification-github-webapp/src/main/webapp/vue-app/githubAdminConnectorExtension/components/GithubAdminConnectorEventItem.vue b/gamification-github-webapp/src/main/webapp/vue-app/githubAdminConnectorExtension/components/GithubAdminConnectorEventItem.vue index 5166d4a2..85671c80 100644 --- a/gamification-github-webapp/src/main/webapp/vue-app/githubAdminConnectorExtension/components/GithubAdminConnectorEventItem.vue +++ b/gamification-github-webapp/src/main/webapp/vue-app/githubAdminConnectorExtension/components/GithubAdminConnectorEventItem.vue @@ -1,8 +1,8 @@