@beseif-solutions/prow-core 0.0.32 → 0.0.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -6,7 +6,7 @@ export declare class Protocol {
|
|
|
6
6
|
evaluate: (req: Protocol.Command) => Promise<any>;
|
|
7
7
|
}
|
|
8
8
|
export declare namespace Protocol {
|
|
9
|
-
type Model = `action` | `trigger` | `
|
|
9
|
+
type Model = `action` | `trigger` | `credential`;
|
|
10
10
|
type Invoke = {
|
|
11
11
|
command: `invoke`;
|
|
12
12
|
model: Protocol.Model;
|
|
@@ -4,14 +4,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.Protocol = void 0;
|
|
7
|
+
const joi_1 = __importDefault(require("joi"));
|
|
7
8
|
const lodash_1 = __importDefault(require("lodash"));
|
|
9
|
+
const core_1 = require("../core");
|
|
8
10
|
const provider_1 = require("../entities/provider");
|
|
9
11
|
const locales_1 = require("./locales");
|
|
10
|
-
const core_1 = require("../core");
|
|
11
|
-
const joi_1 = __importDefault(require("joi"));
|
|
12
12
|
const invokeSchema = joi_1.default.object({
|
|
13
13
|
command: joi_1.default.string().valid(`invoke`).required(),
|
|
14
|
-
model: joi_1.default.string().valid(`action`, `trigger`, `
|
|
14
|
+
model: joi_1.default.string().valid(`action`, `trigger`, `credential`).required(),
|
|
15
15
|
id: joi_1.default.string().required(),
|
|
16
16
|
params: joi_1.default.object({
|
|
17
17
|
function: joi_1.default.string().required(),
|
|
@@ -49,16 +49,21 @@ class Protocol {
|
|
|
49
49
|
if (validation.error) {
|
|
50
50
|
throw new Error(`Invalid request: ${validation.error.message}`);
|
|
51
51
|
}
|
|
52
|
+
const serialize = (elem) => ({
|
|
53
|
+
...lodash_1.default.omit(elem, [`functions`, `helpers`]),
|
|
54
|
+
functions: Object.keys(elem.functions),
|
|
55
|
+
helpers: Object.keys(elem.helpers),
|
|
56
|
+
});
|
|
52
57
|
let response;
|
|
53
58
|
switch (req.command) {
|
|
54
59
|
case `list:actions`:
|
|
55
|
-
response = Object.values(this.provider.actions || {});
|
|
60
|
+
response = Object.values(this.provider.actions || {}).map(serialize);
|
|
56
61
|
break;
|
|
57
62
|
case `list:triggers`:
|
|
58
|
-
response = Object.values(this.provider.triggers || {});
|
|
63
|
+
response = Object.values(this.provider.triggers || {}).map(serialize);
|
|
59
64
|
break;
|
|
60
65
|
case `list:credentials`:
|
|
61
|
-
response = Object.values(this.provider.credentials || {});
|
|
66
|
+
response = Object.values(this.provider.credentials || {}).map(serialize);
|
|
62
67
|
break;
|
|
63
68
|
case `list:sources`:
|
|
64
69
|
response = Object.values(this.provider.sources || {});
|
|
@@ -74,22 +79,20 @@ class Protocol {
|
|
|
74
79
|
});
|
|
75
80
|
break;
|
|
76
81
|
case `invoke`:
|
|
77
|
-
const
|
|
78
|
-
if (!
|
|
79
|
-
|
|
80
|
-
status: 404,
|
|
81
|
-
body: JSON.stringify({ error: `Could not find ${req.model} with ID ${req.id}` }),
|
|
82
|
-
};
|
|
82
|
+
const elem = lodash_1.default.get(this.provider, `${req.model}s.${req.id}`);
|
|
83
|
+
if (!elem) {
|
|
84
|
+
throw new Error(`Could not find ${req.model} with ID ${req.id}`);
|
|
83
85
|
}
|
|
84
|
-
const
|
|
85
|
-
if (!
|
|
86
|
+
const func = lodash_1.default.get(elem, req.params.function);
|
|
87
|
+
if (!func || typeof func !== `function`) {
|
|
86
88
|
throw new Error(`Invalid function ${req.params.function}`);
|
|
87
89
|
}
|
|
88
90
|
const invokeInstance = (0, core_1.core)({
|
|
89
91
|
env: req.context.environment,
|
|
90
92
|
localization: req.context.localization,
|
|
93
|
+
sources: null,
|
|
91
94
|
});
|
|
92
|
-
response = await
|
|
95
|
+
response = await func(invokeInstance, req.params.configuration);
|
|
93
96
|
break;
|
|
94
97
|
default:
|
|
95
98
|
throw new Error(`Invalid command`);
|
package/package.json
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
+
import Joi from "joi";
|
|
1
2
|
import _ from "lodash";
|
|
3
|
+
import { core } from "../core";
|
|
4
|
+
import { Credentials } from "../entities/credentials";
|
|
5
|
+
import { Action, Trigger } from "../entities/events";
|
|
2
6
|
import { Provider, providerSchema } from "../entities/provider";
|
|
3
7
|
import { i18n } from "./locales";
|
|
4
|
-
import { core } from "../core";
|
|
5
|
-
import Joi from "joi";
|
|
6
8
|
|
|
7
9
|
const invokeSchema = Joi.object({
|
|
8
10
|
command: Joi.string().valid(`invoke`).required(),
|
|
9
|
-
model: Joi.string().valid(`action`, `trigger`, `
|
|
11
|
+
model: Joi.string().valid(`action`, `trigger`, `credential`).required(),
|
|
10
12
|
id: Joi.string().required(),
|
|
11
13
|
params: Joi.object({
|
|
12
14
|
function: Joi.string().required(),
|
|
@@ -67,16 +69,22 @@ export class Protocol {
|
|
|
67
69
|
const validation = commandSchema.validate(req);
|
|
68
70
|
if (validation.error) { throw new Error(`Invalid request: ${validation.error.message}`); }
|
|
69
71
|
|
|
72
|
+
const serialize = (elem: Action | Trigger | Credentials) => ({
|
|
73
|
+
..._.omit(elem, [`functions`, `helpers`]),
|
|
74
|
+
functions: Object.keys(elem.functions),
|
|
75
|
+
helpers: Object.keys(elem.helpers),
|
|
76
|
+
});
|
|
77
|
+
|
|
70
78
|
let response: any;
|
|
71
79
|
switch (req.command) {
|
|
72
80
|
case `list:actions`:
|
|
73
|
-
response = Object.values(this.provider.actions || {});
|
|
81
|
+
response = Object.values(this.provider.actions || {}).map(serialize);
|
|
74
82
|
break;
|
|
75
83
|
case `list:triggers`:
|
|
76
|
-
response = Object.values(this.provider.triggers || {});
|
|
84
|
+
response = Object.values(this.provider.triggers || {}).map(serialize);
|
|
77
85
|
break;
|
|
78
86
|
case `list:credentials`:
|
|
79
|
-
response = Object.values(this.provider.credentials || {});
|
|
87
|
+
response = Object.values(this.provider.credentials || {}).map(serialize);
|
|
80
88
|
break;
|
|
81
89
|
case `list:sources`:
|
|
82
90
|
response = Object.values(this.provider.sources || {});
|
|
@@ -92,23 +100,20 @@ export class Protocol {
|
|
|
92
100
|
});
|
|
93
101
|
break;
|
|
94
102
|
case `invoke`:
|
|
95
|
-
const
|
|
96
|
-
if (!
|
|
97
|
-
return {
|
|
98
|
-
status: 404,
|
|
99
|
-
body: JSON.stringify({ error: `Could not find ${req.model} with ID ${req.id}` }),
|
|
100
|
-
};
|
|
101
|
-
}
|
|
103
|
+
const elem = _.get(this.provider, `${req.model}s.${req.id}`);
|
|
104
|
+
if (!elem) { throw new Error(`Could not find ${req.model} with ID ${req.id}`); }
|
|
102
105
|
|
|
103
|
-
const
|
|
104
|
-
if (!
|
|
106
|
+
const func = _.get(elem, req.params.function);
|
|
107
|
+
if (!func || typeof func !== `function`) { throw new Error(`Invalid function ${req.params.function}`); }
|
|
105
108
|
|
|
106
109
|
const invokeInstance = core({
|
|
107
110
|
env: req.context.environment,
|
|
108
111
|
localization: req.context.localization,
|
|
112
|
+
// TODO: sources
|
|
113
|
+
sources: null,
|
|
109
114
|
});
|
|
110
115
|
|
|
111
|
-
response = await
|
|
116
|
+
response = await func(invokeInstance, req.params.configuration);
|
|
112
117
|
break;
|
|
113
118
|
default:
|
|
114
119
|
throw new Error(`Invalid command`);
|
|
@@ -121,7 +126,7 @@ export class Protocol {
|
|
|
121
126
|
|
|
122
127
|
export namespace Protocol {
|
|
123
128
|
|
|
124
|
-
export type Model = `action` | `trigger` | `
|
|
129
|
+
export type Model = `action` | `trigger` | `credential`;
|
|
125
130
|
|
|
126
131
|
export type Invoke = {
|
|
127
132
|
command: `invoke`,
|