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

remove extra slash in snapshot bucket path #4590

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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("");
});
});
});