/ opencodehub / src / lib / plugins.ts
swadhinbiswas feat: OpenCodeHub v1.1.0 - Enterprise Features & Storage Adapters · 2/15/2026
719a1cb
373 lines · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/**
 * OpenCodeHub Plugin API
 *
 * Defines the interface for creating plugins
 */

import type { APIContext } from "astro";
import { logger } from "@/lib/logger";

// Plugin configuration
export interface PluginConfig {
  name: string;
  version: string;
  description?: string;
  author?: string;
  homepage?: string;
  repository?: string;

  // Plugin capabilities
  hooks?: PluginHooks;
  routes?: PluginRoute[];
  components?: PluginComponent[];
  settings?: PluginSetting[];
  commands?: PluginCommand[];
}

// Event hooks
export interface PluginHooks {
  // Repository events
  "repo:create"?: (event: RepoCreateEvent) => Promise<void>;
  "repo:delete"?: (event: RepoDeleteEvent) => Promise<void>;
  "repo:push"?: (event: RepoPushEvent) => Promise<void>;
  "repo:fork"?: (event: RepoForkEvent) => Promise<void>;

  // Issue events
  "issue:create"?: (event: IssueCreateEvent) => Promise<void>;
  "issue:update"?: (event: IssueUpdateEvent) => Promise<void>;
  "issue:close"?: (event: IssueCloseEvent) => Promise<void>;
  "issue:comment"?: (event: IssueCommentEvent) => Promise<void>;

  // Pull request events
  "pr:create"?: (event: PRCreateEvent) => Promise<void>;
  "pr:update"?: (event: PRUpdateEvent) => Promise<void>;
  "pr:merge"?: (event: PRMergeEvent) => Promise<void>;
  "pr:close"?: (event: PRCloseEvent) => Promise<void>;
  "pr:review"?: (event: PRReviewEvent) => Promise<void>;

  // User events
  "user:register"?: (event: UserRegisterEvent) => Promise<void>;
  "user:login"?: (event: UserLoginEvent) => Promise<void>;

  // Pipeline events
  "pipeline:start"?: (event: PipelineStartEvent) => Promise<void>;
  "pipeline:complete"?: (event: PipelineCompleteEvent) => Promise<void>;
  "pipeline:fail"?: (event: PipelineFailEvent) => Promise<void>;

  // Webhook events
  "webhook:receive"?: (event: WebhookReceiveEvent) => Promise<void>;
}

// Event types
export interface BaseEvent {
  timestamp: Date;
  actor: { id: string; username: string };
}

export interface RepoCreateEvent extends BaseEvent {
  repository: { id: string; owner: string; name: string };
}

export interface RepoDeleteEvent extends BaseEvent {
  repository: { id: string; owner: string; name: string };
}

export interface RepoPushEvent extends BaseEvent {
  repository: { id: string; owner: string; name: string };
  ref: string;
  before: string;
  after: string;
  commits: Array<{
    id: string;
    message: string;
    author: { name: string; email: string };
    timestamp: Date;
  }>;
}

export interface RepoForkEvent extends BaseEvent {
  repository: { id: string; owner: string; name: string };
  forkRepository: { id: string; owner: string; name: string };
}

export interface IssueCreateEvent extends BaseEvent {
  repository: { id: string; owner: string; name: string };
  issue: { id: string; number: number; title: string };
}

export interface IssueUpdateEvent extends BaseEvent {
  repository: { id: string; owner: string; name: string };
  issue: { id: string; number: number; title: string };
  changes: Record<string, { from: any; to: any }>;
}

export interface IssueCloseEvent extends BaseEvent {
  repository: { id: string; owner: string; name: string };
  issue: { id: string; number: number; title: string };
}

export interface IssueCommentEvent extends BaseEvent {
  repository: { id: string; owner: string; name: string };
  issue: { id: string; number: number; title: string };
  comment: { id: string; body: string };
}

export interface PRCreateEvent extends BaseEvent {
  repository: { id: string; owner: string; name: string };
  pullRequest: { id: string; number: number; title: string };
}

export interface PRUpdateEvent extends BaseEvent {
  repository: { id: string; owner: string; name: string };
  pullRequest: { id: string; number: number; title: string };
  changes: Record<string, { from: any; to: any }>;
}

export interface PRMergeEvent extends BaseEvent {
  repository: { id: string; owner: string; name: string };
  pullRequest: { id: string; number: number; title: string };
  mergeCommit: string;
}

export interface PRCloseEvent extends BaseEvent {
  repository: { id: string; owner: string; name: string };
  pullRequest: { id: string; number: number; title: string };
}

export interface PRReviewEvent extends BaseEvent {
  repository: { id: string; owner: string; name: string };
  pullRequest: { id: string; number: number; title: string };
  review: { id: string; state: "approved" | "changes_requested" | "commented" };
}

export interface UserRegisterEvent extends BaseEvent {
  user: { id: string; username: string; email: string };
}

export interface UserLoginEvent extends BaseEvent {
  user: { id: string; username: string };
}

export interface PipelineStartEvent extends BaseEvent {
  repository: { id: string; owner: string; name: string };
  pipeline: { id: string; workflow: string };
}

export interface PipelineCompleteEvent extends BaseEvent {
  repository: { id: string; owner: string; name: string };
  pipeline: { id: string; workflow: string; conclusion: "success" | "failure" };
}

