Skip to content

Commit 63b0ed4

Browse files
Resolve PR comments
1 parent e69091c commit 63b0ed4

File tree

3 files changed

+61
-49
lines changed

3 files changed

+61
-49
lines changed

Sources/App/Commands/Ingest.swift

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,7 @@ func ingest(client: Client,
125125
s3Readme = .error("\(error)")
126126
}
127127

128-
let fork: Fork?
129-
do {
130-
fork = try await getFork(on: database, metadata: metadata)
131-
} catch {
132-
fork = nil
133-
Current.logger().warning("updating forked from failed")
134-
}
128+
let fork: Fork? = try? await getFork(on: database, parent: metadata.repository?.parent)
135129

136130
try await updateRepository(on: database,
137131
for: repo,
@@ -223,13 +217,10 @@ func updateRepository(on database: Database,
223217
try await repository.save(on: database)
224218
}
225219

226-
func getFork(on database: Database, metadata: Github.Metadata) async throws -> Fork? {
227-
guard let url = metadata.repository?.parent?.url,
228-
let parentUrl = URL(string: url)?.normalizedParent?.absoluteString else {
229-
return nil
230-
}
220+
func getFork(on database: Database, parent: Github.Metadata.Parent?) async -> Fork? {
221+
guard let parentUrl = parent?.normalizedURL else { return nil }
231222

232-
if let packageId = try await Package.query(on: database)
223+
if let packageId = try? await Package.query(on: database)
233224
.filter(\.$url, .custom("ilike"), parentUrl)
234225
.first()?.id {
235226
return .parentId(packageId)
@@ -250,11 +241,13 @@ private extension Github.Metadata.Repository {
250241
var repositoryName: String? { name }
251242
}
252243

253-
private extension URL {
254-
var normalizedParent: Self? {
255-
guard var components = URLComponents(url: self, resolvingAgainstBaseURL: false) else { return nil }
256-
if components.scheme == "http" { components.scheme = "https" }
257-
if !components.path.hasSuffix(".git") { components.path = components.path + ".git" }
258-
return components.url!
244+
private extension Github.Metadata.Parent {
245+
// Returns a normalized version of the URL. Adding a `.git` if not present.
246+
var normalizedURL: String? {
247+
guard let url else { return nil }
248+
guard let normalizedURL = URL(string: url)?.normalized?.absoluteString else {
249+
return nil
250+
}
251+
return normalizedURL
259252
}
260253
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright Dave Verwer, Sven A. Schmidt, and other contributors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import Foundation
16+
17+
extension URL {
18+
var normalized: Self? {
19+
guard var components = URLComponents(url: self, resolvingAgainstBaseURL: false) else { return nil }
20+
if components.scheme == "http" { components.scheme = "https" }
21+
if !components.path.hasSuffix(".git") { components.path = components.path + ".git" }
22+
return components.url!
23+
}
24+
}

Tests/AppTests/IngestorTests.swift

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -604,36 +604,31 @@ class IngestorTests: AppTestCase {
604604
try await Package(id: .id0, url: "https://github.com/foo/parent.git".url, processingStage: .analysis).save(on: app.db)
605605
try await Package(url: "https://github.com/bar/forked.git", processingStage: .analysis).save(on: app.db)
606606

607-
var metadata = Github.Metadata.mock
608-
609-
do { // test lookup when package is in the index
610-
metadata.repository?.parent = .init(url: "https://github.com/foo/parent.git")
611-
let fork = try await getFork(on: app.db, metadata: metadata)
612-
XCTAssertEqual(fork, .parentId(.id0))
613-
}
614-
615-
do { // test lookup when package is in the index but with different case in URL
616-
metadata.repository?.parent = .init(url: "https://github.com/Foo/Parent.git")
617-
let fork = try await getFork(on: app.db, metadata: metadata)
618-
XCTAssertEqual(fork, .parentId(.id0))
619-
}
607+
var parent: Github.Metadata.Parent?
620608

621-
do { // test whem metadata repo url doesn't have `.git` at end
622-
metadata.repository?.parent = .init(url: "https://github.com/Foo/Parent")
623-
let fork = try await getFork(on: app.db, metadata: metadata)
624-
XCTAssertEqual(fork, .parentId(.id0))
625-
}
626-
627-
do { // test lookup when package is not in the index
628-
metadata.repository?.parent = .init(url: "https://github.com/some/other.git")
629-
let fork = try await getFork(on: app.db, metadata: metadata)
630-
XCTAssertEqual(fork, .parentURL("https://github.com/some/other.git"))
631-
}
632-
633-
do { // test lookup when parent url is nil
634-
metadata.repository?.parent = .init(url: nil)
635-
let fork = try await getFork(on: app.db, metadata: metadata)
636-
XCTAssertEqual(fork, nil)
637-
}
609+
// test lookup when package is in the index
610+
parent = .init(url: "https://github.com/foo/parent.git")
611+
let fork = await getFork(on: app.db, parent: parent)
612+
XCTAssertEqual(fork, .parentId(.id0))
613+
614+
// test lookup when package is in the index but with different case in URL
615+
parent = .init(url: "https://github.com/Foo/Parent.git")
616+
let fork2 = await getFork(on: app.db, parent: parent)
617+
XCTAssertEqual(fork2, .parentId(.id0))
618+
619+
// test whem metadata repo url doesn't have `.git` at end
620+
parent = .init(url: "https://github.com/Foo/Parent")
621+
let fork3 = await getFork(on: app.db, parent: parent)
622+
XCTAssertEqual(fork3, .parentId(.id0))
623+
624+
// test lookup when package is not in the index
625+
parent = .init(url: "https://github.com/some/other.git")
626+
let fork4 = await getFork(on: app.db, parent: parent)
627+
XCTAssertEqual(fork4, .parentURL("https://github.com/some/other.git"))
628+
629+
// test lookup when parent url is nil
630+
parent = nil
631+
let fork5 = await getFork(on: app.db, parent: parent)
632+
XCTAssertEqual(fork5, nil)
638633
}
639634
}

0 commit comments

Comments
 (0)