plugin-git-manager 1.2.14 → 1.2.16
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/dist/client/156.c27c2392746e7866.js +10 -0
- package/dist/client/434.1d19d4d6acc114f1.js +10 -0
- package/dist/client/900.ee2fa037aff389e2.js +10 -0
- package/dist/client/index.js +1 -1
- package/dist/client-v2/356.11ed4f8d56a6c08a.js +10 -0
- package/dist/client-v2/434.f8836ea116648de1.js +10 -0
- package/dist/client-v2/900.bef1c41d267e50c5.js +10 -0
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +8 -7
- package/dist/locale/en-US.json +73 -2
- package/dist/locale/vi-VN.json +72 -1
- package/dist/locale/zh-CN.json +49 -0
- package/dist/server/actions/git-actions.js +31 -15
- package/dist/server/actions/gitlab-api.js +46 -20
- package/dist/server/actions/review.js +13 -10
- package/dist/server/actions/subtree.js +430 -0
- package/dist/server/collections/gitAccounts.js +86 -0
- package/dist/server/collections/gitRepositories.js +11 -10
- package/dist/server/collections/gitSubtreeConfigs.js +67 -0
- package/dist/server/collections/gitSubtreeRuns.js +60 -0
- package/dist/server/migrations/20260712000000-add-git-accounts.js +186 -0
- package/dist/server/plugin.js +76 -12
- package/dist/server/poller.js +12 -24
- package/dist/server/utils/get-repo-account.js +49 -0
- package/package.json +1 -1
- package/src/client/components/CommitHistory.tsx +21 -5
- package/src/client/components/GitAccounts.tsx +179 -0
- package/src/client/components/GitManagerSettings.tsx +10 -6
- package/src/client/components/GitOperations.tsx +27 -18
- package/src/client/components/LLMModelSelect.tsx +2 -2
- package/src/client/components/MarkdownView.tsx +1 -4
- package/src/client/components/MergeRequests.tsx +238 -129
- package/src/client/components/PollingStatus.tsx +10 -5
- package/src/client/components/RepositoryConfig.tsx +32 -14
- package/src/client/components/RepositoryPermissions.tsx +1 -8
- package/src/client/index.tsx +11 -0
- package/src/client-v2/components/SubtreeSplits.tsx +503 -0
- package/src/client-v2/locale.ts +8 -0
- package/src/client-v2/plugin.tsx +12 -5
- package/src/locale/en-US.json +73 -2
- package/src/locale/vi-VN.json +72 -1
- package/src/locale/zh-CN.json +49 -0
- package/src/server/__tests__/smoke.test.ts +1 -1
- package/src/server/__tests__/subtree.test.ts +73 -0
- package/src/server/actions/git-actions.ts +40 -19
- package/src/server/actions/gitlab-api.ts +59 -28
- package/src/server/actions/review.ts +13 -10
- package/src/server/actions/role-permissions.ts +2 -1
- package/src/server/actions/subtree.ts +460 -0
- package/src/server/collections/gitAccounts.ts +56 -0
- package/src/server/collections/gitRepositories.ts +11 -10
- package/src/server/collections/gitSubtreeConfigs.ts +37 -0
- package/src/server/collections/gitSubtreeRuns.ts +30 -0
- package/src/server/migrations/20260712000000-add-git-accounts.ts +198 -0
- package/src/server/plugin.ts +79 -5
- package/src/server/poller.ts +31 -32
- package/src/server/utils/get-repo-account.ts +23 -0
- package/dist/client/434.f82454801cf84906.js +0 -10
- package/dist/client/900.ec1fa7f16eb8cce6.js +0 -10
- package/dist/client-v2/434.c2f944f911a56da0.js +0 -10
- package/dist/client-v2/900.7a5e80bf21feda66.js +0 -10
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
import { Context } from '@nocobase/actions';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import * as os from 'os';
|
|
4
|
+
import * as path from 'path';
|
|
5
|
+
import type { SimpleGit } from 'simple-git';
|
|
6
|
+
import { createGit, validateBranch, validateLocalPath, withAuth } from './git-actions';
|
|
7
|
+
import { getRepoAccount } from '../utils/get-repo-account';
|
|
8
|
+
import { redactError, redactPat } from '../utils/redact';
|
|
9
|
+
|
|
10
|
+
export type SubtreePolicy = 'fastForward' | 'replace' | 'merge';
|
|
11
|
+
export type SubtreeRelationship = 'target-missing' | 'already-up-to-date' | 'fast-forward' | 'diverged';
|
|
12
|
+
export const SUBTREE_EXECUTION_MODE = 'app' as const;
|
|
13
|
+
|
|
14
|
+
interface RecordLike {
|
|
15
|
+
get(attribute: string): unknown;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface SubtreeConfig {
|
|
19
|
+
id: number | string;
|
|
20
|
+
repositoryId: number | string;
|
|
21
|
+
sourceBranch: string;
|
|
22
|
+
sourcePrefix: string;
|
|
23
|
+
targetBranch: string;
|
|
24
|
+
remoteName: string;
|
|
25
|
+
enabled: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface PreviewResult {
|
|
29
|
+
sourceSha: string;
|
|
30
|
+
splitSha: string;
|
|
31
|
+
targetExists: boolean;
|
|
32
|
+
targetSha: string | null;
|
|
33
|
+
relationship: SubtreeRelationship;
|
|
34
|
+
recommendedPolicy: SubtreePolicy;
|
|
35
|
+
availablePolicies: SubtreePolicy[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface RunParams {
|
|
39
|
+
configId: number | string;
|
|
40
|
+
policy: SubtreePolicy;
|
|
41
|
+
push: boolean;
|
|
42
|
+
expectedTargetSha?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function getActionParams(ctx: Context): Record<string, unknown> {
|
|
46
|
+
const requestBody = (ctx as Context & { request?: { body?: Record<string, unknown> } }).request?.body || {};
|
|
47
|
+
return { ...ctx.action.params, ...ctx.action.params?.values, ...requestBody };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function stringValue(value: unknown, fallback = ''): string {
|
|
51
|
+
return typeof value === 'string' ? value.trim() : fallback;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function booleanValue(value: unknown, fallback: boolean): boolean {
|
|
55
|
+
return typeof value === 'boolean' ? value : fallback;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function recordToConfig(record: RecordLike): SubtreeConfig {
|
|
59
|
+
return {
|
|
60
|
+
id: record.get('id') as number | string,
|
|
61
|
+
repositoryId: record.get('repositoryId') as number | string,
|
|
62
|
+
sourceBranch: stringValue(record.get('sourceBranch')),
|
|
63
|
+
sourcePrefix: stringValue(record.get('sourcePrefix')),
|
|
64
|
+
targetBranch: stringValue(record.get('targetBranch')),
|
|
65
|
+
remoteName: stringValue(record.get('remoteName'), 'origin') || 'origin',
|
|
66
|
+
enabled: record.get('enabled') !== false,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function validateSubtreePrefix(prefix: string): string {
|
|
71
|
+
const normalized = prefix.replace(/\\/g, '/').replace(/^\.\//, '').replace(/\/$/, '');
|
|
72
|
+
if (!normalized || path.posix.isAbsolute(normalized) || /^[a-zA-Z]:\//.test(normalized)) {
|
|
73
|
+
throw new Error('Source folder must be a non-empty relative path');
|
|
74
|
+
}
|
|
75
|
+
if (normalized.split('/').some((segment) => !segment || segment === '.' || segment === '..')) {
|
|
76
|
+
throw new Error('Source folder contains an invalid path segment');
|
|
77
|
+
}
|
|
78
|
+
return normalized;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function validateRemoteName(remoteName: string): string {
|
|
82
|
+
if (!/^(?!-)[a-zA-Z0-9._-]+$/.test(remoteName)) {
|
|
83
|
+
throw new Error('Invalid Git remote name');
|
|
84
|
+
}
|
|
85
|
+
return remoteName;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function validateSubtreePolicy(policy: unknown): SubtreePolicy {
|
|
89
|
+
if (policy === 'fastForward' || policy === 'replace' || policy === 'merge') return policy;
|
|
90
|
+
throw new Error('Invalid subtree update policy');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function validateSubtreeConfigInput(values: Record<string, unknown>): void {
|
|
94
|
+
const sourceBranch = stringValue(values.sourceBranch);
|
|
95
|
+
const targetBranch = stringValue(values.targetBranch);
|
|
96
|
+
if (sourceBranch) validateBranch(sourceBranch);
|
|
97
|
+
if (targetBranch) validateBranch(targetBranch);
|
|
98
|
+
if (sourceBranch && targetBranch && sourceBranch === targetBranch) {
|
|
99
|
+
throw new Error('Source branch and target branch must be different');
|
|
100
|
+
}
|
|
101
|
+
if (values.sourcePrefix !== undefined) validateSubtreePrefix(stringValue(values.sourcePrefix));
|
|
102
|
+
if (values.remoteName !== undefined) validateRemoteName(stringValue(values.remoteName));
|
|
103
|
+
if (values.defaultPolicy !== undefined) validateSubtreePolicy(values.defaultPolicy);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function validateRunAgainstPreview(
|
|
107
|
+
policy: SubtreePolicy,
|
|
108
|
+
preview: Pick<PreviewResult, 'relationship' | 'targetSha'>,
|
|
109
|
+
expectedTargetSha?: string,
|
|
110
|
+
): void {
|
|
111
|
+
if (policy === 'fastForward' && preview.relationship === 'diverged') {
|
|
112
|
+
throw new Error('Target branch cannot be fast-forwarded to the new subtree commit');
|
|
113
|
+
}
|
|
114
|
+
if (policy === 'replace' && preview.targetSha) {
|
|
115
|
+
if (!expectedTargetSha) throw new Error('Replace policy requires the preview target SHA');
|
|
116
|
+
if (expectedTargetSha !== preview.targetSha) {
|
|
117
|
+
throw new Error('Target branch changed after preview; run preview again');
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export async function recoverStuckSubtreeRuns(app: {
|
|
123
|
+
db: { getRepository: (name: string) => { update: (options: unknown) => Promise<unknown> } };
|
|
124
|
+
}): Promise<void> {
|
|
125
|
+
const finishedAt = new Date();
|
|
126
|
+
const staleBefore = new Date(finishedAt.getTime() - 60 * 60 * 1000);
|
|
127
|
+
await app.db.getRepository('gitSubtreeRuns').update({
|
|
128
|
+
filter: { status: 'running', startedAt: { $lt: staleBefore } },
|
|
129
|
+
values: { status: 'failed', finishedAt, error: 'Subtree run was interrupted by a server restart' },
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async function resolveOptionalRef(git: SimpleGit, ref: string): Promise<string | null> {
|
|
134
|
+
try {
|
|
135
|
+
return (await git.revparse(['--verify', ref])).trim();
|
|
136
|
+
} catch {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function isAncestor(git: SimpleGit, ancestor: string, descendant: string): Promise<boolean> {
|
|
142
|
+
try {
|
|
143
|
+
await git.raw(['merge-base', '--is-ancestor', ancestor, descendant]);
|
|
144
|
+
return true;
|
|
145
|
+
} catch {
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async function assertCleanWorkingTree(git: SimpleGit): Promise<void> {
|
|
151
|
+
const status = await git.status();
|
|
152
|
+
if (!status.isClean()) throw new Error('Repository working tree must be clean before running a subtree split');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async function assertGitBranchFormat(git: SimpleGit, branch: string): Promise<void> {
|
|
156
|
+
try {
|
|
157
|
+
await git.raw(['check-ref-format', '--branch', branch]);
|
|
158
|
+
} catch {
|
|
159
|
+
throw new Error(`Invalid branch name: ${branch}`);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async function createPreview(git: SimpleGit, config: SubtreeConfig): Promise<PreviewResult> {
|
|
164
|
+
validateBranch(config.sourceBranch);
|
|
165
|
+
validateBranch(config.targetBranch);
|
|
166
|
+
if (config.sourceBranch === config.targetBranch) {
|
|
167
|
+
throw new Error('Source branch and target branch must be different');
|
|
168
|
+
}
|
|
169
|
+
const prefix = validateSubtreePrefix(config.sourcePrefix);
|
|
170
|
+
const remoteName = validateRemoteName(config.remoteName);
|
|
171
|
+
const sourceRef = `refs/remotes/${remoteName}/${config.sourceBranch}`;
|
|
172
|
+
const targetRef = `refs/remotes/${remoteName}/${config.targetBranch}`;
|
|
173
|
+
|
|
174
|
+
await assertCleanWorkingTree(git);
|
|
175
|
+
await assertGitBranchFormat(git, config.sourceBranch);
|
|
176
|
+
await assertGitBranchFormat(git, config.targetBranch);
|
|
177
|
+
await git.fetch(remoteName, ['--prune']);
|
|
178
|
+
const sourceSha = await resolveOptionalRef(git, sourceRef);
|
|
179
|
+
if (!sourceSha) throw new Error('Source branch was not found on the configured remote');
|
|
180
|
+
|
|
181
|
+
let objectType = '';
|
|
182
|
+
try {
|
|
183
|
+
objectType = (await git.raw(['cat-file', '-t', `${sourceSha}:${prefix}`])).trim();
|
|
184
|
+
} catch {
|
|
185
|
+
throw new Error('Source folder was not found in the selected source branch');
|
|
186
|
+
}
|
|
187
|
+
if (objectType !== 'tree') throw new Error('Selected source path is not a folder');
|
|
188
|
+
|
|
189
|
+
const splitSha = (await git.raw(['subtree', 'split', '--prefix', prefix, sourceSha])).trim();
|
|
190
|
+
if (!/^[a-f0-9]{40,64}$/i.test(splitSha)) throw new Error('Git subtree split did not return a valid commit');
|
|
191
|
+
|
|
192
|
+
const targetSha = await resolveOptionalRef(git, targetRef);
|
|
193
|
+
let relationship: SubtreeRelationship = 'target-missing';
|
|
194
|
+
if (targetSha === splitSha) relationship = 'already-up-to-date';
|
|
195
|
+
else if (targetSha) relationship = (await isAncestor(git, targetSha, splitSha)) ? 'fast-forward' : 'diverged';
|
|
196
|
+
|
|
197
|
+
return {
|
|
198
|
+
sourceSha,
|
|
199
|
+
splitSha,
|
|
200
|
+
targetExists: !!targetSha,
|
|
201
|
+
targetSha,
|
|
202
|
+
relationship,
|
|
203
|
+
recommendedPolicy: relationship === 'diverged' ? 'merge' : 'fastForward',
|
|
204
|
+
availablePolicies: ['fastForward', 'replace', 'merge'],
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async function updateTargetRef(git: SimpleGit, config: SubtreeConfig, sha: string): Promise<void> {
|
|
209
|
+
await git.raw(['branch', '-f', config.targetBranch, sha]);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async function pushTarget(
|
|
213
|
+
git: SimpleGit,
|
|
214
|
+
config: SubtreeConfig,
|
|
215
|
+
sha: string,
|
|
216
|
+
forceWithLeaseSha?: string,
|
|
217
|
+
): Promise<void> {
|
|
218
|
+
const args = ['push'];
|
|
219
|
+
if (forceWithLeaseSha) {
|
|
220
|
+
args.push(`--force-with-lease=refs/heads/${config.targetBranch}:${forceWithLeaseSha}`);
|
|
221
|
+
}
|
|
222
|
+
args.push(config.remoteName, `${sha}:refs/heads/${config.targetBranch}`);
|
|
223
|
+
await git.raw(args);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async function mergeTarget(git: SimpleGit, config: SubtreeConfig, preview: PreviewResult): Promise<string> {
|
|
227
|
+
if (!preview.targetSha) return preview.splitSha;
|
|
228
|
+
|
|
229
|
+
const temporaryRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'nocobase-git-subtree-'));
|
|
230
|
+
const worktreePath = path.join(temporaryRoot, 'worktree');
|
|
231
|
+
try {
|
|
232
|
+
await git.raw(['worktree', 'add', '--detach', worktreePath, preview.targetSha]);
|
|
233
|
+
const worktreeGit = createGit(worktreePath);
|
|
234
|
+
try {
|
|
235
|
+
await worktreeGit.raw([
|
|
236
|
+
'-c',
|
|
237
|
+
'user.name=NocoBase Git Manager',
|
|
238
|
+
'-c',
|
|
239
|
+
'user.email=git-manager@nocobase.local',
|
|
240
|
+
'merge',
|
|
241
|
+
preview.splitSha,
|
|
242
|
+
'--allow-unrelated-histories',
|
|
243
|
+
'--no-edit',
|
|
244
|
+
]);
|
|
245
|
+
} catch (error) {
|
|
246
|
+
await worktreeGit.raw(['merge', '--abort']).catch(() => undefined);
|
|
247
|
+
throw new Error(`Subtree merge has conflicts: ${redactPat(redactError(error).message)}`);
|
|
248
|
+
}
|
|
249
|
+
return (await worktreeGit.revparse(['HEAD'])).trim();
|
|
250
|
+
} finally {
|
|
251
|
+
await git.raw(['worktree', 'remove', '--force', worktreePath]).catch(() => undefined);
|
|
252
|
+
fs.rmSync(temporaryRoot, { recursive: true, force: true });
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
async function loadConfigAndRepository(ctx: Context, configId: number | string) {
|
|
257
|
+
const configRecord = await ctx.db.getRepository('gitSubtreeConfigs').findOne({ filterByTk: configId });
|
|
258
|
+
if (!configRecord) ctx.throw(404, 'Subtree configuration not found');
|
|
259
|
+
const config = recordToConfig(configRecord as RecordLike);
|
|
260
|
+
if (!config.enabled) ctx.throw(400, 'Subtree configuration is disabled');
|
|
261
|
+
|
|
262
|
+
const repo = await ctx.db.getRepository('gitRepositories').findOne({ filterByTk: config.repositoryId });
|
|
263
|
+
if (!repo) ctx.throw(404, 'Repository not found');
|
|
264
|
+
const account = await getRepoAccount(ctx.db, repo);
|
|
265
|
+
if (!account) ctx.throw(400, 'Repository has no Git account configured');
|
|
266
|
+
|
|
267
|
+
return { configRecord, config, repo, account };
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export async function subtreeOptions(ctx: Context, next: () => Promise<void>) {
|
|
271
|
+
const params = getActionParams(ctx);
|
|
272
|
+
const repositoryId = params.repositoryId as number | string;
|
|
273
|
+
if (!repositoryId) ctx.throw(400, 'repositoryId is required');
|
|
274
|
+
const remoteName = validateRemoteName(stringValue(params.remoteName, 'origin') || 'origin');
|
|
275
|
+
const sourceBranch = stringValue(params.sourceBranch);
|
|
276
|
+
if (sourceBranch) validateBranch(sourceBranch);
|
|
277
|
+
|
|
278
|
+
const repo = await ctx.db.getRepository('gitRepositories').findOne({ filterByTk: repositoryId });
|
|
279
|
+
if (!repo) ctx.throw(404, 'Repository not found');
|
|
280
|
+
const account = await getRepoAccount(ctx.db, repo);
|
|
281
|
+
if (!account) ctx.throw(400, 'Repository has no Git account configured');
|
|
282
|
+
const localPath = validateLocalPath(stringValue(repo.get('localPath')));
|
|
283
|
+
const repoUrl = stringValue(repo.get('repoUrl'));
|
|
284
|
+
const git = createGit(localPath);
|
|
285
|
+
|
|
286
|
+
try {
|
|
287
|
+
const data = await withAuth(
|
|
288
|
+
git,
|
|
289
|
+
localPath,
|
|
290
|
+
repoUrl,
|
|
291
|
+
account.pat,
|
|
292
|
+
async () => {
|
|
293
|
+
if (sourceBranch) await assertGitBranchFormat(git, sourceBranch);
|
|
294
|
+
await git.fetch(remoteName, ['--prune']);
|
|
295
|
+
const branchOutput = await git.raw([
|
|
296
|
+
'for-each-ref',
|
|
297
|
+
'--format=%(refname:strip=3)',
|
|
298
|
+
`refs/remotes/${remoteName}`,
|
|
299
|
+
]);
|
|
300
|
+
const branches = branchOutput
|
|
301
|
+
.split('\n')
|
|
302
|
+
.map((branch) => branch.trim())
|
|
303
|
+
.filter((branch) => branch && branch !== 'HEAD');
|
|
304
|
+
let folders: string[] = [];
|
|
305
|
+
if (sourceBranch) {
|
|
306
|
+
const sourceRef = `refs/remotes/${remoteName}/${sourceBranch}`;
|
|
307
|
+
if (!(await resolveOptionalRef(git, sourceRef))) {
|
|
308
|
+
throw new Error('Source branch was not found on the configured remote');
|
|
309
|
+
}
|
|
310
|
+
const folderOutput = await git.raw(['ls-tree', '-d', '-r', '--name-only', sourceRef]);
|
|
311
|
+
folders = folderOutput
|
|
312
|
+
.split('\n')
|
|
313
|
+
.map((folder) => folder.trim())
|
|
314
|
+
.filter(Boolean);
|
|
315
|
+
}
|
|
316
|
+
return { branches: Array.from(new Set(branches)).sort(), folders };
|
|
317
|
+
},
|
|
318
|
+
account.username,
|
|
319
|
+
remoteName,
|
|
320
|
+
);
|
|
321
|
+
ctx.body = { success: true, data };
|
|
322
|
+
} catch (error) {
|
|
323
|
+
const safeError = redactError(error);
|
|
324
|
+
ctx.throw(400, ctx.t(safeError.message, { ns: 'plugin-git-manager' }));
|
|
325
|
+
}
|
|
326
|
+
await next();
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export async function subtreePreview(ctx: Context, next: () => Promise<void>) {
|
|
330
|
+
const params = getActionParams(ctx);
|
|
331
|
+
const configId = params.configId as number | string;
|
|
332
|
+
if (!configId) ctx.throw(400, 'configId is required');
|
|
333
|
+
const { config, repo, account } = await loadConfigAndRepository(ctx, configId);
|
|
334
|
+
const localPath = validateLocalPath(stringValue(repo.get('localPath')));
|
|
335
|
+
const repoUrl = stringValue(repo.get('repoUrl'));
|
|
336
|
+
const git = createGit(localPath);
|
|
337
|
+
|
|
338
|
+
try {
|
|
339
|
+
const preview = await withAuth(
|
|
340
|
+
git,
|
|
341
|
+
localPath,
|
|
342
|
+
repoUrl,
|
|
343
|
+
account.pat,
|
|
344
|
+
() => createPreview(git, config),
|
|
345
|
+
account.username,
|
|
346
|
+
config.remoteName,
|
|
347
|
+
);
|
|
348
|
+
ctx.body = { success: true, data: preview };
|
|
349
|
+
} catch (error) {
|
|
350
|
+
const safeError = redactError(error);
|
|
351
|
+
ctx.throw(400, ctx.t(safeError.message, { ns: 'plugin-git-manager' }));
|
|
352
|
+
}
|
|
353
|
+
await next();
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Executes the complete subtree workflow inside the interactive app request.
|
|
358
|
+
* This action intentionally does not enqueue work in the automatic-review queue.
|
|
359
|
+
*/
|
|
360
|
+
export async function subtreeRunOnAppProcess(ctx: Context, next: () => Promise<void>) {
|
|
361
|
+
const params = getActionParams(ctx);
|
|
362
|
+
const runParams: RunParams = {
|
|
363
|
+
configId: params.configId as number | string,
|
|
364
|
+
policy: validateSubtreePolicy(params.policy),
|
|
365
|
+
push: booleanValue(params.push, true),
|
|
366
|
+
expectedTargetSha: stringValue(params.expectedTargetSha) || undefined,
|
|
367
|
+
};
|
|
368
|
+
if (!runParams.configId) ctx.throw(400, 'configId is required');
|
|
369
|
+
if (runParams.policy === 'replace' && ctx.action.actionName !== 'subtreeReplace') {
|
|
370
|
+
ctx.throw(403, 'Replace policy requires the destructive subtree permission');
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const { config, repo, account } = await loadConfigAndRepository(ctx, runParams.configId);
|
|
374
|
+
const localPath = validateLocalPath(stringValue(repo.get('localPath')));
|
|
375
|
+
const repoUrl = stringValue(repo.get('repoUrl'));
|
|
376
|
+
const git = createGit(localPath);
|
|
377
|
+
const runsRepository = ctx.db.getRepository('gitSubtreeRuns');
|
|
378
|
+
const startedAt = new Date();
|
|
379
|
+
const run = await runsRepository.create({
|
|
380
|
+
values: {
|
|
381
|
+
configId: config.id,
|
|
382
|
+
policy: runParams.policy,
|
|
383
|
+
executionMode: SUBTREE_EXECUTION_MODE,
|
|
384
|
+
status: 'running',
|
|
385
|
+
startedAt,
|
|
386
|
+
triggeredById: (ctx as Context & { state?: { currentUser?: { id?: number | string } } }).state?.currentUser?.id,
|
|
387
|
+
},
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
try {
|
|
391
|
+
const result = await withAuth(
|
|
392
|
+
git,
|
|
393
|
+
localPath,
|
|
394
|
+
repoUrl,
|
|
395
|
+
account.pat,
|
|
396
|
+
async () => {
|
|
397
|
+
const preview = await createPreview(git, config);
|
|
398
|
+
let targetAfterSha = preview.splitSha;
|
|
399
|
+
|
|
400
|
+
validateRunAgainstPreview(runParams.policy, preview, runParams.expectedTargetSha);
|
|
401
|
+
if (runParams.policy === 'merge') targetAfterSha = await mergeTarget(git, config, preview);
|
|
402
|
+
|
|
403
|
+
if (runParams.push) {
|
|
404
|
+
await pushTarget(
|
|
405
|
+
git,
|
|
406
|
+
config,
|
|
407
|
+
targetAfterSha,
|
|
408
|
+
runParams.policy === 'replace' && preview.targetSha ? preview.targetSha : undefined,
|
|
409
|
+
);
|
|
410
|
+
} else {
|
|
411
|
+
await updateTargetRef(git, config, targetAfterSha);
|
|
412
|
+
}
|
|
413
|
+
return { preview, targetAfterSha };
|
|
414
|
+
},
|
|
415
|
+
account.username,
|
|
416
|
+
config.remoteName,
|
|
417
|
+
);
|
|
418
|
+
|
|
419
|
+
const finishedAt = new Date();
|
|
420
|
+
await runsRepository.update({
|
|
421
|
+
filterByTk: run.get('id'),
|
|
422
|
+
values: {
|
|
423
|
+
sourceSha: result.preview.sourceSha,
|
|
424
|
+
splitSha: result.preview.splitSha,
|
|
425
|
+
targetBeforeSha: result.preview.targetSha,
|
|
426
|
+
targetAfterSha: result.targetAfterSha,
|
|
427
|
+
status: 'success',
|
|
428
|
+
finishedAt,
|
|
429
|
+
output: `Updated ${config.targetBranch} using ${runParams.policy}`,
|
|
430
|
+
},
|
|
431
|
+
});
|
|
432
|
+
await ctx.db.getRepository('gitSubtreeConfigs').update({
|
|
433
|
+
filterByTk: config.id,
|
|
434
|
+
values: {
|
|
435
|
+
lastRunAt: finishedAt,
|
|
436
|
+
lastSplitSha: result.preview.splitSha,
|
|
437
|
+
lastStatus: 'success',
|
|
438
|
+
lastError: null,
|
|
439
|
+
},
|
|
440
|
+
});
|
|
441
|
+
ctx.body = {
|
|
442
|
+
success: true,
|
|
443
|
+
data: { runId: run.get('id'), executionMode: SUBTREE_EXECUTION_MODE, ...result },
|
|
444
|
+
};
|
|
445
|
+
} catch (error) {
|
|
446
|
+
const safeError = redactError(error);
|
|
447
|
+
const status = /conflict/i.test(safeError.message) ? 'conflict' : 'failed';
|
|
448
|
+
const finishedAt = new Date();
|
|
449
|
+
await runsRepository.update({
|
|
450
|
+
filterByTk: run.get('id'),
|
|
451
|
+
values: { status, finishedAt, error: safeError.message },
|
|
452
|
+
});
|
|
453
|
+
await ctx.db.getRepository('gitSubtreeConfigs').update({
|
|
454
|
+
filterByTk: config.id,
|
|
455
|
+
values: { lastRunAt: finishedAt, lastStatus: status, lastError: safeError.message },
|
|
456
|
+
});
|
|
457
|
+
ctx.throw(status === 'conflict' ? 409 : 400, ctx.t(safeError.message, { ns: 'plugin-git-manager' }));
|
|
458
|
+
}
|
|
459
|
+
await next();
|
|
460
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { defineCollection } from '@nocobase/database';
|
|
2
|
+
|
|
3
|
+
export default defineCollection({
|
|
4
|
+
name: 'gitAccounts',
|
|
5
|
+
title: 'Git Accounts',
|
|
6
|
+
autoGenId: true,
|
|
7
|
+
createdAt: true,
|
|
8
|
+
updatedAt: true,
|
|
9
|
+
fields: [
|
|
10
|
+
{
|
|
11
|
+
type: 'string',
|
|
12
|
+
name: 'name',
|
|
13
|
+
interface: 'input',
|
|
14
|
+
uiSchema: { title: 'Account Name', type: 'string', 'x-component': 'Input', required: true },
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
type: 'string',
|
|
18
|
+
name: 'provider',
|
|
19
|
+
defaultValue: 'gitlab',
|
|
20
|
+
interface: 'select',
|
|
21
|
+
uiSchema: {
|
|
22
|
+
title: 'Provider',
|
|
23
|
+
type: 'string',
|
|
24
|
+
'x-component': 'Select',
|
|
25
|
+
enum: [
|
|
26
|
+
{ value: 'gitlab', label: 'GitLab' },
|
|
27
|
+
{ value: 'github', label: 'GitHub' },
|
|
28
|
+
],
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
type: 'string',
|
|
33
|
+
name: 'baseUrl',
|
|
34
|
+
interface: 'input',
|
|
35
|
+
uiSchema: { title: 'Base URL', type: 'string', 'x-component': 'Input' },
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
type: 'string',
|
|
39
|
+
name: 'username',
|
|
40
|
+
interface: 'input',
|
|
41
|
+
uiSchema: { title: 'Username', type: 'string', 'x-component': 'Input', required: true },
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
type: 'string',
|
|
45
|
+
name: 'pat',
|
|
46
|
+
interface: 'password',
|
|
47
|
+
uiSchema: { title: 'Personal Access Token', type: 'string', 'x-component': 'Password', required: true },
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
type: 'hasMany',
|
|
51
|
+
name: 'repositories',
|
|
52
|
+
target: 'gitRepositories',
|
|
53
|
+
foreignKey: 'gitAccountId',
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
});
|
|
@@ -20,10 +20,17 @@ export default defineCollection({
|
|
|
20
20
|
uiSchema: { title: 'Repository URL', type: 'string', 'x-component': 'Input' },
|
|
21
21
|
},
|
|
22
22
|
{
|
|
23
|
-
type: '
|
|
24
|
-
name: '
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
type: 'belongsTo',
|
|
24
|
+
name: 'gitAccount',
|
|
25
|
+
target: 'gitAccounts',
|
|
26
|
+
foreignKey: 'gitAccountId',
|
|
27
|
+
onDelete: 'RESTRICT',
|
|
28
|
+
interface: 'm2o',
|
|
29
|
+
uiSchema: {
|
|
30
|
+
title: 'Git Account',
|
|
31
|
+
'x-component': 'AssociationField',
|
|
32
|
+
'x-component-props': { fieldNames: { label: 'name', value: 'id' } },
|
|
33
|
+
},
|
|
27
34
|
},
|
|
28
35
|
{
|
|
29
36
|
type: 'string',
|
|
@@ -31,12 +38,6 @@ export default defineCollection({
|
|
|
31
38
|
interface: 'input',
|
|
32
39
|
uiSchema: { title: 'Local Path', type: 'string', 'x-component': 'Input' },
|
|
33
40
|
},
|
|
34
|
-
{
|
|
35
|
-
type: 'string',
|
|
36
|
-
name: 'pat',
|
|
37
|
-
interface: 'password',
|
|
38
|
-
uiSchema: { title: 'Personal Access Token', type: 'string', 'x-component': 'Password' },
|
|
39
|
-
},
|
|
40
41
|
{
|
|
41
42
|
type: 'string',
|
|
42
43
|
name: 'defaultBranch',
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { defineCollection } from '@nocobase/database';
|
|
2
|
+
|
|
3
|
+
export default defineCollection({
|
|
4
|
+
name: 'gitSubtreeConfigs',
|
|
5
|
+
title: 'Git Subtree Configurations',
|
|
6
|
+
autoGenId: true,
|
|
7
|
+
createdAt: true,
|
|
8
|
+
updatedAt: true,
|
|
9
|
+
fields: [
|
|
10
|
+
{ type: 'string', name: 'name', allowNull: false },
|
|
11
|
+
{
|
|
12
|
+
type: 'belongsTo',
|
|
13
|
+
name: 'repository',
|
|
14
|
+
target: 'gitRepositories',
|
|
15
|
+
foreignKey: 'repositoryId',
|
|
16
|
+
onDelete: 'CASCADE',
|
|
17
|
+
},
|
|
18
|
+
{ type: 'string', name: 'sourceBranch', allowNull: false },
|
|
19
|
+
{ type: 'string', name: 'sourcePrefix', allowNull: false },
|
|
20
|
+
{ type: 'string', name: 'targetBranch', allowNull: false },
|
|
21
|
+
{ type: 'string', name: 'remoteName', allowNull: false, defaultValue: 'origin' },
|
|
22
|
+
{ type: 'string', name: 'defaultPolicy', allowNull: false, defaultValue: 'fastForward' },
|
|
23
|
+
{ type: 'boolean', name: 'pushAfterRun', allowNull: false, defaultValue: true },
|
|
24
|
+
{ type: 'boolean', name: 'enabled', allowNull: false, defaultValue: true },
|
|
25
|
+
{ type: 'date', name: 'lastRunAt' },
|
|
26
|
+
{ type: 'string', name: 'lastSplitSha' },
|
|
27
|
+
{ type: 'string', name: 'lastStatus' },
|
|
28
|
+
{ type: 'text', name: 'lastError' },
|
|
29
|
+
{
|
|
30
|
+
type: 'hasMany',
|
|
31
|
+
name: 'runs',
|
|
32
|
+
target: 'gitSubtreeRuns',
|
|
33
|
+
foreignKey: 'configId',
|
|
34
|
+
onDelete: 'CASCADE',
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { defineCollection } from '@nocobase/database';
|
|
2
|
+
|
|
3
|
+
export default defineCollection({
|
|
4
|
+
name: 'gitSubtreeRuns',
|
|
5
|
+
title: 'Git Subtree Runs',
|
|
6
|
+
autoGenId: true,
|
|
7
|
+
createdAt: true,
|
|
8
|
+
updatedAt: true,
|
|
9
|
+
fields: [
|
|
10
|
+
{
|
|
11
|
+
type: 'belongsTo',
|
|
12
|
+
name: 'config',
|
|
13
|
+
target: 'gitSubtreeConfigs',
|
|
14
|
+
foreignKey: 'configId',
|
|
15
|
+
onDelete: 'CASCADE',
|
|
16
|
+
},
|
|
17
|
+
{ type: 'string', name: 'policy', allowNull: false },
|
|
18
|
+
{ type: 'string', name: 'executionMode', allowNull: false, defaultValue: 'app' },
|
|
19
|
+
{ type: 'string', name: 'sourceSha' },
|
|
20
|
+
{ type: 'string', name: 'splitSha' },
|
|
21
|
+
{ type: 'string', name: 'targetBeforeSha' },
|
|
22
|
+
{ type: 'string', name: 'targetAfterSha' },
|
|
23
|
+
{ type: 'string', name: 'status', allowNull: false, defaultValue: 'running' },
|
|
24
|
+
{ type: 'date', name: 'startedAt', allowNull: false },
|
|
25
|
+
{ type: 'date', name: 'finishedAt' },
|
|
26
|
+
{ type: 'text', name: 'output' },
|
|
27
|
+
{ type: 'text', name: 'error' },
|
|
28
|
+
{ type: 'bigInt', name: 'triggeredById' },
|
|
29
|
+
],
|
|
30
|
+
});
|