fde-console 0.0.0-stage → 2.9.68
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +128 -2
- package/README.zh-CN.md +130 -0
- package/assets/icon.svg +9 -0
- package/cordis.patch.yml +7 -0
- package/lib/agentActivity.js +1180 -0
- package/lib/agentContext.js +146 -0
- package/lib/agentTranscript.js +1398 -0
- package/lib/analyze.js +625 -0
- package/lib/attribute.js +205 -0
- package/lib/backup.js +366 -0
- package/lib/desktopAgents.js +123 -0
- package/lib/diary.js +333 -0
- package/lib/dingtalk.js +412 -0
- package/lib/feishu.js +580 -0
- package/lib/giteaClient.js +810 -0
- package/lib/giteeClient.js +372 -0
- package/lib/githubClient.js +361 -0
- package/lib/heatmap.js +99 -0
- package/lib/imNotify.js +378 -0
- package/lib/inbox.js +438 -0
- package/lib/instanceLock.js +122 -0
- package/lib/jobs.js +56 -0
- package/lib/learn.js +1388 -0
- package/lib/learnImport.js +283 -0
- package/lib/learnSeeds.js +97 -0
- package/lib/learnSeedsAiEng.js +77 -0
- package/lib/mcpCore.js +135 -0
- package/lib/members.js +160 -0
- package/lib/migration.js +403 -0
- package/lib/overview.js +376 -0
- package/lib/postmortem.js +148 -0
- package/lib/radarIgnore.js +134 -0
- package/lib/redact.js +76 -0
- package/lib/router.js +246 -0
- package/lib/routes-gitea.js +262 -0
- package/lib/routes-setup.js +40 -0
- package/lib/scorecard.js +111 -0
- package/lib/sessionRef.js +227 -0
- package/lib/sessionSnapshot.js +67 -0
- package/lib/sessionSuggest.js +186 -0
- package/lib/settings.js +783 -0
- package/lib/setupCheck.js +330 -0
- package/lib/ssrfGuard.js +145 -0
- package/lib/store.js +564 -0
- package/lib/storeDb.js +418 -0
- package/lib/taskEvents.js +243 -0
- package/lib/webhookOut.js +125 -0
- package/lib/weekly.js +439 -0
- package/lib/workSearch.js +220 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +46 -4
- package/plugin/adapter.js +157 -0
- package/plugin/client.js +282 -0
- package/plugin/index.mjs +116 -0
- package/plugin/static.js +99 -0
- package/scripts/backup-data.js +41 -0
- package/scripts/mcp-server.mjs +68 -0
- package/scripts/migrate-data.js +301 -0
- package/scripts/migrate-storage.js +47 -0
- package/scripts/seed-demo.js +200 -0
- package/scripts/write-agent-context.js +43 -0
- package/test/helpers/appAssembler.js +1078 -0
- package/web/client/fde-client.js +292 -0
- package/web/css/app.css +1510 -0
- package/web/index.html +69 -0
- package/web/js/api.js +72 -0
- package/web/js/app.js +313 -0
- package/web/js/icons.js +48 -0
- package/web/js/mock.js +1150 -0
- package/web/js/state.js +7 -0
- package/web/js/ui.js +276 -0
- package/web/js/views/activity.js +410 -0
- package/web/js/views/agents.js +892 -0
- package/web/js/views/board.js +1656 -0
- package/web/js/views/learn.js +530 -0
- package/web/js/views/members.js +344 -0
- package/web/js/views/onboarding.js +103 -0
- package/web/js/views/overview.js +508 -0
- package/web/js/views/radar.js +1359 -0
- package/web/js/views/search.js +149 -0
- package/web/js/views/settings.js +1351 -0
- package/web/js/views/topology.js +471 -0
- package/web/mock.json +2363 -0
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// giteeClient.js — v2.9.18 Gitee(gitee.com)组织动态源适配器。
|
|
3
|
+
// 数据源:GET {baseUrl}/api/v5/orgs/{org}/events(组织级事件流,最新在前,per_page=100 × eventsPages),
|
|
4
|
+
// 真机探针实证(2026-10-25,示例组织):事件 GitHub 风格——type=PushEvent 同名,但字段差异:
|
|
5
|
+
// - repo 用 full_name(GitHub 是 repo.name)
|
|
6
|
+
// - payload.commits[] 无 timestamp 字段(GitHub 有)→ occurredAt 退事件 created_at
|
|
7
|
+
// - payload.commits[].url 是 API 地址(/api/v5/...)→ 动态链接自拼 {baseUrl}/{full_name}/commit/{sha}
|
|
8
|
+
// - created_at 带 +08:00 时区(Date.parse 可解析)
|
|
9
|
+
// 映射到与 giteaClient/githubClient 完全相同的 11 字段活动模型与词表(commit/pr/branch_created/branch_deleted):
|
|
10
|
+
// PushEvent.payload.commits[] 逐条 → 'commit'
|
|
11
|
+
// PullRequestEvent / CreateEvent / DeleteEvent 按 GitHub 同构口径容错映射(Gitee v5 与 GitHub 事件
|
|
12
|
+
// 类型命名同族;窗口内未实证到样本的类型由 skippedEventTypes 计数兜底,映射不炸)
|
|
13
|
+
// 其他事件类型(Issue/Note/PushHook…)跳过 → skippedEventTypes 计数与 distinct 名单
|
|
14
|
+
// 红线(与 gitea/github 两 client 同一套):
|
|
15
|
+
// - 凭据 key:git config --global http.<gitee.baseUrl>/.extraheader(值 = 私人令牌原文,非 header)
|
|
16
|
+
// - 凭据仅存本模块内存(credCache)+ 注册进 redact;绝不写文件、不进子进程 env、不进日志
|
|
17
|
+
// - Gitee v5 令牌走 URL query(access_token)——错误消息出 URL 前先剥参再 redact(双防线)
|
|
18
|
+
// - 出站前 assertSafeUrl(白名单 = gitea ∪ github ∪ gitee 三节)+ redirect:'error'
|
|
19
|
+
// 状态落盘:data/gitee-status.json(独立文件,绝不与 gitea/github-status.json 互相覆盖)。
|
|
20
|
+
// enabled !== true 时零出网、零写盘(pollActivities 直接返回 null)。
|
|
21
|
+
|
|
22
|
+
const path = require('node:path');
|
|
23
|
+
const { execFileSync } = require('node:child_process');
|
|
24
|
+
const { redact, registerSecret } = require('./redact.js');
|
|
25
|
+
const { assertSafeUrl, validateWhitelist } = require('./ssrfGuard.js');
|
|
26
|
+
const gitea = require('./giteaClient.js');
|
|
27
|
+
|
|
28
|
+
const SEVEN_DAYS_MS = 7 * 24 * 3600 * 1000;
|
|
29
|
+
const REDACTED = '[REDACTED]';
|
|
30
|
+
|
|
31
|
+
// ---------------------------------------------------------------- credential
|
|
32
|
+
|
|
33
|
+
/** 凭据缓存:gitKey -> value|null。仅内存,进程退出即消失。 */
|
|
34
|
+
const credCache = new Map();
|
|
35
|
+
|
|
36
|
+
function credentialGitKey(cfg) {
|
|
37
|
+
return `http.${cfg.gitee.baseUrl}/.extraheader`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 读 Gitee 私人令牌(git config --global http.<baseUrl>/.extraheader,值 = 令牌原文)。
|
|
42
|
+
* 纪律与 gitea/github 两 client 一致:execFileSync 直调、不经 shell、不注入 env、注册脱敏。
|
|
43
|
+
* @returns {string|null} 令牌原文,未配置返回 null(公开组织匿名可拉,限流更严)
|
|
44
|
+
*/
|
|
45
|
+
function readCredential(cfg) {
|
|
46
|
+
const gitKey = credentialGitKey(cfg);
|
|
47
|
+
if (credCache.has(gitKey)) return credCache.get(gitKey);
|
|
48
|
+
let value = null;
|
|
49
|
+
try {
|
|
50
|
+
const out = execFileSync('git', ['config', '--global', gitKey], {
|
|
51
|
+
encoding: 'utf8',
|
|
52
|
+
timeout: 5000,
|
|
53
|
+
windowsHide: true
|
|
54
|
+
// 不传 env —— 子进程环境变量里绝不出现凭据
|
|
55
|
+
});
|
|
56
|
+
value = String(out || '').trim() || null;
|
|
57
|
+
} catch {
|
|
58
|
+
value = null;
|
|
59
|
+
}
|
|
60
|
+
if (value) registerSecret(value);
|
|
61
|
+
credCache.set(gitKey, value);
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// -------------------------------------------------------------------- http
|
|
66
|
+
|
|
67
|
+
/** URL 出错误消息前剥掉 access_token 参数(令牌不进日志/状态文件的第一道防线),再过 redact */
|
|
68
|
+
function scrubUrl(url) {
|
|
69
|
+
try {
|
|
70
|
+
const u = new URL(String(url));
|
|
71
|
+
if (u.searchParams.has('access_token')) {
|
|
72
|
+
u.searchParams.set('access_token', REDACTED);
|
|
73
|
+
return u.toString();
|
|
74
|
+
}
|
|
75
|
+
return String(url);
|
|
76
|
+
} catch {
|
|
77
|
+
return String(url);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function fetchJson(url, token, cfg, timeoutMs = 15000) {
|
|
82
|
+
assertSafeUrl(url, cfg); // 出站末级复检(白名单 = gitea ∪ github ∪ gitee 三节)
|
|
83
|
+
const ac = new AbortController();
|
|
84
|
+
const timer = setTimeout(() => ac.abort(), timeoutMs);
|
|
85
|
+
try {
|
|
86
|
+
const res = await fetch(url, {
|
|
87
|
+
method: 'GET',
|
|
88
|
+
headers: { 'User-Agent': 'fde-console-activity-poller' },
|
|
89
|
+
signal: ac.signal,
|
|
90
|
+
redirect: 'error'
|
|
91
|
+
});
|
|
92
|
+
if (!res.ok) {
|
|
93
|
+
let detail = '';
|
|
94
|
+
try { detail = (await res.text()).slice(0, 200); } catch { /* ignore */ }
|
|
95
|
+
const err = new Error(redact(`Gitee HTTP ${res.status} ${res.statusText} ${detail} (${scrubUrl(url)})`));
|
|
96
|
+
err.status = res.status;
|
|
97
|
+
throw err;
|
|
98
|
+
}
|
|
99
|
+
return await res.json();
|
|
100
|
+
} catch (err) {
|
|
101
|
+
if (err instanceof Error && !err.message.includes(REDACTED)) err.message = redact(err.message);
|
|
102
|
+
throw err;
|
|
103
|
+
} finally {
|
|
104
|
+
clearTimeout(timer);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 带 access_token query 参数的事件流 URL(scrubUrl 保证令牌不进任何错误出口) */
|
|
109
|
+
function eventsUrl(cfg, org, page) {
|
|
110
|
+
const base = String(cfg.gitee.baseUrl || 'https://gitee.com').replace(/\/+$/, '');
|
|
111
|
+
const token = readCredential(cfg);
|
|
112
|
+
const u = new URL(`${base}/api/v5/orgs/${encodeURIComponent(org)}/events`);
|
|
113
|
+
u.searchParams.set('per_page', '100');
|
|
114
|
+
u.searchParams.set('page', String(page));
|
|
115
|
+
if (token) u.searchParams.set('access_token', token);
|
|
116
|
+
return u.toString();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// ------------------------------------------------------------------ mapping
|
|
120
|
+
|
|
121
|
+
function firstLine(s) {
|
|
122
|
+
return String(s || '').split('\n')[0].trim();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** 提交说明正文首段(与 gitea/github 两 client 同口径:跳过首行与开头空行,收至下一空行,≤300 字) */
|
|
126
|
+
function messageBodyOf(message) {
|
|
127
|
+
const lines = String(message || '').split('\n').slice(1);
|
|
128
|
+
let i = 0;
|
|
129
|
+
while (i < lines.length && lines[i].trim() === '') i += 1;
|
|
130
|
+
const para = [];
|
|
131
|
+
while (i < lines.length && lines[i].trim() !== '') { para.push(lines[i]); i += 1; }
|
|
132
|
+
const text = para.join('\n').trim();
|
|
133
|
+
return text.length > 300 ? text.slice(0, 300) : text;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function stripRefsPrefix(ref) {
|
|
137
|
+
return String(ref || '').replace(/^refs\/heads\//, '');
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Gitee org events → 活动数组(统一 11 字段,词表与 gitea/github 一致)。
|
|
142
|
+
* @returns {{activities: object[], skippedEventCount: number, skippedEventTypes: string[]}}
|
|
143
|
+
*/
|
|
144
|
+
function mapEvents(events, sinceMs, fetchedAt, webBase) {
|
|
145
|
+
const activities = [];
|
|
146
|
+
const skippedTypes = new Set();
|
|
147
|
+
let skippedEventCount = 0;
|
|
148
|
+
|
|
149
|
+
for (const e of events || []) {
|
|
150
|
+
if (!e) continue;
|
|
151
|
+
const type = String(e.type || '');
|
|
152
|
+
const actor = (e.actor && e.actor.login) || null;
|
|
153
|
+
const repo = (e.repo && (e.repo.full_name || e.repo.name)) || ''; // 探针实证:Gitee 用 full_name
|
|
154
|
+
const created = String(e.created_at || '');
|
|
155
|
+
const createdMs = Date.parse(created);
|
|
156
|
+
if (Number.isFinite(createdMs) && createdMs < sinceMs) continue; // 条目整体落窗外
|
|
157
|
+
|
|
158
|
+
if (type === 'PushEvent') {
|
|
159
|
+
const payload = e.payload || {};
|
|
160
|
+
const branch = stripRefsPrefix(payload.ref) || null;
|
|
161
|
+
const commits = Array.isArray(payload.commits) ? payload.commits : [];
|
|
162
|
+
for (const c of commits) {
|
|
163
|
+
if (!c || !c.sha) continue;
|
|
164
|
+
// 探针实证:Gitee commit 无 timestamp(GitHub 有)→ occurredAt 退事件 created_at
|
|
165
|
+
activities.push({
|
|
166
|
+
type: 'commit',
|
|
167
|
+
actor,
|
|
168
|
+
repo: repo || null,
|
|
169
|
+
branch,
|
|
170
|
+
sha: String(c.sha),
|
|
171
|
+
title: firstLine(c.message),
|
|
172
|
+
messageBody: messageBodyOf(c.message),
|
|
173
|
+
// commits[].url 是 API 地址(/api/v5/...)——动态链接自拼 web 地址
|
|
174
|
+
url: `${webBase}/${repo}/commit/${c.sha}`,
|
|
175
|
+
occurredAt: created,
|
|
176
|
+
fetchedAt
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (type === 'PullRequestEvent') {
|
|
183
|
+
// GitHub 同构容错:Gitee v5 事件类型命名与 GitHub 同族,PR 事件按同款字段口径映射;
|
|
184
|
+
// 字段缺席自然走 skipped 计数,不炸轮询
|
|
185
|
+
const payload = e.payload || {};
|
|
186
|
+
const pr = payload.pull_request || payload;
|
|
187
|
+
const action = String(payload.action || '');
|
|
188
|
+
const opened = action === 'opened' || action === 'reopened';
|
|
189
|
+
const merged = (action === 'closed' && pr.merged === true) || action === 'merged';
|
|
190
|
+
if (opened || merged) {
|
|
191
|
+
const num = payload.number != null ? payload.number : pr.number;
|
|
192
|
+
activities.push({
|
|
193
|
+
type: 'pr',
|
|
194
|
+
actor,
|
|
195
|
+
repo: repo || null,
|
|
196
|
+
branch: null,
|
|
197
|
+
sha: null,
|
|
198
|
+
title: firstLine(pr.title) || (num != null ? `PR #${num}` : 'PR'),
|
|
199
|
+
messageBody: '',
|
|
200
|
+
url: String(pr.html_url || (num != null && repo ? `${webBase}/${repo}/pulls/${num}` : '')),
|
|
201
|
+
occurredAt: created,
|
|
202
|
+
fetchedAt
|
|
203
|
+
});
|
|
204
|
+
} else {
|
|
205
|
+
skippedEventCount += 1;
|
|
206
|
+
skippedTypes.add(`${type}:${action || '?'}`);
|
|
207
|
+
}
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
if (type === 'CreateEvent' || type === 'DeleteEvent') {
|
|
212
|
+
const payload = e.payload || {};
|
|
213
|
+
if (payload.ref_type === 'branch' && payload.ref) {
|
|
214
|
+
const branch = String(payload.ref);
|
|
215
|
+
activities.push({
|
|
216
|
+
type: type === 'CreateEvent' ? 'branch_created' : 'branch_deleted',
|
|
217
|
+
actor,
|
|
218
|
+
repo: repo || null,
|
|
219
|
+
branch,
|
|
220
|
+
sha: null,
|
|
221
|
+
title: branch,
|
|
222
|
+
messageBody: '',
|
|
223
|
+
url: `${webBase}/${repo}/tree/${branch.split('/').map(encodeURIComponent).join('/')}`,
|
|
224
|
+
occurredAt: created,
|
|
225
|
+
fetchedAt
|
|
226
|
+
});
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
skippedEventCount += 1; // ref_type=repository/tag 等
|
|
230
|
+
skippedTypes.add(`${type}:${payload.ref_type || '?'}`);
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
skippedEventCount += 1;
|
|
235
|
+
if (type) skippedTypes.add(type);
|
|
236
|
+
}
|
|
237
|
+
return { activities, skippedEventCount, skippedEventTypes: [...skippedTypes] };
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// -------------------------------------------------------------------- poll
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* 轮询 Gitee 组织事件 → 与 Gitea/GitHub 源共写同一 data/activities.json(mergeActivities 口径一致)。
|
|
244
|
+
* 状态写 data/gitee-status.json(独立文件)。enabled !== true 时零出网零写盘返回 null。
|
|
245
|
+
* @param {object} [cfg] 缺省 gitea.loadConfig()
|
|
246
|
+
* @returns {Promise<object[]|null>} 落盘后的全量 activity 数组(倒序);源未启用返回 null
|
|
247
|
+
* @throws HOST_NOT_ALLOWED / DANGEROUS_WHITELIST_ENTRY / GITEE_ERROR(message 已脱敏)
|
|
248
|
+
*/
|
|
249
|
+
async function pollActivities(cfg) {
|
|
250
|
+
const conf = cfg || gitea.loadConfig();
|
|
251
|
+
const ge = conf.gitee || {};
|
|
252
|
+
if (ge.enabled !== true) return null;
|
|
253
|
+
validateWhitelist(conf);
|
|
254
|
+
|
|
255
|
+
const fetchedAt = new Date().toISOString();
|
|
256
|
+
const sinceMs = Date.now() - SEVEN_DAYS_MS;
|
|
257
|
+
const base = String(ge.baseUrl || 'https://gitee.com').replace(/\/+$/, '');
|
|
258
|
+
const org = String(ge.org || '').trim();
|
|
259
|
+
const pages = Number(ge.eventsPages) > 0 ? Math.min(Number(ge.eventsPages), 10) : 3;
|
|
260
|
+
|
|
261
|
+
let ok = true;
|
|
262
|
+
let lastError = null;
|
|
263
|
+
let rawEvents = [];
|
|
264
|
+
let skippedEventCount = 0;
|
|
265
|
+
const skippedTypeSet = new Set();
|
|
266
|
+
|
|
267
|
+
try {
|
|
268
|
+
if (!org) throw new Error('gitee.org 未配置');
|
|
269
|
+
// fetch 期 conf.github/baseUrl 覆写为 gitee 节口径——assertSafeUrl 白名单按节取,构造时透传整包即可
|
|
270
|
+
for (let page = 1; page <= pages; page++) {
|
|
271
|
+
const url = eventsUrl(conf, org, page);
|
|
272
|
+
const arr = await fetchJson(url, readCredential(conf), conf);
|
|
273
|
+
const list = Array.isArray(arr) ? arr : [];
|
|
274
|
+
const mapped = mapEvents(list, sinceMs, fetchedAt, base);
|
|
275
|
+
rawEvents.push(...mapped.activities);
|
|
276
|
+
skippedEventCount += mapped.skippedEventCount;
|
|
277
|
+
for (const t of mapped.skippedEventTypes) skippedTypeSet.add(t);
|
|
278
|
+
if (list.length < 100) break; // 末页
|
|
279
|
+
}
|
|
280
|
+
} catch (err) {
|
|
281
|
+
ok = false;
|
|
282
|
+
lastError = redact((err && err.message) || String(err));
|
|
283
|
+
if (err && (err.code === 'HOST_NOT_ALLOWED' || err.code === 'DANGEROUS_WHITELIST_ENTRY')) {
|
|
284
|
+
// 守卫错误:状态照记,错误原码上抛(路由映射 400 而非 502)
|
|
285
|
+
writeStatus(conf, { enabled: true, ok: false, lastPollAt: fetchedAt, lastError, skippedEventCount, skippedEventTypes: [...skippedTypeSet] });
|
|
286
|
+
throw err;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// 与 Gitea/GitHub 源同一存储:读现库 → 合并去重(type|url)→ 滚动 ≤maxActivities → 原子写回
|
|
291
|
+
const max = (conf.gitea && conf.gitea.maxActivities) || 500;
|
|
292
|
+
const merged = gitea.mergeActivities(gitea.readExistingActivities(conf), rawEvents, max);
|
|
293
|
+
// v2.9.33 批次 2:activities 写统一出口(存储迁移后经 store.saveActivities 路由)
|
|
294
|
+
require('./store.js').saveActivities({ activitiesPath: gitea.resolveDataPath(conf, 'activities.json') }, merged);
|
|
295
|
+
writeStatus(conf, {
|
|
296
|
+
enabled: true,
|
|
297
|
+
ok,
|
|
298
|
+
lastPollAt: fetchedAt,
|
|
299
|
+
lastError,
|
|
300
|
+
skippedEventCount,
|
|
301
|
+
skippedEventTypes: [...skippedTypeSet]
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
// 归因钩子与 Gitea/GitHub 轮询同款:失败只记日志,不影响已落盘数据
|
|
305
|
+
try {
|
|
306
|
+
require('./attribute.js').attributeAfterPoll(conf);
|
|
307
|
+
} catch (err) {
|
|
308
|
+
console.error('[attribute] Gitee 轮询后归因失败(不影响轮询):', redact((err && err.message) || String(err)));
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
if (!ok) {
|
|
312
|
+
const err = new Error(lastError || 'Gitee 拉取失败');
|
|
313
|
+
err.code = 'GITEE_ERROR';
|
|
314
|
+
throw err;
|
|
315
|
+
}
|
|
316
|
+
return merged;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function writeStatus(cfg, status) {
|
|
320
|
+
try {
|
|
321
|
+
gitea.atomicWrite(gitea.resolveDataPath(cfg, 'gitee-status.json'), JSON.stringify(status, null, 2));
|
|
322
|
+
} catch (err) {
|
|
323
|
+
console.error('[gitee] 状态文件写入失败:', redact((err && err.message) || String(err)));
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* 连接探测(v2.9.19 设置页「测试连接」后端,镜像 githubClient 同名函数):
|
|
329
|
+
* 拉 events 第 1 页 per_page=1(最小流量)。不改状态文件、与 enabled 开关无关。
|
|
330
|
+
* @param {{org?:string, baseUrl?:string}} [override] 表单当前值覆盖(保存前先测)
|
|
331
|
+
* @returns {Promise<{ok:boolean, latencyMs:number, hasToken:boolean, total?:number,
|
|
332
|
+
* latest?:{type:string,repo:string,occurredAt:string}, error?:string, code?:string}>} 永不 throw
|
|
333
|
+
*/
|
|
334
|
+
async function testConnection(cfg, override = {}) {
|
|
335
|
+
const ge = { ...(cfg.gitee || {}) };
|
|
336
|
+
if (override.org !== undefined && String(override.org).trim()) ge.org = String(override.org).trim();
|
|
337
|
+
if (override.baseUrl !== undefined && String(override.baseUrl).trim()) ge.baseUrl = String(override.baseUrl).trim();
|
|
338
|
+
const conf = { ...cfg, gitee: ge };
|
|
339
|
+
const started = Date.now();
|
|
340
|
+
const org = String(ge.org || '').trim();
|
|
341
|
+
if (!org) return { ok: false, latencyMs: 0, hasToken: false, error: 'org 未配置(先填组织名)' };
|
|
342
|
+
try {
|
|
343
|
+
const base = String(ge.baseUrl || 'https://gitee.com').replace(/\/+$/, '');
|
|
344
|
+
const u = new URL(`${base}/api/v5/orgs/${encodeURIComponent(org)}/events`);
|
|
345
|
+
u.searchParams.set('per_page', '1');
|
|
346
|
+
u.searchParams.set('page', '1');
|
|
347
|
+
const token = readCredential(conf);
|
|
348
|
+
if (token) u.searchParams.set('access_token', token);
|
|
349
|
+
const arr = await fetchJson(u.toString(), token, conf);
|
|
350
|
+
const list = Array.isArray(arr) ? arr : [];
|
|
351
|
+
const latest = list[0];
|
|
352
|
+
return {
|
|
353
|
+
ok: true,
|
|
354
|
+
latencyMs: Date.now() - started,
|
|
355
|
+
hasToken: !!token,
|
|
356
|
+
total: list.length,
|
|
357
|
+
latest: latest
|
|
358
|
+
? { type: String(latest.type || ''), repo: (latest.repo && (latest.repo.full_name || latest.repo.name)) || '', occurredAt: String(latest.created_at || '') }
|
|
359
|
+
: null
|
|
360
|
+
};
|
|
361
|
+
} catch (err) {
|
|
362
|
+
return {
|
|
363
|
+
ok: false,
|
|
364
|
+
latencyMs: Date.now() - started,
|
|
365
|
+
hasToken: !!readCredential(conf),
|
|
366
|
+
error: redact((err && err.message) || String(err)),
|
|
367
|
+
code: err && err.code
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
module.exports = { pollActivities, readCredential, mapEvents, eventsUrl, scrubUrl, testConnection };
|