Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/src/content/docs/guide/ops.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ A reconcile activity is also available via `activity("reconcilePr", args)`: give

`comment` mode is the one that constrains its trigger. The body lands as a single comment on the pull request that triggered the run. Every re-run edits that same comment instead of adding another; the activity finds it again by a hidden marker written as the comment's first line. A branch pushed to five times then carries one current body instead of five stale ones. The pull request itself comes out of the run's own event payload (`GITHUB_EVENT_PATH`, with `GITHUB_REF` as the fallback). So the mode requires a `pull_request` trigger and fails by name on a run without one. Its generated workflow gets exactly `contents: read` and `pull-requests: write`.

`issue` mode needs no trigger at all; a cron Op with a nightly finding is the case it exists for. It is sticky the same way `comment` mode is: a hidden marker on the first line of the issue body finds the one this Op already owns, and a re-run edits it in place instead of opening another. This holds on GitHub, GitHub Enterprise Server, and Forgejo alike, each read off `GITHUB_REPOSITORY` since a GitHub Actions and a Forgejo Actions job set it the same way, and on GitLab CI, read off `CI_PROJECT_ID`, as its own project issue. It never closes the issue itself, on either forge. The search only looks at OPEN issues, which makes that a concrete decision rather than an implicit one. Closing the issue by hand, once whatever it named is actually fixed, is how an operator tells the next run to open a fresh issue for a fresh finding, rather than have the bot silently rewrite a closed issue nobody is watching. Reopening a closed issue on the next finding was considered and rejected: this activity has no reliable signal for "the last finding got fixed" as opposed to "this run happened not to check."

## Gate steps

