Skip to content

Commit 4681390

Browse files
author
sunlei
committed
gitlab query
1 parent 3257e91 commit 4681390

File tree

8 files changed

+191
-7
lines changed

8 files changed

+191
-7
lines changed

server/node-service/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@
4747
"express-async-errors": "^3.1.1",
4848
"firebase-admin": "^11.5.0",
4949
"formdata-node": "4",
50+
"graphql": "^16.6.0",
51+
"graphql-request": "^5.1.0",
5052
"jsonpath": "^1.1.1",
5153
"lodash": "^4.17.21",
5254
"loglevel": "^1.8.1",
5355
"morgan": "^1.10.0",
5456
"openapi-types": "^12.1.0",
5557
"openblocks-core": "^0.0.7",
56-
"openblocks-sdk": "^0.0.35",
58+
"openblocks-sdk": "0.0.38",
5759
"pino": "^8.11.0",
5860
"stylis": "^4.1.3",
5961
"swagger-client": "^3.18.5",

server/node-service/src/common/util.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
import yaml from "yaml";
22
import fs from "fs";
33

4+
export function kvToRecord(
5+
kvs: { key: string; value: string }[],
6+
trimEmpty: boolean = true
7+
): Record<string, string> {
8+
const ret: Record<string, string> = {};
9+
(kvs || []).forEach(({ key, value }) => {
10+
if (trimEmpty && !value) {
11+
return;
12+
}
13+
ret[key] = value;
14+
});
15+
return ret;
16+
}
17+
418
export function toString(value: any): string {
519
if (value === undefined || value === null) {
620
return "";
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { kvToRecord } from "../../common/util";
2+
import { ConfigToType, DataSourcePlugin, PluginContext } from "openblocks-sdk/dataSource";
3+
import { graphQLQueryConfig } from "../graphql/queryConfig";
4+
import { runGraphQL } from "../graphql/run";
5+
6+
export const dataSourceConfig = {
7+
type: "dataSource",
8+
params: [
9+
{
10+
type: "textInput",
11+
label: "Endpoint",
12+
key: "endpoint",
13+
rules: [{ required: true }],
14+
placeholder: "https://gitlab.mycompany.com/api/graphql",
15+
},
16+
{
17+
type: "password",
18+
label: "Access Token",
19+
key: "token",
20+
rules: [{ required: true }],
21+
},
22+
{
23+
type: "keyValueInput",
24+
label: "Headers",
25+
key: "headers",
26+
valueType: "string",
27+
},
28+
],
29+
} as const;
30+
31+
const gitlabPlugin: DataSourcePlugin<
32+
ConfigToType<typeof graphQLQueryConfig>,
33+
ConfigToType<typeof dataSourceConfig>
34+
> = {
35+
id: "gitlab",
36+
name: "Gitlab",
37+
category: "api",
38+
icon: "gitlab.svg",
39+
dataSourceConfig,
40+
queryConfig: graphQLQueryConfig,
41+
run: function (actionData, dataSourceConfig, context: PluginContext): Promise<any> {
42+
const { endpoint, token, headers: defaultHeaders } = dataSourceConfig;
43+
const { query, variables: variableKvs, headers: queryHeaders } = actionData;
44+
const headers = {
45+
...kvToRecord(defaultHeaders),
46+
...kvToRecord(queryHeaders),
47+
"PRIVATE-TOKEN": token,
48+
};
49+
const variables = kvToRecord(variableKvs);
50+
return runGraphQL(endpoint, query, variables, headers);
51+
},
52+
};
53+
54+
export default gitlabPlugin;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export const graphQLQueryConfig = {
2+
type: "query",
3+
label: "Action",
4+
actions: [
5+
{
6+
label: "Query",
7+
actionName: "query",
8+
params: [
9+
{
10+
type: "graphqlInput",
11+
label: "Query",
12+
key: "query",
13+
},
14+
{
15+
type: "keyValueInput",
16+
label: "Variables",
17+
key: "variables",
18+
valueType: "json",
19+
},
20+
{
21+
type: "keyValueInput",
22+
label: "Headers",
23+
key: "headers",
24+
valueType: "string",
25+
},
26+
],
27+
},
28+
],
29+
} as const;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { badRequest } from "../../common/error";
2+
import { request, Variables, ClientError } from "graphql-request";
3+
4+
export async function runGraphQL(
5+
endpoint: string,
6+
query: string,
7+
variables: Variables,
8+
headers: Record<string, string>
9+
) {
10+
try {
11+
const ret = await request(endpoint, query, variables, headers);
12+
return ret;
13+
} catch (e) {
14+
console.info(e);
15+
if (e instanceof ClientError) {
16+
throw badRequest(e.message);
17+
}
18+
throw e;
19+
}
20+
}

server/node-service/src/plugins/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import cloudinaryPlugin from "./cloudinary";
2727
import notionPlugin from "./notion";
2828
import datadogPlugin from "./datadog";
2929
import twilioPlugin from "./twilio";
30+
import gitlabPlugin from "./gitlab";
3031

3132
let plugins: (DataSourcePlugin | DataSourcePluginFactory)[] = [
3233
s3Plugin,
@@ -58,6 +59,7 @@ let plugins: (DataSourcePlugin | DataSourcePluginFactory)[] = [
5859
notionPlugin,
5960
datadogPlugin,
6061
twilioPlugin,
62+
gitlabPlugin,
6163
];
6264

6365
try {

server/node-service/src/services/plugin.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ export async function evalToValue<T extends Config>(
9292
config.type === "textInput" ||
9393
config.type === "select" ||
9494
config.type === "password" ||
95-
config.type === "sqlInput"
95+
config.type === "sqlInput" ||
96+
config.type === "graphqlInput"
9697
) {
9798
return toString(evalCodeToValue(dsl, context));
9899
}
@@ -109,6 +110,18 @@ export async function evalToValue<T extends Config>(
109110
return evalCodeToValue(dsl, context, true);
110111
}
111112

113+
if (config.type === "keyValueInput") {
114+
if (!Array.isArray(dsl)) {
115+
return [];
116+
}
117+
return dsl.map(({ key, value }) => {
118+
return {
119+
key: evalCodeToValue(key, context),
120+
value: evalCodeToValue(value, context, config.valueType === "json"),
121+
};
122+
});
123+
}
124+
112125
throw new ServiceError(`invalid plugin definition, unknown config type: ${(config as any).type}`);
113126
}
114127

server/node-service/yarn.lock

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4051,6 +4051,15 @@ __metadata:
40514051
languageName: node
40524052
linkType: hard
40534053

4054+
"@graphql-typed-document-node/core@npm:^3.1.1":
4055+
version: 3.1.2
4056+
resolution: "@graphql-typed-document-node/core@npm:3.1.2"
4057+
peerDependencies:
4058+
graphql: ^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0
4059+
checksum: a61afa025acdabd7833e4f654a5802fc1a526171f81e0c435c8e651050a5a0682499a2c7a51304ceb61fde36cd69fc7975ce5e1b16b9ba7ea474c649f33eea8b
4060+
languageName: node
4061+
linkType: hard
4062+
40544063
"@grpc/grpc-js@npm:~1.8.0":
40554064
version: 1.8.8
40564065
resolution: "@grpc/grpc-js@npm:1.8.8::__archiveUrl=https%3A%2F%2Fregistry.npmjs.org%2F%40grpc%2Fgrpc-js%2F-%2Fgrpc-js-1.8.8.tgz"
@@ -6397,6 +6406,13 @@ __metadata:
63976406
languageName: node
63986407
linkType: hard
63996408

6409+
"extract-files@npm:^9.0.0":
6410+
version: 9.0.0
6411+
resolution: "extract-files@npm:9.0.0"
6412+
checksum: c31781d090f8d8f62cc541f1023b39ea863f24bd6fb3d4011922d71cbded70cef8191f2b70b43ec6cb5c5907cdad1dc5e9f29f78228936c10adc239091d8ab64
6413+
languageName: node
6414+
linkType: hard
6415+
64006416
"fast-deep-equal@npm:^3.1.1":
64016417
version: 3.1.3
64026418
resolution: "fast-deep-equal@npm:3.1.3::__archiveUrl=https%3A%2F%2Fregistry.npmjs.org%2Ffast-deep-equal%2F-%2Ffast-deep-equal-3.1.3.tgz"
@@ -6542,6 +6558,17 @@ __metadata:
65426558
languageName: node
65436559
linkType: hard
65446560

6561+
"form-data@npm:^3.0.0":
6562+
version: 3.0.1
6563+
resolution: "form-data@npm:3.0.1"
6564+
dependencies:
6565+
asynckit: ^0.4.0
6566+
combined-stream: ^1.0.8
6567+
mime-types: ^2.1.12
6568+
checksum: b019e8d35c8afc14a2bd8a7a92fa4f525a4726b6d5a9740e8d2623c30e308fbb58dc8469f90415a856698933c8479b01646a9dff33c87cc4e76d72aedbbf860d
6569+
languageName: node
6570+
linkType: hard
6571+
65456572
"form-data@npm:^4.0.0":
65466573
version: 4.0.0
65476574
resolution: "form-data@npm:4.0.0::__archiveUrl=https%3A%2F%2Fregistry.npmjs.org%2Fform-data%2F-%2Fform-data-4.0.0.tgz"
@@ -6834,6 +6861,27 @@ __metadata:
68346861
languageName: node
68356862
linkType: hard
68366863

6864+
"graphql-request@npm:^5.1.0":
6865+
version: 5.1.0
6866+
resolution: "graphql-request@npm:5.1.0"
6867+
dependencies:
6868+
"@graphql-typed-document-node/core": ^3.1.1
6869+
cross-fetch: ^3.1.5
6870+
extract-files: ^9.0.0
6871+
form-data: ^3.0.0
6872+
peerDependencies:
6873+
graphql: 14 - 16
6874+
checksum: 8b65d5c1b0cad8996a843b52305b2712ffbf842c700487d4cee1ed719f22baacaacfddafb65572bcddd67d57fd1d2d389dee2430ffba4b34c3ab542d079446c7
6875+
languageName: node
6876+
linkType: hard
6877+
6878+
"graphql@npm:^16.6.0":
6879+
version: 16.6.0
6880+
resolution: "graphql@npm:16.6.0"
6881+
checksum: bf1d9e3c1938ce3c1a81e909bd3ead1ae4707c577f91cff1ca2eca474bfbc7873d5d7b942e1e9777ff5a8304421dba57a4b76d7a29eb19de8711cb70e3c2415e
6882+
languageName: node
6883+
linkType: hard
6884+
68376885
"gtoken@npm:^6.1.0":
68386886
version: 6.1.2
68396887
resolution: "gtoken@npm:6.1.2::__archiveUrl=https%3A%2F%2Fregistry.npmjs.org%2Fgtoken%2F-%2Fgtoken-6.1.2.tgz"
@@ -8719,13 +8767,13 @@ __metadata:
87198767
languageName: node
87208768
linkType: hard
87218769

8722-
"openblocks-sdk@npm:^0.0.35":
8723-
version: 0.0.35
8724-
resolution: "openblocks-sdk@npm:0.0.35"
8770+
"openblocks-sdk@npm:0.0.38":
8771+
version: 0.0.38
8772+
resolution: "openblocks-sdk@npm:0.0.38"
87258773
peerDependencies:
87268774
react: ">=17"
87278775
react-dom: ">=17"
8728-
checksum: 1e91bc8153d2db85fbf5ec870a678948a6bc2a719aca01a6d1ae5ef758c5ef01a943719db12639b181dc51144a43fcc3edd9ffae4ed712a6a18b5f6f6e784529
8776+
checksum: 11bc829871c8037378f35e37d97ca8702a7adbd0c228ea9125befb8a63c8cf883cd9a8ce1818dc70a4865186118da313ae5b7660cd038a6fd0e9dab669e871a9
87298777
languageName: node
87308778
linkType: hard
87318779

@@ -9812,6 +9860,8 @@ __metadata:
98129860
express-async-errors: ^3.1.1
98139861
firebase-admin: ^11.5.0
98149862
formdata-node: 4
9863+
graphql: ^16.6.0
9864+
graphql-request: ^5.1.0
98159865
jest: ^29.3.1
98169866
jsonpath: ^1.1.1
98179867
lodash: ^4.17.21
@@ -9821,7 +9871,7 @@ __metadata:
98219871
nodemon: ^2.0.20
98229872
openapi-types: ^12.1.0
98239873
openblocks-core: ^0.0.7
9824-
openblocks-sdk: ^0.0.35
9874+
openblocks-sdk: 0.0.38
98259875
pino: ^8.11.0
98269876
postman-to-openapi: ^3.0.1
98279877
stylis: ^4.1.3

0 commit comments

Comments
 (0)