plugin-workflow-ci 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.
Files changed (60) hide show
  1. package/client-v2.js +1 -0
  2. package/client.js +1 -0
  3. package/dist/client/index.js +10 -0
  4. package/dist/client-v2/index.js +10 -0
  5. package/dist/externalVersion.js +21 -0
  6. package/dist/index.js +48 -0
  7. package/dist/locale/en-US.json +105 -0
  8. package/dist/locale/vi-VN.json +105 -0
  9. package/dist/locale/zh-CN.json +105 -0
  10. package/dist/server/actions/ci-manager.js +307 -0
  11. package/dist/server/collections/ci-gate-runs.js +85 -0
  12. package/dist/server/collections/ci-git-feedback-outbox.js +51 -0
  13. package/dist/server/collections/ci-pipelines.js +88 -0
  14. package/dist/server/index.js +62 -0
  15. package/dist/server/instructions/CiInstructions.js +180 -0
  16. package/dist/server/migrations/20261206000001-harden-workflow-ci-v020.js +91 -0
  17. package/dist/server/plugin.js +255 -0
  18. package/dist/server/services/AiTriageService.js +215 -0
  19. package/dist/server/services/CiGateWorkflowService.js +1776 -0
  20. package/dist/server/services/EphemeralContainerService.js +891 -0
  21. package/dist/server/services/GitFeedbackService.js +995 -0
  22. package/dist/server/services/constants-internal.js +65 -0
  23. package/dist/shared/collection-schemas.js +125 -0
  24. package/dist/shared/constants.js +120 -0
  25. package/dist/shared/runtime-context.js +67 -0
  26. package/dist/shared/types.js +24 -0
  27. package/dist/shared/ui/CiPipelinesSettingsView.js +700 -0
  28. package/package.json +58 -4
  29. package/server.js +1 -0
  30. package/src/client/index.tsx +10 -0
  31. package/src/client/locale.ts +31 -0
  32. package/src/client/plugin.tsx +62 -0
  33. package/src/client/workflow.tsx +119 -0
  34. package/src/client-v2/index.tsx +10 -0
  35. package/src/client-v2/locale.ts +32 -0
  36. package/src/client-v2/plugin.tsx +77 -0
  37. package/src/client-v2/workflow.tsx +124 -0
  38. package/src/index.ts +11 -0
  39. package/src/locale/en-US.json +105 -0
  40. package/src/locale/vi-VN.json +105 -0
  41. package/src/locale/zh-CN.json +105 -0
  42. package/src/server/actions/ci-manager.ts +377 -0
  43. package/src/server/collections/ci-gate-runs.ts +65 -0
  44. package/src/server/collections/ci-git-feedback-outbox.ts +31 -0
  45. package/src/server/collections/ci-pipelines.ts +68 -0
  46. package/src/server/index.ts +17 -0
  47. package/src/server/instructions/CiInstructions.ts +180 -0
  48. package/src/server/migrations/20261206000001-harden-workflow-ci-v020.ts +78 -0
  49. package/src/server/plugin.ts +274 -0
  50. package/src/server/services/AiTriageService.ts +254 -0
  51. package/src/server/services/CiGateWorkflowService.ts +2310 -0
  52. package/src/server/services/EphemeralContainerService.ts +1170 -0
  53. package/src/server/services/GitFeedbackService.ts +1224 -0
  54. package/src/server/services/constants-internal.ts +27 -0
  55. package/src/shared/collection-schemas.ts +98 -0
  56. package/src/shared/constants.ts +50 -0
  57. package/src/shared/runtime-context.tsx +55 -0
  58. package/src/shared/types.ts +203 -0
  59. package/src/shared/ui/CiPipelinesSettingsView.tsx +1035 -0
  60. package/README.md +0 -3
