Skip to content

Commit

Permalink
fix: respect CLOUDLFARE_ACCOUNT_ID with wrangler pages project (#6927)
Browse files Browse the repository at this point in the history
* let account id env override pages cache

* changeset

* fixup

* changeset
  • Loading branch information
emily-shen authored Oct 10, 2024
1 parent a24c86b commit 2af75ed
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .changeset/cuddly-waves-pump.md
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 7 additions & 0 deletions .changeset/warm-games-brush.md
Original file line number Diff line number Diff line change
@@ -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)
35 changes: 35 additions & 0 deletions packages/wrangler/src/__tests__/pages/project-create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>;
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"
);
});
});
33 changes: 33 additions & 0 deletions packages/wrangler/src/__tests__/pages/project-delete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
20 changes: 18 additions & 2 deletions packages/wrangler/src/__tests__/pages/project-list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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("");
Expand Down
10 changes: 7 additions & 3 deletions packages/wrangler/src/pages/projects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -23,7 +24,8 @@ export function ListOptions(yargs: CommonYargsArgv) {
export async function ListHandler() {
const config = getConfigCache<PagesConfigCache>(PAGES_CONFIG_CACHE_FILENAME);

const accountId = await requireAuth(config);
const accountId =
getCloudflareAccountIdFromEnv() ?? (await requireAuth(config));

const projects: Array<Project> = await listProjects({ accountId });

Expand Down Expand Up @@ -106,7 +108,8 @@ export async function CreateHandler({
projectName,
}: StrictYargsOptionsToInterface<typeof CreateOptions>) {
const config = getConfigCache<PagesConfigCache>(PAGES_CONFIG_CACHE_FILENAME);
const accountId = await requireAuth(config);
const accountId =
getCloudflareAccountIdFromEnv() ?? (await requireAuth(config));

const isInteractive = process.stdin.isTTY;
if (!projectName && isInteractive) {
Expand Down Expand Up @@ -204,7 +207,8 @@ export async function DeleteHandler(
args: StrictYargsOptionsToInterface<typeof DeleteOptions>
) {
const config = getConfigCache<PagesConfigCache>(PAGES_CONFIG_CACHE_FILENAME);
const accountId = await requireAuth(config);
const accountId =
getCloudflareAccountIdFromEnv() ?? (await requireAuth(config));

const confirmed =
args.yes ||
Expand Down

0 comments on commit 2af75ed

Please sign in to comment.