From f1b477231c585bcdab6192d50584b97667fd8fd9 Mon Sep 17 00:00:00 2001 From: Rahul Sethi <5822355+RamIdeas@users.noreply.github.com> Date: Fri, 8 Mar 2024 23:13:24 +0000 Subject: [PATCH] port Source display logic from legacy deployments command for versions view/list --- .../__tests__/versions/versions.list.test.ts | 16 +++---- .../__tests__/versions/versions.view.test.ts | 4 +- packages/wrangler/src/versions/list.ts | 47 +++++++++++++++++-- packages/wrangler/src/versions/view.ts | 15 +----- 4 files changed, 56 insertions(+), 26 deletions(-) diff --git a/packages/wrangler/src/__tests__/versions/versions.list.test.ts b/packages/wrangler/src/__tests__/versions/versions.list.test.ts index 83f9fbeac006..c9dd7250637f 100644 --- a/packages/wrangler/src/__tests__/versions/versions.list.test.ts +++ b/packages/wrangler/src/__tests__/versions/versions.list.test.ts @@ -44,28 +44,28 @@ describe("versions list", () => { "Version ID: 40000000-0000-0000-0000-000000000000 Created: 1/1/2021, 12:00:00 AM Author: Jean-Luc-Picard@federation.org - Source: wrangler + Source: Upload Tag: - Message: - Version ID: 30000000-0000-0000-0000-000000000000 Created: 2/2/2021, 12:00:00 AM Author: Kathryn-Janeway@federation.org - Source: wrangler + Source: Rollback Tag: - Message: Rolled back for this version Version ID: 20000000-0000-0000-0000-000000000000 Created: 2/3/2021, 12:00:00 AM Author: Kathryn-Janeway@federation.org - Source: wrangler + Source: Wrangler 🤠 Tag: - Message: - Version ID: 10000000-0000-0000-0000-000000000000 Created: 1/4/2021, 12:00:00 AM Author: Jean-Luc-Picard@federation.org - Source: wrangler + Source: Rollback Tag: - Message: - " @@ -89,28 +89,28 @@ describe("versions list", () => { "Version ID: 40000000-0000-0000-0000-000000000000 Created: 1/1/2021, 12:00:00 AM Author: Jean-Luc-Picard@federation.org - Source: wrangler + Source: Upload Tag: - Message: - Version ID: 30000000-0000-0000-0000-000000000000 Created: 2/2/2021, 12:00:00 AM Author: Kathryn-Janeway@federation.org - Source: wrangler + Source: Rollback Tag: - Message: Rolled back for this version Version ID: 20000000-0000-0000-0000-000000000000 Created: 2/3/2021, 12:00:00 AM Author: Kathryn-Janeway@federation.org - Source: wrangler + Source: Wrangler 🤠 Tag: - Message: - Version ID: 10000000-0000-0000-0000-000000000000 Created: 1/4/2021, 12:00:00 AM Author: Jean-Luc-Picard@federation.org - Source: wrangler + Source: Rollback Tag: - Message: - " diff --git a/packages/wrangler/src/__tests__/versions/versions.view.test.ts b/packages/wrangler/src/__tests__/versions/versions.view.test.ts index 7c390bf54200..05bb97cf2c99 100644 --- a/packages/wrangler/src/__tests__/versions/versions.view.test.ts +++ b/packages/wrangler/src/__tests__/versions/versions.view.test.ts @@ -112,7 +112,7 @@ describe("versions view", () => { "Version ID: 10000000-0000-0000-0000-000000000000 Created: 1/1/2021, 12:00:00 AM Author: Jean-Luc-Picard@federation.org - Source: wrangler + Source: Upload Tag: - Message: - " @@ -170,7 +170,7 @@ describe("versions view", () => { "Version ID: 10000000-0000-0000-0000-000000000000 Created: 1/1/2021, 12:00:00 AM Author: Jean-Luc-Picard@federation.org - Source: wrangler + Source: Upload Tag: - Message: - " diff --git a/packages/wrangler/src/versions/list.ts b/packages/wrangler/src/versions/list.ts index 1ff5cafe2f3c..e512cb89048f 100644 --- a/packages/wrangler/src/versions/list.ts +++ b/packages/wrangler/src/versions/list.ts @@ -29,7 +29,7 @@ type ApiVersion = { author_id: string; author_email: string; }; - annotations?: Record & { + annotations?: { "workers/triggered_by"?: "upload" | string; "workers/message"?: string; "workers/tag"?: string; @@ -75,14 +75,14 @@ export async function versionsListHandler(args: VersionsListArgs) { "Version ID:": version.id, "Created:": new Date(version.metadata["created_on"]).toLocaleString(), "Author:": version.metadata.author_email, - "Source:": version.metadata.source, + "Source:": getSource(version), "Tag:": version.annotations?.["workers/tag"] ?? BLANK_INPUT, "Message:": version.annotations?.["workers/message"] ?? BLANK_INPUT, }); } } -function getConfig( +export function getConfig( args: Pick ) { const configPath = @@ -91,3 +91,44 @@ function getConfig( return config; } + +export function getSource(version: { + metadata: Pick; + annotations?: Pick< + NonNullable, + "workers/triggered_by" + >; +}) { + return version.annotations?.["workers/triggered_by"] === undefined + ? formatSource(version.metadata.source) + : formatTrigger(version.annotations["workers/triggered_by"]); +} + +export function formatSource(source: string): string { + switch (source) { + case "api": + return "API 📡"; + case "dash": + return "Dashboard 🖥️"; + case "wrangler": + return "Wrangler 🤠"; + case "terraform": + return "Terraform 🏗️"; + default: + return "Other"; + } +} +export function formatTrigger(trigger: string): string { + switch (trigger) { + case "upload": + return "Upload"; + case "secret": + return "Secret Change"; + case "rollback": + return "Rollback"; + case "promotion": + return "Promotion"; + default: + return "Unknown"; + } +} diff --git a/packages/wrangler/src/versions/view.ts b/packages/wrangler/src/versions/view.ts index 263956de5ba9..19e6597294dc 100644 --- a/packages/wrangler/src/versions/view.ts +++ b/packages/wrangler/src/versions/view.ts @@ -1,11 +1,10 @@ -import path from "path"; import { fetchResult } from "../cfetch"; -import { findWranglerToml, readConfig } from "../config"; import { UserError } from "../errors"; import * as metrics from "../metrics"; import { printWranglerBanner } from "../update-check"; import { requireAuth } from "../user"; import renderLabelledValues from "../utils/render-labelled-values"; +import { getConfig, getSource } from "./list"; import type { CommonYargsArgv, StrictYargsOptionsToInterface, @@ -81,18 +80,8 @@ export async function versionsViewHandler(args: VersionsViewArgs) { "Version ID:": version.id, "Created:": new Date(version.metadata["created_on"]).toLocaleString(), "Author:": version.metadata.author_email, - "Source:": version.metadata.source, + "Source:": getSource(version), "Tag:": version.annotations?.["workers/tag"] ?? BLANK_INPUT, "Message:": version.annotations?.["workers/message"] ?? BLANK_INPUT, }); } - -function getConfig( - args: Pick -) { - const configPath = - args.config || (args.name && findWranglerToml(path.dirname(args.name))); - const config = readConfig(configPath, args); - - return config; -}