@@ -0,0 +1,62 @@
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
+ var __create = Object.create;
11
+ var __defProp = Object.defineProperty;
12
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
13
+ var __getOwnPropNames = Object.getOwnPropertyNames;
14
+ var __getProtoOf = Object.getPrototypeOf;
15
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
16
+ var __export = (target, all) => {
17
+ for (var name in all)
18
+ __defProp(target, name, { get: all[name], enumerable: true });
19
+ };
20
+ var __copyProps = (to, from, except, desc) => {
21
+ if (from && typeof from === "object" || typeof from === "function") {
22
+ for (let key of __getOwnPropNames(from))
23
+ if (!__hasOwnProp.call(to, key) && key !== except)
24
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
25
+ }
26
+ return to;
27
+ };
28
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
29
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
30
+ // If the importer is in node compatibility mode or this is not an ESM
31
+ // file that has been converted to a CommonJS file using a Babel-
32
+ // compatible transform (i.e. "__esModule" has not been set), then set
33
+ // "default" to the CommonJS "module.exports" for node compatibility.
34
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
35
+ mod
36
+ ));
37
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
38
+ var server_exports = {};
39
+ __export(server_exports, {
40
+ PluginWorkflowCiServer: () => import_plugin.PluginWorkflowCiServer,
41
+ default: () => import_plugin.default
42
+ });
43
+ module.exports = __toCommonJS(server_exports);
44
+ var import_plugin = __toESM(require("./plugin"));
45
+ __reExport(server_exports, require("./services/GitFeedbackService"), module.exports);
46
+ __reExport(server_exports, require("./services/EphemeralContainerService"), module.exports);
47
+ __reExport(server_exports, require("./services/AiTriageService"), module.exports);
48
+ __reExport(server_exports, require("./services/CiGateWorkflowService"), module.exports);
49
+ __reExport(server_exports, require("./instructions/CiInstructions"), module.exports);
50
+ __reExport(server_exports, require("../shared/constants"), module.exports);
51
+ __reExport(server_exports, require("../shared/types"), module.exports);
52
+ // Annotate the CommonJS export names for ESM import in node:
53
+ 0 && (module.exports = {
54
+ PluginWorkflowCiServer,
55
+ ...require("./services/GitFeedbackService"),
56
+ ...require("./services/EphemeralContainerService"),
57
+ ...require("./services/AiTriageService"),
58
+ ...require("./services/CiGateWorkflowService"),
59
+ ...require("./instructions/CiInstructions"),
60
+ ...require("../shared/constants"),
61
+ ...require("../shared/types")
62
+ });
@@ -0,0 +1,180 @@
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
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __export = (target, all) => {
15
+ for (var name in all)
16
+ __defProp(target, name, { get: all[name], enumerable: true });
17
+ };
18
+ var __copyProps = (to, from, except, desc) => {
19
+ if (from && typeof from === "object" || typeof from === "function") {
20
+ for (let key of __getOwnPropNames(from))
21
+ if (!__hasOwnProp.call(to, key) && key !== except)
22
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
23
+ }
24
+ return to;
25
+ };
26
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
+ var CiInstructions_exports = {};
28
+ __export(CiInstructions_exports, {
29
+ CiStageInstruction: () => CiStageInstruction
30
+ });
31
+ module.exports = __toCommonJS(CiInstructions_exports);
32
+ var import_plugin_workflow = require("@nocobase/plugin-workflow");
33
+ function resolveGateRunId(node, processor) {
34
+ const cfg = node.config && typeof node.config === "object" ? node.config : {};
35
+ const parsed = typeof processor.getParsedValue === "function" ? processor.getParsedValue(cfg, node.id) : cfg;
36
+ if ((parsed == null ? void 0 : parsed.gateRunId) !== void 0 && (parsed == null ? void 0 : parsed.gateRunId) !== "") {
37
+ const explicit = Number(parsed.gateRunId);
38
+ if (Number.isFinite(explicit) && explicit > 0) return explicit;
39
+ }
40
+ const ctx = processor.execution.context;
41
+ if (!ctx || typeof ctx !== "object") return null;
42
+ const data = ctx.data;
43
+ if (typeof data === "number" || typeof data === "string") {
44
+ const n = Number(data);
45
+ return Number.isFinite(n) && n > 0 ? n : null;
46
+ }
47
+ if (data && typeof data === "object") {
48
+ const obj = data;
49
+ if (obj.gateRunId !== void 0) {
50
+ const n = Number(obj.gateRunId);
51
+ if (Number.isFinite(n) && n > 0) return n;
52
+ }
53
+ if (obj.id !== void 0) {
54
+ const n = Number(obj.id);
55
+ if (Number.isFinite(n) && n > 0) return n;
56
+ }
57
+ }
58
+ return null;
59
+ }
60
+ class CiStageInstruction extends import_plugin_workflow.Instruction {
61
+ constructor(workflow, service, role) {
62
+ super(workflow);
63
+ this.service = service;
64
+ this.role = role;
65
+ }
66
+ async executeInBackground(params) {
67
+ await new Promise((resolve) => {
68
+ setImmediate(resolve);
69
+ });
70
+ const { gateRunId, workflowId, executionId, jobId, abort } = params;
71
+ try {
72
+ if (this.role === "dispatch") {
73
+ const out = await this.service.runDispatchStage(gateRunId, {
74
+ workflowId,
75
+ executionId,
76
+ jobId,
77
+ signal: abort.signal
78
+ });
79
+ if (!out.waitingTestRun) {
80
+ await this.service.settleJobExclusively(jobId, import_plugin_workflow.JOB_STATUS.RESOLVED, out.payload);
81
+ }
82
+ return;
83
+ }
84
+ if (this.role === "triage") {
85
+ const payload2 = await this.service.runAiTriageStage(gateRunId);
86
+ await this.service.settleJobExclusively(jobId, import_plugin_workflow.JOB_STATUS.RESOLVED, payload2);
87
+ return;
88
+ }
89
+ if (this.role === "feedback") {
90
+ const payload2 = await this.service.runPublishGitFeedbackStage(gateRunId);
91
+ await this.service.settleJobExclusively(jobId, import_plugin_workflow.JOB_STATUS.RESOLVED, payload2);
92
+ return;
93
+ }
94
+ if (this.role === "wait_approval") {
95
+ const out = await this.service.runWaitApprovalStage(gateRunId, {
96
+ workflowId,
97
+ executionId,
98
+ jobId
99
+ });
100
+ if (!out.waitingApproval) {
101
+ await this.service.settleJobExclusively(jobId, import_plugin_workflow.JOB_STATUS.RESOLVED, out.payload);
102
+ }
103
+ return;
104
+ }
105
+ const payload = await this.service.runPromoteDeployStage(gateRunId, abort.signal);
106
+ await this.service.settleJobExclusively(jobId, import_plugin_workflow.JOB_STATUS.RESOLVED, payload);
107
+ } catch (error) {
108
+ const msg = error instanceof Error ? error.message : String(error);
109
+ await this.service.markGateRunFailed(gateRunId, msg);
110
+ await this.service.settleJobExclusively(jobId, import_plugin_workflow.JOB_STATUS.FAILED, { gateRunId, error: msg });
111
+ } finally {
112
+ abort.dispose();
113
+ }
114
+ }
115
+ async run(node, prevJob, processor) {
116
+ const gateRunId = resolveGateRunId(node, processor);
117
+ if (!gateRunId) {
118
+ return {
119
+ status: import_plugin_workflow.JOB_STATUS.ERROR,
120
+ result: { error: "Missing ciGateRuns id in workflow execution context" }
121
+ };
122
+ }
123
+ const execMeta = {
124
+ workflowId: Number(processor.execution.workflowId),
125
+ executionId: Number(processor.execution.id)
126
+ };
127
+ if (processor.isInstructionSync(node)) {
128
+ try {
129
+ if (this.role === "dispatch") {
130
+ const out = await this.service.runDispatchStage(gateRunId, execMeta);
131
+ return { status: import_plugin_workflow.JOB_STATUS.RESOLVED, result: out.payload };
132
+ }
133
+ if (this.role === "triage") {
134
+ return { status: import_plugin_workflow.JOB_STATUS.RESOLVED, result: await this.service.runAiTriageStage(gateRunId) };
135
+ }
136
+ if (this.role === "feedback") {
137
+ return { status: import_plugin_workflow.JOB_STATUS.RESOLVED, result: await this.service.runPublishGitFeedbackStage(gateRunId) };
138
+ }
139
+ if (this.role === "wait_approval") {
140
+ const out = await this.service.runWaitApprovalStage(gateRunId, execMeta);
141
+ return { status: import_plugin_workflow.JOB_STATUS.RESOLVED, result: out.payload };
142
+ }
143
+ return { status: import_plugin_workflow.JOB_STATUS.RESOLVED, result: await this.service.runPromoteDeployStage(gateRunId) };
144
+ } catch (error) {
145
+ const msg = error instanceof Error ? error.message : String(error);
146
+ await this.service.markGateRunFailed(gateRunId, msg);
147
+ return { status: import_plugin_workflow.JOB_STATUS.FAILED, result: { gateRunId, error: msg } };
148
+ }
149
+ }
150
+ const savedJob = processor.saveJob({
151
+ status: import_plugin_workflow.JOB_STATUS.PENDING,
152
+ nodeId: node.id,
153
+ nodeKey: node.key,
154
+ upstreamId: (prevJob == null ? void 0 : prevJob.id) ?? null,
155
+ result: { gateRunId, role: this.role, state: "running" }
156
+ });
157
+ const abort = processor.createBackgroundAbortHandle();
158
+ try {
159
+ await processor.exit();
160
+ } catch (error) {
161
+ abort.dispose();
162
+ throw error;
163
+ }
164
+ const backgroundPromise = this.executeInBackground({
165
+ gateRunId,
166
+ workflowId: execMeta.workflowId,
167
+ executionId: execMeta.executionId,
168
+ jobId: Number(savedJob.id),
169
+ abort
170
+ });
171
+ this.service.trackBackgroundTask(backgroundPromise);
172
+ }
173
+ async resume(_node, job) {
174
+ return job;
175
+ }
176
+ }
177
+ // Annotate the CommonJS export names for ESM import in node:
178
+ 0 && (module.exports = {
179
+ CiStageInstruction
180
+ });
@@ -0,0 +1,91 @@
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
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __export = (target, all) => {
15
+ for (var name in all)
16
+ __defProp(target, name, { get: all[name], enumerable: true });
17
+ };
18
+ var __copyProps = (to, from, except, desc) => {
19
+ if (from && typeof from === "object" || typeof from === "function") {
20
+ for (let key of __getOwnPropNames(from))
21
+ if (!__hasOwnProp.call(to, key) && key !== except)
22
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
23
+ }
24
+ return to;
25
+ };
26
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
+ var harden_workflow_ci_v020_exports = {};
28
+ __export(harden_workflow_ci_v020_exports, {
29
+ default: () => HardenWorkflowCiV020Migration
30
+ });
31
+ module.exports = __toCommonJS(harden_workflow_ci_v020_exports);
32
+ var import_database = require("@nocobase/database");
33
+ var import_server = require("@nocobase/server");
34
+ var import_constants = require("../../shared/constants");
35
+ class HardenWorkflowCiV020Migration extends import_server.Migration {
36
+ on = "afterLoad";
37
+ resolveTableName(collectionName) {
38
+ const col = this.db.getCollection(collectionName);
39
+ if (!(col == null ? void 0 : col.model)) return null;
40
+ const rawTable = col.model.getTableName();
41
+ return typeof rawTable === "string" ? rawTable : rawTable.tableName;
42
+ }
43
+ async ensureColumn(tableName, columnName, definition) {
44
+ const qi = this.db.sequelize.getQueryInterface();
45
+ const desc = await qi.describeTable(tableName).catch(() => null);
46
+ if (!desc || Object.prototype.hasOwnProperty.call(desc, columnName)) {
47
+ return;
48
+ }
49
+ await qi.addColumn(tableName, columnName, {
50
+ allowNull: true,
51
+ ...definition
52
+ });
53
+ }
54
+ async up() {
55
+ const pipesTable = this.resolveTableName(import_constants.CI_PIPELINES_COLLECTION);
56
+ if (pipesTable) {
57
+ await this.ensureColumn(pipesTable, "protectedBranches", { type: import_database.DataTypes.JSON });
58
+ await this.ensureColumn(pipesTable, "qaInstanceId", { type: import_database.DataTypes.BIGINT });
59
+ await this.ensureColumn(pipesTable, "qaEndpointId", { type: import_database.DataTypes.BIGINT });
60
+ await this.ensureColumn(pipesTable, "qaStackId", { type: import_database.DataTypes.BIGINT });
61
+ await this.ensureColumn(pipesTable, "qaAppServices", { type: import_database.DataTypes.JSON });
62
+ await this.ensureColumn(pipesTable, "dbBackwardCompatible", {
63
+ type: import_database.DataTypes.BOOLEAN,
64
+ defaultValue: true
65
+ });
66
+ await this.ensureColumn(pipesTable, "promotionHalted", {
67
+ type: import_database.DataTypes.BOOLEAN,
68
+ defaultValue: false
69
+ });
70
+ await this.ensureColumn(pipesTable, "promotionHaltReason", { type: import_database.DataTypes.TEXT });
71
+ await this.ensureColumn(pipesTable, "lastKnownGoodSnapshot", { type: import_database.DataTypes.JSON });
72
+ }
73
+ const runsTable = this.resolveTableName(import_constants.CI_GATE_RUNS_COLLECTION);
74
+ if (runsTable) {
75
+ await this.ensureColumn(runsTable, "buildRunId", { type: import_database.DataTypes.BIGINT });
76
+ await this.ensureColumn(runsTable, "operationId", { type: import_database.DataTypes.STRING(128) });
77
+ await this.ensureColumn(runsTable, "verdictReasonCode", { type: import_database.DataTypes.STRING(64) });
78
+ await this.ensureColumn(runsTable, "approvalState", {
79
+ type: import_database.DataTypes.STRING(32),
80
+ defaultValue: "not_required"
81
+ });
82
+ await this.ensureColumn(runsTable, "approvalRecord", { type: import_database.DataTypes.JSON });
83
+ await this.ensureColumn(runsTable, "deployOutcome", {
84
+ type: import_database.DataTypes.STRING(32),
85
+ defaultValue: "not_applicable"
86
+ });
87
+ await this.ensureColumn(runsTable, "rollbackRecord", { type: import_database.DataTypes.JSON });
88
+ await this.ensureColumn(runsTable, "gitFeedbackSyncState", { type: import_database.DataTypes.STRING(32) });
89
+ }
90
+ }
91
+ }
@@ -0,0 +1,255 @@
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
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __export = (target, all) => {
15
+ for (var name in all)
16
+ __defProp(target, name, { get: all[name], enumerable: true });
17
+ };
18
+ var __copyProps = (to, from, except, desc) => {
19
+ if (from && typeof from === "object" || typeof from === "function") {
20
+ for (let key of __getOwnPropNames(from))
21
+ if (!__hasOwnProp.call(to, key) && key !== except)
22
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
23
+ }
24
+ return to;
25
+ };
26
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
27
+ var plugin_exports = {};
28
+ __export(plugin_exports, {
29
+ CLUSTER_HOT_RELOAD_COMPLETED_EVENT: () => CLUSTER_HOT_RELOAD_COMPLETED_EVENT,
30
+ PluginWorkflowCiServer: () => PluginWorkflowCiServer,
31
+ default: () => plugin_default
32
+ });
33
+ module.exports = __toCommonJS(plugin_exports);
34
+ var import_node_path = require("node:path");
35
+ var import_server = require("@nocobase/server");
36
+ var import_constants = require("../shared/constants");
37
+ var import_ci_manager = require("./actions/ci-manager");
38
+ var import_CiInstructions = require("./instructions/CiInstructions");
39
+ var import_AiTriageService = require("./services/AiTriageService");
40
+ var import_CiGateWorkflowService = require("./services/CiGateWorkflowService");
41
+ var import_EphemeralContainerService = require("./services/EphemeralContainerService");
42
+ var import_GitFeedbackService = require("./services/GitFeedbackService");
43
+ const CLUSTER_HOT_RELOAD_COMPLETED_EVENT = "cluster-manager:hot-reload-completed";
44
+ function maskWebhookSecretInPayload(payload) {
45
+ if (!payload || typeof payload !== "object") return payload;
46
+ if (Array.isArray(payload)) return payload.map(maskWebhookSecretInPayload);
47
+ const obj = payload;
48
+ if (typeof obj.toJSON === "function") {
49
+ return maskWebhookSecretInPayload(obj.toJSON());
50
+ }
51
+ const clone = { ...obj };
52
+ if (typeof clone.webhookSecret === "string" && clone.webhookSecret.length > 0) {
53
+ clone.webhookSecret = import_constants.SECRET_MASK;
54
+ }
55
+ if (Array.isArray(clone.data)) {
56
+ clone.data = clone.data.map(maskWebhookSecretInPayload);
57
+ } else if (clone.data && typeof clone.data === "object") {
58
+ clone.data = maskWebhookSecretInPayload(clone.data);
59
+ }
60
+ if (Array.isArray(clone.rows)) {
61
+ clone.rows = clone.rows.map(maskWebhookSecretInPayload);
62
+ }
63
+ return clone;
64
+ }
65
+ class PluginWorkflowCiServer extends import_server.Plugin {
66
+ gitFeedbackService;
67
+ containerService;
68
+ aiTriageService;
69
+ workflowService;
70
+ handleTestRunCompleted = (testRun) => {
71
+ if (!this.workflowService) return;
72
+ const deferred = new Promise((resolve2) => {
73
+ setTimeout(resolve2, 50);
74
+ }).then(() => this.workflowService.handleTestRunCompleted(testRun)).catch((error) => {
75
+ var _a, _b;
76
+ (_b = (_a = this.app.logger) == null ? void 0 : _a.warn) == null ? void 0 : _b.call(_a, "[plugin-workflow-ci] Failed to handle test-manager:run-completed event", {
77
+ error: error instanceof Error ? error.message : String(error)
78
+ });
79
+ });
80
+ this.workflowService.trackBackgroundTask(deferred);
81
+ };
82
+ handlePortainerRunSaved = (model, options) => {
83
+ var _a;
84
+ const dispatch = () => {
85
+ if (!this.workflowService) return;
86
+ const deferred = new Promise((resolve2) => {
87
+ setImmediate(resolve2);
88
+ }).then(() => this.workflowService.handlePortainerBuildRunSaved(model)).catch((error) => {
89
+ var _a2, _b;
90
+ (_b = (_a2 = this.app.logger) == null ? void 0 : _a2.warn) == null ? void 0 : _b.call(_a2, "[plugin-workflow-ci] Failed to trigger pre-deploy CI gate from Portainer build", {
91
+ error: error instanceof Error ? error.message : String(error)
92
+ });
93
+ });
94
+ this.workflowService.trackBackgroundTask(deferred);
95
+ };
96
+ if (typeof ((_a = options == null ? void 0 : options.transaction) == null ? void 0 : _a.afterCommit) === "function") {
97
+ options.transaction.afterCommit(dispatch);
98
+ return;
99
+ }
100
+ dispatch();
101
+ };
102
+ handleClusterHotReloadCompleted = (payload) => {
103
+ if (!this.workflowService) return;
104
+ const deferred = new Promise((resolve2) => {
105
+ setImmediate(resolve2);
106
+ }).then(() => this.workflowService.triggerClusterHotReloadSmoke(payload || {})).catch((error) => {
107
+ var _a, _b;
108
+ (_b = (_a = this.app.logger) == null ? void 0 : _a.warn) == null ? void 0 : _b.call(_a, "[plugin-workflow-ci] Failed to trigger cluster hot-reload smoke run", {
109
+ error: error instanceof Error ? error.message : String(error)
110
+ });
111
+ });
112
+ this.workflowService.trackBackgroundTask(deferred);
113
+ };
114
+ handleAfterStart = async () => {
115
+ var _a;
116
+ await ((_a = this.workflowService) == null ? void 0 : _a.start());
117
+ };
118
+ handleStop = async () => {
119
+ var _a;
120
+ await ((_a = this.workflowService) == null ? void 0 : _a.stop());
121
+ };
122
+ handleDestroy = async () => {
123
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
124
+ (_b = (_a = this.app).off) == null ? void 0 : _b.call(_a, import_constants.TEST_RUN_COMPLETED_EVENT, this.handleTestRunCompleted);
125
+ (_d = (_c = this.app).off) == null ? void 0 : _d.call(_c, CLUSTER_HOT_RELOAD_COMPLETED_EVENT, this.handleClusterHotReloadCompleted);
126
+ this.db.off(`${import_constants.PORTAINER_PIPELINE_RUNS_COLLECTION}.afterSave`, this.handlePortainerRunSaved);
127
+ (_f = (_e = this.app).off) == null ? void 0 : _f.call(_e, "afterStart", this.handleAfterStart);
128
+ (_h = (_g = this.app).off) == null ? void 0 : _h.call(_g, "beforeStop", this.handleStop);
129
+ (_j = (_i = this.app).off) == null ? void 0 : _j.call(_i, "beforeDestroy", this.handleDestroy);
130
+ await ((_k = this.workflowService) == null ? void 0 : _k.stop());
131
+ };
132
+ async beforeLoad() {
133
+ this.db.addMigrations({
134
+ namespace: this.name,
135
+ directory: (0, import_node_path.resolve)(__dirname, "migrations"),
136
+ context: {
137
+ plugin: this
138
+ }
139
+ });
140
+ await this.db.import({
141
+ directory: (0, import_node_path.resolve)(__dirname, "collections")
142
+ });
143
+ this.db.on(`${import_constants.CI_PIPELINES_COLLECTION}.beforeSave`, async (model) => {
144
+ if (model.changed("key") && model.get("key")) {
145
+ const cleanKey = String(model.get("key")).trim().replace(/[^a-zA-Z0-9_-]+/g, "-").slice(0, 64);
146
+ if (!cleanKey) {
147
+ throw new Error("Valid CI pipeline key is required");
148
+ }
149
+ model.set("key", cleanKey);
150
+ }
151
+ if (this.workflowService) {
152
+ await this.workflowService.assertValidPipelineConfiguration(model.toJSON());
153
+ }
154
+ if (model.changed("webhookSecret")) {
155
+ const rawVal = model.get("webhookSecret");
156
+ if (rawVal === import_constants.SECRET_MASK) {
157
+ model.set("webhookSecret", model.previous("webhookSecret"));
158
+ } else if (typeof rawVal === "string" && rawVal.trim()) {
159
+ const encryptor = this.app.aesEncryptor;
160
+ if (encryptor) {
161
+ model.set("webhookSecret", await encryptor.encrypt(rawVal.trim()));
162
+ }
163
+ }
164
+ }
165
+ });
166
+ }
167
+ async load() {
168
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
169
+ const workflow = this.app.pm.get("workflow") || this.app.pm.get("@nocobase/plugin-workflow");
170
+ this.gitFeedbackService = new import_GitFeedbackService.GitFeedbackService(this.app);
171
+ this.containerService = new import_EphemeralContainerService.EphemeralContainerService(this.app);
172
+ this.aiTriageService = new import_AiTriageService.AiTriageService(this.app);
173
+ this.workflowService = new import_CiGateWorkflowService.CiGateWorkflowService(
174
+ this.app,
175
+ workflow,
176
+ this.gitFeedbackService,
177
+ this.containerService,
178
+ this.aiTriageService
179
+ );
180
+ if (workflow == null ? void 0 : workflow.registerInstruction) {
181
+ workflow.registerInstruction(
182
+ import_constants.CI_INSTRUCTIONS.dispatchRun,
183
+ new import_CiInstructions.CiStageInstruction(workflow, this.workflowService, "dispatch")
184
+ );
185
+ workflow.registerInstruction(
186
+ import_constants.CI_INSTRUCTIONS.aiTriage,
187
+ new import_CiInstructions.CiStageInstruction(workflow, this.workflowService, "triage")
188
+ );
189
+ workflow.registerInstruction(
190
+ import_constants.CI_INSTRUCTIONS.publishGitFeedback,
191
+ new import_CiInstructions.CiStageInstruction(workflow, this.workflowService, "feedback")
192
+ );
193
+ workflow.registerInstruction(
194
+ import_constants.CI_INSTRUCTIONS.waitApproval,
195
+ new import_CiInstructions.CiStageInstruction(workflow, this.workflowService, "wait_approval")
196
+ );
197
+ workflow.registerInstruction(
198
+ import_constants.CI_INSTRUCTIONS.promoteDeploy,
199
+ new import_CiInstructions.CiStageInstruction(workflow, this.workflowService, "promote")
200
+ );
201
+ }
202
+ this.app.resourceManager.define({
203
+ name: import_constants.CI_MANAGER_RESOURCE,
204
+ actions: (0, import_ci_manager.createCiManagerResourceActions)(this.app, () => this.workflowService)
205
+ });
206
+ this.app.resourceManager.use(async (ctx, next) => {
207
+ var _a2;
208
+ await next();
209
+ if (((_a2 = ctx.action) == null ? void 0 : _a2.resourceName) === import_constants.CI_PIPELINES_COLLECTION && ctx.body) {
210
+ ctx.body = maskWebhookSecretInPayload(ctx.body);
211
+ }
212
+ });
213
+ this.app.acl.registerSnippet({
214
+ name: import_constants.CI_MANAGER_ACL_SNIPPET,
215
+ actions: [
216
+ `${import_constants.CI_PIPELINES_COLLECTION}:*`,
217
+ `${import_constants.CI_GATE_RUNS_COLLECTION}:*`,
218
+ `${import_constants.CI_GIT_FEEDBACK_OUTBOX_COLLECTION}:*`,
219
+ `${import_constants.CI_MANAGER_RESOURCE}:*`
220
+ ]
221
+ });
222
+ const teamActions = [
223
+ "listMeta",
224
+ "listGateRuns",
225
+ "triggerGate",
226
+ "approveDiffAndPromote",
227
+ "clearPromotionHalt",
228
+ "triggerHotReloadSmoke",
229
+ "pollNow"
230
+ ];
231
+ for (const action of teamActions) {
232
+ this.app.acl.allow(import_constants.CI_MANAGER_RESOURCE, action, "loggedIn");
233
+ }
234
+ this.app.acl.allow(import_constants.CI_MANAGER_RESOURCE, "webhook", "public");
235
+ (_b = (_a = this.app).on) == null ? void 0 : _b.call(_a, import_constants.TEST_RUN_COMPLETED_EVENT, this.handleTestRunCompleted);
236
+ (_d = (_c = this.app).on) == null ? void 0 : _d.call(_c, CLUSTER_HOT_RELOAD_COMPLETED_EVENT, this.handleClusterHotReloadCompleted);
237
+ this.db.on(`${import_constants.PORTAINER_PIPELINE_RUNS_COLLECTION}.afterSave`, this.handlePortainerRunSaved);
238
+ (_f = (_e = this.app).on) == null ? void 0 : _f.call(_e, "afterStart", this.handleAfterStart);
239
+ if (typeof this.app.prependListener === "function") {
240
+ this.app.prependListener("beforeStop", this.handleStop);
241
+ } else {
242
+ (_h = (_g = this.app).on) == null ? void 0 : _h.call(_g, "beforeStop", this.handleStop);
243
+ }
244
+ (_j = (_i = this.app).on) == null ? void 0 : _j.call(_i, "beforeDestroy", this.handleDestroy);
245
+ }
246
+ async beforeDisable() {
247
+ await this.handleDestroy();
248
+ }
249
+ }
250
+ var plugin_default = PluginWorkflowCiServer;
251
+ // Annotate the CommonJS export names for ESM import in node:
252
+ 0 && (module.exports = {
253
+ CLUSTER_HOT_RELOAD_COMPLETED_EVENT,
254
+ PluginWorkflowCiServer
255
+ });