@ipul-labs/forge 1.13.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/README.md +273 -0
- package/bin/README.md +210 -0
- package/bin/cli/analyze.js +516 -0
- package/bin/cli/apply.js +264 -0
- package/bin/cli/cifix.js +300 -0
- package/bin/cli/close.js +125 -0
- package/bin/cli/doctor.js +161 -0
- package/bin/cli/init.js +306 -0
- package/bin/cli/ink.js +57 -0
- package/bin/cli/issue.js +116 -0
- package/bin/cli/issues.js +155 -0
- package/bin/cli/merge.js +175 -0
- package/bin/cli/refine.js +253 -0
- package/bin/cli/repos.js +91 -0
- package/bin/cli/resolution.js +126 -0
- package/bin/cli/session.js +230 -0
- package/bin/cli/sessionStore.js +214 -0
- package/bin/cli/ship.js +762 -0
- package/bin/cli/utils.js +145 -0
- package/bin/cli/validate.js +280 -0
- package/bin/cli/watch.js +274 -0
- package/bin/forge-agent.js +174 -0
- package/dist/assets/index-CNTCMbW9.js +80 -0
- package/dist/assets/index-InpYS-zg.css +1 -0
- package/dist/favicon.svg +8 -0
- package/dist/index.html +19 -0
- package/dist-lib/favicon.svg +8 -0
- package/dist-lib/index.d.ts +1 -0
- package/dist-lib/style.css +1 -0
- package/dist-lib/ui.d.ts +2 -0
- package/dist-lib/ui.js +7390 -0
- package/dist-lib/views/Forge/ForgeDashboard.d.ts +5 -0
- package/dist-lib/views/Forge/activeJobs.d.ts +29 -0
- package/dist-lib/views/Forge/analysisCache.d.ts +64 -0
- package/dist-lib/views/Forge/api.d.ts +4 -0
- package/dist-lib/views/Forge/components/AnalysisProgress.d.ts +13 -0
- package/dist-lib/views/Forge/components/CITerminal.d.ts +44 -0
- package/dist-lib/views/Forge/components/DiffViewer.d.ts +37 -0
- package/dist-lib/views/Forge/components/ForgeConnect.d.ts +6 -0
- package/dist-lib/views/Forge/components/IssueList.d.ts +28 -0
- package/dist-lib/views/Forge/components/MRStatus.d.ts +9 -0
- package/dist-lib/views/Forge/components/NewTaskModal.d.ts +1 -0
- package/dist-lib/views/Forge/components/OpenMRList.d.ts +11 -0
- package/dist-lib/views/Forge/components/PipelineStatus.d.ts +12 -0
- package/dist-lib/views/Forge/components/PipelinesList.d.ts +9 -0
- package/dist-lib/views/Forge/components/RolloutStatus.d.ts +5 -0
- package/dist-lib/views/Forge/components/UsageWidget.d.ts +8 -0
- package/dist-lib/views/Forge/components/ValidationPanel.d.ts +22 -0
- package/dist-lib/views/Forge/context/ForgeProvider.d.ts +23 -0
- package/dist-lib/views/Forge/hooks/useConfig.d.ts +54 -0
- package/dist-lib/views/Forge/hooks/useForgeDashboard.d.ts +107 -0
- package/dist-lib/views/Forge/hooks/useForgeQuota.d.ts +22 -0
- package/dist-lib/views/Forge/phases/AnalyzingView.d.ts +1 -0
- package/dist-lib/views/Forge/phases/IdleView.d.ts +1 -0
- package/dist-lib/views/Forge/phases/MonitoringIssueTab.d.ts +1 -0
- package/dist-lib/views/Forge/phases/MonitoringPipelineTab.d.ts +1 -0
- package/dist-lib/views/Forge/phases/MonitoringView.d.ts +1 -0
- package/dist-lib/views/Forge/phases/ReviewingView.d.ts +1 -0
- package/dist-lib/views/Forge/phases/SelectedIssueTab.d.ts +11 -0
- package/dist-lib/views/Forge/phases/SelectedMRTab.d.ts +1 -0
- package/dist-lib/views/Forge/phases/SelectedView.d.ts +1 -0
- package/dist-lib/views/Forge/phases/shared/CloseIssueConfirm.d.ts +1 -0
- package/dist-lib/views/Forge/phases/shared/DetailTabs.d.ts +14 -0
- package/dist-lib/views/Forge/phases/shared/EnvironmentsPanel.d.ts +9 -0
- package/dist-lib/views/Forge/phases/shared/ForgeActivity.d.ts +1 -0
- package/dist-lib/views/Forge/phases/shared/IssueHeaderBlock.d.ts +13 -0
- package/dist-lib/views/Forge/phases/shared/MergedMRList.d.ts +4 -0
- package/dist-lib/views/Forge/phases/shared/PreviousAnalyses.d.ts +1 -0
- package/dist-lib/views/Forge/phases/shared/ResolutionCheckPanel.d.ts +6 -0
- package/dist-lib/views/Forge/settings/ConnectionRow.d.ts +16 -0
- package/dist-lib/views/Forge/settings/ConnectionsSection.d.ts +7 -0
- package/dist-lib/views/Forge/settings/DefaultsSection.d.ts +7 -0
- package/dist-lib/views/Forge/settings/ReposSection.d.ts +7 -0
- package/dist-lib/views/Forge/settings/SettingsView.d.ts +1 -0
- package/dist-lib/views/Forge/shell/ConfigBar.d.ts +1 -0
- package/dist-lib/views/Forge/shell/ForgeRouter.d.ts +9 -0
- package/dist-lib/views/Forge/shell/ForgeShell.d.ts +7 -0
- package/dist-lib/views/Forge/shell/Sidebar.d.ts +1 -0
- package/dist-lib/views/Forge/shell/SidebarFooterMenu.d.ts +1 -0
- package/dist-lib/views/Forge/shell/SidebarNav.d.ts +1 -0
- package/dist-lib/views/Forge/shell/SidebarNavItem.d.ts +10 -0
- package/dist-lib/views/Forge/shell/TopBar.d.ts +5 -0
- package/dist-lib/views/Forge/types.d.ts +235 -0
- package/dist-lib/views/Forge/utils.d.ts +11 -0
- package/package.json +88 -0
- package/server/forge/core/ForgeAgent.js +1144 -0
- package/server/forge/core/agentClient.js +36 -0
- package/server/forge/core/branchName.js +41 -0
- package/server/forge/core/claudeClient.js +356 -0
- package/server/forge/core/configStore.js +214 -0
- package/server/forge/core/gitlabClient.js +340 -0
- package/server/forge/core/jobs.js +180 -0
- package/server/forge/core/k8s.js +54 -0
- package/server/forge/core/localRepo.js +136 -0
- package/server/forge/core/openclawClient.js +125 -0
- package/server/forge/core/prompts.js +369 -0
- package/server/forge/core/providers/azuredevops.js +408 -0
- package/server/forge/core/providers/base.js +208 -0
- package/server/forge/core/providers/github.js +349 -0
- package/server/forge/core/providers/gitlab.js +529 -0
- package/server/forge/core/providers/index.js +47 -0
- package/server/forge/core/quotaClient.js +101 -0
- package/server/forge/core/validator/attribute.js +84 -0
- package/server/forge/core/validator/clone.js +91 -0
- package/server/forge/core/validator/cmd.js +98 -0
- package/server/forge/core/validator/fs.js +9 -0
- package/server/forge/core/validator/overlay.js +52 -0
- package/server/forge/core/validator/projectType.js +87 -0
- package/server/forge/core/validator/runners/go.js +107 -0
- package/server/forge/core/validator/runners/node.js +84 -0
- package/server/forge/core/validator/tdd.js +105 -0
- package/server/forge/core/validator.js +166 -0
- package/server/forge/routes.js +1012 -0
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* server/forge/core/gitlabClient.js
|
|
3
|
+
*
|
|
4
|
+
* Thin wrapper around the GitLab REST API v4.
|
|
5
|
+
* All calls use process.env.GITLAB_TOKEN for auth and
|
|
6
|
+
* process.env.FORGE_GITLAB_PROJECT for the default project path.
|
|
7
|
+
*
|
|
8
|
+
* Required env vars:
|
|
9
|
+
* GITLAB_TOKEN — personal access token or CI job token
|
|
10
|
+
* FORGE_GITLAB_PROJECT — project path, e.g. "ipul/web-app"
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const GITLAB_API = "https://gitlab.com/api/v4";
|
|
14
|
+
|
|
15
|
+
// ─── Internal helpers ─────────────────────────────────────────────────────────
|
|
16
|
+
|
|
17
|
+
function authHeaders() {
|
|
18
|
+
return {
|
|
19
|
+
"PRIVATE-TOKEN": process.env.GITLAB_TOKEN,
|
|
20
|
+
"Content-Type": "application/json",
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function request(path, options = {}) {
|
|
25
|
+
const url = `${GITLAB_API}${path}`;
|
|
26
|
+
const res = await fetch(url, {
|
|
27
|
+
...options,
|
|
28
|
+
headers: { ...authHeaders(), ...options.headers },
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
if (!res.ok) {
|
|
32
|
+
const text = await res.text().catch(() => "");
|
|
33
|
+
throw new Error(`GitLab ${res.status} on ${path}: ${text}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return res.json();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function encodeProject(project) {
|
|
40
|
+
return encodeURIComponent(project ?? process.env.FORGE_GITLAB_PROJECT);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ─── Issues ───────────────────────────────────────────────────────────────────
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Returns open issues for the project, newest first.
|
|
47
|
+
* @param {string} [project]
|
|
48
|
+
*/
|
|
49
|
+
export async function createIssue(title, description = "", project) {
|
|
50
|
+
return request(
|
|
51
|
+
`/projects/${encodeProject(project)}/issues`,
|
|
52
|
+
{
|
|
53
|
+
method: "POST",
|
|
54
|
+
body: JSON.stringify({ title, description }),
|
|
55
|
+
},
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export async function updateIssue(issueIid, { title, description }, project) {
|
|
60
|
+
return request(`/projects/${encodeProject(project)}/issues/${issueIid}`, {
|
|
61
|
+
method: "PUT",
|
|
62
|
+
body: JSON.stringify({ title, description }),
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export async function closeIssue(issueIid, project) {
|
|
67
|
+
return request(`/projects/${encodeProject(project)}/issues/${issueIid}`, {
|
|
68
|
+
method: "PUT",
|
|
69
|
+
body: JSON.stringify({ state_event: "close" }),
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export async function listOpenIssues(project) {
|
|
74
|
+
return request(
|
|
75
|
+
`/projects/${encodeProject(project)}/issues?state=opened&order_by=updated_at&per_page=25`,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Returns a single issue by internal ID (iid).
|
|
81
|
+
* @param {number} issueIid
|
|
82
|
+
* @param {string} [project]
|
|
83
|
+
*/
|
|
84
|
+
export async function getIssue(issueIid, project) {
|
|
85
|
+
return request(
|
|
86
|
+
`/projects/${encodeProject(project)}/issues/${issueIid}`,
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// ─── Repository ───────────────────────────────────────────────────────────────
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Returns the recursive file tree under `srcPath`.
|
|
94
|
+
* Filters to source files only (skips build artifacts, lock files, etc.).
|
|
95
|
+
* @param {string} [srcPath]
|
|
96
|
+
* @param {string} [ref]
|
|
97
|
+
* @param {string} [project]
|
|
98
|
+
*/
|
|
99
|
+
export async function getFileTree(srcPath = "src", ref = "main", project) {
|
|
100
|
+
const proj = encodeProject(project);
|
|
101
|
+
const allItems = [];
|
|
102
|
+
let page = 1;
|
|
103
|
+
|
|
104
|
+
while (true) {
|
|
105
|
+
const items = await request(
|
|
106
|
+
`/projects/${proj}/repository/tree` +
|
|
107
|
+
`?path=${srcPath}&recursive=true&ref=${ref}&per_page=100&page=${page}`,
|
|
108
|
+
);
|
|
109
|
+
allItems.push(...items);
|
|
110
|
+
if (items.length < 100) break;
|
|
111
|
+
page++;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return allItems
|
|
115
|
+
.filter((item) => item.type === "blob")
|
|
116
|
+
.filter((item) => /\.(ts|tsx|js|jsx|json|css|yaml|yml|md)$/.test(item.path))
|
|
117
|
+
.map((item) => item.path);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Returns the decoded UTF-8 content of a single file.
|
|
122
|
+
* @param {string} filePath
|
|
123
|
+
* @param {string} [ref]
|
|
124
|
+
* @param {string} [project]
|
|
125
|
+
*/
|
|
126
|
+
export async function getFileContent(filePath, ref = "main", project) {
|
|
127
|
+
const encoded = encodeURIComponent(filePath);
|
|
128
|
+
const data = await request(
|
|
129
|
+
`/projects/${encodeProject(project)}/repository/files/${encoded}?ref=${ref}`,
|
|
130
|
+
);
|
|
131
|
+
return Buffer.from(data.content, "base64").toString("utf-8");
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// ─── Mutations ────────────────────────────────────────────────────────────────
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Creates a new branch from `ref`.
|
|
138
|
+
* @param {string} branchName
|
|
139
|
+
* @param {string} [ref]
|
|
140
|
+
* @param {string} [project]
|
|
141
|
+
*/
|
|
142
|
+
export async function createBranch(branchName, ref = "main", project) {
|
|
143
|
+
return request(
|
|
144
|
+
`/projects/${encodeProject(project)}/repository/branches`,
|
|
145
|
+
{
|
|
146
|
+
method: "POST",
|
|
147
|
+
body: JSON.stringify({ branch: branchName, ref }),
|
|
148
|
+
},
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Commits multiple file changes in a single commit.
|
|
154
|
+
* @param {string} branch
|
|
155
|
+
* @param {string} commitMessage
|
|
156
|
+
* @param {Array<{action: string, file_path: string, content: string}>} actions
|
|
157
|
+
* @param {string} [project]
|
|
158
|
+
*/
|
|
159
|
+
export async function commitFiles(branch, commitMessage, actions, project) {
|
|
160
|
+
return request(
|
|
161
|
+
`/projects/${encodeProject(project)}/repository/commits`,
|
|
162
|
+
{
|
|
163
|
+
method: "POST",
|
|
164
|
+
body: JSON.stringify({ branch, commit_message: commitMessage, actions }),
|
|
165
|
+
},
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Opens a merge request from `sourceBranch` into `targetBranch`.
|
|
171
|
+
* @param {{ sourceBranch: string, targetBranch?: string, title: string, description?: string }} opts
|
|
172
|
+
* @param {string} [project]
|
|
173
|
+
*/
|
|
174
|
+
export async function createMR(
|
|
175
|
+
{ sourceBranch, targetBranch = "main", title, description = "" },
|
|
176
|
+
project,
|
|
177
|
+
) {
|
|
178
|
+
return request(
|
|
179
|
+
`/projects/${encodeProject(project)}/merge_requests`,
|
|
180
|
+
{
|
|
181
|
+
method: "POST",
|
|
182
|
+
body: JSON.stringify({
|
|
183
|
+
source_branch: sourceBranch,
|
|
184
|
+
target_branch: targetBranch,
|
|
185
|
+
title,
|
|
186
|
+
description,
|
|
187
|
+
remove_source_branch: true,
|
|
188
|
+
}),
|
|
189
|
+
},
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Returns open MRs created by Forge for a specific issue (branch prefix forge/issue-{iid}-).
|
|
195
|
+
* @param {number} issueIid
|
|
196
|
+
* @param {string} [project]
|
|
197
|
+
*/
|
|
198
|
+
/**
|
|
199
|
+
* Returns all open MRs created by Forge (branch prefix forge/).
|
|
200
|
+
* Includes head_pipeline for quick status display.
|
|
201
|
+
* @param {string} [project]
|
|
202
|
+
*/
|
|
203
|
+
export async function listAllForgeMRs(project) {
|
|
204
|
+
const all = await request(
|
|
205
|
+
`/projects/${encodeProject(project)}/merge_requests?state=opened&per_page=50`,
|
|
206
|
+
);
|
|
207
|
+
return all.filter((mr) => mr.source_branch?.startsWith("forge/"));
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export async function listForgeMRsForIssue(issueIid, project) {
|
|
211
|
+
const all = await request(
|
|
212
|
+
`/projects/${encodeProject(project)}/merge_requests?state=opened&per_page=50`,
|
|
213
|
+
);
|
|
214
|
+
return all.filter((mr) =>
|
|
215
|
+
mr.source_branch?.startsWith(`forge/issue-${issueIid}-`),
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Returns merged MRs created by Forge for a specific issue.
|
|
221
|
+
* @param {number} issueIid
|
|
222
|
+
* @param {string} [project]
|
|
223
|
+
*/
|
|
224
|
+
export async function listMergedForgeMRsForIssue(issueIid, project) {
|
|
225
|
+
const all = await request(
|
|
226
|
+
`/projects/${encodeProject(project)}/merge_requests?state=merged&per_page=50`,
|
|
227
|
+
);
|
|
228
|
+
return all.filter((mr) =>
|
|
229
|
+
mr.source_branch?.startsWith(`forge/issue-${issueIid}-`),
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// ─── Jobs ─────────────────────────────────────────────────────────────────────
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Returns the plain-text trace (log output) for a single job.
|
|
237
|
+
* @param {number} jobId
|
|
238
|
+
* @param {string} [project]
|
|
239
|
+
*/
|
|
240
|
+
export async function getJobTrace(jobId, project) {
|
|
241
|
+
const proj = encodeProject(project);
|
|
242
|
+
const url = `${GITLAB_API}/projects/${proj}/jobs/${jobId}/trace`;
|
|
243
|
+
const res = await fetch(url, { headers: authHeaders() });
|
|
244
|
+
if (!res.ok) {
|
|
245
|
+
const text = await res.text().catch(() => "");
|
|
246
|
+
throw new Error(`GitLab ${res.status} on jobs/${jobId}/trace: ${text}`);
|
|
247
|
+
}
|
|
248
|
+
return res.text();
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// ─── MR detail + rebase ───────────────────────────────────────────────────────
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Returns a single MR with full detail including conflict/rebase status.
|
|
255
|
+
* @param {number} mrIid
|
|
256
|
+
* @param {string} [project]
|
|
257
|
+
*/
|
|
258
|
+
export async function getMR(mrIid, project) {
|
|
259
|
+
return request(
|
|
260
|
+
`/projects/${encodeProject(project)}/merge_requests/${mrIid}`,
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Triggers a rebase of the MR source branch onto the target branch.
|
|
266
|
+
* @param {number} mrIid
|
|
267
|
+
* @param {string} [project]
|
|
268
|
+
*/
|
|
269
|
+
export async function rebaseMR(mrIid, project) {
|
|
270
|
+
return request(
|
|
271
|
+
`/projects/${encodeProject(project)}/merge_requests/${mrIid}/rebase`,
|
|
272
|
+
{ method: "PUT" },
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Returns the diff for a merge request (all changed files).
|
|
278
|
+
* @param {number} mrIid
|
|
279
|
+
* @param {string} [project]
|
|
280
|
+
*/
|
|
281
|
+
export async function getMRDiffs(mrIid, project) {
|
|
282
|
+
const proj = encodeProject(project);
|
|
283
|
+
const allDiffs = [];
|
|
284
|
+
let page = 1;
|
|
285
|
+
while (true) {
|
|
286
|
+
const items = await request(
|
|
287
|
+
`/projects/${proj}/merge_requests/${mrIid}/diffs?per_page=20&page=${page}`,
|
|
288
|
+
);
|
|
289
|
+
allDiffs.push(...items);
|
|
290
|
+
if (items.length < 20) break;
|
|
291
|
+
page++;
|
|
292
|
+
}
|
|
293
|
+
return allDiffs;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// ─── Pipelines ────────────────────────────────────────────────────────────────
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Returns all pipelines associated with an MR.
|
|
300
|
+
* @param {number} mrIid
|
|
301
|
+
* @param {string} [project]
|
|
302
|
+
*/
|
|
303
|
+
export async function getMRPipelines(mrIid, project) {
|
|
304
|
+
return request(
|
|
305
|
+
`/projects/${encodeProject(project)}/merge_requests/${mrIid}/pipelines`,
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Returns a single pipeline with full status detail.
|
|
311
|
+
* @param {number} pipelineId
|
|
312
|
+
* @param {string} [project]
|
|
313
|
+
*/
|
|
314
|
+
export async function getPipeline(pipelineId, project) {
|
|
315
|
+
return request(
|
|
316
|
+
`/projects/${encodeProject(project)}/pipelines/${pipelineId}`,
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Returns all jobs for a pipeline.
|
|
322
|
+
* @param {number} pipelineId
|
|
323
|
+
* @param {string} [project]
|
|
324
|
+
*/
|
|
325
|
+
export async function getPipelineJobs(pipelineId, project) {
|
|
326
|
+
return request(
|
|
327
|
+
`/projects/${encodeProject(project)}/pipelines/${pipelineId}/jobs`,
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Returns pipelines that ran on a specific commit SHA.
|
|
333
|
+
* @param {string} sha
|
|
334
|
+
* @param {string} [project]
|
|
335
|
+
*/
|
|
336
|
+
export async function getProjectPipelinesBySha(sha, project) {
|
|
337
|
+
return request(
|
|
338
|
+
`/projects/${encodeProject(project)}/pipelines?sha=${encodeURIComponent(sha)}&per_page=5`,
|
|
339
|
+
);
|
|
340
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* server/forge/core/jobs.js
|
|
3
|
+
*
|
|
4
|
+
* Unified in-memory job store with event buffering + multi-subscriber replay.
|
|
5
|
+
*
|
|
6
|
+
* Why this exists: SSE endpoints like refine / ci-fix / validate / analyze
|
|
7
|
+
* run a long task and stream events to one EventSource. If the browser
|
|
8
|
+
* reloads or React re-mounts the component, that stream dies. Before this
|
|
9
|
+
* module, the job also died — its state was deleted the moment the stream
|
|
10
|
+
* was consumed, so there was nothing to reconnect to.
|
|
11
|
+
*
|
|
12
|
+
* With this store:
|
|
13
|
+
* 1. `createJob(type, runner)` starts the runner immediately in the
|
|
14
|
+
* background and buffers every event it emits.
|
|
15
|
+
* 2. `subscribe(id, handler)` replays the buffer synchronously, then wires
|
|
16
|
+
* the handler to future events until the job is terminal.
|
|
17
|
+
* 3. A reconnecting client gets the full history on reconnect and then
|
|
18
|
+
* live events — identical to if it had been watching all along.
|
|
19
|
+
*
|
|
20
|
+
* Process-restart survival is out of scope for v1 (Map is in-memory only).
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
const TTL_MS = 10 * 60 * 1000;
|
|
24
|
+
const SWEEP_INTERVAL_MS = 60 * 1000;
|
|
25
|
+
|
|
26
|
+
const jobs = new Map();
|
|
27
|
+
let sweeper = null;
|
|
28
|
+
|
|
29
|
+
function ensureSweeper() {
|
|
30
|
+
if (sweeper) return;
|
|
31
|
+
sweeper = setInterval(() => {
|
|
32
|
+
const now = Date.now();
|
|
33
|
+
for (const [id, job] of jobs) {
|
|
34
|
+
if (job.expiresAt && job.expiresAt <= now && job.subscribers.size === 0) {
|
|
35
|
+
jobs.delete(id);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}, SWEEP_INTERVAL_MS);
|
|
39
|
+
sweeper.unref?.();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function newId(prefix) {
|
|
43
|
+
return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Create and start a job. `runner` is an async function that receives
|
|
48
|
+
* `(send, signal)` — any value it returns is captured as `doneResult`.
|
|
49
|
+
* A terminal `done`, `error`, or `cancelled` event is emitted automatically
|
|
50
|
+
* when runner settles; runners must not emit those themselves.
|
|
51
|
+
*
|
|
52
|
+
* The `signal` is an `AbortSignal` that fires when a client calls `cancelJob`.
|
|
53
|
+
* Runners should forward it to any subprocess / fetch / SDK call that
|
|
54
|
+
* supports AbortSignal so the underlying work actually stops (and API/CLI
|
|
55
|
+
* costs aren't incurred for work the user no longer wants).
|
|
56
|
+
*/
|
|
57
|
+
export function createJob(type, runner, { idPrefix = type } = {}) {
|
|
58
|
+
ensureSweeper();
|
|
59
|
+
const id = newId(idPrefix);
|
|
60
|
+
const controller = new AbortController();
|
|
61
|
+
const job = {
|
|
62
|
+
id,
|
|
63
|
+
type,
|
|
64
|
+
status: "running",
|
|
65
|
+
events: [],
|
|
66
|
+
doneResult: undefined,
|
|
67
|
+
error: null,
|
|
68
|
+
createdAt: Date.now(),
|
|
69
|
+
finishedAt: null,
|
|
70
|
+
expiresAt: null,
|
|
71
|
+
subscribers: new Set(),
|
|
72
|
+
controller,
|
|
73
|
+
};
|
|
74
|
+
jobs.set(id, job);
|
|
75
|
+
|
|
76
|
+
const send = (event) => {
|
|
77
|
+
job.events.push(event);
|
|
78
|
+
for (const sub of job.subscribers) {
|
|
79
|
+
try { sub(event); } catch { /* subscriber errors don't break fanout */ }
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
(async () => {
|
|
84
|
+
try {
|
|
85
|
+
const result = await runner(send, controller.signal);
|
|
86
|
+
if (job.status === "cancelled") return;
|
|
87
|
+
job.doneResult = result ?? null;
|
|
88
|
+
job.status = "done";
|
|
89
|
+
send({ type: "done" });
|
|
90
|
+
} catch (e) {
|
|
91
|
+
if (job.status === "cancelled") return;
|
|
92
|
+
// If the runner threw because of our own abort, prefer the cancelled
|
|
93
|
+
// terminal over a misleading error event.
|
|
94
|
+
if (controller.signal.aborted) {
|
|
95
|
+
job.status = "cancelled";
|
|
96
|
+
send({ type: "cancelled" });
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
job.status = "error";
|
|
100
|
+
job.error = e?.message ?? String(e);
|
|
101
|
+
send({ type: "error", message: job.error });
|
|
102
|
+
} finally {
|
|
103
|
+
job.finishedAt = Date.now();
|
|
104
|
+
job.expiresAt = job.finishedAt + TTL_MS;
|
|
105
|
+
}
|
|
106
|
+
})();
|
|
107
|
+
|
|
108
|
+
return id;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Cancel a running job. Aborts the signal, emits a terminal `cancelled`
|
|
113
|
+
* event, and marks the job non-running. No-op if the job is already
|
|
114
|
+
* terminal or doesn't exist — returns whether a cancel was actually issued.
|
|
115
|
+
*/
|
|
116
|
+
export function cancelJob(id) {
|
|
117
|
+
const job = jobs.get(id);
|
|
118
|
+
if (!job) return false;
|
|
119
|
+
if (job.status !== "running") return false;
|
|
120
|
+
job.status = "cancelled";
|
|
121
|
+
try {
|
|
122
|
+
job.controller.abort();
|
|
123
|
+
} catch { /* ignore */ }
|
|
124
|
+
// Emit the terminal *before* the runner's catch block runs, so reconnecting
|
|
125
|
+
// clients always see a cancellation event in the buffer.
|
|
126
|
+
job.events.push({ type: "cancelled" });
|
|
127
|
+
for (const sub of job.subscribers) {
|
|
128
|
+
try { sub({ type: "cancelled" }); } catch { /* ignore */ }
|
|
129
|
+
}
|
|
130
|
+
job.finishedAt = Date.now();
|
|
131
|
+
job.expiresAt = job.finishedAt + TTL_MS;
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function getJob(id) {
|
|
136
|
+
return jobs.get(id) ?? null;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Replay buffered events to `handler`, then subscribe for live events until
|
|
141
|
+
* the job reaches terminal state. Returns an `unsubscribe` function, or
|
|
142
|
+
* `null` if the job doesn't exist.
|
|
143
|
+
*
|
|
144
|
+
* The terminal `done`/`error` event is part of the buffered replay — callers
|
|
145
|
+
* that want to end the response on terminal should check for it inside
|
|
146
|
+
* their handler.
|
|
147
|
+
*/
|
|
148
|
+
export function subscribe(id, handler) {
|
|
149
|
+
const job = jobs.get(id);
|
|
150
|
+
if (!job) return null;
|
|
151
|
+
|
|
152
|
+
for (const e of job.events) {
|
|
153
|
+
try { handler(e); } catch { /* ignore */ }
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (job.status !== "running") {
|
|
157
|
+
return () => {};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
job.subscribers.add(handler);
|
|
161
|
+
return () => { job.subscribers.delete(handler); };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Public status snapshot — safe to send to clients. Excludes the event
|
|
166
|
+
* buffer (potentially large) and subscribers (not serialisable).
|
|
167
|
+
*/
|
|
168
|
+
export function snapshot(id) {
|
|
169
|
+
const job = jobs.get(id);
|
|
170
|
+
if (!job) return null;
|
|
171
|
+
return {
|
|
172
|
+
id: job.id,
|
|
173
|
+
type: job.type,
|
|
174
|
+
status: job.status,
|
|
175
|
+
createdAt: job.createdAt,
|
|
176
|
+
finishedAt: job.finishedAt,
|
|
177
|
+
error: job.error,
|
|
178
|
+
eventCount: job.events.length,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import * as k8s from "@kubernetes/client-node";
|
|
2
|
+
|
|
3
|
+
let kc = new k8s.KubeConfig();
|
|
4
|
+
|
|
5
|
+
function loadConfig() {
|
|
6
|
+
kc = new k8s.KubeConfig();
|
|
7
|
+
const token = process.env.K8S_CLUSTER_TOKEN;
|
|
8
|
+
const server = process.env.K8S_CLUSTER_SERVER;
|
|
9
|
+
|
|
10
|
+
if (token && server) {
|
|
11
|
+
kc.loadFromOptions({
|
|
12
|
+
clusters: [{ name: "cluster", server, skipTLSVerify: process.env.K8S_SKIP_TLS_VERIFY === "true" }],
|
|
13
|
+
users: [{ name: "admin", token }],
|
|
14
|
+
contexts: [{ name: "default", cluster: "cluster", user: "admin" }],
|
|
15
|
+
currentContext: "default",
|
|
16
|
+
});
|
|
17
|
+
} else {
|
|
18
|
+
kc.loadFromDefault();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
loadConfig();
|
|
23
|
+
|
|
24
|
+
function getAppsApi() {
|
|
25
|
+
return kc.makeApiClient(k8s.AppsV1Api);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function formatDeployment(dep) {
|
|
29
|
+
const containers = dep.spec?.template?.spec?.containers || [];
|
|
30
|
+
const status = dep.status || {};
|
|
31
|
+
return {
|
|
32
|
+
name: dep.metadata?.name,
|
|
33
|
+
namespace: dep.metadata?.namespace,
|
|
34
|
+
image: containers[0]?.image || "unknown",
|
|
35
|
+
replicas: {
|
|
36
|
+
desired: dep.spec?.replicas || 0,
|
|
37
|
+
ready: status.readyReplicas || 0,
|
|
38
|
+
available: status.availableReplicas || 0,
|
|
39
|
+
unavailable: status.unavailableReplicas || 0,
|
|
40
|
+
updated: status.updatedReplicas || 0,
|
|
41
|
+
},
|
|
42
|
+
conditions: (status.conditions || []).map((c) => ({
|
|
43
|
+
type: c.type,
|
|
44
|
+
status: c.status,
|
|
45
|
+
message: c.message,
|
|
46
|
+
})),
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function listDeployments(namespace) {
|
|
51
|
+
const api = getAppsApi();
|
|
52
|
+
const res = await api.listNamespacedDeployment({ namespace });
|
|
53
|
+
return (res.items || []).map(formatDeployment);
|
|
54
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* server/forge/core/localRepo.js
|
|
3
|
+
*
|
|
4
|
+
* Shared helpers for maintaining a shallow working clone of a repo under
|
|
5
|
+
* ~/.forge/cache/<repo>/ and overlaying proposed file changes onto it.
|
|
6
|
+
*
|
|
7
|
+
* Used by:
|
|
8
|
+
* - validator.js → run lint/typecheck/test against the overlaid tree
|
|
9
|
+
* - ForgeAgent.refineIssue → give the refine model real Read/Glob/Grep access
|
|
10
|
+
* against the current state of the working tree (FORGE_REFINE_REPO_ACCESS)
|
|
11
|
+
*
|
|
12
|
+
* Scope matches validator v1: GitLab HTTPS clones only. GitHub / Azure DevOps
|
|
13
|
+
* raise a clear "not supported yet" error so the caller can fall back cleanly.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { spawn } from "child_process";
|
|
17
|
+
import fs from "fs/promises";
|
|
18
|
+
import os from "os";
|
|
19
|
+
import path from "path";
|
|
20
|
+
|
|
21
|
+
const CACHE_ROOT = process.env.FORGE_VALIDATE_CACHE
|
|
22
|
+
?? path.join(os.homedir(), ".forge", "cache");
|
|
23
|
+
|
|
24
|
+
function runGit(args, opts = {}, onLog) {
|
|
25
|
+
return new Promise((resolve) => {
|
|
26
|
+
const proc = spawn("git", args, {
|
|
27
|
+
...opts,
|
|
28
|
+
env: { ...process.env, ...(opts.env ?? {}), FORCE_COLOR: "0", NO_COLOR: "1", TERM: "dumb" },
|
|
29
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
30
|
+
});
|
|
31
|
+
const lines = [];
|
|
32
|
+
const collect = (buf) => {
|
|
33
|
+
for (const raw of buf.toString().split("\n")) {
|
|
34
|
+
const line = raw.replace(/\r$/, "");
|
|
35
|
+
if (!line) continue;
|
|
36
|
+
lines.push(line);
|
|
37
|
+
onLog?.(line);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
proc.stdout.on("data", collect);
|
|
41
|
+
proc.stderr.on("data", collect);
|
|
42
|
+
proc.on("error", (err) => resolve({ code: -1, output: lines.join("\n"), error: err.message }));
|
|
43
|
+
proc.on("close", (code) => resolve({ code: code ?? -1, output: lines.join("\n"), error: null }));
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function exists(p) {
|
|
48
|
+
try { await fs.access(p); return true; } catch { return false; }
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function sanitizeRepo(repo) {
|
|
52
|
+
return repo.replace(/[^a-zA-Z0-9_./-]/g, "_");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function gitlabCloneUrl(project) {
|
|
56
|
+
const token = process.env.GITLAB_TOKEN;
|
|
57
|
+
if (!token) throw new Error("GITLAB_TOKEN is required to clone the repo locally.");
|
|
58
|
+
return `https://oauth2:${token}@gitlab.com/${project}.git`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function resolveCloneUrl(repoInput) {
|
|
62
|
+
const colon = repoInput.indexOf(":");
|
|
63
|
+
if (colon > 0) {
|
|
64
|
+
const prefix = repoInput.slice(0, colon).toLowerCase();
|
|
65
|
+
if (prefix === "gitlab") return gitlabCloneUrl(repoInput.slice(colon + 1));
|
|
66
|
+
if (prefix === "github" || prefix === "azuredevops" || prefix === "azure") {
|
|
67
|
+
throw new Error(`Local clone for ${prefix} repos isn't supported yet — GitLab only.`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return gitlabCloneUrl(repoInput);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function cachePathFor(repo) {
|
|
74
|
+
return path.join(CACHE_ROOT, sanitizeRepo(repo));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Ensures the repo is cloned and reset to a clean branch at origin/main.
|
|
79
|
+
* Reuses any existing clone under ~/.forge/cache/<repo>/.
|
|
80
|
+
*
|
|
81
|
+
* @param {string} repo Repo id (may carry a "gitlab:" prefix)
|
|
82
|
+
* @param {string} branchName Workspace branch to check out
|
|
83
|
+
* @param {(msg: string) => void} [onLog]
|
|
84
|
+
* @returns {Promise<string>} Absolute path to the working tree
|
|
85
|
+
*/
|
|
86
|
+
export async function ensureWorkingTree(repo, branchName, onLog) {
|
|
87
|
+
const dir = cachePathFor(repo);
|
|
88
|
+
const hasRepo = await exists(path.join(dir, ".git"));
|
|
89
|
+
const log = (m) => onLog?.(m);
|
|
90
|
+
|
|
91
|
+
if (!hasRepo) {
|
|
92
|
+
log(`Cloning ${repo} → ${dir}`);
|
|
93
|
+
await fs.mkdir(dir, { recursive: true });
|
|
94
|
+
const clone = await runGit(["clone", "--depth=1", resolveCloneUrl(repo), dir], {}, onLog);
|
|
95
|
+
if (clone.code !== 0) throw new Error(`git clone failed (code ${clone.code})`);
|
|
96
|
+
} else {
|
|
97
|
+
log(`Reusing clone at ${dir}`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const fetch = await runGit(["fetch", "--depth=1", "origin", "main"], { cwd: dir }, onLog);
|
|
101
|
+
if (fetch.code !== 0) throw new Error(`git fetch failed (code ${fetch.code})`);
|
|
102
|
+
|
|
103
|
+
const reset = await runGit(["reset", "--hard", "origin/main"], { cwd: dir }, onLog);
|
|
104
|
+
if (reset.code !== 0) throw new Error(`git reset failed (code ${reset.code})`);
|
|
105
|
+
await runGit(["clean", "-fd"], { cwd: dir }, onLog);
|
|
106
|
+
|
|
107
|
+
const checkout = await runGit(["checkout", "-B", branchName], { cwd: dir }, onLog);
|
|
108
|
+
if (checkout.code !== 0) throw new Error(`git checkout ${branchName} failed (code ${checkout.code})`);
|
|
109
|
+
|
|
110
|
+
return dir;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Writes proposed file contents into the working tree (or deletes files for
|
|
115
|
+
* action="delete"). Refuses paths that escape the clone directory.
|
|
116
|
+
*
|
|
117
|
+
* @param {string} dir
|
|
118
|
+
* @param {Array<{file: string, action: 'create'|'modify'|'delete', proposed?: string}>} changes
|
|
119
|
+
* @param {(msg: string) => void} [onLog]
|
|
120
|
+
*/
|
|
121
|
+
export async function overlayChanges(dir, changes, onLog) {
|
|
122
|
+
for (const c of changes) {
|
|
123
|
+
const full = path.join(dir, c.file);
|
|
124
|
+
if (!full.startsWith(dir + path.sep) && full !== dir) {
|
|
125
|
+
throw new Error(`Refusing to write outside clone dir: ${c.file}`);
|
|
126
|
+
}
|
|
127
|
+
if (c.action === "delete") {
|
|
128
|
+
await fs.rm(full, { force: true });
|
|
129
|
+
onLog?.(`- ${c.file}`);
|
|
130
|
+
} else {
|
|
131
|
+
await fs.mkdir(path.dirname(full), { recursive: true });
|
|
132
|
+
await fs.writeFile(full, c.proposed ?? "");
|
|
133
|
+
onLog?.(`+ ${c.file} (${c.action})`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|