Skip to content

Commit

Permalink
Resort to fallback URL when package can't be found
Browse files Browse the repository at this point in the history
  • Loading branch information
supersonicbyte committed Sep 18, 2024
1 parent 0b40fce commit 57a0b21
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ extension API.PackageController.GetRoute {
static func forkedFromInfo(on database: Database, fork: Fork?) async -> Model.ForkedFromInfo? {
guard let forkedFrom = fork else { return nil }
switch forkedFrom {
case let .parentId(id):
return await Model.ForkedFromInfo.query(on: database, packageId: id)
case .parentId(let id, let fallbackURL):
return await Model.ForkedFromInfo.query(on: database, packageId: id, fallbackURL: fallbackURL)
case let .parentURL(url):
return .fromGitHub(url: url)
}
Expand All @@ -100,15 +100,15 @@ extension API.PackageController.GetRoute {


extension API.PackageController.GetRoute.Model.ForkedFromInfo {
static func query(on database: Database, packageId: Package.Id) async -> Self? {
static func query(on database: Database, packageId: Package.Id, fallbackURL: String) async -> Self? {
let model = try? await Joined3<Package, Repository, Version>
.query(on: database, packageId: packageId, version: .defaultBranch)
.first()

guard let repoName = model?.repository.name,
let ownerName = model?.repository.ownerName,
let owner = model?.repository.owner else {
return nil
return .fromGitHub(url: fallbackURL)
}

return .fromSPI(originalOwner: owner,
Expand Down
11 changes: 10 additions & 1 deletion Tests/AppTests/API+PackageController+GetRoute+ModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class API_PackageController_GetRoute_ModelTests: SnapshotTestCase {
.save(on: app.db)

// MUT
let forkedFrom = await API.PackageController.GetRoute.Model.ForkedFromInfo.query(on: app.db, packageId: .id0)
let forkedFrom = await API.PackageController.GetRoute.Model.ForkedFromInfo.query(on: app.db, packageId: .id0, fallbackURL: "https://github.com/original/original.git")

// validate
XCTAssertEqual(forkedFrom, .fromSPI(originalOwner: "original",
Expand All @@ -141,6 +141,15 @@ class API_PackageController_GetRoute_ModelTests: SnapshotTestCase {
originalPackageName: "OriginalPkg"))
}

func test_ForkedFromInfo_query_fallback() async throws {
// when the package can't be found resort to fallback URL
// MUT
let forkedFrom = await API.PackageController.GetRoute.Model.ForkedFromInfo.query(on: app.db, packageId: .id0, fallbackURL: "https://github.com/original/original.git")

// validate
XCTAssertEqual(forkedFrom, .fromGitHub(url: "https://github.original/original.git"))

Check failure on line 150 in Tests/AppTests/API+PackageController+GetRoute+ModelTests.swift

View workflow job for this annotation

GitHub Actions / Test

test_ForkedFromInfo_query_fallback, XCTAssertEqual failed: ("Optional(App.API.PackageController.GetRoute.Model.ForkedFromInfo.fromGitHub(url: "https://github.com/original/original.git"))") is not equal to ("Optional(App.API.PackageController.GetRoute.Model.ForkedFromInfo.fromGitHub(url: "https://github.original/original.git"))") -
}

func test_gitHubOwnerUrl() throws {
var model = API.PackageController.GetRoute.Model.mock
model.repositoryOwner = "owner"
Expand Down

0 comments on commit 57a0b21

Please sign in to comment.