export interface PipelineFailEvent extends BaseEvent {
  repository: { id: string; owner: string; name: string };
  pipeline: { id: string; workflow: string; error: string };
}

export interface WebhookReceiveEvent extends BaseEvent {
  webhook: { id: string; url: string };
  payload: any;
}

// Plugin routes
export interface PluginRoute {
  path: string;
  method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
  handler: (context: APIContext) => Promise<Response>;
  middleware?: Array<
    (context: APIContext, next: () => Promise<Response>) => Promise<Response>
  >;
}

// Plugin components
export interface PluginComponent {
  name: string;
  slot:
  | "repo-header"
  | "repo-sidebar"
  | "issue-sidebar"
  | "pr-sidebar"
  | "settings-tab"
  | "user-profile";
  component: React.ComponentType<any>;
}

// Plugin settings
export interface PluginSetting {
  key: string;
  type: "string" | "number" | "boolean" | "select" | "multiselect";
  label: string;
  description?: string;
  default?: any;
  options?: Array<{ label: string; value: any }>;
  required?: boolean;
  secret?: boolean;
}

// Plugin commands
export interface PluginCommand {
  name: string;
  description: string;
  handler: (args: string[], context: CommandContext) => Promise<void>;
}

export interface CommandContext {
  user: { id: string; username: string };
  repository?: { id: string; owner: string; name: string };
  output: (message: string) => void;
}

// Plugin context provided to all plugin handlers
export interface PluginContext {
  db: any; // Database connection
  storage: any; // Storage adapter
  config: Record<string, any>; // Plugin settings
  logger: {
    debug: (message: string, ...args: any[]) => void;
    info: (message: string, ...args: any[]) => void;
    warn: (message: string, ...args: any[]) => void;
    error: (message: string, ...args: any[]) => void;
  };
  api: {
    repos: any;
    issues: any;
    users: any;
    // ... other API modules
  };
}

// Helper function to define a plugin
export function definePlugin(config: PluginConfig): PluginConfig {
  return config;
}

// Plugin manager
export class PluginManager {
  private plugins: Map<string, PluginConfig> = new Map();
  private hooks: Map<string, Array<{ plugin: string; handler: Function }>> =
    new Map();

  async loadPlugin(pluginPath: string): Promise<void> {
    const module = await import(pluginPath);
    const config: PluginConfig = module.default;

    if (!config.name || !config.version) {
      throw new Error(`Invalid plugin: ${pluginPath}`);
    }

    this.plugins.set(config.name, config);

    // Register hooks
    if (config.hooks) {
      for (const [event, handler] of Object.entries(config.hooks)) {
        if (!this.hooks.has(event)) {
          this.hooks.set(event, []);
        }
        this.hooks.get(event)!.push({ plugin: config.name, handler });
      }
    }

    logger.info({ name: config.name, version: config.version }, "Plugin loaded");
  }

  async unloadPlugin(name: string): Promise<void> {
    const config = this.plugins.get(name);
    if (!config) return;

    // Remove hooks
    for (const [event, handlers] of this.hooks) {
      this.hooks.set(
        event,
        handlers.filter((h) => h.plugin !== name)
      );
    }

    this.plugins.delete(name);
    logger.info({ name }, "Plugin unloaded");
  }

  async emit<T extends keyof PluginHooks>(
    event: T,
    data: Parameters<NonNullable<PluginHooks[T]>>[0]
  ): Promise<void> {
    const handlers = this.hooks.get(event) || [];

    for (const { plugin, handler } of handlers) {
      try {
        await handler(data);
      } catch (error) {
        logger.error({ err: error, plugin, event }, "Plugin error");
      }
    }
  }

  getPlugin(name: string): PluginConfig | undefined {
    return this.plugins.get(name);
  }

  getAllPlugins(): PluginConfig[] {
    return Array.from(this.plugins.values());
  }

  getPluginRoutes(): PluginRoute[] {
    const routes: PluginRoute[] = [];
    for (const plugin of this.plugins.values()) {
      if (plugin.routes) {
        routes.push(...plugin.routes);
      }
    }
    return routes;
  }

  getPluginComponents(slot: PluginComponent["slot"]): PluginComponent[] {
    const components: PluginComponent[] = [];
    for (const plugin of this.plugins.values()) {
      if (plugin.components) {
        components.push(...plugin.components.filter((c) => c.slot === slot));
      }
    }
    return components;
  }

  /**
   * Load all plugins from a directory
   */
  async loadPluginsFromDirectory(directory: string): Promise<void> {
    try {
      const fs = await import("fs/promises");
      const path = await import("path");

      // Check if directory exists
      try {
        await fs.access(directory);
      } catch {
        logger.warn({ directory }, "Plugin directory does not exist, skipping");
        return;
      }

      const entries = await fs.readdir(directory, { withFileTypes: true });

      for (const entry of entries) {
        if (entry.isDirectory() || (entry.isFile() && (entry.name.endsWith(".js") || entry.name.endsWith(".mjs")))) {
          const pluginPath = path.join(directory, entry.name);
          try {
            await this.loadPlugin(pluginPath);
          } catch (error) {
            logger.error({ error, pluginPath }, "Failed to load plugin");
          }
        }
      }
    } catch (error) {
      logger.error({ error, directory }, "Failed to scan plugin directory");
    }
  }
}

// Global plugin manager instance
export const pluginManager = new PluginManager();

export default {
  definePlugin,
  PluginManager,
  pluginManager,
};