Skip to content

Commit 3eafc1f

Browse files
Merge pull request #559 from shapehq/feature/sort-specifications-alphabetically
Sort specifications alphabetically
2 parents 581476c + 78d57a0 commit 3eafc1f

File tree

2 files changed

+70
-12
lines changed

2 files changed

+70
-12
lines changed

__test__/projects/GitHubProjectDataSource.test.ts

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,6 @@ test("It supports multiple OpenAPI specifications on a branch", async () => {
194194
id: "main",
195195
name: "main",
196196
specifications: [{
197-
id: "foo-service.yml",
198-
name: "foo-service.yml",
199-
url: "/api/blob/acme/foo-openapi/foo-service.yml?ref=12345678",
200-
editURL: "https://github.com/acme/foo-openapi/edit/main/foo-service.yml",
201-
isDefault: false
202-
}, {
203197
id: "bar-service.yml",
204198
name: "bar-service.yml",
205199
url: "/api/blob/acme/foo-openapi/bar-service.yml?ref=12345678",
@@ -211,6 +205,13 @@ test("It supports multiple OpenAPI specifications on a branch", async () => {
211205
url: "/api/blob/acme/foo-openapi/baz-service.yml?ref=12345678",
212206
editURL: "https://github.com/acme/foo-openapi/edit/main/baz-service.yml",
213207
isDefault: false
208+
},
209+
{
210+
id: "foo-service.yml",
211+
name: "foo-service.yml",
212+
url: "/api/blob/acme/foo-openapi/foo-service.yml?ref=12345678",
213+
editURL: "https://github.com/acme/foo-openapi/edit/main/foo-service.yml",
214+
isDefault: false
214215
}],
215216
url: "https://github.com/acme/foo-openapi/tree/main",
216217
isDefault: true
@@ -663,6 +664,55 @@ test("It prioritizes main, master, develop, and development branch names when so
663664
expect(projects[0].versions[5].name).toEqual("anne")
664665
})
665666

667+
test("It sorts specifications alphabetically", async () => {
668+
const sut = new GitHubProjectDataSource({
669+
repositoryNameSuffix: "-openapi",
670+
repositoryDataSource: {
671+
async getRepositories() {
672+
return [{
673+
owner: "acme",
674+
name: "foo-openapi",
675+
defaultBranchRef: {
676+
id: "12345678",
677+
name: "main"
678+
},
679+
configYaml: {
680+
text: "name: Hello World"
681+
},
682+
branches: [{
683+
id: "12345678",
684+
name: "anne",
685+
files: [{
686+
name: "z-openapi.yml",
687+
}, {
688+
name: "a-openapi.yml",
689+
}, {
690+
name: "1-openapi.yml",
691+
}]
692+
}],
693+
tags: [{
694+
id: "12345678",
695+
name: "cathrine",
696+
files: [{
697+
name: "o-openapi.yml",
698+
}, {
699+
name: "2-openapi.yml",
700+
}]
701+
}]
702+
}]
703+
}
704+
},
705+
encryptionService: noopEncryptionService,
706+
remoteConfigEncoder: base64RemoteConfigEncoder
707+
})
708+
const projects = await sut.getProjects()
709+
expect(projects[0].versions[0].specifications[0].name).toEqual("1-openapi.yml")
710+
expect(projects[0].versions[0].specifications[1].name).toEqual("a-openapi.yml")
711+
expect(projects[0].versions[0].specifications[2].name).toEqual("z-openapi.yml")
712+
expect(projects[0].versions[1].specifications[0].name).toEqual("2-openapi.yml")
713+
expect(projects[0].versions[1].specifications[1].name).toEqual("o-openapi.yml")
714+
})
715+
666716
test("It identifies the default branch in returned versions", async () => {
667717
const sut = new GitHubProjectDataSource({
668718
repositoryNameSuffix: "-openapi",
@@ -753,15 +803,15 @@ test("It adds remote versions from the project configuration", async () => {
753803
name: "Anne",
754804
isDefault: false,
755805
specifications: [{
756-
id: "huey",
757-
name: "Huey",
758-
url: `/api/remotes/${base64RemoteConfigEncoder.encode({ url: "https://example.com/huey.yml" })}`,
759-
isDefault: false
760-
}, {
761806
id: "dewey",
762807
name: "Dewey",
763808
url: `/api/remotes/${base64RemoteConfigEncoder.encode({ url: "https://example.com/dewey.yml" })}`,
764809
isDefault: false
810+
}, {
811+
id: "huey",
812+
name: "Huey",
813+
url: `/api/remotes/${base64RemoteConfigEncoder.encode({ url: "https://example.com/huey.yml" })}`,
814+
isDefault: false
765815
}]
766816
}, {
767817
id: "bobby",

src/features/projects/data/GitHubProjectDataSource.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ export default class GitHubProjectDataSource implements IProjectDataSource {
6565
).filter(version => {
6666
return version.specifications.length > 0
6767
})
68-
.map(version => this.setDefaultSpecification(version, config?.defaultSpecificationName));
68+
.map(version => this.setDefaultSpecification(version, config?.defaultSpecificationName))
69+
.map(version => this.sortSpecificationsByName(version));
6970
const defaultName = repository.name.replace(new RegExp(this.repositoryNameSuffix + "$"), "")
7071
return {
7172
id: `${repository.owner}-${defaultName}`,
@@ -259,4 +260,11 @@ export default class GitHubProjectDataSource implements IProjectDataSource {
259260
}))
260261
}
261262
}
263+
264+
private sortSpecificationsByName(version: Version): Version {
265+
return {
266+
...version,
267+
specifications: version.specifications.sort((a, b) => a.name.localeCompare(b.name))
268+
}
269+
}
262270
}

0 commit comments

Comments
 (0)