plugin-workflow-portainer 0.0.0-stage → 0.2.0
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.
- package/LICENSE +176 -0
- package/README.md +31 -3
- package/client-v2.js +1 -0
- package/client.js +1 -0
- package/dist/client/127.09e6675863ac8138.js +10 -0
- package/dist/client/57.67f04a547ead06b8.js +10 -0
- package/dist/client/660.546c702d1ddf7eff.js +10 -0
- package/dist/client/981.e38099c59bf992f0.js +10 -0
- package/dist/client/index.js +10 -0
- package/dist/client-v2/127.9f65dc2e3d0df10a.js +10 -0
- package/dist/client-v2/57.a60e16e4b4e4fa28.js +10 -0
- package/dist/client-v2/660.29ae3c3b4040039d.js +10 -0
- package/dist/client-v2/981.8627062881ad83d1.js +10 -0
- package/dist/client-v2/index.js +10 -0
- package/dist/externalVersion.js +20 -0
- package/dist/index.js +48 -0
- package/dist/locale/en-US.json +193 -0
- package/dist/locale/vi-VN.json +193 -0
- package/dist/locale/zh-CN.json +193 -0
- package/dist/node_modules/joi/dist/joi-browser.min.js +1 -0
- package/dist/node_modules/joi/lib/annotate.js +175 -0
- package/dist/node_modules/joi/lib/base.js +1069 -0
- package/dist/node_modules/joi/lib/cache.js +143 -0
- package/dist/node_modules/joi/lib/common.js +216 -0
- package/dist/node_modules/joi/lib/compile.js +283 -0
- package/dist/node_modules/joi/lib/errors.js +271 -0
- package/dist/node_modules/joi/lib/extend.js +312 -0
- package/dist/node_modules/joi/lib/index.d.ts +2365 -0
- package/dist/node_modules/joi/lib/index.js +1 -0
- package/dist/node_modules/joi/lib/manifest.js +476 -0
- package/dist/node_modules/joi/lib/messages.js +207 -0
- package/dist/node_modules/joi/lib/modify.js +267 -0
- package/dist/node_modules/joi/lib/ref.js +414 -0
- package/dist/node_modules/joi/lib/schemas.js +302 -0
- package/dist/node_modules/joi/lib/state.js +166 -0
- package/dist/node_modules/joi/lib/template.js +463 -0
- package/dist/node_modules/joi/lib/trace.js +346 -0
- package/dist/node_modules/joi/lib/types/alternatives.js +364 -0
- package/dist/node_modules/joi/lib/types/any.js +174 -0
- package/dist/node_modules/joi/lib/types/array.js +809 -0
- package/dist/node_modules/joi/lib/types/binary.js +100 -0
- package/dist/node_modules/joi/lib/types/boolean.js +150 -0
- package/dist/node_modules/joi/lib/types/date.js +233 -0
- package/dist/node_modules/joi/lib/types/function.js +93 -0
- package/dist/node_modules/joi/lib/types/keys.js +1084 -0
- package/dist/node_modules/joi/lib/types/link.js +184 -0
- package/dist/node_modules/joi/lib/types/number.js +363 -0
- package/dist/node_modules/joi/lib/types/object.js +22 -0
- package/dist/node_modules/joi/lib/types/string.js +850 -0
- package/dist/node_modules/joi/lib/types/symbol.js +102 -0
- package/dist/node_modules/joi/lib/validator.js +750 -0
- package/dist/node_modules/joi/lib/values.js +263 -0
- package/dist/node_modules/joi/node_modules/@hapi/topo/lib/index.d.ts +60 -0
- package/dist/node_modules/joi/node_modules/@hapi/topo/lib/index.js +225 -0
- package/dist/node_modules/joi/node_modules/@hapi/topo/package.json +30 -0
- package/dist/node_modules/joi/package.json +1 -0
- package/dist/server/actions/portainer.js +317 -0
- package/dist/server/collections/portainer-instances.js +54 -0
- package/dist/server/index.js +54 -0
- package/dist/server/instructions/PortainerStackControlInstruction.js +156 -0
- package/dist/server/instructions/PortainerStackRedeployInstruction.js +254 -0
- package/dist/server/instructions/helpers.js +211 -0
- package/dist/server/plugin.js +177 -0
- package/dist/server/services/PortainerClient.js +669 -0
- package/dist/shared/collection.js +57 -0
- package/dist/shared/constants.js +66 -0
- package/dist/shared/env.js +238 -0
- package/dist/shared/types.js +24 -0
- package/package.json +50 -4
- package/server.js +1 -0
- package/src/client/index.tsx +50 -0
- package/src/client/instructions.tsx +76 -0
- package/src/client-v2/__tests__/workflow-registration.test.tsx +242 -0
- package/src/client-v2/components/ControlFieldset.tsx +46 -0
- package/src/client-v2/components/PortainerEnvEditor.tsx +223 -0
- package/src/client-v2/components/PortainerSettingsPage.tsx +707 -0
- package/src/client-v2/components/PortainerTargetFields.tsx +309 -0
- package/src/client-v2/components/RedeployFieldset.tsx +124 -0
- package/src/client-v2/components/StackDetailDrawer.tsx +790 -0
- package/src/client-v2/index.tsx +50 -0
- package/src/client-v2/instructions.tsx +162 -0
- package/src/client-v2/locale.ts +24 -0
- package/src/index.ts +11 -0
- package/src/locale/en-US.json +193 -0
- package/src/locale/vi-VN.json +193 -0
- package/src/locale/zh-CN.json +193 -0
- package/src/server/__tests__/portainer-workflow.test.ts +559 -0
- package/src/server/actions/portainer.ts +361 -0
- package/src/server/collections/portainer-instances.ts +34 -0
- package/src/server/index.ts +13 -0
- package/src/server/instructions/PortainerStackControlInstruction.ts +139 -0
- package/src/server/instructions/PortainerStackRedeployInstruction.ts +255 -0
- package/src/server/instructions/helpers.ts +237 -0
- package/src/server/plugin.ts +174 -0
- package/src/server/services/PortainerClient.ts +846 -0
- package/src/shared/collection.ts +32 -0
- package/src/shared/constants.ts +25 -0
- package/src/shared/env.ts +243 -0
- package/src/shared/types.ts +164 -0
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { Form } from 'antd';
|
|
11
|
+
import React from 'react';
|
|
12
|
+
import { renderToStaticMarkup } from 'react-dom/server';
|
|
13
|
+
import PluginWorkflowPortainerClient from '../../client';
|
|
14
|
+
import { formatBytes } from '../components/StackDetailDrawer';
|
|
15
|
+
import PluginWorkflowPortainerClientV2 from '../index';
|
|
16
|
+
import { INSTRUCTION_GROUP_KEY, INSTRUCTION_TYPES } from '../../shared/constants';
|
|
17
|
+
import { isWorkflowVariableValue } from '../../shared/env';
|
|
18
|
+
|
|
19
|
+
const runtimes = vi.hoisted(() => {
|
|
20
|
+
class V1Instruction {}
|
|
21
|
+
class V2Instruction {}
|
|
22
|
+
const ExtendCollectionsProvider = () => null;
|
|
23
|
+
return { V1Instruction, V2Instruction, ExtendCollectionsProvider };
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
vi.mock('@nocobase/client', () => ({ Plugin: class Plugin {} }));
|
|
27
|
+
vi.mock('@nocobase/client-v2', () => ({
|
|
28
|
+
Plugin: class Plugin {},
|
|
29
|
+
ExtendCollectionsProvider: runtimes.ExtendCollectionsProvider,
|
|
30
|
+
useApp: () => ({
|
|
31
|
+
i18n: { t: (key: string) => key },
|
|
32
|
+
apiClient: { request: vi.fn(async () => ({ data: { data: [] } })) },
|
|
33
|
+
}),
|
|
34
|
+
}));
|
|
35
|
+
vi.mock('@nocobase/plugin-workflow/client', () => ({
|
|
36
|
+
Instruction: runtimes.V1Instruction,
|
|
37
|
+
}));
|
|
38
|
+
// Intentionally omit `isWorkflowVariableValue` to match NocoBase 2.2.49 runtime exports.
|
|
39
|
+
vi.mock('@nocobase/plugin-workflow/client-v2', () => ({
|
|
40
|
+
Instruction: runtimes.V2Instruction,
|
|
41
|
+
defaultFieldNames: { value: 'value', label: 'label', children: 'children' },
|
|
42
|
+
WorkflowVariableInput: ({ placeholder }: { placeholder?: string }) => (
|
|
43
|
+
<input data-placeholder={placeholder ?? ''} readOnly />
|
|
44
|
+
),
|
|
45
|
+
WorkflowVariableTextArea: ({ placeholder }: { placeholder?: string }) => (
|
|
46
|
+
<textarea data-placeholder={placeholder ?? ''} readOnly />
|
|
47
|
+
),
|
|
48
|
+
WorkflowVariableJsonTextArea: ({ placeholder }: { placeholder?: string }) => (
|
|
49
|
+
<textarea data-placeholder={placeholder ?? ''} readOnly />
|
|
50
|
+
),
|
|
51
|
+
WorkflowVariableWrapper: ({
|
|
52
|
+
value,
|
|
53
|
+
onChange,
|
|
54
|
+
render,
|
|
55
|
+
}: {
|
|
56
|
+
value?: unknown;
|
|
57
|
+
onChange?: (val: unknown) => void;
|
|
58
|
+
render: (props: { value?: unknown; onChange?: (val: unknown) => void }) => React.ReactNode;
|
|
59
|
+
}) => <>{render({ value, onChange })}</>,
|
|
60
|
+
}));
|
|
61
|
+
vi.mock('@ant-design/icons', () => ({
|
|
62
|
+
CloudUploadOutlined: () => null,
|
|
63
|
+
ClusterOutlined: () => null,
|
|
64
|
+
ReloadOutlined: () => null,
|
|
65
|
+
CloudDownloadOutlined: () => null,
|
|
66
|
+
DeleteOutlined: () => null,
|
|
67
|
+
PlusOutlined: () => null,
|
|
68
|
+
CheckCircleOutlined: () => null,
|
|
69
|
+
CloseCircleOutlined: () => null,
|
|
70
|
+
CodeOutlined: () => null,
|
|
71
|
+
FileTextOutlined: () => null,
|
|
72
|
+
PauseCircleOutlined: () => null,
|
|
73
|
+
PlayCircleOutlined: () => null,
|
|
74
|
+
EyeOutlined: () => null,
|
|
75
|
+
}));
|
|
76
|
+
|
|
77
|
+
function createStrictWorkflow(baseClass: new (...args: never[]) => object) {
|
|
78
|
+
const instructions = new Map<string, Record<string, unknown>>();
|
|
79
|
+
const groups = new Map<string, { key: string; label: string }>();
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
instructions,
|
|
83
|
+
groups,
|
|
84
|
+
registerInstructionGroup(key: string, group: { key: string; label: string }) {
|
|
85
|
+
groups.set(key, group);
|
|
86
|
+
},
|
|
87
|
+
registerInstruction(type: string, ctorOrInstance: object | (new () => object)) {
|
|
88
|
+
const instance = typeof ctorOrInstance === 'function' ? new ctorOrInstance() : ctorOrInstance;
|
|
89
|
+
if (!(instance instanceof baseClass)) {
|
|
90
|
+
throw new TypeError(`Instruction ${type} does not match runtime Instruction base class`);
|
|
91
|
+
}
|
|
92
|
+
instructions.set(type, instance as Record<string, unknown>);
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
describe('Portainer workflow dual-runtime client registration', () => {
|
|
98
|
+
it('registers v1 instructions extending @nocobase/plugin-workflow/client Instruction and collection reload hook', async () => {
|
|
99
|
+
const workflow = createStrictWorkflow(runtimes.V1Instruction);
|
|
100
|
+
const collections = new Map<string, object>();
|
|
101
|
+
const reloadCallbacks: Array<() => void> = [];
|
|
102
|
+
const dataSource = {
|
|
103
|
+
collectionManager: {
|
|
104
|
+
getCollection: (name: string) => collections.get(name),
|
|
105
|
+
addCollections: (items: Array<{ name: string }>) => {
|
|
106
|
+
for (const item of items) collections.set(item.name, item);
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
addReloadCallback: (cb: () => void) => reloadCallbacks.push(cb),
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const pluginCtx = {
|
|
113
|
+
app: {
|
|
114
|
+
pm: { get: vi.fn(() => workflow) },
|
|
115
|
+
dataSourceManager: { getDataSource: vi.fn(() => dataSource) },
|
|
116
|
+
pluginSettingsManager: { add: vi.fn() },
|
|
117
|
+
},
|
|
118
|
+
t: (key: string) => key,
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
await PluginWorkflowPortainerClient.prototype.load.call(pluginCtx as unknown as PluginWorkflowPortainerClient);
|
|
122
|
+
|
|
123
|
+
expect(collections.has('portainerInstances')).toBe(true);
|
|
124
|
+
collections.delete('portainerInstances');
|
|
125
|
+
reloadCallbacks[0]();
|
|
126
|
+
expect(collections.has('portainerInstances')).toBe(true);
|
|
127
|
+
|
|
128
|
+
expect(workflow.groups.has(INSTRUCTION_GROUP_KEY)).toBe(true);
|
|
129
|
+
expect(workflow.instructions.has(INSTRUCTION_TYPES.redeploy)).toBe(true);
|
|
130
|
+
expect(workflow.instructions.has(INSTRUCTION_TYPES.control)).toBe(true);
|
|
131
|
+
|
|
132
|
+
const redeployInst = workflow.instructions.get(INSTRUCTION_TYPES.redeploy) as {
|
|
133
|
+
useVariables: (node: { key: string; title: string }) => { children: Array<{ value: string }> };
|
|
134
|
+
};
|
|
135
|
+
const vars = redeployInst.useVariables({ key: 'build_node', title: 'Build Stack' });
|
|
136
|
+
const childKeys = vars.children.map((item) => item.value);
|
|
137
|
+
expect(childKeys).toContain('stackId');
|
|
138
|
+
expect(childKeys).toContain('endpointId');
|
|
139
|
+
expect(childKeys).toContain('healthy');
|
|
140
|
+
expect(childKeys).toContain('appliedEnv');
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('registers v2 instructions extending @nocobase/plugin-workflow/client-v2 Instruction and settings page', async () => {
|
|
144
|
+
const workflow = createStrictWorkflow(runtimes.V2Instruction);
|
|
145
|
+
const addMenuItem = vi.fn();
|
|
146
|
+
const addPageTabItem = vi.fn();
|
|
147
|
+
|
|
148
|
+
const pluginCtx = {
|
|
149
|
+
app: {
|
|
150
|
+
pm: { get: vi.fn(() => workflow) },
|
|
151
|
+
addProvider: vi.fn(),
|
|
152
|
+
},
|
|
153
|
+
pluginSettingsManager: {
|
|
154
|
+
addMenuItem,
|
|
155
|
+
addPageTabItem,
|
|
156
|
+
},
|
|
157
|
+
t: (key: string) => key,
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
await PluginWorkflowPortainerClientV2.prototype.load.call(pluginCtx as unknown as PluginWorkflowPortainerClientV2);
|
|
161
|
+
|
|
162
|
+
expect(pluginCtx.app.addProvider).toHaveBeenCalledWith(runtimes.ExtendCollectionsProvider, {
|
|
163
|
+
collections: [expect.objectContaining({ name: 'portainerInstances' })],
|
|
164
|
+
});
|
|
165
|
+
expect(workflow.instructions.get(INSTRUCTION_TYPES.redeploy)).toBeInstanceOf(runtimes.V2Instruction);
|
|
166
|
+
expect(workflow.instructions.get(INSTRUCTION_TYPES.control)).toBeInstanceOf(runtimes.V2Instruction);
|
|
167
|
+
expect(addMenuItem).toHaveBeenCalledTimes(1);
|
|
168
|
+
expect(addPageTabItem).toHaveBeenCalledTimes(1);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('loads and renders RedeployFieldset and ControlFieldset without relying on upstream isWorkflowVariableValue', async () => {
|
|
172
|
+
expect(isWorkflowVariableValue('{{$context.data.stackId}}')).toBe(true);
|
|
173
|
+
expect(isWorkflowVariableValue(' {{ $jobsMapByNodeKey.n1.endpointId }} ')).toBe(true);
|
|
174
|
+
expect(isWorkflowVariableValue(42)).toBe(false);
|
|
175
|
+
expect(isWorkflowVariableValue('plain-text')).toBe(false);
|
|
176
|
+
|
|
177
|
+
const workflow = createStrictWorkflow(runtimes.V2Instruction);
|
|
178
|
+
const pluginCtx = {
|
|
179
|
+
app: {
|
|
180
|
+
pm: { get: vi.fn(() => workflow) },
|
|
181
|
+
addProvider: vi.fn(),
|
|
182
|
+
},
|
|
183
|
+
pluginSettingsManager: {
|
|
184
|
+
addMenuItem: vi.fn(),
|
|
185
|
+
addPageTabItem: vi.fn(),
|
|
186
|
+
},
|
|
187
|
+
t: (key: string) => key,
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
await PluginWorkflowPortainerClientV2.prototype.load.call(pluginCtx as unknown as PluginWorkflowPortainerClientV2);
|
|
191
|
+
|
|
192
|
+
const redeployInst = workflow.instructions.get(INSTRUCTION_TYPES.redeploy) as {
|
|
193
|
+
FieldsetLoader: () => Promise<{ default: React.ComponentType }>;
|
|
194
|
+
};
|
|
195
|
+
const controlInst = workflow.instructions.get(INSTRUCTION_TYPES.control) as {
|
|
196
|
+
FieldsetLoader: () => Promise<{ default: React.ComponentType }>;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const { default: RedeployFieldset } = await redeployInst.FieldsetLoader();
|
|
200
|
+
const { default: ControlFieldset } = await controlInst.FieldsetLoader();
|
|
201
|
+
|
|
202
|
+
const redeployHtml = renderToStaticMarkup(
|
|
203
|
+
<Form
|
|
204
|
+
initialValues={{
|
|
205
|
+
config: {
|
|
206
|
+
mode: 'stack',
|
|
207
|
+
instanceId: 3,
|
|
208
|
+
endpointId: '{{$context.data.endpointId}}',
|
|
209
|
+
stackId: '{{$context.data.gitStackId}}',
|
|
210
|
+
envStrategy: 'merge',
|
|
211
|
+
envPairs: [{ name: 'IMAGE_TAG', value: '{{$context.data.imageTag}}', secret: false }],
|
|
212
|
+
},
|
|
213
|
+
}}
|
|
214
|
+
>
|
|
215
|
+
<RedeployFieldset />
|
|
216
|
+
</Form>,
|
|
217
|
+
);
|
|
218
|
+
expect(redeployHtml).toContain('Execution mode');
|
|
219
|
+
expect(redeployHtml).toContain('Target Stack');
|
|
220
|
+
expect(redeployHtml).toContain('Environment merge strategy');
|
|
221
|
+
|
|
222
|
+
const controlHtml = renderToStaticMarkup(
|
|
223
|
+
<Form
|
|
224
|
+
initialValues={{
|
|
225
|
+
config: {
|
|
226
|
+
action: 'inspect',
|
|
227
|
+
instanceId: 3,
|
|
228
|
+
endpointId: 1,
|
|
229
|
+
stackId: 2,
|
|
230
|
+
},
|
|
231
|
+
}}
|
|
232
|
+
>
|
|
233
|
+
<ControlFieldset />
|
|
234
|
+
</Form>,
|
|
235
|
+
);
|
|
236
|
+
expect(controlHtml).toContain('Stack operation');
|
|
237
|
+
expect(controlHtml).toContain('Target Stack');
|
|
238
|
+
expect(formatBytes(0)).toBe('0 B');
|
|
239
|
+
expect(formatBytes(1536)).toBe('1.5 KB');
|
|
240
|
+
expect(formatBytes(104857600)).toBe('100 MB');
|
|
241
|
+
});
|
|
242
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { Checkbox, Form, Radio } from 'antd';
|
|
11
|
+
import React from 'react';
|
|
12
|
+
import { useT } from '../locale';
|
|
13
|
+
import { PortainerTargetFields } from './PortainerTargetFields';
|
|
14
|
+
|
|
15
|
+
export function ControlFieldset() {
|
|
16
|
+
const t = useT();
|
|
17
|
+
|
|
18
|
+
return (
|
|
19
|
+
<>
|
|
20
|
+
<Form.Item
|
|
21
|
+
name={['config', 'action']}
|
|
22
|
+
label={t('Stack operation')}
|
|
23
|
+
initialValue="inspect"
|
|
24
|
+
extra={t(
|
|
25
|
+
'Inspect retrieves Stack status, current Env keys, and container health without restarting services; Start and Stop control the Stack lifecycle.',
|
|
26
|
+
)}
|
|
27
|
+
>
|
|
28
|
+
<Radio.Group
|
|
29
|
+
options={[
|
|
30
|
+
{ value: 'inspect', label: t('Inspect Stack & Health') },
|
|
31
|
+
{ value: 'start', label: t('Start Stack') },
|
|
32
|
+
{ value: 'stop', label: t('Stop Stack') },
|
|
33
|
+
]}
|
|
34
|
+
/>
|
|
35
|
+
</Form.Item>
|
|
36
|
+
|
|
37
|
+
<PortainerTargetFields />
|
|
38
|
+
|
|
39
|
+
<Form.Item name={['config', 'ignoreFail']} valuePropName="checked" initialValue={false}>
|
|
40
|
+
<Checkbox>{t('Ignore failed execution and continue workflow')}</Checkbox>
|
|
41
|
+
</Form.Item>
|
|
42
|
+
</>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export default ControlFieldset;
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is part of the NocoBase (R) project.
|
|
3
|
+
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
|
|
4
|
+
* Authors: NocoBase Team.
|
|
5
|
+
*
|
|
6
|
+
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
|
+
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { CloudDownloadOutlined, DeleteOutlined, PlusOutlined } from '@ant-design/icons';
|
|
11
|
+
import { useApp } from '@nocobase/client-v2';
|
|
12
|
+
import {
|
|
13
|
+
WorkflowVariableInput,
|
|
14
|
+
WorkflowVariableJsonTextArea,
|
|
15
|
+
WorkflowVariableTextArea,
|
|
16
|
+
} from '@nocobase/plugin-workflow/client-v2';
|
|
17
|
+
import { Button, Checkbox, Collapse, Flex, Form, Input, message, Radio, Space } from 'antd';
|
|
18
|
+
import React, { useState } from 'react';
|
|
19
|
+
import { PORTAINER_RESOURCE } from '../../shared/constants';
|
|
20
|
+
import type { ConfigurableEnvItem } from '../../shared/types';
|
|
21
|
+
import { useT } from '../locale';
|
|
22
|
+
import type { StaticStackContext } from './PortainerTargetFields';
|
|
23
|
+
|
|
24
|
+
interface StackEnvEntry {
|
|
25
|
+
name: string;
|
|
26
|
+
value: string;
|
|
27
|
+
sensitive?: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function PortainerEnvEditor({ stackContext }: { stackContext?: StaticStackContext }) {
|
|
31
|
+
const t = useT();
|
|
32
|
+
const api = useApp().apiClient;
|
|
33
|
+
const form = Form.useFormInstance();
|
|
34
|
+
const [loadingStackEnv, setLoadingStackEnv] = useState(false);
|
|
35
|
+
|
|
36
|
+
const canLoadFromStack = Boolean(stackContext?.stack?.id);
|
|
37
|
+
|
|
38
|
+
const handleLoadFromStack = async () => {
|
|
39
|
+
const stackId = stackContext?.stack?.id;
|
|
40
|
+
if (!stackId) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
setLoadingStackEnv(true);
|
|
44
|
+
try {
|
|
45
|
+
const res = await api.request<{ data?: { env?: StackEnvEntry[] } }>({
|
|
46
|
+
url: `${PORTAINER_RESOURCE}:getStackDetail`,
|
|
47
|
+
method: 'get',
|
|
48
|
+
params: {
|
|
49
|
+
...(stackContext?.instanceId ? { instanceId: stackContext.instanceId } : {}),
|
|
50
|
+
stackId,
|
|
51
|
+
includeValues: true,
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
const remoteEnv = Array.isArray(res?.data?.data?.env) ? res.data.data.env : [];
|
|
55
|
+
const currentPairs = (form.getFieldValue(['config', 'envPairs']) as ConfigurableEnvItem[] | undefined) ?? [];
|
|
56
|
+
const existingNames = new Set(currentPairs.map((item) => String(item?.name ?? '').trim()).filter(Boolean));
|
|
57
|
+
|
|
58
|
+
let importedCount = 0;
|
|
59
|
+
let skippedSensitiveCount = 0;
|
|
60
|
+
const nextPairs = [...currentPairs];
|
|
61
|
+
|
|
62
|
+
for (const entry of remoteEnv) {
|
|
63
|
+
const keyName = String(entry.name ?? '').trim();
|
|
64
|
+
if (!keyName || existingNames.has(keyName)) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (entry.sensitive) {
|
|
68
|
+
skippedSensitiveCount += 1;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
nextPairs.push({
|
|
72
|
+
name: keyName,
|
|
73
|
+
value: entry.value,
|
|
74
|
+
secret: false,
|
|
75
|
+
});
|
|
76
|
+
existingNames.add(keyName);
|
|
77
|
+
importedCount += 1;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
form.setFieldValue(['config', 'envPairs'], nextPairs);
|
|
81
|
+
if (skippedSensitiveCount > 0) {
|
|
82
|
+
message.info(
|
|
83
|
+
t('Loaded {{count}} variables ({{sensitive}} sensitive keys stay preserved on Portainer in Merge mode)', {
|
|
84
|
+
count: importedCount,
|
|
85
|
+
sensitive: skippedSensitiveCount,
|
|
86
|
+
}),
|
|
87
|
+
);
|
|
88
|
+
} else {
|
|
89
|
+
message.success(t('Loaded {{count}} variables from Portainer Stack', { count: importedCount }));
|
|
90
|
+
}
|
|
91
|
+
} catch {
|
|
92
|
+
message.error(t('Failed to load environment variables from Portainer Stack'));
|
|
93
|
+
} finally {
|
|
94
|
+
setLoadingStackEnv(false);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
return (
|
|
99
|
+
<>
|
|
100
|
+
<Form.Item
|
|
101
|
+
name={['config', 'envStrategy']}
|
|
102
|
+
label={t('Environment merge strategy')}
|
|
103
|
+
initialValue="merge"
|
|
104
|
+
extra={t(
|
|
105
|
+
'Merge keeps existing Portainer Stack variables intact and overrides matching keys; Replace overwrites the entire Stack Env list.',
|
|
106
|
+
)}
|
|
107
|
+
>
|
|
108
|
+
<Radio.Group
|
|
109
|
+
options={[
|
|
110
|
+
{ value: 'merge', label: t('Merge & override existing Stack Env') },
|
|
111
|
+
{ value: 'replace', label: t('Replace all Stack Env') },
|
|
112
|
+
]}
|
|
113
|
+
/>
|
|
114
|
+
</Form.Item>
|
|
115
|
+
|
|
116
|
+
<Form.Item
|
|
117
|
+
label={
|
|
118
|
+
<Flex align="center" justify="space-between" gap="middle" style={{ width: '100%' }}>
|
|
119
|
+
<span>{t('Environment variables (Key-Value)')}</span>
|
|
120
|
+
{canLoadFromStack ? (
|
|
121
|
+
<Button
|
|
122
|
+
size="small"
|
|
123
|
+
htmlType="button"
|
|
124
|
+
icon={<CloudDownloadOutlined />}
|
|
125
|
+
loading={loadingStackEnv}
|
|
126
|
+
onClick={handleLoadFromStack}
|
|
127
|
+
>
|
|
128
|
+
{t('Load Env from Stack')}
|
|
129
|
+
</Button>
|
|
130
|
+
) : null}
|
|
131
|
+
</Flex>
|
|
132
|
+
}
|
|
133
|
+
extra={t(
|
|
134
|
+
'Add variables such as IMAGE_TAG or runtime settings. Mark sensitive values as Secret to redact them in workflow execution logs.',
|
|
135
|
+
)}
|
|
136
|
+
>
|
|
137
|
+
<Form.List name={['config', 'envPairs']}>
|
|
138
|
+
{(fields, { add, remove }) => (
|
|
139
|
+
<Space direction="vertical" style={{ display: 'flex' }}>
|
|
140
|
+
{fields.map((field, index) => {
|
|
141
|
+
const currentName = String(form.getFieldValue(['config', 'envPairs', field.name, 'name']) ?? '').trim();
|
|
142
|
+
const deleteLabel = currentName
|
|
143
|
+
? `${t('Delete environment variable')} ${currentName}`
|
|
144
|
+
: `${t('Delete environment variable')} #${index + 1}`;
|
|
145
|
+
|
|
146
|
+
return (
|
|
147
|
+
<Flex key={field.key} align="center" gap="small" wrap="wrap" style={{ width: '100%' }}>
|
|
148
|
+
<Form.Item name={[field.name, 'name']} style={{ flex: '1 1 160px', marginBottom: 0 }}>
|
|
149
|
+
<Input placeholder="IMAGE_TAG" autoCorrect="off" autoCapitalize="none" spellCheck={false} />
|
|
150
|
+
</Form.Item>
|
|
151
|
+
<Form.Item name={[field.name, 'value']} style={{ flex: '2 1 220px', marginBottom: 0 }}>
|
|
152
|
+
<WorkflowVariableInput placeholder={t('Value or {{variable}}')} />
|
|
153
|
+
</Form.Item>
|
|
154
|
+
<Form.Item
|
|
155
|
+
name={[field.name, 'secret']}
|
|
156
|
+
valuePropName="checked"
|
|
157
|
+
initialValue={false}
|
|
158
|
+
style={{ marginBottom: 0 }}
|
|
159
|
+
>
|
|
160
|
+
<Checkbox>{t('Secret')}</Checkbox>
|
|
161
|
+
</Form.Item>
|
|
162
|
+
<Button
|
|
163
|
+
htmlType="button"
|
|
164
|
+
aria-label={deleteLabel}
|
|
165
|
+
icon={<DeleteOutlined />}
|
|
166
|
+
onClick={() => remove(field.name)}
|
|
167
|
+
/>
|
|
168
|
+
</Flex>
|
|
169
|
+
);
|
|
170
|
+
})}
|
|
171
|
+
<Button
|
|
172
|
+
htmlType="button"
|
|
173
|
+
icon={<PlusOutlined />}
|
|
174
|
+
onClick={() => add({ name: '', value: '', secret: false })}
|
|
175
|
+
style={{ width: '100%' }}
|
|
176
|
+
>
|
|
177
|
+
{t('Add environment variable')}
|
|
178
|
+
</Button>
|
|
179
|
+
</Space>
|
|
180
|
+
)}
|
|
181
|
+
</Form.List>
|
|
182
|
+
</Form.Item>
|
|
183
|
+
|
|
184
|
+
<Collapse
|
|
185
|
+
size="small"
|
|
186
|
+
style={{ marginBottom: 16 }}
|
|
187
|
+
items={[
|
|
188
|
+
{
|
|
189
|
+
key: 'bulk-env',
|
|
190
|
+
label: t('Bulk .env text & dynamic JSON environment'),
|
|
191
|
+
children: (
|
|
192
|
+
<>
|
|
193
|
+
<Form.Item
|
|
194
|
+
name={['config', 'envText']}
|
|
195
|
+
label={t('Bulk .env format')}
|
|
196
|
+
extra={t('Enter KEY=VALUE lines. Supports upstream workflow variables.')}
|
|
197
|
+
>
|
|
198
|
+
<WorkflowVariableTextArea
|
|
199
|
+
autoSize={{ minRows: 3, maxRows: 8 }}
|
|
200
|
+
placeholder={'IMAGE_TAG=v1.0.0\nNODE_ENV=production'}
|
|
201
|
+
/>
|
|
202
|
+
</Form.Item>
|
|
203
|
+
<Form.Item
|
|
204
|
+
name={['config', 'dynamicEnv']}
|
|
205
|
+
label={t('Dynamic environment JSON / variable')}
|
|
206
|
+
extra={t(
|
|
207
|
+
'Pass a JSON object {"KEY":"VAL"}, an array [{"name":"KEY","value":"VAL"}], or a workflow variable from a build catalog record.',
|
|
208
|
+
)}
|
|
209
|
+
style={{ marginBottom: 0 }}
|
|
210
|
+
>
|
|
211
|
+
<WorkflowVariableJsonTextArea
|
|
212
|
+
autoSize={{ minRows: 2, maxRows: 6 }}
|
|
213
|
+
placeholder={'{"IMAGE_TAG": "{{$context.data.imageTag}}"}'}
|
|
214
|
+
/>
|
|
215
|
+
</Form.Item>
|
|
216
|
+
</>
|
|
217
|
+
),
|
|
218
|
+
},
|
|
219
|
+
]}
|
|
220
|
+
/>
|
|
221
|
+
</>
|
|
222
|
+
);
|
|
223
|
+
}
|