Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Fix auto-release rewinding to an older commit (#74768)

Nemanja Glumacfb4d33bb382c
diff --git a/release/src/github.ts b/release/src/github.ts
index 9a67e169968..2fc4758e477 100644
--- a/release/src/github.ts
+++ b/release/src/github.ts
@@ -261,7 +261,24 @@ export async function getLatestGreenCommit({
return null;
}
-// note: only checks if the commit is the last one released for the major version
+/**
+ * True if `ref` is not strictly newer than the most recent release for this
+ * major — i.e. its code is already shipped. We compare the last release tag
+ * against the branch tip and check membership of `ref` in the returned commits
+ * (which are exactly the commits on the branch that are NOT in the last
+ * release's history). The previous SHA-equality check only caught the case
+ * where `ref` was *the* last release tag's commit, and missed any older commit
+ * further back in history — see DEV-2025.
+ *
+ * Known limitation: `compareCommitsWithBasehead` returns at most 250 commits
+ * in a single page (the `commits` array is truncated even though `total_commits`
+ * reflects the full count). If a release branch ever accumulates more than 250
+ * commits since its last release tag, a candidate beyond the 250th would be
+ * missing from `data.commits` and the gate would incorrectly say "already
+ * released" → skip. Today's release cadence keeps `ahead_by` in single digits,
+ * so this is theoretical, but worth knowing if cadence ever changes — the fix
+ * is `github.paginate(github.rest.repos.compareCommitsWithBasehead, …)`.
+ */
export async function hasCommitBeenReleased({
github,
owner,
@@ -269,7 +286,6 @@ export async function hasCommitBeenReleased({
ref,
majorVersion,
}: GithubProps & { ref: string; majorVersion: number }) {
- // TODO: a better approach would be to fetch the ref and see if it has any release tags
const lastTag = await getLastReleaseTag({
github,
owner,
@@ -287,9 +303,26 @@ export async function hasCommitBeenReleased({
const lastTagSha = tagDetail.data.object.sha;
- console.log({ lastTag, lastTagSha, ref });
+ // Short-circuit when the candidate IS the last release tag's commit. The
+ // compare API excludes the base from `data.commits`, so this case would
+ // otherwise reach the membership check and return `true` only as a
+ // side effect of "base is not in the commits list" — calling it out
+ // explicitly is clearer and saves an API call.
+ if (lastTagSha === ref) {
+ return true;
+ }
+
+ const { data } = await github.rest.repos.compareCommitsWithBasehead({
+ owner,
+ repo,
+ basehead: `${lastTagSha}...refs/heads/release-x.${majorVersion}.x`,
+ });
+
+ const isAheadOfLastRelease = data.commits.some(c => c.sha === ref);
+
+ console.log({ lastTag, lastTagSha, ref, isAheadOfLastRelease });
- return lastTagSha === ref;
+ return !isAheadOfLastRelease;
}
export async function getOpenBackportPrs({
diff --git a/release/src/github.unit.spec.ts b/release/src/github.unit.spec.ts
new file mode 100644
index 00000000000..da8e575b3aa
--- /dev/null
+++ b/release/src/github.unit.spec.ts
@@ -0,0 +1,68 @@
+import { hasCommitBeenReleased } from "./github";
+
+// release-x.61.x window for these tests: lastTagSha is at index 0 (oldest),
+// branch tip at the end. Commits NEWER than lastTagSha are those after it.
+const LAST_TAG_SHA = "aaaa".repeat(10);
+const PARENT_OF_LAST = "0000".repeat(10);
+const NEWER_1 = "bbbb".repeat(10);
+const NEWER_2 = "cccc".repeat(10);
+
+const NEW_COMMITS_LIST = [{ sha: NEWER_1 }, { sha: NEWER_2 }];
+
+describe("hasCommitBeenReleased", () => {
+ const build = ({
+ lastTag = "v0.61.2.7",
+ commits = NEW_COMMITS_LIST,
+ }: { lastTag?: string; commits?: Array<{ sha: string }> } = {}) => ({
+ paginate: jest.fn().mockResolvedValue([{ ref: `refs/tags/${lastTag}` }]),
+ rest: {
+ git: {
+ getRef: jest
+ .fn()
+ .mockResolvedValue({ data: { object: { sha: LAST_TAG_SHA } } }),
+ },
+ repos: {
+ compareCommitsWithBasehead: jest
+ .fn()
+ .mockResolvedValue({ data: { commits } }),
+ },
+ },
+ });
+
+ const call = (github: ReturnType<typeof build>, ref: string) =>
+ hasCommitBeenReleased({
+ github: github as any,
+ owner: "metabase",
+ repo: "metabase",
+ ref,
+ majorVersion: 61,
+ });
+
+ it("returns false when the candidate is newer than the last release", async () => {
+ await expect(call(build(), NEWER_2)).resolves.toBe(false);
+ });
+
+ // The DEV-2025 regression: the candidate is an ancestor of the last release
+ // tag (e.g. green commit older than a manual/override release). It must be
+ // treated as already released — the previous SHA-equality check missed this.
+ it("returns true when the candidate is older than the last release", async () => {
+ await expect(call(build(), PARENT_OF_LAST)).resolves.toBe(true);
+ });
+
+ it("short-circuits when the candidate IS the last release tag's commit", async () => {
+ const github = build();
+ await expect(call(github, LAST_TAG_SHA)).resolves.toBe(true);
+ expect(github.rest.repos.compareCommitsWithBasehead).not.toHaveBeenCalled();
+ });
+
+ it("compares against the release branch tip for the given major", async () => {
+ const github = build();
+ await call(github, NEWER_1);
+
+ expect(github.rest.repos.compareCommitsWithBasehead).toHaveBeenCalledWith(
+ expect.objectContaining({
+ basehead: `${LAST_TAG_SHA}...refs/heads/release-x.61.x`,
+ }),
+ );
+ });
+});