@mastra/github-signals 0.4.1-alpha.0 → 0.5.0-alpha.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,13 @@
1
+ export type GithubAppOwner = {
2
+ login: string;
3
+ type: 'User' | 'Organization';
4
+ };
5
+ export type GithubAppOwnerCommandRunner = (args: readonly string[]) => Promise<{
6
+ stdout: string;
7
+ }>;
8
+ export declare class GithubAppOwnerResolver {
9
+ #private;
10
+ constructor(runGhApi?: GithubAppOwnerCommandRunner);
11
+ getOwner(botLogin: string, isCurrentGeneration?: () => boolean): Promise<GithubAppOwner | undefined>;
12
+ }
13
+ //# sourceMappingURL=github-app-owner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"github-app-owner.d.ts","sourceRoot":"","sources":["../src/github-app-owner.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,cAAc,GAAG;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,GAAG,cAAc,CAAC;CAC/B,CAAC;AAEF,MAAM,MAAM,2BAA2B,GAAG,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,KAAK,OAAO,CAAC;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC;AAqBnG,qBAAa,sBAAsB;;IAIjC,YAAY,QAAQ,GAAE,2BAA6C,EAElE;IAEK,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,mBAAmB,CAAC,EAAE,MAAM,OAAO,GAAG,OAAO,CAAC,cAAc,GAAG,SAAS,CAAC,CA2BzG;CAWF"}
package/dist/index.cjs CHANGED
@@ -30,6 +30,63 @@ let _mastra_core_signals = require("@mastra/core/signals");
30
30
  let _mastra_core_tools = require("@mastra/core/tools");
31
31
  let zod = require("zod");
32
32
  zod = __toESM(zod, 1);
33
+ //#region src/github-app-owner.ts
34
+ const OWNER_CACHE_TTL_MS = 1440 * 60 * 1e3;
35
+ let execFileAsync$1;
36
+ const defaultRunGhApi = async (args) => {
37
+ if (!execFileAsync$1) {
38
+ const { execFile } = await import("child_process");
39
+ execFileAsync$1 = (0, util.promisify)(execFile);
40
+ }
41
+ return execFileAsync$1("gh", args);
42
+ };
43
+ var GithubAppOwnerResolver = class {
44
+ #cache = /* @__PURE__ */ new Map();
45
+ #runGhApi;
46
+ constructor(runGhApi = defaultRunGhApi) {
47
+ this.#runGhApi = runGhApi;
48
+ }
49
+ async getOwner(botLogin, isCurrentGeneration) {
50
+ const appSlug = botLogin.replace(/\[bot\]$/i, "");
51
+ if (!appSlug || isCurrentGeneration && !isCurrentGeneration()) return void 0;
52
+ const cacheKey = appSlug.toLowerCase();
53
+ const cached = this.#cache.get(cacheKey);
54
+ if (cached?.expiresAt && cached.expiresAt > Date.now()) {
55
+ if (isCurrentGeneration && !isCurrentGeneration()) return void 0;
56
+ return cached.owner;
57
+ }
58
+ if (cached) this.#cache.delete(cacheKey);
59
+ try {
60
+ const { stdout } = await this.#runGhApi(["api", `apps/${appSlug}`]);
61
+ const parsed = JSON.parse(stdout);
62
+ const owner = this.#parseOwner(parsed);
63
+ if (!owner || isCurrentGeneration && !isCurrentGeneration()) return void 0;
64
+ this.#cache.set(cacheKey, {
65
+ owner,
66
+ expiresAt: Date.now() + OWNER_CACHE_TTL_MS
67
+ });
68
+ if (isCurrentGeneration && !isCurrentGeneration()) {
69
+ this.#cache.delete(cacheKey);
70
+ return;
71
+ }
72
+ return owner;
73
+ } catch {
74
+ return;
75
+ }
76
+ }
77
+ #parseOwner(value) {
78
+ if (!value || typeof value !== "object" || !("owner" in value)) return void 0;
79
+ const owner = value.owner;
80
+ if (!owner || typeof owner !== "object" || !("login" in owner) || !("type" in owner)) return void 0;
81
+ if (typeof owner.login !== "string" || !owner.login.trim()) return void 0;
82
+ if (owner.type !== "User" && owner.type !== "Organization") return void 0;
83
+ return {
84
+ login: owner.login,
85
+ type: owner.type
86
+ };
87
+ }
88
+ };
89
+ //#endregion
33
90
  //#region src/index.ts
