Skip to content

Commit

Permalink
remove extra slash in snapshot bucket path (#4590)
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig O'Donnell authored May 2, 2024
1 parent 4192bfe commit 27413c2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { App, SnapshotSettings } from "@types";
import { usePrevious } from "@src/hooks/usePrevious";
import { useCreateSnapshot } from "../api/createSnapshot";
import { useSnapshotSettings } from "../api/getSnapshotSettings";
import { Utilities } from "@src/utilities/utilities";

const DESTINATIONS = [
{
Expand Down Expand Up @@ -103,28 +104,28 @@ export const DashboardSnapshotsCard = (props: Props) => {
if (store?.aws) {
return setState({
selectedDestination: find(DESTINATIONS, ["value", "aws"]),
locationStr: `${store?.bucket}${store?.path ? `/${store?.path}` : ""}`,
locationStr: Utilities.snapshotLocationStr(store?.bucket, store?.path),
});
}

if (store?.azure) {
return setState({
selectedDestination: find(DESTINATIONS, ["value", "azure"]),
locationStr: `${store?.bucket}${store?.path ? `/${store?.path}` : ""}`,
locationStr: Utilities.snapshotLocationStr(store?.bucket, store?.path),
});
}

if (store?.gcp) {
return setState({
selectedDestination: find(DESTINATIONS, ["value", "gcp"]),
locationStr: `${store?.bucket}${store?.path ? `/${store?.path}` : ""}`,
locationStr: Utilities.snapshotLocationStr(store?.bucket, store?.path),
});
}

if (store?.other) {
return setState({
selectedDestination: find(DESTINATIONS, ["value", "other"]),
locationStr: `${store?.bucket}${store?.path ? `/${store?.path}` : ""}`,
locationStr: Utilities.snapshotLocationStr(store?.bucket, store?.path),
});
}

Expand Down
14 changes: 14 additions & 0 deletions web/src/utilities/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -1032,4 +1032,18 @@ export const Utilities = {
}
}
},

snapshotLocationStr(bucket, path) {
if (!bucket) {
return "";
}
if (!path) {
return bucket;
}
let prefix = path;
if (prefix && prefix.startsWith("/")) {
prefix = prefix.slice(1);
}
return `${bucket}/${prefix}`;
},
};
25 changes: 25 additions & 0 deletions web/src/utilities/utilities.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,29 @@ describe("Utilities", () => {
expect(Utilities.isInitialAppInstall(app)).toBe(false);
});
});

describe("snapshotLocationStr", () => {
it("should return bucket name if path is empty or undefined", () => {
expect(Utilities.snapshotLocationStr("my-bucket", "")).toBe("my-bucket");
expect(Utilities.snapshotLocationStr("my-bucket", undefined)).toBe(
"my-bucket"
);
});

it("should return bucket name and path if path is not empty", () => {
expect(Utilities.snapshotLocationStr("my-bucket", "my-path")).toBe(
"my-bucket/my-path"
);
});

it("should return bucket name and path if path is not empty and begins with a slash", () => {
expect(Utilities.snapshotLocationStr("my-bucket", "/my-path")).toBe(
"my-bucket/my-path"
);
});

it("should not error if bucket and path are undefined", () => {
expect(Utilities.snapshotLocationStr(undefined, undefined)).toBe("");
});
});
});

0 comments on commit 27413c2

Please sign in to comment.