Skip to content

Commit

Permalink
feat: Added Four Events - Meeds-io/MIPs#121 (#2)
Browse files Browse the repository at this point in the history
These events were added stringCommentCreated, suggestionAdded,
suggestionApproved, approveSuggestion
  • Loading branch information
sergeByishimo authored Mar 1, 2024
1 parent dd7335d commit 410dae9
Show file tree
Hide file tree
Showing 26 changed files with 511 additions and 424 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2023 Meeds Lab [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 io.meeds.gamification.crowdin.model;

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

@Getter
@Setter
@AllArgsConstructor
public class Event {

String name;

String sender;

String receiver;

String objectId;

String objectType;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.meeds.gamification.crowdin.plugin;

import io.meeds.gamification.model.EventDTO;
import io.meeds.gamification.service.EventRegistry;
import jakarta.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.exoplatform.container.component.BaseComponentPlugin;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

@Component
public class SuggestionTriggerPlugin extends BaseComponentPlugin {

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

protected String EVENT_TITLE = "suggestionAdded";
protected String EVENT_TYPE = "crowdin";
protected String EVENT_TRIGGER = "suggestion.added";
protected List<String> EVENT_CANCELLER_EVENTS = Collections.singletonList("suggestionDeleted");


}
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,15 @@
import io.meeds.gamification.crowdin.rest.model.WebHookList;
import io.meeds.gamification.crowdin.rest.model.WebHookRestEntity;
import io.meeds.gamification.crowdin.services.WebhookService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.apache.commons.lang3.StringUtils;

import org.exoplatform.commons.ObjectAlreadyExistsException;
import org.exoplatform.commons.exception.ObjectNotFoundException;
import org.exoplatform.services.security.ConversationState;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -91,6 +87,30 @@ public List<WebHookRestEntity> getWebHooks(@RequestParam("offset") int offset,
}
}

@GetMapping("{webHookId}")
@Secured("rewarding")
@Operation(summary = "Retrieves a webHook by its technical identifier", method = "GET")
@ApiResponse(responseCode = "200", description = "Request fulfilled")
@ApiResponse(responseCode = "404", description = "Not found")
@ApiResponse(responseCode = "400", description = "Bad request")
@ApiResponse(responseCode = "401", description = "Unauthorized")
@ApiResponse(responseCode = "503", description = "Service unavailable")
public WebHook getWebHookById(@Parameter(description = "WebHook technical identifier", required = true) @PathVariable("webHookId") long webHookId) {
if (webHookId == 0) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "WebHook Id must be not null");
}
String currentUser = getCurrentUser();
try {
return webhookService.getWebhookId(webHookId, currentUser);
} catch (IllegalArgumentException e) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
} catch (IllegalAccessException e) {
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, e.getMessage());
} catch (ObjectNotFoundException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage());
}
}

