Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

13.1 KB
Raw Blame
import { BRANDING_NAME } from '@lobechat/business-const';
import debug from 'debug';

import { onChangeRequestSettled } from '@/business/server/scm/onChangeRequestSettled';
import { isFailingCheck, ScmChangeRequestModel, ScmInstallationModel } from '@/database/models/scm';
import type { ScmChangeRequestItem, ScmInstallationItem } from '@/database/schemas';
import type { LobeChatDatabase } from '@/database/type';

import { resolveChangeRequestLinks, resolveChangeRequestOwner } from './links';
import type { ScmInboundEvent, ScmIngestOutcome } from './types';

const log = debug('lobe-server:scm:ingest');

/**
 * Apply one normalized provider event to the database. This is the whole
 * "state" half of the closed loop: installations, change requests, CI
 * rollups and review decisions end up in `scm_*` tables. The "control"
 * half (accept the acceptance on merge, wake the agent on CI failure) hangs
 * off {@link ScmIngestService.onChangeRequestEvent} and ships separately.
 *
 * Every branch is idempotent: replaying a delivery converges to the same
 * rows, which is what lets the delivery ledger reject duplicates cheaply
 * instead of guarding each write.
 */
export class ScmIngestService {
  constructor(private db: LobeChatDatabase) {}

  /**
   * Hook for the control half: called after a change-request-level event
   * has been persisted, with the row it landed on. Default is a no-op.
   */
  onChangeRequestEvent: (params: {
    event: Extract<ScmInboundEvent, { type: 'change_request' | 'checks' | 'review' }>;
    kind: string;
    row: ScmChangeRequestItem;
  }) => Promise<void> = async () => {};

  apply = async (event: ScmInboundEvent): Promise<ScmIngestOutcome> => {
    switch (event.type) {
      case 'ignored': {
        return { detail: event.reason, status: 'skipped' };
      }
      case 'installation': {
        return this.applyInstallation(event);
      }
      case 'installation_repositories': {
        return this.applyInstallationRepositories(event);
      }
      case 'change_request': {
        return this.applyChangeRequest(event);
      }
      case 'checks': {
        return this.applyChecks(event);
      }
      case 'review': {
        return this.applyReview(event);
      }
    }
  };

  // --------------- Installations ---------------

  /**
   * Webhook-side installation lifecycle. A `created` event for an
   * installation LobeHub has never bound is *not* inserted: the row needs a
   * LobeHub user, which only the install callback knows. Until that
   * callback runs, events for the installation are skipped as unbound.
   */
  private applyInstallation = async (
    event: Extract<ScmInboundEvent, { type: 'installation' }>,
  ): Promise<ScmIngestOutcome> => {
    const { installation, action } = event;
    const existing = await ScmInstallationModel.findByProviderInstallationId(
      this.db,
      installation.provider,
      installation.installationId,
    );

    if (!existing) {
      return {
        detail: `installation ${installation.installationId} is not bound to a ${BRANDING_NAME} scope`,
        status: 'skipped',
      };
    }

    switch (action) {
      case 'deleted': {
        await ScmInstallationModel.markRevoked(this.db, existing.id);
        break;
      }
      case 'suspend': {
        await ScmInstallationModel.setSuspended(this.db, existing.id, true);
        break;
      }
      case 'unsuspend': {
        await ScmInstallationModel.setSuspended(this.db, existing.id, false);
        break;
      }
      case 'created':
      case 'new_permissions_accepted': {
        await ScmInstallationModel.refreshSnapshot(this.db, existing.id, {
          accountExternalId: installation.accountExternalId,
          accountLogin: installation.accountLogin,
          accountType: installation.accountType,
          metadata: { ...existing.metadata, ...installation.metadata },
          repositories: installation.repositories?.length
            ? installation.repositories
            : existing.repositories,
          repositorySelection: installation.repositorySelection,
          suspendedAt: installation.suspendedAt ?? null,
        });
        break;
      }
    }

    return { detail: `installation ${existing.id} ${action}`, status: 'processed' };
  };

