plugin-git-manager 1.2.11 → 1.2.13

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.
@@ -1,242 +1,250 @@
1
- import { Plugin } from '@nocobase/server';
2
- import { resolve } from 'path';
3
- import { DataTypes } from 'sequelize';
4
- import * as gitActions from './actions/git-actions';
5
- import * as gitlabApi from './actions/gitlab-api';
6
- import * as reviewActions from './actions/review';
7
- import * as pollerActions from './actions/poller';
8
- import * as rolePermissionsActions from './actions/role-permissions';
9
- import { recoverStuckReviews, registerReviewQueue, unregisterReviewQueue } from './actions/review';
10
- import { registerGitReviewAiTools } from './ai-tools';
11
- import { startPoller, stopPoller } from './poller';
12
-
13
- export class PluginGitManagerServer extends Plugin {
14
- // @ts-ignore
15
- declare app: any;
16
- // @ts-ignore
17
- declare db: any;
18
- async beforeLoad() {
19
- await (this as any).app.db.import({
20
- directory: resolve(__dirname, 'collections'),
21
- });
22
-
23
- (this as any).app.db.addMigrations({
24
- namespace: (this as any).name,
25
- directory: resolve(__dirname, 'migrations'),
26
- context: { plugin: this },
27
- });
28
- }
29
-
30
- async load() {
31
- // Ensure dayjs timezone + utc plugins are loaded globally to prevent 'm.startOf is not a function' errors
32
- const dayjsLib = require('dayjs');
33
- const utcPlugin = require('dayjs/plugin/utc');
34
- const timezonePlugin = require('dayjs/plugin/timezone');
35
- dayjsLib.extend(utcPlugin);
36
- dayjsLib.extend(timezonePlugin);
37
-
38
- (this as any).app.resourceManager.define({
39
- name: 'gitManager',
40
- actions: {
41
- clone: gitActions.clone,
42
- pull: gitActions.pull,
43
- push: gitActions.push,
44
- fetch: gitActions.fetch,
45
- diff: gitActions.diff,
46
- status: gitActions.status,
47
- log: gitActions.log,
48
- branches: gitActions.branches,
49
- checkout: gitActions.checkout,
50
- fileTree: gitActions.fileTree,
51
- fileContent: gitActions.fileContent,
52
- commitDetail: gitActions.commitDetail,
53
- mergeRequests: gitlabApi.mergeRequests,
54
- mergeRequestDetail: gitlabApi.mergeRequestDetail,
55
- mergeRequestNotes: gitlabApi.mergeRequestNotes,
56
- triggerReview: reviewActions.triggerReview,
57
- reviewApprovePost: reviewActions.reviewApprovePost,
58
- reviewReject: reviewActions.reviewReject,
59
- pollNow: pollerActions.pollNow,
60
- pollerStatus: pollerActions.pollerStatus,
61
- rolePermissions: rolePermissionsActions.rolePermissions,
62
- updateRolePermissions: rolePermissionsActions.updateRolePermissions,
63
- },
64
- });
65
-
66
- // Suppress noisy workflow pre-action/post-action warnings for custom resources
67
- (this as any).app.use(async (ctx, next) => {
68
- if (ctx.logger && ctx.logger.warn) {
69
- const originalWarn = ctx.logger.warn.bind(ctx.logger);
70
- ctx.logger.warn = (message: any, ...args: any[]) => {
71
- if (
72
- typeof message === 'string' &&
73
- message.includes('[Workflow') &&
74
- message.includes('collection') &&
75
- message.includes('not found')
76
- ) {
77
- return ctx.logger;
78
- }
79
- return originalWarn(message, ...args);
80
- };
81
- }
82
- return next();
83
- });
84
-
85
- registerReviewQueue((this as any).app);
86
- registerGitReviewAiTools((this as any).app);
87
-
88
- (this as any).app.on('afterStart', async () => {
89
- await ensureAutoReviewFlowSchema((this as any).app).catch(
90
- (err) => (this as any).app.log?.error?.('plugin-git-manager: ensure schema error', err),
91
- );
92
- // Sweep any review left in `running` state from a previous process.
93
- recoverStuckReviews((this as any).app).catch(
94
- (err) => (this as any).app.log?.error?.('plugin-git-manager: recoverStuckReviews error', err),
95
- );
96
- startPoller((this as any).app);
97
- });
98
- (this as any).app.on('beforeStop', () => {
99
- unregisterReviewQueue((this as any).app);
100
- stopPoller();
101
- });
102
- (this as any).app.on('beforeDestroy', () => {
103
- unregisterReviewQueue((this as any).app);
104
- stopPoller();
105
- });
106
-
107
- // Read-only operations available to all plugin users
108
- (this as any).app.acl.registerSnippet({
109
- name: `pm.${(this as any).name}.read`,
110
- actions: [
111
- 'gitRepositories:list',
112
- 'gitRepositories:get',
113
- 'gitReviewFlows:list',
114
- 'gitReviewFlows:get',
115
- 'gitCodeReviews:list',
116
- 'gitCodeReviews:get',
117
- 'gitManager:status',
118
- 'gitManager:log',
119
- 'gitManager:branches',
120
- 'gitManager:diff',
121
- 'gitManager:fileTree',
122
- 'gitManager:fileContent',
123
- 'gitManager:commitDetail',
124
- 'gitManager:mergeRequests',
125
- 'gitManager:mergeRequestDetail',
126
- 'gitManager:mergeRequestNotes',
127
- 'gitManager:pollerStatus',
128
- ],
129
- });
130
-
131
- // Write operations require separate permission
132
- (this as any).app.acl.registerSnippet({
133
- name: `pm.${(this as any).name}.write`,
134
- actions: [
135
- 'gitRepositories:create',
136
- 'gitRepositories:update',
137
- 'gitRepositories:destroy',
138
- 'gitReviewFlows:create',
139
- 'gitReviewFlows:update',
140
- 'gitReviewFlows:destroy',
141
- 'gitCodeReviews:create',
142
- 'gitCodeReviews:update',
143
- 'gitCodeReviews:destroy',
144
- 'gitManager:clone',
145
- 'gitManager:pull',
146
- 'gitManager:push',
147
- 'gitManager:fetch',
148
- 'gitManager:checkout',
149
- 'gitManager:triggerReview',
150
- 'gitManager:reviewApprovePost',
151
- 'gitManager:reviewReject',
152
- 'gitManager:pollNow',
153
- ],
154
- });
155
-
156
- // Repositories config — CRUD on gitRepositories only (no operations)
157
- (this as any).app.acl.registerSnippet({
158
- name: `pm.${(this as any).name}.repositories`,
159
- actions: [
160
- 'gitRepositories:*',
161
- ],
162
- });
163
-
164
- // Full management — all git manager actions
165
- (this as any).app.acl.registerSnippet({
166
- name: `pm.${(this as any).name}.manage`,
167
- actions: [
168
- `pm.${(this as any).name}.repositories`,
169
- `pm.${(this as any).name}.read`,
170
- `pm.${(this as any).name}.write`,
171
- ],
172
- });
173
-
174
- // Prevent overwriting PAT with obfuscated value on updates
175
- (this as any).app.resourceManager.use(async (ctx, next) => {
176
- if (ctx.action?.resourceName === 'gitRepositories' && ['create', 'update'].includes(ctx.action?.actionName)) {
177
- if (ctx.action.params?.values?.pat === '••••••••') {
178
- delete ctx.action.params.values.pat;
179
- }
180
- if (ctx.request.body?.pat === '••••••••') {
181
- delete ctx.request.body.pat;
182
- }
183
- }
184
- return next();
185
- });
186
-
187
- // Strip PAT from API responses — scoped to gitRepositories only
188
- (this as any).app.resourceManager.use(async (ctx, next) => {
189
- if (ctx.action?.resourceName !== 'gitRepositories') {
190
- return next();
191
- }
192
- await next();
193
- if (ctx.body) {
194
- const items = Array.isArray(ctx.body)
195
- ? ctx.body
196
- : ctx.body?.data
197
- ? Array.isArray(ctx.body.data)
198
- ? ctx.body.data
199
- : [ctx.body.data]
200
- : [ctx.body];
201
- items.forEach((item) => {
202
- if (item && typeof item === 'object') {
203
- if (item.pat) item.pat = '••••••••';
204
- if (item.dataValues?.pat) item.dataValues.pat = '••••••••';
205
- }
206
- });
207
- }
208
- });
209
- }
210
-
211
- async install() {
212
- await (this as any).app.db.getCollection('gitRepositories')?.sync();
213
- }
214
-
215
- async beforeDisable() {
216
- unregisterReviewQueue((this as any).app);
217
- stopPoller();
218
- }
219
-
220
- async beforeUnload() {
221
- unregisterReviewQueue((this as any).app);
222
- stopPoller();
223
- }
224
- }
225
-
226
- export async function ensureAutoReviewFlowSchema(app: any) {
227
- const sequelize = app.db?.sequelize;
228
- const queryInterface = sequelize?.getQueryInterface?.();
229
- if (!queryInterface) return;
230
-
231
- const tablePrefix = app.db.options?.tablePrefix || '';
232
- const tableName = `${tablePrefix}gitRepositories`;
233
- const tableInfo = await queryInterface.describeTable(tableName).catch(() => null);
234
- if (!tableInfo || tableInfo.autoReviewFlowId) return;
235
-
236
- await queryInterface.addColumn(tableName, 'autoReviewFlowId', {
237
- type: DataTypes.INTEGER,
238
- allowNull: true,
239
- });
240
- }
241
-
242
- export default PluginGitManagerServer;
1
+ import { Plugin } from '@nocobase/server';
2
+ import { resolve } from 'path';
3
+ import { DataTypes } from 'sequelize';
4
+ import * as gitActions from './actions/git-actions';
5
+ import * as gitlabApi from './actions/gitlab-api';
6
+ import * as reviewActions from './actions/review';
7
+ import * as pollerActions from './actions/poller';
8
+ import * as rolePermissionsActions from './actions/role-permissions';
9
+ import { recoverStuckReviews, registerReviewQueue, unregisterReviewQueue } from './actions/review';
10
+ import { registerGitReviewAiTools } from './ai-tools';
11
+ import { startPoller, stopPoller } from './poller';
12
+
13
+ export class PluginGitManagerServer extends Plugin {
14
+ // @ts-ignore
15
+ declare app: any;
16
+ // @ts-ignore
17
+ declare db: any;
18
+ async beforeLoad() {
19
+ await (this as any).app.db.import({
20
+ directory: resolve(__dirname, 'collections'),
21
+ });
22
+
23
+ (this as any).app.db.addMigrations({
24
+ namespace: (this as any).name,
25
+ directory: resolve(__dirname, 'migrations'),
26
+ context: { plugin: this },
27
+ });
28
+ }
29
+
30
+ async load() {
31
+ // Ensure dayjs timezone + utc plugins are loaded globally to prevent 'm.startOf is not a function' errors
32
+ const dayjsLib = require('dayjs');
33
+ const utcPlugin = require('dayjs/plugin/utc');
34
+ const timezonePlugin = require('dayjs/plugin/timezone');
35
+ dayjsLib.extend(utcPlugin);
36
+ dayjsLib.extend(timezonePlugin);
37
+
38
+ (this as any).app.resourceManager.define({
39
+ name: 'gitManager',
40
+ actions: {
41
+ clone: gitActions.clone,
42
+ pull: gitActions.pull,
43
+ push: gitActions.push,
44
+ fetch: gitActions.fetch,
45
+ diff: gitActions.diff,
46
+ status: gitActions.status,
47
+ log: gitActions.log,
48
+ branches: gitActions.branches,
49
+ checkout: gitActions.checkout,
50
+ fileTree: gitActions.fileTree,
51
+ fileContent: gitActions.fileContent,
52
+ commitDetail: gitActions.commitDetail,
53
+ mergeRequests: gitlabApi.mergeRequests,
54
+ mergeRequestDetail: gitlabApi.mergeRequestDetail,
55
+ mergeRequestNotes: gitlabApi.mergeRequestNotes,
56
+ triggerReview: reviewActions.triggerReview,
57
+ reviewApprovePost: reviewActions.reviewApprovePost,
58
+ reviewReject: reviewActions.reviewReject,
59
+ pollNow: pollerActions.pollNow,
60
+ pollerStatus: pollerActions.pollerStatus,
61
+ rolePermissions: rolePermissionsActions.rolePermissions,
62
+ updateRolePermissions: rolePermissionsActions.updateRolePermissions,
63
+ },
64
+ });
65
+
66
+ // Suppress noisy workflow pre-action/post-action warnings for custom resources
67
+ (this as any).app.use(async (ctx, next) => {
68
+ if (ctx.logger && ctx.logger.warn) {
69
+ const originalWarn = ctx.logger.warn.bind(ctx.logger);
70
+ ctx.logger.warn = (message: any, ...args: any[]) => {
71
+ if (
72
+ typeof message === 'string' &&
73
+ message.includes('[Workflow') &&
74
+ message.includes('collection') &&
75
+ message.includes('not found')
76
+ ) {
77
+ return ctx.logger;
78
+ }
79
+ return originalWarn(message, ...args);
80
+ };
81
+ }
82
+ return next();
83
+ });
84
+
85
+ registerReviewQueue((this as any).app);
86
+ registerGitReviewAiTools((this as any).app);
87
+
88
+ (this as any).app.on('afterStart', async () => {
89
+ await ensureAutoReviewFlowSchema((this as any).app).catch(
90
+ (err) => (this as any).app.log?.error?.('plugin-git-manager: ensure schema error', err),
91
+ );
92
+ // Sweep any review left in `running` state from a previous process.
93
+ recoverStuckReviews((this as any).app).catch(
94
+ (err) => (this as any).app.log?.error?.('plugin-git-manager: recoverStuckReviews error', err),
95
+ );
96
+ startPoller((this as any).app);
97
+ });
98
+ (this as any).app.on('beforeStop', () => {
99
+ unregisterReviewQueue((this as any).app);
100
+ stopPoller();
101
+ });
102
+ (this as any).app.on('beforeDestroy', () => {
103
+ unregisterReviewQueue((this as any).app);
104
+ stopPoller();
105
+ });
106
+
107
+ // Read-only operations available to all plugin users
108
+ (this as any).app.acl.registerSnippet({
109
+ name: `pm.${(this as any).name}.read`,
110
+ actions: [
111
+ 'gitRepositories:list',
112
+ 'gitRepositories:get',
113
+ 'gitReviewFlows:list',
114
+ 'gitReviewFlows:get',
115
+ 'gitCodeReviews:list',
116
+ 'gitCodeReviews:get',
117
+ 'gitManager:status',
118
+ 'gitManager:log',
119
+ 'gitManager:branches',
120
+ 'gitManager:diff',
121
+ 'gitManager:fileTree',
122
+ 'gitManager:fileContent',
123
+ 'gitManager:commitDetail',
124
+ 'gitManager:mergeRequests',
125
+ 'gitManager:mergeRequestDetail',
126
+ 'gitManager:mergeRequestNotes',
127
+ 'gitManager:pollerStatus',
128
+ ],
129
+ });
130
+
131
+ // Write operations require separate permission
132
+ (this as any).app.acl.registerSnippet({
133
+ name: `pm.${(this as any).name}.write`,
134
+ actions: [
135
+ 'gitRepositories:create',
136
+ 'gitRepositories:update',
137
+ 'gitRepositories:destroy',
138
+ 'gitReviewFlows:create',
139
+ 'gitReviewFlows:update',
140
+ 'gitReviewFlows:destroy',
141
+ 'gitCodeReviews:create',
142
+ 'gitCodeReviews:update',
143
+ 'gitCodeReviews:destroy',
144
+ 'gitManager:clone',
145
+ 'gitManager:pull',
146
+ 'gitManager:push',
147
+ 'gitManager:fetch',
148
+ 'gitManager:checkout',
149
+ 'gitManager:triggerReview',
150
+ 'gitManager:reviewApprovePost',
151
+ 'gitManager:reviewReject',
152
+ 'gitManager:pollNow',
153
+ ],
154
+ });
155
+
156
+ // Repositories config — CRUD on gitRepositories only (no operations)
157
+ (this as any).app.acl.registerSnippet({
158
+ name: `pm.${(this as any).name}.repositories`,
159
+ actions: ['gitRepositories:*'],
160
+ });
161
+
162
+ // Full management — all git manager actions
163
+ (this as any).app.acl.registerSnippet({
164
+ name: `pm.${(this as any).name}.manage`,
165
+ actions: [
166
+ `pm.${(this as any).name}.repositories`,
167
+ `pm.${(this as any).name}.read`,
168
+ `pm.${(this as any).name}.write`,
169
+ ],
170
+ });
171
+
172
+ // Prevent overwriting PAT with obfuscated value on updates
173
+ (this as any).app.resourceManager.use(async (ctx, next) => {
174
+ if (ctx.action?.resourceName === 'gitRepositories' && ['create', 'update'].includes(ctx.action?.actionName)) {
175
+ if (ctx.action.params?.values?.pat === '••••••••') {
176
+ delete ctx.action.params.values.pat;
177
+ }
178
+ if (ctx.request.body?.pat === '••••••••') {
179
+ delete ctx.request.body.pat;
180
+ }
181
+ }
182
+ return next();
183
+ });
184
+
185
+ // Strip PAT from API responses — scoped to gitRepositories only
186
+ (this as any).app.resourceManager.use(async (ctx, next) => {
187
+ if (ctx.action?.resourceName !== 'gitRepositories') {
188
+ return next();
189
+ }
190
+ await next();
191
+ if (ctx.body) {
192
+ const items = Array.isArray(ctx.body)
193
+ ? ctx.body
194
+ : ctx.body?.data
195
+ ? Array.isArray(ctx.body.data)
196
+ ? ctx.body.data
197
+ : [ctx.body.data]
198
+ : [ctx.body];
199
+ items.forEach((item) => {
200
+ if (item && typeof item === 'object') {
201
+ if (item.pat) item.pat = '••••••••';
202
+ if (item.dataValues?.pat) item.dataValues.pat = '••••••••';
203
+ }
204
+ });
205
+ }
206
+ });
207
+ }
208
+
209
+ async install() {
210
+ await (this as any).app.db.getCollection('gitRepositories')?.sync();
211
+ }
212
+
213
+ async beforeDisable() {
214
+ unregisterReviewQueue((this as any).app);
215
+ stopPoller();
216
+ }
217
+
218
+ async beforeUnload() {
219
+ unregisterReviewQueue((this as any).app);
220
+ stopPoller();
221
+ }
222
+ }
223
+
224
+ export async function ensureAutoReviewFlowSchema(app: any) {
225
+ const sequelize = app.db?.sequelize;
226
+ const queryInterface = sequelize?.getQueryInterface?.();
227
+ if (!queryInterface) return;
228
+
229
+ const tablePrefix = app.db.options?.tablePrefix || '';
230
+
231
+ const repoTable = `${tablePrefix}gitRepositories`;
232
+ const repoInfo = await queryInterface.describeTable(repoTable).catch(() => null);
233
+ if (repoInfo && !repoInfo.autoReviewFlowId) {
234
+ await queryInterface.addColumn(repoTable, 'autoReviewFlowId', {
235
+ type: DataTypes.INTEGER,
236
+ allowNull: true,
237
+ });
238
+ }
239
+
240
+ const reviewTable = `${tablePrefix}gitCodeReviews`;
241
+ const reviewInfo = await queryInterface.describeTable(reviewTable).catch(() => null);
242
+ if (reviewInfo && !reviewInfo.folderPath) {
243
+ await queryInterface.addColumn(reviewTable, 'folderPath', {
244
+ type: DataTypes.STRING,
245
+ allowNull: true,
246
+ });
247
+ }
248
+ }
249
+
250
+ export default PluginGitManagerServer;