Skip to content
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
249 changes: 249 additions & 0 deletions __test__/projects/GitHubProjectDataSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1091,3 +1091,252 @@ test("It identifies the default branch in returned versions", async () => {
.map(e => e.name)
expect(defaultVersionNames).toEqual(["development"])
})

test("It adds remote versions from the project configuration", async () => {
const rawProjectConfig = `
remoteVersions:
- name: Anne
specifications:
- name: Huey
url: https://example.com/huey.yml
- name: Dewey
url: https://example.com/dewey.yml
- name: Bobby
specifications:
- name: Louie
url: https://example.com/louie.yml
`
const sut = new GitHubProjectDataSource({
dataSource: {
async getRepositories() {
return [{
name: "foo",
owner: {
login: "acme"
},
defaultBranchRef: {
name: "main",
target: {
oid: "12345678"
}
},
configYml: {
text: rawProjectConfig
},
branches: {
edges: []
},
tags: {
edges: []
}
}]
}
}
})
const projects = await sut.getProjects()
expect(projects[0].versions).toEqual([{
id: "anne",
name: "Anne",
isDefault: false,
specifications: [{
id: "huey",
name: "Huey",
url: `/api/proxy?url=${encodeURIComponent("https://example.com/huey.yml")}`
}, {
id: "dewey",
name: "Dewey",
url: `/api/proxy?url=${encodeURIComponent("https://example.com/dewey.yml")}`
}]
}, {
id: "bobby",
name: "Bobby",
isDefault: false,
specifications: [{
id: "louie",
name: "Louie",
url: `/api/proxy?url=${encodeURIComponent("https://example.com/louie.yml")}`
}]
}])
})

test("It modifies ID of remote version if the ID already exists", async () => {
const rawProjectConfig = `
remoteVersions:
- name: Bar
specifications:
- name: Baz
url: https://example.com/baz.yml
- name: Bar
specifications:
- name: Hello
url: https://example.com/hello.yml
`
const sut = new GitHubProjectDataSource({
dataSource: {
async getRepositories() {
return [{
name: "foo",
owner: {
login: "acme"
},
defaultBranchRef: {
name: "bar",
target: {
oid: "12345678"
}
},
configYml: {
text: rawProjectConfig
},
branches: {
edges: [{
node: {
name: "bar",
target: {
oid: "12345678",
tree: {
entries: [{
name: "openapi.yml"
}]
}
}
}
}]
},
tags: {
edges: []
}
}]
}
}
})
const projects = await sut.getProjects()
expect(projects[0].versions).toEqual([{
id: "bar",
name: "bar",
url: "https://github.com/acme/foo/tree/bar",
isDefault: true,
specifications: [{
id: "openapi.yml",
name: "openapi.yml",
url: "/api/blob/acme/foo/openapi.yml?ref=12345678",
editURL: "https://github.com/acme/foo/edit/bar/openapi.yml"
}]
}, {
id: "bar1",
name: "Bar",
isDefault: false,
specifications: [{
id: "baz",
name: "Baz",
url: `/api/proxy?url=${encodeURIComponent("https://example.com/baz.yml")}`
}]
}, {
id: "bar2",
name: "Bar",
isDefault: false,
specifications: [{
id: "hello",
name: "Hello",
url: `/api/proxy?url=${encodeURIComponent("https://example.com/hello.yml")}`
}]
}])
})

test("It lets users specify the ID of a remote version", async () => {
const rawProjectConfig = `
remoteVersions:
- id: some-version
name: Bar
specifications:
- name: Baz
url: https://example.com/baz.yml
`
const sut = new GitHubProjectDataSource({
dataSource: {
async getRepositories() {
return [{
name: "foo",
owner: {
login: "acme"
},
defaultBranchRef: {
name: "bar",
target: {
oid: "12345678"
}
},
configYml: {
text: rawProjectConfig
},
branches: {
edges: []
},
tags: {
edges: []
}
}]
}
}
})
const projects = await sut.getProjects()
expect(projects[0].versions).toEqual([{
id: "some-version",
name: "Bar",
isDefault: false,
specifications: [{
id: "baz",
name: "Baz",
url: `/api/proxy?url=${encodeURIComponent("https://example.com/baz.yml")}`
}]
}])
})

test("It lets users specify the ID of a remote specification", async () => {
const rawProjectConfig = `
remoteVersions:
- name: Bar
specifications:
- id: some-spec
name: Baz
url: https://example.com/baz.yml
`
const sut = new GitHubProjectDataSource({
dataSource: {
async getRepositories() {
return [{
name: "foo",
owner: {
login: "acme"
},
defaultBranchRef: {
name: "bar",
target: {
oid: "12345678"
}
},
configYml: {
text: rawProjectConfig
},
branches: {
edges: []
},
tags: {
edges: []
}
}]
}
}
})
const projects = await sut.getProjects()
expect(projects[0].versions).toEqual([{
id: "bar",
name: "Bar",
isDefault: false,
specifications: [{
id: "some-spec",
name: "Baz",
url: `/api/proxy?url=${encodeURIComponent("https://example.com/baz.yml")}`
}]
}])
})
24 changes: 24 additions & 0 deletions __test__/projects/getSelection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,27 @@ test("It returns a undefined specification when the selected specification canno
expect(sut.version!.id).toEqual("bar")
expect(sut.specification).toBeUndefined()
})

test("It moves specification ID to version ID if needed", () => {
const sut = getSelection({
path: "/foo/bar/baz",
projects: [{
id: "foo",
name: "foo",
displayName: "foo",
versions: [{
id: "bar/baz",
name: "bar",
isDefault: false,
specifications: [{
id: "hello",
name: "hello.yml",
url: "https://example.com/hello.yml"
}]
}]
}]
})
expect(sut.project!.id).toEqual("foo")
expect(sut.version!.id).toEqual("bar/baz")
expect(sut.specification!.id).toEqual("hello")
})
17 changes: 17 additions & 0 deletions src/app/api/proxy/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { NextRequest, NextResponse } from "next/server"
import { makeAPIErrorResponse } from "@/common"

export async function GET(req: NextRequest) {
const rawURL = req.nextUrl.searchParams.get("url")
if (!rawURL) {
return makeAPIErrorResponse(400, "Missing \"url\" query parameter.")
}
let url: URL
try {
url = new URL(rawURL)
} catch {
return makeAPIErrorResponse(400, "Invalid \"url\" query parameter.")
}
const file = await fetch(url).then(r => r.blob())
return new NextResponse(file, { status: 200 })
}
Loading