  private applyInstallationRepositories = async (
    event: Extract<ScmInboundEvent, { type: 'installation_repositories' }>,
  ): Promise<ScmIngestOutcome> => {
    const existing = await ScmInstallationModel.findByProviderInstallationId(
      this.db,
      'github',
      event.installationId,
    );
    if (!existing) {
      return { detail: `installation ${event.installationId} is not bound`, status: 'skipped' };
    }

    await ScmInstallationModel.applyRepositoryChange(this.db, existing.id, {
      added: event.added,
      removed: event.removed,
    });
    return {
      detail: `installation ${existing.id}: +${event.added.length} -${event.removed.length}`,
      status: 'processed',
    };
  };

  // --------------- Change requests ---------------

  private resolveBoundInstallation = async (
    installationId: string,
  ): Promise<ScmInstallationItem | null> => {
    const installation = await ScmInstallationModel.findByProviderInstallationId(
      this.db,
      'github',
      installationId,
    );
    if (!installation || installation.revokedAt) return null;
    return installation;
  };

  private applyChangeRequest = async (
    event: Extract<ScmInboundEvent, { type: 'change_request' }>,
  ): Promise<ScmIngestOutcome> => {
    const installation = await this.resolveBoundInstallation(event.installationId);
    if (!installation) {
      return { detail: `installation ${event.installationId} is not bound`, status: 'skipped' };
    }

    // The installation says which repositories we listen to; it does not say
    // whose work a pull request is. An agent that opened one is not required
    // to live in the workspace the installation is bound to, so the owner
    // comes from the author the provider named.
    const scope = await resolveChangeRequestOwner(this.db, {
      authorExternalId: event.changeRequest.authorExternalId,
      installation,
      provider: 'github',
    });
    const { links, workspaceId } = await resolveChangeRequestLinks(this.db, {
      body: event.body,
      number: event.changeRequest.number,
      repoFullName: event.changeRequest.repoFullName,
      scope,
      url: event.changeRequest.url,
    });

    // The row follows the records it matched, so the conversation is looked
    // up where it actually lives.
    const matched = Boolean(links.topicId || links.acceptanceId || links.workId);

    // A delivery that resolves nothing — the acceptance link was edited out
    // of the body, say — carries less context, not a new owner. The owner
    // below then only seeds a new row; `keepOwner` leaves an existing one
    // where it was, decided under the row lock so a concurrent delivery
    // that did match cannot be undone by one that did not.
    const owner =
      scope.kind === 'author'
        ? { userId: scope.userId, workspaceId: matched ? workspaceId : installation.workspaceId }
        : { userId: scope.userId, workspaceId: scope.workspaceId ?? null };

    const row = await ScmChangeRequestModel.upsert(this.db, {
      ...event.changeRequest,
      eventAt: event.occurredAt,
      eventKind: event.kind,
      links: { ...links, installationId: installation.id },
      keepOwner: !matched,
      metadata: { ...event.changeRequest.metadata, routedBy: scope.kind },
      userId: owner.userId,
      workspaceId: owner.workspaceId,
    });

    log(
      '%s %s#%d -> %s (owner=%s/%s via %s, acceptance=%s topic=%s)',
      event.kind,
      row.repoFullName,
      row.number,
      row.id,
      owner.userId,
      owner.workspaceId ?? 'personal',
      scope.kind,
      row.acceptanceId ?? '-',
      row.topicId ?? '-',
    );

    await this.onChangeRequestEvent({ event, kind: event.kind, row });
    await this.notifySettled(row);
    return { detail: `${row.id} ${event.kind}`, status: 'processed' };
  };

  /**
   * Merged / closed follow-up through the `@/business` slot (default impl is
   * a no-op). A failure is logged and never fails the ingest.
   */
  private notifySettled = async (row: ScmChangeRequestItem) => {
    if (row.state !== 'merged' && row.state !== 'closed') return;
    try {
      await onChangeRequestSettled({
        changeRequestId: row.id,
        settledAt: (row.state === 'merged' ? row.mergedAt : row.closedAt) ?? new Date(),
        state: row.state,
        userId: row.userId,
        workId: row.workId,
        workspaceId: row.workspaceId,
      });
    } catch (error) {
      console.error('[ScmIngestService] change-request settled follow-up failed:', error);
    }
  };

