From 2af75edb3c0722c04793c74f46aa099f4a3f27a9 Mon Sep 17 00:00:00 2001 From: emily-shen <69125074+emily-shen@users.noreply.github.com> Date: Thu, 10 Oct 2024 15:26:20 +0100 Subject: [PATCH] fix: respect CLOUDLFARE_ACCOUNT_ID with `wrangler pages project` (#6927) * let account id env override pages cache * changeset * fixup * changeset --- .changeset/cuddly-waves-pump.md | 7 ++++ .changeset/warm-games-brush.md | 7 ++++ .../__tests__/pages/project-create.test.ts | 35 +++++++++++++++++++ .../__tests__/pages/project-delete.test.ts | 33 +++++++++++++++++ .../src/__tests__/pages/project-list.test.ts | 20 +++++++++-- packages/wrangler/src/pages/projects.tsx | 10 ++++-- 6 files changed, 107 insertions(+), 5 deletions(-) create mode 100644 .changeset/cuddly-waves-pump.md create mode 100644 .changeset/warm-games-brush.md diff --git a/.changeset/cuddly-waves-pump.md b/.changeset/cuddly-waves-pump.md new file mode 100644 index 000000000000..e553f4f6faef --- /dev/null +++ b/.changeset/cuddly-waves-pump.md @@ -0,0 +1,7 @@ +--- +"wrangler": patch +--- + +fix: respect `CLOUDLFARE_ACCOUNT_ID` with `wrangler pages project` commands + +Fixes [#4947](https://github.com/cloudflare/workers-sdk/issues/4947) diff --git a/.changeset/warm-games-brush.md b/.changeset/warm-games-brush.md new file mode 100644 index 000000000000..7e896af0e9b3 --- /dev/null +++ b/.changeset/warm-games-brush.md @@ -0,0 +1,7 @@ +--- +"wrangler": patch +--- + +fix: respect CLOUDFLARE_ACCOUNT_ID with `wrangler pages project` + +Fixes [#4947](https://github.com/cloudflare/workers-sdk/issues/4947) diff --git a/packages/wrangler/src/__tests__/pages/project-create.test.ts b/packages/wrangler/src/__tests__/pages/project-create.test.ts index a64f1248ca98..a0645c99e228 100644 --- a/packages/wrangler/src/__tests__/pages/project-create.test.ts +++ b/packages/wrangler/src/__tests__/pages/project-create.test.ts @@ -148,4 +148,39 @@ describe("pages project create", () => { To deploy a folder of assets, run 'wrangler pages deploy [directory]'." `); }); + + it("should override cached accountId with CLOUDFLARE_ACCOUNT_ID environmental variable if provided", async () => { + msw.use( + http.post( + "*/accounts/:accountId/pages/projects", + async ({ request, params }) => { + const body = (await request.json()) as Record; + expect(params.accountId).toEqual("new-account-id"); + return HttpResponse.json( + { + success: true, + errors: [], + messages: [], + result: { + ...body, + subdomain: "an-existing-project.pages.dev", + }, + }, + { status: 200 } + ); + }, + { once: true } + ) + ); + vi.mock("getConfigCache", () => { + return { + account_id: "original-account-id", + project_name: "an-existing-project", + }; + }); + vi.stubEnv("CLOUDFLARE_ACCOUNT_ID", "new-account-id"); + await runWrangler( + "pages project create an-existing-project --production-branch=main --compatibility-date 2022-03-08" + ); + }); }); diff --git a/packages/wrangler/src/__tests__/pages/project-delete.test.ts b/packages/wrangler/src/__tests__/pages/project-delete.test.ts index 6996e522402e..3d1879ce1155 100644 --- a/packages/wrangler/src/__tests__/pages/project-delete.test.ts +++ b/packages/wrangler/src/__tests__/pages/project-delete.test.ts @@ -110,4 +110,37 @@ describe("pages project delete", () => { Successfully deleted some-project-name" `); }); + + it("should override cached accountId with CLOUDFLARE_ACCOUNT_ID environmental variable if provided", async () => { + msw.use( + http.delete( + "*/accounts/:accountId/pages/projects/:projectName", + async ({ params }) => { + expect(params.accountId).toEqual("new-account-id"); + return HttpResponse.json( + { + result: null, + success: true, + errors: [], + messages: [], + }, + { status: 200 } + ); + }, + { once: true } + ) + ); + mockConfirm({ + text: `Are you sure you want to delete "an-existing-project"? This action cannot be undone.`, + result: true, + }); + vi.mock("getConfigCache", () => { + return { + account_id: "original-account-id", + project_name: "an-existing-project", + }; + }); + vi.stubEnv("CLOUDFLARE_ACCOUNT_ID", "new-account-id"); + await runWrangler("pages project delete an-existing-project"); + }); }); diff --git a/packages/wrangler/src/__tests__/pages/project-list.test.ts b/packages/wrangler/src/__tests__/pages/project-list.test.ts index 23a9738a2820..8b7aac1ba1e9 100644 --- a/packages/wrangler/src/__tests__/pages/project-list.test.ts +++ b/packages/wrangler/src/__tests__/pages/project-list.test.ts @@ -75,13 +75,29 @@ describe("pages project list", () => { await runWrangler("pages project list"); expect(requests.count).toEqual(2); }); + + it("should override cached accountId with CLOUDFLARE_ACCOUNT_ID environmental variable if provided", async () => { + vi.mock("getConfigCache", () => { + return { + account_id: "original-account-id", + project_name: "an-existing-project", + }; + }); + vi.stubEnv("CLOUDFLARE_ACCOUNT_ID", "new-account-id"); + const requests = mockProjectListRequest([], "new-account-id"); + await runWrangler("pages project list"); + expect(requests.count).toBe(1); + }); }); /* -------------------------------------------------- */ /* Helper Functions */ /* -------------------------------------------------- */ -function mockProjectListRequest(projects: unknown[]) { +function mockProjectListRequest( + projects: unknown[], + accountId = "some-account-id" +) { const requests = { count: 0 }; msw.use( http.get( @@ -94,7 +110,7 @@ function mockProjectListRequest(projects: unknown[]) { const page = Number(url.searchParams.get("page")); const expectedPageSize = 10; const expectedPage = requests.count; - expect(params.accountId).toEqual("some-account-id"); + expect(params.accountId).toEqual(accountId); expect(pageSize).toEqual(expectedPageSize); expect(page).toEqual(expectedPage); expect(await request.text()).toEqual(""); diff --git a/packages/wrangler/src/pages/projects.tsx b/packages/wrangler/src/pages/projects.tsx index 761099a23671..5297c85a2fa7 100644 --- a/packages/wrangler/src/pages/projects.tsx +++ b/packages/wrangler/src/pages/projects.tsx @@ -8,6 +8,7 @@ import { FatalError } from "../errors"; import { logger } from "../logger"; import * as metrics from "../metrics"; import { requireAuth } from "../user"; +import { getCloudflareAccountIdFromEnv } from "../user/auth-variables"; import { renderToString } from "../utils/render"; import { PAGES_CONFIG_CACHE_FILENAME } from "./constants"; import type { @@ -23,7 +24,8 @@ export function ListOptions(yargs: CommonYargsArgv) { export async function ListHandler() { const config = getConfigCache(PAGES_CONFIG_CACHE_FILENAME); - const accountId = await requireAuth(config); + const accountId = + getCloudflareAccountIdFromEnv() ?? (await requireAuth(config)); const projects: Array = await listProjects({ accountId }); @@ -106,7 +108,8 @@ export async function CreateHandler({ projectName, }: StrictYargsOptionsToInterface) { const config = getConfigCache(PAGES_CONFIG_CACHE_FILENAME); - const accountId = await requireAuth(config); + const accountId = + getCloudflareAccountIdFromEnv() ?? (await requireAuth(config)); const isInteractive = process.stdin.isTTY; if (!projectName && isInteractive) { @@ -204,7 +207,8 @@ export async function DeleteHandler( args: StrictYargsOptionsToInterface ) { const config = getConfigCache(PAGES_CONFIG_CACHE_FILENAME); - const accountId = await requireAuth(config); + const accountId = + getCloudflareAccountIdFromEnv() ?? (await requireAuth(config)); const confirmed = args.yes ||