koishi-plugin-githubsth 1.0.1-beta → 1.0.1-test2

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.
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.apply = apply;
4
4
  function apply(ctx) {
5
5
  const logger = ctx.logger('githubsth');
6
+ // Allow . in repo name (e.g. koishi.js) but disallow / inside parts.
7
+ // owner: [\w-]+ (alphanumeric, underscore, dash)
8
+ // repo: [\w-\.]+ (alphanumeric, underscore, dash, dot)
6
9
  const repoRegex = /^[\w-]+\/[\w-\.]+$/;
7
10
  ctx.command('githubsth.trust', '管理信任仓库', { authority: 3 })
8
11
  .alias('gh.trust');
@@ -10,9 +13,11 @@ function apply(ctx) {
10
13
  .action(async ({ session }, repo) => {
11
14
  if (!repo)
12
15
  return '请指定仓库名称 (owner/repo)。';
13
- if (!repoRegex.test(repo))
16
+ if (!repoRegex.test(repo)) {
14
17
  return '仓库名称格式不正确 (应为 owner/repo)。';
18
+ }
15
19
  try {
20
+ logger.debug(`Adding trusted repo: ${repo}`);
16
21
  // Check existence first to avoid UNIQUE constraint error log from driver
17
22
  const existing = await ctx.database.get('github_trusted_repo', { repo });
18
23
  if (existing.length > 0) {
@@ -27,18 +32,17 @@ function apply(ctx) {
27
32
  return `已添加信任仓库: ${repo}`;
28
33
  }
29
34
  catch (e) {
35
+ logger.warn('Failed to add trusted repo:', e);
30
36
  if (e.code === 'SQLITE_CONSTRAINT') {
31
37
  return '该仓库已在信任列表中。';
32
38
  }
33
- logger.warn(e);
34
- return '添加失败,请查看日志。';
39
+ return `添加失败: ${e.message}`;
35
40
  }
36
41
  });
37
42
  ctx.command('githubsth.trust.remove <repo>', '移除信任仓库')
38
43
  .action(async ({ session }, repo) => {
39
44
  if (!repo)
40
45
  return '请指定仓库名称 (owner/repo)。';
41
- // No regex check here needed strictly, but good for consistency
42
46
  const result = await ctx.database.remove('github_trusted_repo', { repo });
43
47
  if (result.matched === 0)
44
48
  return '未找到该信任仓库。';
@@ -35,6 +35,19 @@ var __importStar = (this && this.__importStar) || (function () {
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.apply = apply;
37
37
  const repo = __importStar(require("./repo"));
38
+ const admin = __importStar(require("./admin"));
39
+ const subscribe = __importStar(require("./subscribe"));
38
40
  function apply(ctx, config) {
41
+ console.log('Applying githubsth commands...');
42
+ // Register parent command to show help
43
+ ctx.command('githubsth', 'GitHub 推送通知')
44
+ .action(({ session }) => {
45
+ session?.execute('help githubsth');
46
+ });
39
47
  ctx.plugin(repo, config);
48
+ console.log('githubsth.repo loaded');
49
+ ctx.plugin(admin, config);
50
+ console.log('githubsth.admin loaded');
51
+ ctx.plugin(subscribe, config);
52
+ console.log('githubsth.subscribe loaded');
40
53
  }
@@ -1,3 +1,8 @@
1
1
  import { Context } from 'koishi';
2
2
  import { Config } from '../config';
3
+ declare module 'koishi' {
4
+ interface Context {
5
+ github?: any;
6
+ }
7
+ }
3
8
  export declare function apply(ctx: Context, config: Config): void;
@@ -10,10 +10,28 @@ function apply(ctx, config) {
10
10
  const fullRepo = name || (config.defaultOwner ? `${config.defaultOwner}/${config.defaultRepo}` : name);
11
11
  if (!fullRepo)
12
12
  return session?.text('.specify_repo');
13
- // 在实际实现中,我们将在此处使用 GitHub API。
14
- // 由于我们没有 API 客户端的具体设置细节(例如 Octokit 或适配器内部实现),
15
- // 我们将返回一个占位符,以确认命令结构正常工作。
16
- // TODO: 使用 session.bot 或专用服务实现实际的 GitHub API 调用。
17
- return session?.text('.repo_info', [fullRepo.split('/')[0], fullRepo.split('/')[1] || '?', 'Demo Description', '100']);
13
+ try {
14
+ let data;
15
+ if (ctx.github && typeof ctx.github.request === 'function') {
16
+ // If adapter provides a request method (hypothetical, based on common adapter patterns)
17
+ data = await ctx.github.request('GET /repos/:owner/:repo', {
18
+ owner: fullRepo.split('/')[0],
19
+ repo: fullRepo.split('/')[1]
20
+ });
21
+ }
22
+ else {
23
+ const headers = {
24
+ 'User-Agent': 'Koishi-Plugin-GithubSth'
25
+ };
26
+ data = await ctx.http.get(`https://api.github.com/repos/${fullRepo}`, { headers });
27
+ }
28
+ return session?.text('.repo_info', [data.owner.login, data.name, data.description || '无描述', data.stargazers_count]);
29
+ }
30
+ catch (e) {
31
+ if (e.response?.status === 404) {
32
+ return session?.text('.not_found');
33
+ }
34
+ return session?.text('.error', [e.message]);
35
+ }
18
36
  });
19
37
  }
package/lib/index.d.ts CHANGED
@@ -3,6 +3,7 @@ import { Config } from './config';
3
3
  export declare const name = "githubsth";
4
4
  export declare const inject: {
5
5
  required: string[];
6
+ optional: string[];
6
7
  };
7
8
  export * from './config';
8
9
  export declare function apply(ctx: Context, config: Config): void;
package/lib/index.js CHANGED
@@ -42,15 +42,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
42
42
  exports.inject = exports.name = void 0;
43
43
  exports.apply = apply;
44
44
  const commands = __importStar(require("./commands"));
45
- const admin = __importStar(require("./commands/admin"));
46
- const subscribe = __importStar(require("./commands/subscribe"));
47
45
  const database = __importStar(require("./database"));
48
46
  const zh_CN_1 = __importDefault(require("./locales/zh-CN"));
49
47
  const notifier_1 = require("./services/notifier");
50
48
  const formatter_1 = require("./services/formatter");
51
49
  exports.name = 'githubsth';
52
50
  exports.inject = {
53
- required: ['database', 'github'],
51
+ required: ['database'],
52
+ optional: ['github'],
54
53
  };
55
54
  __exportStar(require("./config"), exports);
56
55
  function apply(ctx, config) {
@@ -62,9 +61,13 @@ function apply(ctx, config) {
62
61
  ctx.plugin(formatter_1.Formatter);
63
62
  ctx.plugin(notifier_1.Notifier, config);
64
63
  // 注册命令
65
- ctx.plugin(commands, config);
66
- ctx.plugin(admin);
67
- ctx.plugin(subscribe);
68
- // 加载成功日志
69
- console.log('githubsth plugin loaded');
64
+ // admin and subscribe are already loaded in commands/index.ts, remove duplicate loading here
65
+ try {
66
+ ctx.plugin(commands, config);
67
+ // 加载成功日志
68
+ console.log('githubsth plugin loaded');
69
+ }
70
+ catch (e) {
71
+ console.error('githubsth plugin failed to load:', e);
72
+ }
70
73
  }
@@ -22,9 +22,23 @@ class Notifier extends koishi_1.Service {
22
22
  this.ctx.on('github/pull_request_review', (payload) => this.handleEvent('pull_request_review', payload));
23
23
  }
24
24
  async handleEvent(event, payload) {
25
- const repoName = payload.repository?.full_name;
26
- if (!repoName)
25
+ let repoName = payload.repository?.full_name;
26
+ // Try to fallback if repoName is missing
27
+ if (!repoName && payload.issue?.repository_url) {
28
+ const parts = payload.issue.repository_url.split('/');
29
+ if (parts.length >= 2) {
30
+ repoName = `${parts[parts.length - 2]}/${parts[parts.length - 1]}`;
31
+ }
32
+ }
33
+ if (!repoName && payload.pull_request?.base?.repo?.full_name) {
34
+ repoName = payload.pull_request.base.repo.full_name;
35
+ }
36
+ if (!repoName) {
37
+ if (this.config.debug) {
38
+ this.ctx.logger('notifier').warn(`Event ${event} missing repository info. Keys: ${Object.keys(payload).join(', ')}`);
39
+ }
27
40
  return;
41
+ }
28
42
  if (this.config.debug) {
29
43
  this.ctx.logger('notifier').info(`Received event ${event} for ${repoName}`);
30
44
  this.ctx.logger('notifier').debug(JSON.stringify(payload, null, 2));
@@ -45,8 +59,15 @@ class Notifier extends koishi_1.Service {
45
59
  return false;
46
60
  return true;
47
61
  });
48
- if (matchedRules.length === 0)
62
+ if (matchedRules.length === 0) {
63
+ if (this.config.debug) {
64
+ this.ctx.logger('notifier').debug(`No matching rules for ${repoName} (event: ${event})`);
65
+ }
49
66
  return;
67
+ }
68
+ if (this.config.debug) {
69
+ this.ctx.logger('notifier').debug(`Found ${matchedRules.length} matching rules for ${repoName}`);
70
+ }
50
71
  let message = null;
51
72
  // Ensure formatter is loaded
52
73
  if (!this.ctx.formatter) {
@@ -85,9 +106,16 @@ class Notifier extends koishi_1.Service {
85
106
  message = this.ctx.formatter.formatPullRequestReview(payload);
86
107
  break;
87
108
  }
88
- if (!message)
109
+ if (!message) {
110
+ if (this.config.debug) {
111
+ this.ctx.logger('notifier').debug(`Formatter returned null for event ${event}`);
112
+ }
89
113
  return;
114
+ }
90
115
  for (const rule of matchedRules) {
116
+ if (this.config.debug) {
117
+ this.ctx.logger('notifier').debug(`Sending message to channel ${rule.channelId} (platform: ${rule.platform || 'any'})`);
118
+ }
91
119
  await this.sendMessage(rule, message);
92
120
  }
93
121
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-githubsth",
3
- "version": "1.0.1-beta",
3
+ "version": "1.0.1-test2",
4
4
  "description": "Github Subscriptions Notifications, push notifications for GitHub subscriptions For koishi",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -39,10 +39,11 @@
39
39
  },
40
40
  "service": {
41
41
  "required": [
42
- "database",
43
- "github"
42
+ "database"
44
43
  ],
45
- "optional": []
44
+ "optional": [
45
+ "github"
46
+ ]
46
47
  }
47
48
  }
48
49
  }