  private applyChecks = async (
    event: Extract<ScmInboundEvent, { type: 'checks' }>,
  ): Promise<ScmIngestOutcome> => {
    const installation = await this.resolveBoundInstallation(event.installationId);
    if (!installation) {
      return { detail: `installation ${event.installationId} is not bound`, status: 'skipped' };
    }

    // Resolve target rows: by number when the provider attached them, else by head sha.
    const rows: ScmChangeRequestItem[] = [];
    for (const number of event.numbers) {
      const row = await ScmChangeRequestModel.findByIdentity(
        this.db,
        'github',
        event.repoFullName,
        number,
      );
      if (row) rows.push(row);
    }
    if (rows.length === 0) {
      rows.push(
        ...(await ScmChangeRequestModel.findByHeadSha(
          this.db,
          'github',
          event.repoFullName,
          event.headSha,
        )),
      );
    }
    if (rows.length === 0) {
      return {
        detail: `no tracked change request for ${event.repoFullName}@${event.headSha.slice(0, 7)}`,
        status: 'skipped',
      };
    }

    const touched: string[] = [];
    for (const row of rows) {
      const result = await ScmChangeRequestModel.applyChecks(this.db, row.id, {
        checks: event.checks,
        headSha: event.headSha,
      });
      if (!result?.applied) continue;
      touched.push(row.id);

      // Emit per-check failure the moment it lands, and the green rollup once
      // nothing is pending — those two are what the control half acts on.
      //
      // Read the failure off the *merged* row, not off the payload: a
      // redelivered or late failure for a check that has since gone green
      // loses to the newer result in `mergeChecks`, and classifying it from
      // the payload would wake the agent against a green row with nothing
      // to fix — and spend one of its three wakes doing it.
      const arrived = new Set(event.checks.filter(isFailingCheck).map((c) => c.externalId));
      const failedNow = (result.row.checks ?? []).some(
        (check) => isFailingCheck(check) && arrived.has(check.externalId),
      );
      const kind = failedNow
        ? 'ci_failed'
        : result.ciStatus === 'success' && result.previousCiStatus !== 'success'
          ? 'ci_passed'
          : null;
      if (kind) {
        await ScmChangeRequestModel.recordEvent(this.db, row.id, kind);
        await this.onChangeRequestEvent({ event, kind, row: result.row });
      }
    }

    return touched.length > 0
      ? { detail: touched.join(','), status: 'processed' }
      : { detail: `stale checks for ${event.headSha.slice(0, 7)}`, status: 'skipped' };
  };

  private applyReview = async (
    event: Extract<ScmInboundEvent, { type: 'review' }>,
  ): Promise<ScmIngestOutcome> => {
    const installation = await this.resolveBoundInstallation(event.installationId);
    if (!installation) {
      return { detail: `installation ${event.installationId} is not bound`, status: 'skipped' };
    }

    const row = await ScmChangeRequestModel.findByIdentity(
      this.db,
      'github',
      event.repoFullName,
      event.number,
    );
    if (!row) {
      return {
        detail: `no tracked change request for ${event.repoFullName}#${event.number}`,
        status: 'skipped',
      };
    }

    // A plain comment carries no verdict and leaves the rollup alone; a
    // dismissal drops that reviewer's verdict from it.
    const decision =
      event.kind === 'review_approved'
        ? ('approved' as const)
        : event.kind === 'review_changes_requested'
          ? ('changes_requested' as const)
          : null;
    if (decision || event.kind === 'review_dismissed') {
      const { applied } = await ScmChangeRequestModel.applyReviewerDecision(this.db, row.id, {
        at: event.occurredAt,
        decision,
        reviewerId: event.actor?.externalId,
      });
      // The reviewer has since said something newer. Recording this event
      // would rewind `lastEventAt`, and handing it to the control half
      // would wake the agent over a verdict that no longer stands.
      if (!applied) {
        return { detail: `${row.id} ${event.kind} superseded`, status: 'skipped' };
      }
    }
    await ScmChangeRequestModel.recordEvent(this.db, row.id, event.kind, event.occurredAt);

    const fresh = (await ScmChangeRequestModel.findById(this.db, row.id)) ?? row;
    await this.onChangeRequestEvent({ event, kind: event.kind, row: fresh });
    return { detail: `${row.id} ${event.kind}`, status: 'processed' };
  };
}