Skip to content

Commit

Permalink
Release 1.3.5 (#48)
Browse files Browse the repository at this point in the history
* task/KAH-824 (Investigate how to run function inside plugin on agent) (#43)

* add route, controller and service handlers and methods

* plugin function call WIP

* add pluginSettings, actionParams

* Fix query autocomplete function

* Remove unnecesary items

* Add plugin existence condition

* Remove unnecessary check

* Function error handling

* Add typeof

Co-authored-by: arispen <[email protected]>

* add pluginSettings to getAutocompleteFromFunction (#44)

* added actionParams

* made autocomplete optional

* added gitignore

* added handler for optional pluginSettings

* removed key.pm

* requireUncached, error handling for autocomplete (#46)

query function

* bump version to 1.3.5

* add uuid package
use uuid for plugins extraction tmp directory

Co-authored-by: kbuchacz <[email protected]>
Co-authored-by: arispen <[email protected]>
Co-authored-by: Chris Bernat <[email protected]>
Co-authored-by: Matan Kadosh <[email protected]>
Co-authored-by: Dawid Zegar <[email protected]>
  • Loading branch information
6 people authored Oct 23, 2020
1 parent d99fdfb commit 8b04239
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ private.ts
SOURCES
SPECS

kaholo-agent.conf
kaholo-agent.conf
64 changes: 44 additions & 20 deletions api/controllers/plugins.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,56 @@ const logger = require("../services/logger");
const pluginsService = require("../services/plugins.service");
const BaseController = require("../models/base-controller.model");

class PluginsController extends BaseController{

async _handleReuqest(promise, req,res){
try{
await promise;
res.status(204).send();
} catch (err){
res.status(500).send(err)
}
class PluginsController extends BaseController {
async _handleReuqest(promise, req, res) {
try {
await promise;
res.status(204).send();
} catch (err) {
res.status(500).send(err);
}
}

async install(req,res){
logger.info("Installing plugin");
await this._handleReuqest(pluginsService.install(req.file.path), req,res);
}
async install(req, res) {
logger.info("Installing plugin");
await this._handleReuqest(pluginsService.install(req.file.path), req, res);
}

async delete(req,res){
logger.info("Deleting plugin");
await this._handleReuqest(pluginsService.delete(req.body.name), req,res);
}
async delete(req, res) {
logger.info("Deleting plugin");
await this._handleReuqest(pluginsService.delete(req.body.name), req, res);
}

list(req,res){
return res.json(pluginsService.getVersions())
async getAutocompleteFromFunction(req, res) {
logger.info("Getting autocomplete from plugin function");
let autocomplete;
let {actionParams, pluginSettings} = req.body;
if(!actionParams || !Array.isArray(actionParams)) {
actionParams = [];
}
if(!pluginSettings || !Array.isArray(pluginSettings)) {
pluginSettings = [];
}
try {
autocomplete = await pluginsService.getAutocompleteFromFunction(
req.params.pluginName,
req.params.functionName,
req.body.query,
pluginSettings,
actionParams
);
res.status(200).json(autocomplete);
} catch (err) {
console.error(err);
res.status(500).send(err);
}
}

list(req, res) {
return res.json(pluginsService.getVersions());
}
}

const pluginsController = new PluginsController();

module.exports = pluginsController;
module.exports = pluginsController;
2 changes: 2 additions & 0 deletions api/routes/plugins.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ router.use(multer({ dest: './uploads/' }).single('file'));
router.post('/', pluginsController.list);
router.post('/install', pluginsController.install);
router.post('/delete', pluginsController.delete);
router.get('/autocomplete-function/:pluginName/:functionName', pluginsController.getAutocompleteFromFunction);


module.exports = router;
32 changes: 31 additions & 1 deletion api/services/plugins.service.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
const fs = require("fs-extra");
const path = require("path");
const rimraf = require("rimraf");
const { v4: uuid4 } = require("uuid");

const zip = require("../../utils/zip");
const exec = require("../../utils/exec");

function requireUncached(module) {
delete require.cache[require.resolve(module)];
return require(module);
}

class PluginsService {
constructor() {
this.plugins = {};
Expand Down Expand Up @@ -79,7 +85,7 @@ class PluginsService {
const tmpPath = path.join(
process.env.BASE_DIR,
"tmp",
"" + new Date().getTime()
`${uuid4()}-${new Date().getTime()}`
);
await zip.extract(zipFilePath, tmpPath);

Expand Down Expand Up @@ -118,6 +124,30 @@ class PluginsService {
await this.loadPluginDir(pluginInstallPath);
}

async getAutocompleteFromFunction(pluginName, functionName, query, pluginSettings, actionParams) {
const pluginConf = this.plugins[pluginName];
let queryFunction;

if (pluginConf) {
queryFunction = requireUncached(pluginConf.main)[functionName];
} else {
throw new Error('Plugin not found!');
}

if (queryFunction && typeof queryFunction === 'function') {
let autocomplete;
try {
autocomplete = queryFunction(query, pluginSettings, actionParams);
} catch (error) {
console.error(error);
throw new Error(`Error in plugin: ${pluginName}, in function: ${functionName}.`);
}
return autocomplete;
} else {
throw new Error('Function not found!');
}
}

delete(name) {
return new Promise((resolve, reject) => {
rimraf(`libs/plugins/${name}`, (err, res) => {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kaholo-agent",
"version": "1.3.4",
"version": "1.3.5",
"description": "Agent for Kaholo",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -35,6 +35,7 @@
"socket.io-client": "2.0.4",
"superagent": "3.8.2",
"unzipper": "0.9.4",
"uuid": "8.3.1",
"winston": "3.3.3"
},
"devDependencies": {
Expand Down

0 comments on commit 8b04239

Please sign in to comment.