Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesPull RequestsActionsSecurityInsightsSettings
✨ AI
More
claude/adoring-hopper-5x74bqclaude/affectionate-feynman-ykrf1hclaude/architecture-audit-design-wxprenclaude/build-status-update-3MXsfclaude/charming-meitner-mllb5rclaude/compare-gate-gluecron-s4mFQclaude/confident-faraday-tikcwbclaude/continue-work-XMTlIclaude/crontech-gluecron-deploy-7MIECclaude/crontech-platform-setup-SeKfwclaude/design-2026claude/ecstatic-ptolemy-jMdigclaude/enhance-github-integration-QNHdGclaude/fix-aa-loop-issue-PonMQclaude/fix-actions-and-processclaude/fix-desktop-errors-XqoW8claude/fix-red-workflowsclaude/fix-website-access-6FKJNclaude/gatetest-integration-hardeningclaude/github-audit-improvements-bDFr9claude/gluecron-launch-status-FoMRlclaude/hopeful-lamport-olfCTclaude/issue-to-pr-and-protectionsclaude/jolly-heisenberg-2sg1Qclaude/launch-preparation-QmTb6claude/new-session-xk1l7claude/plan-platform-architecture-kkN4yclaude/platform-analysis-roadmap-1nUGLclaude/platform-launch-assessment-8dWV8claude/polish-platform-release-AeDrUclaude/resume-previous-work-KzyLwclaude/review-crontech-handoff-qYEVqclaude/review-project-completeness-lHhS2claude/review-readme-docs-ulqPKclaude/serene-edison-rj87weclaude/setup-multi-repo-dev-BCwNQclaude/ship-fixes-and-tests-Jvz1cclaude/site-audit-competitive-pctlwgclaude/site-migration-vercel-XstpKclaude/standalone-product-repos-XHFTDcopilot/feat-smart-empty-states-keyboard-first-enhancementcopilot/feat-smart-morning-digest-review-context-restorecopilot/fix-and-process-workflowscopilot/update-ai-powered-code-reviewfeat/debt-mapfeat/push-policy-codeowners-hardeningfeat/smart-digest-contextfeat/stage-impactfeat/t1-secret-migrationfeat/u-polishfeat/w-self-hostfeat/w2-claude-configfix/agent-journey-orphan-sweepgatetest/auto-fix-1776586424172gatetest/auto-fix-1776586534814gatetest/auto-fix-1776590685143gatetest/auto-fix-1776590808199mainops/redeploy-retriggerstyle/dxt-cta-themeworktree-agent-a3377aad30d55da26worktree-agent-a7ef607b7ee1d6c74
pr-auto-merge-trigger.ts3.5 KB · 108 lines
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
/**
 * Block J16 — Auto-merge trigger called from the commit-status POST path.
 *
 * When a status lands against a commit SHA we find all open auto-merge-
 * enabled PRs in the same repo, resolve each PR's head SHA, and evaluate
 * `computeAutoMergeAction`. If a PR transitions to "ready" we post a
 * one-shot comment + notify the author, then flip `notifiedReady=true` so
 * we don't spam duplicates.
 *
 * Everything here is wrapped in try/catch; a commit-status write never fails
 * because of an auto-merge side-effect.
 */

import { eq } from "drizzle-orm";
import { db } from "../db";
import { prComments, users } from "../db/schema";
import { combinedStatus } from "./commit-statuses";
import {
  computeAutoMergeAction,
  listAutoMergePrsForRepo,
  recordEvaluation,
} from "./pr-auto-merge";
import { notifyMany } from "./notify";
import { resolveRef } from "../git/repository";

export async function attemptAutoMergeForSha(opts: {
  ownerName: string;
  repoName: string;
  repositoryId: string;
  commitSha: string;
}): Promise<number> {
  const normalised = opts.commitSha.toLowerCase();
  const prs = await listAutoMergePrsForRepo(opts.repositoryId);
  if (prs.length === 0) return 0;

  let triggered = 0;
  for (const pr of prs) {
    if (pr.state !== "open" || pr.isDraft) continue;
    let headSha: string | null = null;
    try {
      headSha = await resolveRef(opts.ownerName, opts.repoName, pr.headBranch);
    } catch {
      headSha = null;
    }
    if (!headSha || headSha.toLowerCase() !== normalised) continue;

    let combined: Awaited<ReturnType<typeof combinedStatus>>;
    try {
      combined = await combinedStatus(opts.repositoryId, headSha);
    } catch (err) {
      console.error("[pr-auto-merge-trigger] combinedStatus failed:", err);
      continue;
    }

    const action = computeAutoMergeAction({
      autoMergeEnabled: true,
      prState: pr.state,
      isDraft: pr.isDraft,
      combinedState: combined.state,
      totalChecks: combined.total,
    });

    const { wasAlreadyReady } = await recordEvaluation(
      pr.pullRequestId,
      action,
      combined.state
    );

    if (action.action === "merge" && !wasAlreadyReady) {
      triggered += 1;
      try {
        await db.insert(prComments).values({
          pullRequestId: pr.pullRequestId,
          authorId: pr.authorId,
          body: [
            `\u26A1 **Auto-merge ready**`,
            ``,
            `All ${combined.total} commit status check${combined.total === 1 ? "" : "s"} on \`${headSha.slice(0, 7)}\` are now \`success\`.`,
            `Click **Merge pull request** above to complete the merge.`,
          ].join("\n"),
          isAiReview: false,
        });
      } catch (err) {
        console.error("[pr-auto-merge-trigger] comment failed:", err);
      }
      try {
        // Notify the PR author only — keeps noise low.
        const [authorRow] = await db
          .select({ id: users.id })
          .from(users)
          .where(eq(users.id, pr.authorId))
          .limit(1);
        if (authorRow) {
          await notifyMany([authorRow.id], {
            kind: "pr_opened", // no dedicated kind; reuse a benign one
            title: `${opts.ownerName}/${opts.repoName} PR #${pr.number} ready to merge`,
            body: "All commit status checks passed \u2014 auto-merge is ready.",
            url: `/${opts.ownerName}/${opts.repoName}/pulls/${pr.number}`,
            repositoryId: opts.repositoryId,
          });
        }
      } catch (err) {
        console.error("[pr-auto-merge-trigger] notify failed:", err);
      }
    }
  }
  return triggered;
}