koishi-plugin-minecraft-notifier 1.5.1 → 1.6.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.
@@ -0,0 +1,64 @@
1
+ import { AxiosResponse } from 'axios';
2
+ import { Context } from 'koishi';
3
+ /**
4
+ * Git 平台类型
5
+ */
6
+ export declare enum GitPlatform {
7
+ GITEE = "gitee",
8
+ GITCODE = "gitcode"
9
+ }
10
+ /**
11
+ * 操作结果类型
12
+ */
13
+ interface OperationResult {
14
+ success: boolean;
15
+ data?: AxiosResponse;
16
+ error?: string;
17
+ }
18
+ /**
19
+ * 文件上传参数
20
+ */
21
+ interface FileUploadParams {
22
+ owner: string;
23
+ repo: string;
24
+ path: string;
25
+ content: string;
26
+ message: string;
27
+ token: string;
28
+ branch?: string;
29
+ }
30
+ /**
31
+ * 上传或更新 Gitee 仓库中的文本文件
32
+ * @param ctx - Koishi 上下文
33
+ * @param owner - 仓库所有者用户名
34
+ * @param repo - 仓库名称
35
+ * @param path - 文件路径(例如 'docs/update.txt')
36
+ * @param content - 文件内容(纯文本字符串,会自动 Base64 编码)
37
+ * @param message - Commit 消息
38
+ * @param token - Gitee Personal Access Token
39
+ * @param branch - 分支名(默认 'master')
40
+ * @returns Promise<OperationResult>
41
+ */
42
+ export declare function upsertFileToGitee(ctx: Context, owner: string, repo: string, path: string, content: string, message: string, token: string, branch?: string): Promise<OperationResult>;
43
+ /**
44
+ * 上传或更新 GitCode 仓库中的文本文件
45
+ * @param ctx - Koishi 上下文
46
+ * @param owner - 仓库所有者用户名
47
+ * @param repo - 仓库名称
48
+ * @param path - 文件路径(例如 'docs/update.txt')
49
+ * @param content - 文件内容(纯文本字符串,会自动 Base64 编码)
50
+ * @param message - Commit 消息
51
+ * @param token - GitCode Personal Access Token
52
+ * @param branch - 分支名(默认 'master')
53
+ * @returns Promise<OperationResult>
54
+ */
55
+ export declare function upsertFileToGitCode(ctx: Context, owner: string, repo: string, path: string, content: string, message: string, token: string, branch?: string): Promise<OperationResult>;
56
+ /**
57
+ * 通用上传函数(可指定平台)
58
+ * @param ctx - Koishi 上下文
59
+ * @param platform - Git 平台类型
60
+ * @param params - 上传参数
61
+ * @returns Promise<OperationResult>
62
+ */
63
+ export declare function upsertFileToGit(ctx: Context, platform: GitPlatform, params: FileUploadParams): Promise<OperationResult>;
64
+ export {};
package/lib/index.cjs CHANGED
@@ -304,58 +304,45 @@ var import_autocorrect_node = require("autocorrect-node");
304
304
  var import_node_fs = require("node:fs");
305
305
  var import_node_path = __toESM(require("node:path"), 1);
306
306
 
307
- // src/gitee-helper.ts
307
+ // src/git-platform-helper.ts
308
308
  var import_axios2 = __toESM(require("axios"), 1);
