Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RocketChat channel connection #29

Merged
merged 7 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions project_service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ dependencies {
// las2peer bundle which is not necessary in the runtime path
// compileOnly will be moved into the lib dir afterwards
implementation "i5:las2peer-bundle:${project.property('core.version')}"

implementation "com.konghq:unirest-java:3.13.10"

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import i5.las2peer.api.persistency.EnvelopeOperationFailedException;
import i5.las2peer.restMapper.RESTService;
import i5.las2peer.restMapper.annotations.ServicePath;
import i5.las2peer.services.projectService.chat.ChatManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
Expand Down Expand Up @@ -189,6 +190,15 @@ public JSONObject getProjectMetadataRMI(String system, String projectName) {
return metadata;
}

public JSONObject getProjectChatInfo(String system, String projectName) {
Response r = this.getProjectByName(system, projectName);
if(r.getStatus() != 200) return null;
String entity = (String) r.getEntity();
JSONObject projectJSON = (JSONObject) JSONValue.parse(entity);
JSONObject chatInfo = (JSONObject) projectJSON.get("chatInfo");
return chatInfo;
}

/**
* Creates a new project in the pastry storage. Therefore, the user needs to be
* authorized. First, checks if a project with the given name already exists. If
Expand Down Expand Up @@ -286,6 +296,15 @@ public Response postProject(@PathParam("system") String system, String inputProj
}
}

// check if chat channel should be created
if(this.systemsConfig.isChannelCreationEnabled(system)) {
ChatManager chatManager = this.systemsConfig.getChatManager(system);
JSONObject chatInfo = chatManager.createProjectChannel(project, system);
if(chatInfo != null) {
project.setChatInfo(chatInfo);
}
}

ProjectContainer cc = new ProjectContainer();

cc.addProject(project);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package i5.las2peer.services.projectService.chat;

/**
* Super class for all chat configurations.
* Currently, only RocketChat config is implemented.
*/
public abstract class ChatConfig {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package i5.las2peer.services.projectService.chat;

import i5.las2peer.services.projectService.project.Project;
import org.json.simple.JSONObject;

/**
* Super class for all chat managers.
* Currently, only RocketChat is implemented.
*/
public abstract class ChatManager {

protected ChatConfig config;

public ChatManager(ChatConfig config) {
this.config = config;
}

public abstract JSONObject createProjectChannel(Project project, String systemName);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package i5.las2peer.services.projectService.chat;

import org.json.simple.JSONObject;

/**
* RocketChat instance configuration.
*/
public class RocketChatConfig extends ChatConfig {

/**
* Public url of RocketChat instance.
*/
private String url;

/**
* Auth token of bot account that is used to make API calls.
*/
private String botAuthToken;

/**
* User ID of bot account that is used to make API calls.
*/
private String botUserId;

public RocketChatConfig(String url, String botAuthToken, String botUserId) {
this.url = url;
this.botAuthToken = botAuthToken;
this.botUserId = botUserId;
}

public static RocketChatConfig fromJSON(JSONObject config) {
String url = (String) config.get("url");
String botAuthToken = (String) config.get("botAuthToken");
String botUserId = (String) config.get("botUserId");
return new RocketChatConfig(url, botAuthToken, botUserId);
}

public String getUrl() {
return url;
}

public String getBotAuthToken() {
return botAuthToken;
}

public String getBotUserId() {
return botUserId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package i5.las2peer.services.projectService.chat;

import i5.las2peer.services.projectService.project.Project;
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

/**
* ChatManager for RocketChat. Allows to create channels for las2peer projects.
*/
public class RocketChatManager extends ChatManager {

public RocketChatManager(ChatConfig config) {
super(config);
}

@Override
public JSONObject createProjectChannel(Project project, String systemName) {
String channelName = project.getName().replaceAll(" ", "") + "_" + systemName;

JSONObject body = new JSONObject();
body.put("name", channelName);

HttpResponse<String> response = Unirest.post(getConfig().getUrl() + "/api/v1/channels.create")
.header("X-Auth-Token", getConfig().getBotAuthToken())
.header("X-User-Id", getConfig().getBotUserId())
.header("Content-Type", "application/json")
.body(body.toJSONString())
.asString();

if(!response.isSuccess()) {
System.out.println("RocketChat channel creation failed with status code: " + response.getStatus());
return null;
}

JSONObject res = (JSONObject) JSONValue.parse(response.getBody());
JSONObject resChannel = (JSONObject) res.get("channel");

JSONObject channelInfo = new JSONObject();
channelInfo.put("type", "RocketChat");
channelInfo.put("url", getConfig().getUrl());
channelInfo.put("channelId", resChannel.get("_id"));
channelInfo.put("chatUrl", getConfig().getUrl() + "/channel/" + resChannel.get("_id"));
return channelInfo;
}

private RocketChatConfig getConfig() {
return (RocketChatConfig) this.config;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.HashMap;
import java.util.List;

import org.json.simple.JSONArray;
import org.json.simple.JSONValue;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
Expand Down Expand Up @@ -58,6 +59,8 @@ public class Project implements Serializable {
*/
private HashMap<String, String> memberGitHubUsernames;

private JSONObject chatInfo = new JSONObject();

/**
* Creates a project object from the given JSON string. This constructor should
* be used before storing new projects.
Expand Down Expand Up @@ -152,6 +155,7 @@ public JSONObject toJSONObject() {
if(this.gitHubProjectConnected()) {
jsonProject.put("gitHubProject", this.connectedGitHubProject.toJSONObject());
}
jsonProject.put("chatInfo", this.chatInfo);

return jsonProject;
}
Expand Down Expand Up @@ -286,4 +290,8 @@ public boolean removeNonGroupMembersGitHubAccess(String system, String[] groupMe
}
return changed;
}

public void setChatInfo(JSONObject chatInfo) {
this.chatInfo = chatInfo;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package i5.las2peer.services.projectService.util;

import i5.las2peer.services.projectService.chat.ChatManager;
import i5.las2peer.services.projectService.chat.RocketChatConfig;
import i5.las2peer.services.projectService.chat.RocketChatManager;
import org.json.simple.JSONObject;

import i5.las2peer.services.projectService.ProjectService;
Expand All @@ -18,6 +21,8 @@ public class ProjectServiceSystem {
private static final String JSON_KEY_GITHUB_PROJECTS_ENABLED = "gitHubProjectsEnabled";
private static final String JSON_KEY_GITHUB_ORGANIZATION = "gitHubOrganization";
private static final String JSON_KEY_GITHUB_PERSONAL_ACCESS_TOKEN = "gitHubPersonalAccessToken";

private static final String JSON_KEY_ROCKET_CHAT_CONFIG = "rocketchat";

/**
* Name of the system. Example: SBF
Expand Down Expand Up @@ -53,6 +58,8 @@ public class ProjectServiceSystem {
* to create new GitHub projects in the used GitHub organization.
*/
private String gitHubPersonalAccessToken;

private RocketChatConfig rocketChatConfig = null;

public ProjectServiceSystem(String systemName, JSONObject systemJSON) {
this.name = systemName;
Expand All @@ -75,6 +82,10 @@ public ProjectServiceSystem(String systemName, JSONObject systemJSON) {
this.gitHubPersonalAccessToken = (String) systemJSON.get(JSON_KEY_GITHUB_PERSONAL_ACCESS_TOKEN);
}

if(systemJSON.containsKey(JSON_KEY_ROCKET_CHAT_CONFIG)) {
this.rocketChatConfig = RocketChatConfig.fromJSON((JSONObject) systemJSON.get(JSON_KEY_ROCKET_CHAT_CONFIG));
}

this.eventListenerService = (String) systemJSON.getOrDefault(JSON_KEY_EVENT_LISTENER_SERVICE, null);
}

Expand Down Expand Up @@ -106,4 +117,16 @@ public String getGitHubOrganization() {
public String getGitHubPersonalAccessToken() {
return this.gitHubPersonalAccessToken;
}

public RocketChatConfig getRocketChatConfig() {
return rocketChatConfig;
}

public boolean isChannelCreationEnabled() {
return this.rocketChatConfig != null;
}

public ChatManager getChatManager() {
return new RocketChatManager(this.rocketChatConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import i5.las2peer.services.projectService.chat.ChatManager;
import org.json.simple.JSONObject;

import i5.las2peer.services.projectService.ProjectService;
Expand Down Expand Up @@ -91,4 +93,18 @@ public String getGitHubPATBySystem(String systemName) {
}
return null;
}

public boolean isChannelCreationEnabled(String systemName) {
for(ProjectServiceSystem system : this.systems) {
if(system.getName().equals(systemName)) return system.isChannelCreationEnabled();
}
return false;
}

public ChatManager getChatManager(String systemName) {
for(ProjectServiceSystem system : this.systems) {
if(system.getName().equals(systemName)) return system.getChatManager();
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package i5.las2peer.services.projectService.chat;

import org.json.simple.JSONObject;
import org.junit.Test;

import static org.junit.Assert.*;

public class RocketChatConfigTest {

private static String url = "http://url.com";
private static String botAuthToken = "token";
private static String botUserId = "id";

@Test
public void fromJSONTest() {
JSONObject configJSON = new JSONObject();
configJSON.put("url", url);
configJSON.put("botAuthToken", botAuthToken);
configJSON.put("botUserId", botUserId);

RocketChatConfig chatConfig = RocketChatConfig.fromJSON(configJSON);
assertEquals(chatConfig.getUrl(), url);
assertEquals(chatConfig.getBotAuthToken(), botAuthToken);
assertEquals(chatConfig.getBotUserId(), botUserId);
}
}