diff --git a/CHANGELOG.md b/CHANGELOG.md index c5b5e2c..1545dac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.0.85] - 2026-11-04 + +### Fixed + +- `tinybird preview` no longer hides errors when deleting the existing preview branch: only a 404 is treated as "branch doesn't exist"; other failures are reported as `Failed to delete existing preview branch`. +- CI workflows generated by `tinybird init` no longer run overlapping previews for the same branch: GitHub workflows get a `concurrency` group that cancels the older run, and GitLab jobs get a per-branch `resource_group`. Existing projects can add the same settings to their workflow files. + ## [0.0.84] - 2026-09-08 ### Changed diff --git a/package.json b/package.json index 05090a3..88244ba 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tinybirdco/sdk", - "version": "0.0.84", + "version": "0.0.85", "description": "TypeScript SDK for Tinybird Forward - define datasources and pipes as TypeScript", "type": "module", "main": "./dist/index.js", diff --git a/src/cli/commands/init.test.ts b/src/cli/commands/init.test.ts index e08d3d7..36d0ff8 100644 --- a/src/cli/commands/init.test.ts +++ b/src/cli/commands/init.test.ts @@ -318,6 +318,57 @@ describe("Init Command", () => { }); }); + describe("CI workflow generation", () => { + async function readGeneratedWorkflow( + workflowProvider: "github" | "gitlab", + workflowPath: string + ): Promise { + const originalCwd = process.cwd(); + process.chdir(tempDir); + + try { + const result = await runInit({ + cwd: tempDir, + skipLogin: true, + devMode: "branch", + clientPath: "lib/tinybird.ts", + includeCiWorkflow: true, + workflowProvider, + skipWorkflowPrompt: true, + }); + + expect(result.success).toBe(true); + return fs.readFileSync(path.join(tempDir, workflowPath), "utf-8"); + } finally { + process.chdir(originalCwd); + } + } + + it("serializes GitHub preview workflows by ref", async () => { + const content = await readGeneratedWorkflow( + "github", + ".github/workflows/tinybird-ci.yaml" + ); + + expect(content).toContain("concurrency:"); + expect(content).toContain( + "group: tinybird-preview-${{ github.head_ref || github.ref }}" + ); + expect(content).toContain("cancel-in-progress: true"); + }); + + it("serializes GitLab preview jobs by branch", async () => { + const content = await readGeneratedWorkflow( + "gitlab", + ".gitlab/tinybird-ci.yaml" + ); + + expect(content).toContain( + "resource_group: tinybird-preview-${CI_COMMIT_REF_SLUG}" + ); + }); + }); + describe("directory creation", () => { it("creates lib directory if it does not exist", async () => { expect(fs.existsSync(path.join(tempDir, "lib"))).toBe(false); diff --git a/src/cli/commands/init.ts b/src/cli/commands/init.ts index a19c851..5f53f6a 100644 --- a/src/cli/commands/init.ts +++ b/src/cli/commands/init.ts @@ -143,6 +143,10 @@ on: - "${pathPrefix}tinybird.json" - "${pathPrefix}**/*.ts" +concurrency: + group: tinybird-preview-\${{ github.head_ref || github.ref }} + cancel-in-progress: true + env: TINYBIRD_TOKEN: \${{ secrets.TINYBIRD_TOKEN }} @@ -226,6 +230,7 @@ function generateGitlabCiWorkflow(workingDirectory?: string): string { tinybird_ci: stage: tinybird + resource_group: tinybird-preview-\${CI_COMMIT_REF_SLUG} image: node:22 rules: - changes: diff --git a/src/cli/commands/preview.test.ts b/src/cli/commands/preview.test.ts index 718596e..b9bd4c5 100644 --- a/src/cli/commands/preview.test.ts +++ b/src/cli/commands/preview.test.ts @@ -11,6 +11,12 @@ vi.mock("../../generator/index.js", () => ({ })); vi.mock("../../api/branches.js", () => ({ + BranchApiError: class BranchApiError extends Error { + constructor(message: string, public readonly status: number) { + super(message); + this.name = "BranchApiError"; + } + }, createBranch: vi.fn(), deleteBranch: vi.fn(), getBranch: vi.fn(), @@ -77,7 +83,7 @@ describe("Preview command", () => { it("uses config-only last_partition when creating cloud preview branch", async () => { const { loadConfigAsync } = await import("../config.js"); const { buildFromInclude } = await import("../../generator/index.js"); - const { getBranch, createBranch } = await import("../../api/branches.js"); + const { getBranch, createBranch, BranchApiError } = await import("../../api/branches.js"); const { deployToMain } = await import("../../api/deploy.js"); vi.mocked(loadConfigAsync).mockResolvedValue({ @@ -97,7 +103,7 @@ describe("Preview command", () => { entities: { datasources: {}, pipes: {}, connections: {}, rawDatasources: [], rawPipes: [], sourceFiles: [] }, stats: { datasourceCount: 0, pipeCount: 0, connectionCount: 0 }, }); - vi.mocked(getBranch).mockRejectedValue(new Error("not found")); + vi.mocked(getBranch).mockRejectedValue(new BranchApiError("not found", 404)); vi.mocked(createBranch).mockResolvedValue({ id: "b1", name: "tmp_ci_feature_test", @@ -123,7 +129,7 @@ describe("Preview command", () => { it("creates cloud preview branch without data when branch_data_mode is not set", async () => { const { loadConfigAsync } = await import("../config.js"); const { buildFromInclude } = await import("../../generator/index.js"); - const { getBranch, createBranch } = await import("../../api/branches.js"); + const { getBranch, createBranch, BranchApiError } = await import("../../api/branches.js"); const { deployToMain } = await import("../../api/deploy.js"); vi.mocked(loadConfigAsync).mockResolvedValue({ @@ -143,7 +149,7 @@ describe("Preview command", () => { entities: { datasources: {}, pipes: {}, connections: {}, rawDatasources: [], rawPipes: [], sourceFiles: [] }, stats: { datasourceCount: 0, pipeCount: 0, connectionCount: 0 }, }); - vi.mocked(getBranch).mockRejectedValue(new Error("not found")); + vi.mocked(getBranch).mockRejectedValue(new BranchApiError("not found", 404)); vi.mocked(createBranch).mockResolvedValue({ id: "b1", name: "tmp_ci_feature_test", @@ -211,4 +217,90 @@ describe("Preview command", () => { expect(createBranch).not.toHaveBeenCalled(); }); }); + + describe("existing preview branch cleanup", () => { + const setupCloudPreview = async () => { + const { loadConfigAsync } = await import("../config.js"); + const { buildFromInclude } = await import("../../generator/index.js"); + const { BranchApiError, createBranch, deleteBranch, getBranch } = await import("../../api/branches.js"); + const { deployToMain } = await import("../../api/deploy.js"); + + vi.mocked(loadConfigAsync).mockResolvedValue({ + include: ["test.ts"], + token: "p.test-token", + baseUrl: "https://api.tinybird.co", + configPath: "/test/tinybird.config.json", + devMode: "branch", + cwd: "/test", + gitBranch: "feature-test", + tinybirdBranch: "feature_test", + isMainBranch: false, + branchDataMode: null, + }); + vi.mocked(buildFromInclude).mockResolvedValue({ + resources: { datasources: [], pipes: [], connections: [] }, + entities: { datasources: {}, pipes: {}, connections: {}, rawDatasources: [], rawPipes: [], sourceFiles: [] }, + stats: { datasourceCount: 0, pipeCount: 0, connectionCount: 0 }, + }); + vi.mocked(createBranch).mockResolvedValue({ + id: "b1", + name: "tmp_ci_feature_test", + token: "p.branch", + created_at: "2024-01-01T00:00:00Z", + }); + vi.mocked(deployToMain).mockResolvedValue({ + success: true, + result: "success", + datasourceCount: 0, + pipeCount: 0, + connectionCount: 0, + }); + + return { BranchApiError, createBranch, deleteBranch, getBranch }; + }; + + it("creates a preview branch when the existing branch lookup returns 404", async () => { + const { BranchApiError, createBranch, getBranch } = await setupCloudPreview(); + vi.mocked(getBranch).mockRejectedValue(new BranchApiError("branch not found", 404)); + + const result = await runPreview(); + + expect(result.success).toBe(true); + expect(createBranch).toHaveBeenCalled(); + }); + + it("creates a preview branch when the existing branch is deleted concurrently", async () => { + const { BranchApiError, createBranch, deleteBranch, getBranch } = await setupCloudPreview(); + vi.mocked(getBranch).mockResolvedValue({ + id: "existing", + name: "tmp_ci_feature_test", + created_at: "2024-01-01T00:00:00Z", + }); + vi.mocked(deleteBranch).mockRejectedValue(new BranchApiError("branch not found", 404)); + + const result = await runPreview(); + + expect(result.success).toBe(true); + expect(createBranch).toHaveBeenCalled(); + }); + + it("reports a non-404 failure when deleting the existing preview branch", async () => { + const { BranchApiError, createBranch, deleteBranch, getBranch } = await setupCloudPreview(); + vi.mocked(getBranch).mockResolvedValue({ + id: "existing", + name: "tmp_ci_feature_test", + created_at: "2024-01-01T00:00:00Z", + }); + vi.mocked(deleteBranch).mockRejectedValue( + new BranchApiError("deletion rejected with 422", 422) + ); + + const result = await runPreview(); + + expect(result.success).toBe(false); + expect(result.error).toContain("Failed to delete existing preview branch"); + expect(result.error).toContain("deletion rejected with 422"); + expect(createBranch).not.toHaveBeenCalled(); + }); + }); }); diff --git a/src/cli/commands/preview.ts b/src/cli/commands/preview.ts index def7445..1056a5c 100644 --- a/src/cli/commands/preview.ts +++ b/src/cli/commands/preview.ts @@ -4,7 +4,14 @@ import { loadConfigAsync, LOCAL_BASE_URL, type ResolvedConfig, type DevMode } from "../config.js"; import { buildFromInclude, type BuildFromIncludeResult } from "../../generator/index.js"; -import { createBranch, deleteBranch, getBranch, type CreateBranchOptions, type TinybirdBranch } from "../../api/branches.js"; +import { + BranchApiError, + createBranch, + deleteBranch, + getBranch, + type CreateBranchOptions, + type TinybirdBranch, +} from "../../api/branches.js"; import { deployToMain } from "../../api/deploy.js"; import { buildToTinybird } from "../../api/build.js"; import { @@ -224,6 +231,7 @@ export async function runPreview(options: PreviewCommandOptions = {}): Promise