309
- async function upsertFileToGitee(ctx, owner, repo, path3, content, message, token, branch = "master") {
309
+ var PLATFORM_CONFIG = {
310
+ ["gitee" /* GITEE */]: {
311
+ baseUrl: "https://gitee.com/api/v5",
312
+ name: "Gitee"
313
+ },
314
+ ["gitcode" /* GITCODE */]: {
315
+ baseUrl: "https://api.gitcode.com/api/v5",
316
+ name: "GitCode"
317
+ }
318
+ };
319
+ async function upsertFileToGitPlatform(ctx, platform, params) {
320
+ const {
321
+ owner,
322
+ repo,
323
+ path: path3,
324
+ content,
325
+ message,
326
+ token,
327
+ branch = "master"
328
+ } = params;
329
+ const config = PLATFORM_CONFIG[platform];
330
+ const loggerName = `${platform}-uploader`;
310
331
  const base64Content = Buffer.from(content, "utf-8").toString("base64");
311
- const checkUrl = `https://gitee.com/api/v5/repos/${owner}/${repo}/contents/${path3}?ref=${branch}&access_token=${token}`;
332
+ const checkUrl = `${config.baseUrl}/repos/${owner}/${repo}/contents/${path3}`;
312
333
  let fileSha = null;
313
334
  try {
314
- const checkResponse = await import_axios2.default.get(checkUrl);
315
- if (checkResponse.status === 200) {
316
- if (Array.isArray(checkResponse.data) && checkResponse.data.length === 0) {
317
- } else if (checkResponse.data.sha) {
318
- fileSha = checkResponse.data.sha;
319
- } else {
320
- }
321
- } else {
322
- ctx.logger("minecraft-notifier").warn(
323
- `Unexpected status code when checking file: ${checkResponse.status}`
324
- );
325
- throw new Error(`Unexpected status: ${checkResponse.status}`);
335
+ const checkResponse = await import_axios2.default.get(checkUrl, {
336
+ headers: { Authorization: `Bearer ${token}` },
337
+ params: { ref: branch, access_token: token }
338
+ });
339
+ if (checkResponse.status === 200 && checkResponse.data?.sha) {
340
+ fileSha = checkResponse.data.sha;
326
341
  }
327
342
  } catch (checkError) {
328
- if (checkError.response?.status === 404) {
329
- const createUrl2 = `https://gitee.com/api/v5/repos/${owner}/${repo}/contents/${path3}`;
330
- try {
331
- const createResponse = await import_axios2.default.post(
332
- createUrl2,
333
- {
334
- access_token: token,
335
- content: base64Content,
336
- message,
337
- branch
338
- },
339
- {
340
- headers: {
341
- "Content-Type": "application/json"
342
- }
343
- }
344
- );
345
- return { success: true, data: createResponse };
346
- } catch (createError) {
347
- ctx.logger("minecraft-notifier").warn(
348
- "Gitee Create Error:",
349
- createError.response?.data || createError.message
350
- );
351
- return {
352
- success: false,
353
- error: createError.response?.data?.message || createError.message
354
- };
355
- }
356
- } else {
357
- ctx.logger("minecraft-notifier").warn(
358
- "Gitee Check Error:",
343
+ if (checkError.response?.status !== 404) {
344
+ ctx.logger(loggerName).warn(
345
+ `${config.name} Check Error:`,
359
346
  checkError.response?.data || checkError.message
360
347
  );
361
348
  return {
@@ -364,64 +351,62 @@ async function upsertFileToGitee(ctx, owner, repo, path3, content, message, toke
364
351
  };
365
352
  }
366
353
  }
367
- if (fileSha) {
368
- const updateUrl = `https://gitee.com/api/v5/repos/${owner}/${repo}/contents/${path3}`;
369
- try {
370
- const updateResponse = await import_axios2.default.put(
371
- updateUrl,
372
- {
373
- access_token: token,
374
- content: base64Content,
375
- sha: fileSha,
376
- message,
377
- branch
378
- },
379
- {
380
- headers: {
381
- "Content-Type": "application/json"
382
- }
383
- }
384
- );
385
- return { success: true, data: updateResponse };
386
- } catch (updateError) {
387
- ctx.logger("minecraft-notifier").warn(
388
- "Gitee Update Error:",
389
- updateError.response?.data || updateError.message
390
- );
391
- return {
392
- success: false,
393
- error: updateError.response?.data?.message || updateError.message
394
- };
395
- }
396
- }
397
- const createUrl = `https://gitee.com/api/v5/repos/${owner}/${repo}/contents/${path3}`;
354
+ const apiUrl = `${config.baseUrl}/repos/${owner}/${repo}/contents/${path3}`;
355
+ const requestBody = {
356
+ access_token: token,
357
+ content: base64Content,
358
+ message,
359
+ branch,
360
+ ...fileSha && { sha: fileSha }
361
+ // 如果存在 SHA,则添加到请求体
362
+ };
398
363
  try {
399
- const createResponse = await import_axios2.default.post(
400
- createUrl,
401
- {
402
- access_token: token,
403
- content: base64Content,
404
- message,
405
- branch
406
- },
407
- {
408
- headers: {
409
- "Content-Type": "application/json"
410
- }
364
+ const response = fileSha ? await import_axios2.default.put(apiUrl, requestBody, {
365
+ headers: {
366
+ "Content-Type": "application/json",
367
+ Authorization: `Bearer ${token}`
411
368
  }
412
- );
413
- return { success: true, data: createResponse };
414
- } catch (createError) {
415
- ctx.logger("minecraft-notifier").warn(
416
- "Gitee Create Error:",
417
- createError.response?.data || createError.message
369
+ }) : await import_axios2.default.post(apiUrl, requestBody, {
370
+ headers: {
371
+ "Content-Type": "application/json",
372
+ Authorization: `Bearer ${token}`
373
+ }
374
+ });
375
+ return { success: true, data: response };
376
+ } catch (error) {
377
+ const operation = fileSha ? "Update" : "Create";
378
+ ctx.logger(loggerName).warn(
379
+ `${config.name} ${operation} Error:`,
380
+ error.response?.data || error.message
418
381
  );
419
382
  return {
420
383
  success: false,
421
- error: createError.response?.data?.message || createError.message
384
+ error: error.response?.data?.message || error.message
422
385
  };
423
386
  }
424
387
  }
388
+ async function upsertFileToGitee(ctx, owner, repo, path3, content, message, token, branch = "master") {
389
+ return upsertFileToGitPlatform(ctx, "gitee" /* GITEE */, {
390
+ owner,
391
+ repo,
392
+ path: path3,
393
+ content,
394
+ message,
395
+ token,
396
+ branch
397
+ });
398
+ }
399
+ async function upsertFileToGitCode(ctx, owner, repo, path3, content, message, token, branch = "master") {
400
+ return upsertFileToGitPlatform(ctx, "gitcode" /* GITCODE */, {
401
+ owner,
402
+ repo,
403
+ path: path3,
404
+ content,
405
+ message,
406
+ token,
407
+ branch
408
+ });
409
+ }
425
410
 
426
411
  // src/xaml-generator.ts
427
412
  function generateXaml(summary, version) {
@@ -572,10 +557,12 @@ async function exportXaml(ctx, cfg, summary, version) {
572
557
  "master"
573
558
  ).then((result) => {
574
559
  if (result.success) {
575
- ctx.logger("minecraft-notifier").info("Upsert successful");
560
+ ctx.logger("minecraft-notifier").info(
561
+ "Upsert successful of gitee."
562
+ );
576
563
  } else {
577
564
  ctx.logger("minecraft-notifier").warn(
578
- "Upsert failed:",
565
+ "Upsert failed of gitee:",
579
566
  result.error
580
567
  );
581
568
  }
@@ -591,6 +578,39 @@ async function exportXaml(ctx, cfg, summary, version) {
591
578
  "master"
592
579
  );
593
580
  }
581
+ if (cfg.gitcodeApiToken && cfg.gitcodeOwner && cfg.gitcodeRepo) {
582
+ await upsertFileToGitCode(
583
+ ctx,
584
+ cfg.gitcodeOwner,
585
+ cfg.gitcodeRepo,
586
+ "Custom.xaml",
587
+ xaml,
588
+ `feat: update PCL HomePage XAML for version ${version}`,
589
+ cfg.gitcodeApiToken,
590
+ "master"
591
+ ).then((result) => {
592
+ if (result.success) {
593
+ ctx.logger("minecraft-notifier").info(
594
+ "Upsert successful of gitcode."
595
+ );
596
+ } else {
597
+ ctx.logger("minecraft-notifier").warn(
598
+ "Upsert failed of gitcode:",
599
+ result.error
600
+ );
601
+ }
602
+ });
603
+ await upsertFileToGitCode(
604
+ ctx,
605
+ cfg.gitcodeOwner,
606
+ cfg.gitcodeRepo,
607
+ "Custom.xaml.ini",
608
+ version,
609
+ `feat: update PCL HomePage XAML INI for version ${version}`,
610
+ cfg.gitcodeApiToken,
611
+ "master"
612
+ );
613
+ }
594
614
  return fullXamlPath;
595
615
  }
596
616
 
@@ -912,7 +932,10 @@ var Config = import_koishi.Schema.object({
912
932
  notifyChannel: import_koishi.Schema.array(String).default([]).description("\u7528\u4E8E\u63A5\u6536\u66F4\u65B0\u901A\u77E5\u7684\u9891\u9053 ID \u5217\u8868"),
913
933
  giteeApiToken: import_koishi.Schema.string().role("secret").default("").description("Gitee API \u8BBF\u95EE\u4EE4\u724C\uFF0C\u7528\u4E8E\u4E0A\u4F20 XAML \u6587\u4EF6"),
914
934
  giteeOwner: import_koishi.Schema.string().default("").description("Gitee \u4ED3\u5E93\u6240\u6709\u8005\u7528\u6237\u540D"),
915
- giteeRepo: import_koishi.Schema.string().default("").description("Gitee \u4ED3\u5E93\u540D\u79F0")
935
+ giteeRepo: import_koishi.Schema.string().default("").description("Gitee \u4ED3\u5E93\u540D\u79F0"),
936
+ gitcodeApiToken: import_koishi.Schema.string().role("secret").default("").description("GitCode API \u8BBF\u95EE\u4EE4\u724C\uFF0C\u7528\u4E8E\u4E0A\u4F20 XAML \u6587\u4EF6"),
937
+ gitcodeOwner: import_koishi.Schema.string().default("").description("GitCode \u4ED3\u5E93\u6240\u6709\u8005\u7528\u6237\u540D"),
938
+ gitcodeRepo: import_koishi.Schema.string().default("").description("GitCode \u4ED3\u5E93\u540D\u79F0")
916
939
  });
917
940
  function apply(ctx, cfg) {
918
941
  ctx.database.extend(
@@ -1014,6 +1037,20 @@ function apply(ctx, cfg) {
1014
1037
  await checkNewVersionArticle(ctx, cfg);
1015
1038
  }
1016
1039
  );
1040
+ ctx.command("mc.trigger.gitcode", "\u624B\u52A8\u89E6\u53D1 AI \u66F4\u65B0\u65E5\u5FD7\u603B\u7ED3\u751F\u6210").action(
1041
+ async () => {
1042
+ await upsertFileToGitCode(
1043
+ ctx,
1044
+ cfg.gitcodeOwner,
1045
+ cfg.gitcodeRepo,
1046
+ "Custom.xaml.ini",
1047
+ "25w43a",
1048
+ `feat: update PCL HomePage XAML INI for version 25w43a`,
1049
+ cfg.gitcodeApiToken,
1050
+ "master"
1051
+ );
1052
+ }
1053
+ );
1017
1054
  ctx.setInterval(async () => {
1018
1055
  try {
1019
1056
  await loadData();
package/lib/index.d.ts CHANGED
@@ -37,6 +37,9 @@ export interface Config {
37
37
  giteeApiToken?: string;
38
38
  giteeOwner?: string;
39
39
  giteeRepo?: string;
40
+ gitcodeApiToken?: string;
41
+ gitcodeOwner?: string;
42
+ gitcodeRepo?: string;
40
43
  }
41
44
  export declare const Config: Schema<Config>;
42
45
  export declare function apply(ctx: Context, cfg: Config & {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-minecraft-notifier",
3
3
  "description": "A Minecraft new version notification plugin, also featuring a PCL homepage.",
4
- "version": "1.5.1",
4
+ "version": "1.6.0",
5
5
  "main": "lib/index.cjs",
6
6
  "typings": "lib/index.d.ts",
7
7
  "type": "module",
@@ -1,19 +0,0 @@
1
- import { AxiosResponse } from 'axios';
2
- import { Context } from 'koishi';
3
- /**
4
- * 上传或更新 Gitee 仓库中的文本文件(如果不存在则创建,如果存在则更新)
5
- * @param ctx
6
- * @param owner - 仓库所有者用户名
7
- * @param repo - 仓库名称
8
- * @param path - 文件路径(例如 'docs/update.txt')
9
- * @param content - 文件内容(纯文本字符串,会自动 Base64 编码)
10
- * @param message - Commit 消息
11
- * @param token - Gitee Personal Access Token
12
- * @param branch - 分支名(默认 'master')
13
- * @returns Promise<{ success: boolean; data?: AxiosResponse; error?: string }>
14
- */
15
- export declare function upsertFileToGitee(ctx: Context, owner: string, repo: string, path: string, content: string, message: string, token: string, branch?: string): Promise<{
16
- success: boolean;
17
- data?: AxiosResponse;
18
- error?: string;
19
- }>;