@GetMapping("get-projects")
@Secured("rewarding")
@Operation(summary = "Retrieves a list of projects from crowdin", method = "GET")
Expand Down Expand Up @@ -169,7 +189,7 @@ public void deleteWebhookHook(@Parameter(description = "Crowdin project id", req

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(webHooks);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ public class WebHookBuilder {

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

@Autowired
private WebhookService webhookService;

@Autowired
private CrowdinConsumerStorage crowdinConsumerStorage;

private WebHookBuilder() {
// Class with static methods
}

public WebHookRestEntity toRestEntity(WebhookService webhookService, WebHook webHook) {
public WebHookRestEntity toRestEntity(WebHook webHook) {
if (webHook == null) {
return null;
}
Expand All @@ -66,10 +69,11 @@ public WebHookRestEntity toRestEntity(WebhookService webhookService, WebHook web
remoteProject != null ? remoteProject.getIdentifier() : null,
remoteProject != null ? remoteProject.getDescription() : null,
remoteProject != null ? remoteProject.getAvatarUrl() : null,
webhookService.isWebHookWatchLimitEnabled(webHook.getProjectId()));
webhookService.isWebHookWatchLimitEnabled(webHook.getProjectId()),
remoteProject != null);
}

public List<WebHookRestEntity> toRestEntities(WebhookService webhookService, Collection<WebHook> webHooks) {
return webHooks.stream().map(webHook -> toRestEntity(webhookService, webHook)).toList();
public List<WebHookRestEntity> toRestEntities(Collection<WebHook> webHooks) {
return webHooks.stream().map(this::toRestEntity).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class WebHookRestEntity {
private String avatarUrl;

private boolean watchScopeLimited;
private boolean isTokenValid;

public WebHookRestEntity(long id, // NOSONAR
long webhookId,
Expand All @@ -68,7 +69,8 @@ public WebHookRestEntity(long id, // NOSONAR
String title,
String description,
String avatarUrl,
boolean watchScopeLimited) {
boolean watchScopeLimited,
boolean isTokenValid) {

this.id = id;
this.webhookId = webhookId;
Expand All @@ -84,5 +86,6 @@ public WebHookRestEntity(long id, // NOSONAR
this.description = description;
this.avatarUrl = avatarUrl;
this.watchScopeLimited = watchScopeLimited;
this.isTokenValid = isTokenValid;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void setWebHookWatchLimitEnabled(long projectRemoteId, boolean enabled, S

public void deleteWebhookHook(long projectId, String currentUser) throws IllegalAccessException, ObjectNotFoundException {
if (!Utils.isRewardingManager(currentUser)) {
throw new IllegalAccessException("The user is not authorized to delete GitHub hook");
throw new IllegalAccessException("The user is not authorized to delete Crowdin hook");
}
WebHook webHook = webHookStorage.getWebhookByProjectId(projectId);
if (webHook == null) {
Expand Down Expand Up @@ -154,6 +154,24 @@ public List<WebHook> getWebhooks(int offset, int limit) {
return hooksIds.stream().map(webHookStorage::getWebHookById).toList();
}

public WebHook getWebhookId(long webhookId, String username) throws IllegalAccessException, ObjectNotFoundException {
if (!Utils.isRewardingManager(username)) {
throw new IllegalAccessException(AUTHORIZED_TO_ACCESS_CROWDIN_HOOKS);
}
WebHook webHook = getWebhookId(webhookId);
if (webHook == null) {
throw new ObjectNotFoundException("Webhook doesn't exist");
}
return webHook;
}

public WebHook getWebhookId(long webhookId) {
if (webhookId <= 0) {
throw new IllegalArgumentException("Webhook id is mandatory");
}
return webHookStorage.getWebHookById(webhookId);
}

private void forceUpdateWebhook(WebHook webHook) {
// TODO
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public RemoteProject retrieveRemoteProject(long projectRemoteId, String accessTo
project.setId(jsonObject.getInt("id"));
project.setName(jsonObject.getString("name"));
project.setIdentifier(jsonObject.getString("identifier"));
project.setIdentifier(jsonObject.getString("identifier"));
project.setAvatarUrl(jsonObject.getString("logo"));

return project;
} catch (IllegalArgumentException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@
</init-params>
</component-plugin>
</external-component-plugins>

<import>jar:/conf/portal/gamification-crowdin-connector-configuration.xml</import>

</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
This file is part of the Meeds project (https://meeds.io/).
Copyright (C) 2023 Meeds Lab [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.
-->
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.exoplatform.org/xml/ns/kernel_1_2.xsd http://www.exoplatform.org/xml/ns/kernel_1_2.xsd"
xmlns="http://www.exoplatform.org/xml/ns/kernel_1_2.xsd">

<external-component-plugins>
<target-component>io.meeds.gamification.service.EventRegistry</target-component>
<component-plugin>
<name>StringCommentCreated</name>
<set-method>addPlugin</set-method>
<type>io.meeds.gamification.plugin.EventConfigPlugin</type>
<init-params>
<object-param>
<name>event</name>
<object type="io.meeds.gamification.model.EventDTO">
<field name="title">
<string>stringCommentCreated</string>
</field>
<field name="type">
<string>crowdin</string>
</field>
<field name="trigger">
<string>stringComment.created</string>
</field>
<field name="cancellerEvents">
<collection type="java.util.ArrayList" item-type="java.lang.String">
<value>
<string>stringCommentDeleted</string>
</value>
</collection>
</field>
</object>
</object-param>
</init-params>
</component-plugin>
</external-component-plugins>
<external-component-plugins>
<target-component>io.meeds.gamification.service.EventRegistry</target-component>
<component-plugin>
<name>SuggestionAdded</name>
<set-method>addPlugin</set-method>
<type>io.meeds.gamification.plugin.EventConfigPlugin</type>
<init-params>
<object-param>
<name>event</name>
<object type="io.meeds.gamification.model.EventDTO">
<field name="title">
<string>suggestionAdded</string>
</field>
<field name="type">
<string>crowdin</string>
</field>
<field name="trigger">
<string>suggestion.added</string>
</field>
<field name="cancellerEvents">
<collection type="java.util.ArrayList" item-type="java.lang.String">
<value>
<string>suggestionDeleted</string>
</value>
</collection>
</field>
</object>
</object-param>
</init-params>
</component-plugin>
</external-component-plugins>
<external-component-plugins>
<target-component>io.meeds.gamification.service.EventRegistry</target-component>
<component-plugin>
<name>SuggestionApproved</name>
<set-method>addPlugin</set-method>
<type>io.meeds.gamification.plugin.EventConfigPlugin</type>
<init-params>
<object-param>
<name>event</name>
<object type="io.meeds.gamification.model.EventDTO">
<field name="title">
<string>suggestionApproved</string>
</field>
<field name="type">
<string>crowdin</string>
</field>
<field name="trigger">
<string>suggestion.approved</string>
</field>
<field name="cancellerEvents">
<collection type="java.util.ArrayList" item-type="java.lang.String">
<value>
<string>suggestionDisapproved</string>
</value>
</collection>
</field>
</object>
</object-param>
</init-params>
</component-plugin>
</external-component-plugins>
<external-component-plugins>
<target-component>io.meeds.gamification.service.EventRegistry</target-component>
<component-plugin>
<name>ApproveSuggestion</name>
<set-method>addPlugin</set-method>
<type>io.meeds.gamification.plugin.EventConfigPlugin</type>
<init-params>
<object-param>
<name>event</name>
<object type="io.meeds.gamification.model.EventDTO">
<field name="title">
<string>approveSuggestion</string>
</field>
<field name="type">
<string>crowdin</string>
</field>
<field name="trigger">
<string>approve.suggestion</string>
</field>
<field name="cancellerEvents">
<collection type="java.util.ArrayList" item-type="java.lang.String">
<value>
<string>suggestionDisapproved</string>
</value>
</collection>
</field>
</object>
</object-param>
</init-params>
</component-plugin>
</external-component-plugins>
</configuration>
Loading

0 comments on commit 410dae9

Please sign in to comment.