From 70c7539a78b15236fa80363dda527fb30b40b035 Mon Sep 17 00:00:00 2001 From: Ulrik Andersen Date: Wed, 16 Oct 2024 21:00:54 +0200 Subject: [PATCH] Add support for spaces in specification filenames Performs a URLDecode of the pathname (e.g. "/acmeorg/foo/main/openapi%20with%20spaces.yml") to make sure we are able to find the file in GitHub. --- .../getProjectSelectionFromPath.test.ts | 66 +++++++++++++++++++ .../domain/getProjectSelectionFromPath.ts | 1 + 2 files changed, 67 insertions(+) diff --git a/__test__/projects/getProjectSelectionFromPath.test.ts b/__test__/projects/getProjectSelectionFromPath.test.ts index 7991fb1d..8cdea571 100644 --- a/__test__/projects/getProjectSelectionFromPath.test.ts +++ b/__test__/projects/getProjectSelectionFromPath.test.ts @@ -288,3 +288,69 @@ test("It moves specification ID to version ID if needed", () => { expect(sut.version!.id).toEqual("bar/baz") expect(sut.specification!.id).toEqual("hello") }) + +test("It supports specifications with spaces in their names", () => { + const sut = getProjectSelectionFromPath({ + path: "/acme/foo/main/openapi with spaces.yml", + projects: [ + { + id: 'acme-foo', + name: 'foo', + displayName: "Ulrik's Playground", + versions: [{ + id: 'main', + name: 'main', + specifications: [{ + id: "openapi with spaces.yml", + name: "openapi with spaces.yml", + url: "/api/blob/acme/foo-openapi/openapi with spaces.yml?ref=3dbafacbe57ac931ee750fc973bad3c81c9765bb", + editURL: "https://github.com/acme/foo-openapi/edit/main/openapi with spaces.yml" + }], + url: 'https://github.com/acme/foo-openapi/tree/main', + isDefault: true + }], + imageURL: '/api/blob/acme/foo-openapi/icon.png?ref=3dbafacbe57ac931ee750fc973bad3c81c9765bb', + url: 'https://github.com/acme/foo-openapi', + owner: 'acme', + ownerUrl: 'https://github.com/acme' + } + ] + }) + expect(sut.project!.owner).toEqual("acme") + expect(sut.project!.name).toEqual("foo") + expect(sut.version!.id).toEqual("main") + expect(sut.specification!.id).toEqual("openapi with spaces.yml") +}) + +test("It supports specifications with URL-encoded spaces in their names", () => { + const sut = getProjectSelectionFromPath({ + path: "/acme/foo/main/openapi%20with%20spaces.yml", + projects: [ + { + id: 'acme-foo', + name: 'foo', + displayName: "Ulrik's Playground", + versions: [{ + id: 'main', + name: 'main', + specifications: [{ + id: "openapi with spaces.yml", + name: "openapi with spaces.yml", + url: "/api/blob/acme/foo-openapi/openapi with spaces.yml?ref=3dbafacbe57ac931ee750fc973bad3c81c9765bb", + editURL: "https://github.com/acme/foo-openapi/edit/main/openapi with spaces.yml" + }], + url: 'https://github.com/acme/foo-openapi/tree/main', + isDefault: true + }], + imageURL: '/api/blob/acme/foo-openapi/icon.png?ref=3dbafacbe57ac931ee750fc973bad3c81c9765bb', + url: 'https://github.com/acme/foo-openapi', + owner: 'acme', + ownerUrl: 'https://github.com/acme' + } + ] + }) + expect(sut.project!.owner).toEqual("acme") + expect(sut.project!.name).toEqual("foo") + expect(sut.version!.id).toEqual("main") + expect(sut.specification!.id).toEqual("openapi with spaces.yml") +}) diff --git a/src/features/projects/domain/getProjectSelectionFromPath.ts b/src/features/projects/domain/getProjectSelectionFromPath.ts index e98307eb..b18c405c 100644 --- a/src/features/projects/domain/getProjectSelectionFromPath.ts +++ b/src/features/projects/domain/getProjectSelectionFromPath.ts @@ -16,6 +16,7 @@ export default function getProjectSelectionFromPath({ if (path.startsWith("/")) { path = path.substring(1) } + path = decodeURIComponent(path) const { owner: _owner, projectName: _projectName, versionId, specificationId } = guessSelection(path) // If no project is selected and the user only has a single project then we select that. let owner = _owner