34
91
  let _execFileAsync;
35
92
  async function execFileAsync(file, args, options) {
@@ -664,12 +721,12 @@ var GitcrawlSyncClient = class {
664
721
  join repositories r on r.id=t.repo_id
665
722
  where r.owner=${owner} and r.name=${repo} and t.number=${number} and rt.is_resolved=0`);
666
723
  const latestComments = await this.#queryDb(`select c.author_login, c.author_type, c.is_bot, c.body, json_extract(c.raw_json, '$.html_url') as html_url,
667
- coalesce(c.updated_at_gh, c.created_at_gh) as updated_at
724
+ coalesce(c.updated_at_gh, c.created_at_gh, json_extract(c.raw_json, '$.submitted_at')) as updated_at
668
725
  from comments c
669
726
  join threads t on t.id=c.thread_id
670
727
  join repositories r on r.id=t.repo_id
671
728
  where r.owner=${owner} and r.name=${repo} and t.number=${number}
672
- order by coalesce(c.updated_at_gh, c.created_at_gh) desc
729
+ order by coalesce(c.updated_at_gh, c.created_at_gh, json_extract(c.raw_json, '$.submitted_at')) desc
673
730
  limit 20`);
674
731
  const latestComment = latestComments[0];
675
732
  const checks = normalizeGithubChecksForSnapshot({
@@ -794,6 +851,7 @@ var GithubSignals = class extends _mastra_core_signals.SignalProvider {
794
851
  #options;
795
852
  #syncClient;
796
853
  #repositoryResolver;
854
+ #appOwnerResolver;
797
855
  #polling = /* @__PURE__ */ new Map();
798
856
  #pollingThreadGenerations = /* @__PURE__ */ new Map();
799
857
  #pollingGeneration = 0;
@@ -807,6 +865,7 @@ var GithubSignals = class extends _mastra_core_signals.SignalProvider {
807
865
  this.#options = options;
808
866
  this.#syncClient = options.syncClient ?? new GitcrawlSyncClient({ command: options.gitcrawlCommand });
809
867
  this.#repositoryResolver = options.repositoryResolver ?? new GitRemoteRepositoryResolver();
868
+ this.#appOwnerResolver = new GithubAppOwnerResolver();
810
869
  if (options.getNotificationStreamOptions) this.#agentOptions = { getNotificationStreamOptions: options.getNotificationStreamOptions };
811
870
  }
812
871
  /**
@@ -1425,7 +1484,15 @@ var GithubSignals = class extends _mastra_core_signals.SignalProvider {
1425
1484
  const normalizedUser = user.toLowerCase();
1426
1485
  if (metadata.isBot === true || metadata.authorType?.toLowerCase() === "bot" || normalizedUser.endsWith("[bot]")) {
1427
1486
  if ((this.#options.ignoredBots ?? []).some((bot) => bot.toLowerCase() === normalizedUser)) return false;
1428
- return (this.#options.authorizedBots ?? DEFAULT_AUTHORIZED_BOTS).some((bot) => bot.toLowerCase() === normalizedUser);
1487
+ if ((this.#options.authorizedBots ?? DEFAULT_AUTHORIZED_BOTS).some((bot) => bot.toLowerCase() === normalizedUser)) return true;
1488
+ const appOwner = await this.#appOwnerResolver.getOwner(user, isCurrentGeneration);
1489
+ if (!appOwner || isCurrentGeneration && !isCurrentGeneration()) return false;
1490
+ if (appOwner.type === "Organization") return appOwner.login.toLowerCase() === owner.toLowerCase();
1491
+ if (appOwner.type !== "User") return false;
1492
+ const permission = await this.#loadAuthorPermission(owner, repo, appOwner.login, isCurrentGeneration);
1493
+ if (isCurrentGeneration && !isCurrentGeneration()) return false;
1494
+ const authorizedPermissions = this.#options.authorizedPermissions ?? DEFAULT_AUTHORIZED_PERMISSIONS;
1495
+ return !!permission && authorizedPermissions.includes(permission);
1429
1496
  }
1430
1497
  const permission = await this.#loadAuthorPermission(owner, repo, user, isCurrentGeneration);
1431
1498
  if (isCurrentGeneration && !isCurrentGeneration()) return false;