A gate is a fact on the gate ledger: a run that reaches one with no resolution records that it is waiting and ends there. Typical gate scenarios:
Expand Down
161 changes: 122 additions & 39 deletions packages/core/src/op/activities/lexicon-upgrade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
buildUpgradeSummary,
computeBumpedVersion,
bumpPackageJsonVersion,
lexiconUpgradeIssueMarker,
type CheckPinnedFn,
type CheckRollingFn,
type BumpPackageVersionFn,
Expand Down Expand Up @@ -223,21 +224,93 @@ describe("lexiconUpgrade report mode", () => {

// ── issue mode ────────────────────────────────────────────────────────

describe("lexiconUpgradeIssueMarker (#2297)", () => {
test("deterministic per lexicon, and distinct across lexicons", () => {
expect(lexiconUpgradeIssueMarker("k8s")).toBe("<!-- chant-lexicon-upgrade:k8s -->");
expect(lexiconUpgradeIssueMarker("k8s")).toBe(lexiconUpgradeIssueMarker("k8s"));
expect(lexiconUpgradeIssueMarker("k8s")).not.toBe(lexiconUpgradeIssueMarker("aws"));
});
});

describe("lexiconUpgrade issue mode", () => {
test("opens an issue for a ready upgrade", async () => {
test("outside any known CI job, falls back to gh's own ambient gh issue create, marker-prefixed", async () => {
const checkPinned: CheckPinnedFn = vi.fn(async () => pinnedResult());
const { gh, calls } = recordingGh();

const r = await lexiconUpgrade({
lexicon: "gcp",
mode: "issue",
_checkPinned: checkPinned,
_gh: gh,
vi.stubEnv("GITHUB_REPOSITORY", "");
try {
const r = await lexiconUpgrade({
lexicon: "gcp",
mode: "issue",
_checkPinned: checkPinned,
_gh: gh,
});

expect(r.issueUrl).toBe("https://gh/issue/1");
expect(calls.some((c) => c.includes("gh issue create"))).toBe(true);
expect(calls.some((c) => c.includes("gh pr create"))).toBe(false);
expect(calls.some((c) => c.includes("<!-- chant-lexicon-upgrade:gcp -->"))).toBe(true);
} finally {
vi.unstubAllEnvs();
}
});

test("on a GitHub Actions job (GITHUB_REPOSITORY set), opens the sticky issue via gh api (#2297)", async () => {
const checkPinned: CheckPinnedFn = vi.fn(async () => pinnedResult());
const calls: string[] = [];
const gh = vi.fn(async (cmd: string) => {
calls.push(cmd);
if (cmd.includes("--paginate")) return { stdout: "", stderr: "" }; // no owned issue yet
if (cmd.includes("--method POST")) return { stdout: "https://github.com/acme/infra/issues/11\n", stderr: "" };
return { stdout: "", stderr: "" };
});

expect(r.issueUrl).toBe("https://gh/issue/1");
expect(calls.some((c) => c.includes("gh issue create"))).toBe(true);
expect(calls.some((c) => c.includes("gh pr create"))).toBe(false);
vi.stubEnv("GITHUB_REPOSITORY", "acme/infra");
vi.stubEnv("GITHUB_API_URL", "");
try {
const r = await lexiconUpgrade({
lexicon: "gcp",
mode: "issue",
_checkPinned: checkPinned,
_gh: gh,
});

expect(r.issueUrl).toBe("https://github.com/acme/infra/issues/11");
expect(calls.some((c) => c.includes("gh issue create"))).toBe(false);
const post = calls.find((c) => c.includes("--method POST"));
expect(post).toContain("https://api.github.com/repos/acme/infra/issues");
expect(post).toContain("<!-- chant-lexicon-upgrade:gcp -->");
} finally {
vi.unstubAllEnvs();
}
});

test("a re-run on a GitHub Actions job PATCHes the issue it already owns (#2297)", async () => {
const checkPinned: CheckPinnedFn = vi.fn(async () => pinnedResult());
const calls: string[] = [];
const gh = vi.fn(async (cmd: string) => {
calls.push(cmd);
if (cmd.includes("--paginate")) return { stdout: "11\n", stderr: "" }; // marker search found issue 11
if (cmd.includes("--method PATCH")) return { stdout: "https://github.com/acme/infra/issues/11\n", stderr: "" };
return { stdout: "", stderr: "" };
});

vi.stubEnv("GITHUB_REPOSITORY", "acme/infra");
vi.stubEnv("GITHUB_API_URL", "");
try {
const r = await lexiconUpgrade({
lexicon: "gcp",
mode: "issue",
_checkPinned: checkPinned,
_gh: gh,
});

expect(r.issueUrl).toBe("https://github.com/acme/infra/issues/11");
expect(calls.some((c) => c.includes("--method POST"))).toBe(false);
expect(calls.some((c) => c.includes("gh issue create"))).toBe(false);
} finally {
vi.unstubAllEnvs();
}
});
});

Expand All @@ -260,21 +333,26 @@ describe("lexiconUpgrade breakage handling", () => {
);
const { gh, calls } = recordingGh();

const r = await lexiconUpgrade({
lexicon: "k8s",
mode: "pull-request",
_checkPinned: checkPinned,
_gh: gh,
});

expect(r.validationOk).toBe(false);
expect(r.issueUrl).toBe("https://gh/issue/1");
expect(r.prUrl).toBeUndefined();
// must NOT open a PR, must NOT push
expect(calls.some((c) => c.includes("gh pr create"))).toBe(false);
expect(calls.some((c) => c.includes("gh issue create"))).toBe(true);
// the issue body carries the failure diff
expect(r.summary).toContain("type error");
vi.stubEnv("GITHUB_REPOSITORY", "");
try {
const r = await lexiconUpgrade({
lexicon: "k8s",
mode: "pull-request",
_checkPinned: checkPinned,
_gh: gh,
});

expect(r.validationOk).toBe(false);
expect(r.issueUrl).toBe("https://gh/issue/1");
expect(r.prUrl).toBeUndefined();
// must NOT open a PR, must NOT push
expect(calls.some((c) => c.includes("gh pr create"))).toBe(false);
expect(calls.some((c) => c.includes("gh issue create"))).toBe(true);
// the issue body carries the failure diff
expect(r.summary).toContain("type error");
} finally {
vi.unstubAllEnvs();
}
});

test("upstream fetch error surfaces as a non-upgrade report, never a PR", async () => {
Expand Down Expand Up @@ -473,20 +551,25 @@ describe("lexiconUpgrade pull-request mode: package.json version bump", () => {
return { stdout: "", stderr: "" };
});

const r = await lexiconUpgrade({
lexicon: "aws",
mode: "pull-request",
_checkRolling: checkRolling,
_bumpPackageVersion: bumpFn,
_gh: gh,
});

// Rolling drift → issue, never a baseline-only PR, a branch push, or a bump.
expect(r.mode).toBe("issue");
expect(r.issueUrl).toBe("https://gh/issue/9");
expect(calls.some((c) => c.includes("gh issue create"))).toBe(true);
expect(calls.some((c) => c.includes("gh pr create"))).toBe(false);
expect(calls.some((c) => c.startsWith("git "))).toBe(false);
expect(bumpFn).not.toHaveBeenCalled();
vi.stubEnv("GITHUB_REPOSITORY", "");
try {
const r = await lexiconUpgrade({
lexicon: "aws",
mode: "pull-request",
_checkRolling: checkRolling,
_bumpPackageVersion: bumpFn,
_gh: gh,
});

// Rolling drift → issue, never a baseline-only PR, a branch push, or a bump.
expect(r.mode).toBe("issue");
expect(r.issueUrl).toBe("https://gh/issue/9");
expect(calls.some((c) => c.includes("gh issue create"))).toBe(true);
expect(calls.some((c) => c.includes("gh pr create"))).toBe(false);
expect(calls.some((c) => c.startsWith("git "))).toBe(false);
expect(bumpFn).not.toHaveBeenCalled();
} finally {
vi.unstubAllEnvs();
}
});
});
64 changes: 55 additions & 9 deletions packages/core/src/op/activities/lexicon-upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { join } from "node:path";
import type { UpgradeCheckResult, LexiconId } from "../../codegen/pinned-upgrade";
import type { RollingUpgradeResult, RollingLexicon } from "../../codegen/rolling-upgrade";
import { bumpForSeverity, bumpPackageJsonVersion } from "../../codegen/version-bump";
import { postOrUpdateGithubIssue } from "./reconcile";

export { bumpPackageJsonVersion };

Expand Down Expand Up @@ -94,7 +95,7 @@ export interface LexiconUpgradeResult {
semverLabel: SemverLabel | null;
/** URL of the opened/updated PR (pull-request mode). */
prUrl?: string;
/** URL of the opened issue (issue mode). */
/** URL of the opened or edited issue (issue mode; sticky per lexicon, #2297). */
issueUrl?: string;
/** Full markdown summary, used as PR/issue body or printed in report mode. */
summary: string;
Expand Down Expand Up @@ -284,6 +285,49 @@ function shellQuote(s: string): string {

const defaultGh: GhRunner = async (cmd) => execAsync(cmd);

/**
* The hidden marker that makes this lexicon's upgrade-status issue findable
* across re-runs (#2297): written as the issue body's first line, matched by
* `startswith` — the same recipe reconcile.ts's `issueMarker` names for a
* reconcile finding, kept as its own function because the two activities own
* different issues. Keyed on `lexicon`, not on the validation outcome: a
* lexicon has one upgrade-status issue that flips between "validation
* failed" and "upgrade ready" as reality changes, rather than a second issue
* for the other outcome. `lexicon` is a closed set of known identifiers
* (see `SupportedLexicon`), so unlike `issueMarker` this needs no slugifying.
*/
export function lexiconUpgradeIssueMarker(lexicon: SupportedLexicon): string {
return `<!-- chant-lexicon-upgrade:${lexicon} -->`;
}

/**
* Open or update this lexicon's upgrade-status issue (#2297). On a GitHub
* Actions or Forgejo Actions job (`GITHUB_REPOSITORY` set) this is
* `postOrUpdateGithubIssue`'s sticky recipe from reconcile.ts: find the OPEN
* issue already carrying this lexicon's marker, edit it, open one only when
* there is none — see that function's own doc comment for the search
* mechanism and the close decision (never closed here either). Outside any
* known CI job, falls back to `gh`'s own ambient `gh issue create`, still
* marker-prefixed so a later CI run finds and edits it instead of opening a
* second one.
*/
async function postLexiconIssue(
lexicon: SupportedLexicon,
title: string,
body: string,
gh: GhRunner,
): Promise<string> {
const marker = lexiconUpgradeIssueMarker(lexicon);
const repo = process.env.GITHUB_REPOSITORY;
if (repo) {
return postOrUpdateGithubIssue(repo, marker, title, body, gh);
}
const { stdout } = await gh(
`gh issue create --title ${shellQuote(title)} --body ${shellQuote(`${marker}\n\n${body}`)}`,
);
return stdout.trim();
}

// ── PR idempotency helpers ────────────────────────────────────────────

/**
Expand Down Expand Up @@ -384,6 +428,12 @@ async function getRealApplyBump(): Promise<ApplyBumpFn> {
* - Semver label: "minor" (additive) or "breaking" (changed/removed).
* - Validation failure falls back to issue mode.
*
* In issue mode (#2297): one OPEN issue per lexicon, found by a hidden
* marker and edited in place on every re-run — the same sticky recipe
* reconcile.ts's `issue` mode uses, via the shared `postOrUpdateGithubIssue`.
* Never closed by this activity; see that function's doc comment for the
* close decision, which applies here unchanged.
*
* Never auto-merges or auto-publishes.
*/
export async function lexiconUpgrade(args: LexiconUpgradeArgs): Promise<LexiconUpgradeResult> {
Expand Down Expand Up @@ -493,16 +543,14 @@ export async function lexiconUpgrade(args: LexiconUpgradeArgs): Promise<LexiconU
const effectiveMode: LexiconUpgradeMode = mode === "pull-request" ? "issue" : mode;
if (effectiveMode === "issue") {
const title = `chore(${lexicon}): spec upgrade validation failed`;
const { stdout } = await gh(
`gh issue create --title ${shellQuote(title)} --body ${shellQuote(summary)}`,
);
const issueUrl = await postLexiconIssue(lexicon, title, summary, gh);
return {
lexicon,
mode,
hasUpgrade: true,
deltaText,
semverLabel,
issueUrl: stdout.trim(),
issueUrl,
summary,
validationOk: false,
};
Expand Down Expand Up @@ -543,16 +591,14 @@ export async function lexiconUpgrade(args: LexiconUpgradeArgs): Promise<LexiconU
}

if (effectiveMode === "issue") {
const { stdout } = await gh(
`gh issue create --title ${shellQuote(title)} --body ${shellQuote(summary)}`,
);
const issueUrl = await postLexiconIssue(lexicon, title, summary, gh);
return {
lexicon,
mode: effectiveMode,
hasUpgrade: true,
deltaText,
semverLabel,
issueUrl: stdout.trim(),
issueUrl,
summary,
validationOk: true,
};
Expand Down
Loading
Loading