Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add wrangler pages deployment list #758

Merged
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
7 changes: 7 additions & 0 deletions .changeset/thin-paws-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

feature: Add wrangler pages deployment list

Renders a list of deployments in a Cloudflare Pages project
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"jest": "^27.5.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.5.1",
"timeago.js": "^4.0.2",
"typescript": "^4.6.2"
},
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"path-to-regexp": "^6.2.0",
"selfsigned": "^2.0.0",
"semiver": "^1.1.0",
"timeago.js": "^4.0.2",
"xxhash-wasm": "^1.0.1"
},
"optionalDependencies": {
Expand Down
50 changes: 49 additions & 1 deletion packages/wrangler/src/__tests__/pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
import { mockConsoleMethods } from "./helpers/mock-console";
import { runInTempDir } from "./helpers/run-in-tmp";
import { runWrangler } from "./helpers/run-wrangler";
import type { Project } from "../pages";
import type { Project, Deployment } from "../pages";

describe("subcommand implicit help ran on incomplete command execution", () => {
runInTempDir();
Expand Down Expand Up @@ -129,4 +129,52 @@ describe("subcommand implicit help ran on incomplete command execution", () => {
expect(requests.count).toEqual(2);
});
});

describe("deployment list", () => {
mockAccountId();
mockApiToken();

afterEach(() => {
unsetAllMocks();
});
function mockListRequest(deployments: unknown[]) {
const requests = { count: 0 };
setMockResponse(
"/accounts/:accountId/pages/projects/:project/deployments",
([_url, accountId, project]) => {
requests.count++;
expect(project).toEqual("images");
expect(accountId).toEqual("some-account-id");
return deployments;
}
);
return requests;
}

it("should make request to list deployments", async () => {
const deployments: Deployment[] = [
{
id: "87bbc8fe-16be-45cd-81e0-63d722e82cdf",
url: "https://87bbc8fe.images.pages.dev",
environment: "preview",
latest_stage: {
ended_on: "2021-11-17T14:52:26.133835Z",
status: "success",
},
deployment_trigger: {
metadata: {
branch: "main",
commit_hash: "c7649364c4cb32ad4f65b530b9424e8be5bec9d6",
},
},
project_name: "images",
},
];

const requests = mockListRequest(deployments);
await runWrangler("pages deployment list --project=images");

expect(requests.count).toBe(1);
});
});
});
68 changes: 68 additions & 0 deletions packages/wrangler/src/pages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ export type Project = {
};
};

export type Deployment = {
id: string;
environment: string;
deployment_trigger: {
metadata: {
commit_hash: string;
branch: string;
};
};
url: string;
latest_stage: {
status: string;
ended_on: string;
};
project_name: string;
};

// Defer importing miniflare until we really need it. This takes ~0.5s
// and also modifies some `stream/web` and `undici` prototypes, so we
// don't want to do this if pages commands aren't being called.
Expand Down Expand Up @@ -1102,6 +1119,57 @@ export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
render(<Table data={data}></Table>);
}
)
)
.command("deployment", false, (yargs) =>
yargs.command(
"list",
"List deployments in your Cloudflare Pages project",
(yargs) =>
yargs.options({
project: {
type: "string",
demandOption: true,
description:
"The name of the project you would like to list deployments for",
},
}),
async (args) => {
const config = readConfig(args.config as ConfigPath, args);
const accountId = await requireAuth(config);

const deployments: Array<Deployment> = await fetchResult(
`/accounts/${accountId}/pages/projects/${args.project}/deployments`
);

const titleCase = (word: string) =>
word.charAt(0).toUpperCase() + word.slice(1);

const shortSha = (sha: string) => sha.slice(0, 7);

const getStatus = (deployment: Deployment) => {
// Return a pretty time since timestamp if successful otherwise the status
if (deployment.latest_stage.status === `success`) {
return format(deployment.latest_stage.ended_on);
}
return titleCase(deployment.latest_stage.status);
};

const data = deployments.map((deployment) => {
return {
Environment: titleCase(deployment.environment),
Branch: deployment.deployment_trigger.metadata.branch,
Source: shortSha(
deployment.deployment_trigger.metadata.commit_hash
),
Deployment: deployment.url,
Status: getStatus(deployment),
// TODO: Use a url shortener
Build: `https://dash.cloudflare.com/${accountId}/pages/view/${deployment.project_name}/${deployment.id}`,
};
});
render(<Table data={data}></Table>);
}
)
);
};

Expand Down