Skip to content

Commit

Permalink
Base dependency check on input list instead of server list to avoid r…
Browse files Browse the repository at this point in the history
…esurrecting deleted packages
  • Loading branch information
finestructure committed Jan 15, 2024
1 parent d86d118 commit 9f5e3a4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 32 deletions.
31 changes: 10 additions & 21 deletions Sources/ValidatorCore/Commands/CheckDependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ public struct CheckDependencies: AsyncParsableCommand {
let start = Date()
defer { print("Elapsed (/min):", Date().timeIntervalSince(start)/60) }

let packageList = UniqueCanonicalPackageURLs(try inputSource.packageURLs())

// fetch all dependencies
let api = SwiftPackageIndexAPI(baseURL: apiBaseURL, apiToken: spiApiToken)
let records = try await Current.fetchDependencies(api)
let serverPackages = records.allPackages
print("Total packages (server):", serverPackages.count)
print("Total packages (server):", records.count)
print("Total packages (input):", packageList.count)

let allDependencies = records.allDependencies
let missing = allDependencies.subtracting(serverPackages)
let missing = allDependencies.subtracting(packageList)
print("Not indexed:", missing.count)

let client = HTTPClient(eventLoopGroupProvider: .singleton,
Expand Down Expand Up @@ -80,7 +82,7 @@ public struct CheckDependencies: AsyncParsableCommand {
print(" ... redirected to:", resolved)
}

if serverPackages.contains(resolved.canonicalPackageURL) {
if packageList.contains(resolved.canonicalPackageURL) {
print(" ... ⛔ already indexed")
continue
}
Expand All @@ -105,21 +107,14 @@ public struct CheckDependencies: AsyncParsableCommand {

print("New packages:", newPackages.count)
for (idx, p) in newPackages
.sorted(by: { $0.packageURL.absoluteString < $1.packageURL.absoluteString })
.sorted()
.enumerated() {
print(" ✅ ADD", idx, p.packageURL)
print(" ✅ ADD", idx, p)
}

// merge with existing and sort result
#warning("This is a temporary fix!")
let packageList = try inputSource.packageURLs()
let server = serverPackages.map(\.packageURL)
let deleted = Set(server).subtracting(packageList)
let merged = Array(newPackages.map(\.value.packageURL))
.mergingWithExisting(urls: server)
.mergingWithExisting(urls: packageList)
.filter { !deleted.contains($0) }
.sorted(by: { $0.lowercased() < $1.lowercased() })
let merged = (Array(packageList.map(\.packageURL)) + Array(newPackages.map(\.value.packageURL)))
.sorted()

print("Total:", merged.count)

Expand Down Expand Up @@ -166,12 +161,6 @@ extension CheckDependencies {


extension [SwiftPackageIndexAPI.PackageRecord] {
var allPackages: UniqueCanonicalPackageURLs {
Set(
map { HashedCanonicalPackageURL($0.url) }
)
}

var allDependencies: UniqueCanonicalPackageURLs {
let deps = flatMap { $0.resolvedDependencies ?? [] }
return Set(
Expand Down
13 changes: 13 additions & 0 deletions Sources/ValidatorCore/UniqueCanonicalPackageURLs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ extension UniqueCanonicalPackageURLs {
let res = insert(.init(newMember, transform: \.canonicalPath))
return (res.inserted, res.memberAfterInsert.value)
}

init(_ urls: [PackageURL]) {
self = Set(urls.map(\.canonicalPackageURL).map { .init($0, transform: \.canonicalPath) })
}

func sorted() -> [PackageURL] {
map(\.packageURL).sorted()
}
}


extension [PackageURL] {
func sorted() -> Self {
sorted(by: { $0.lowercased() < $1.lowercased() })
}
}
12 changes: 1 addition & 11 deletions Tests/ValidatorTests/ExtensionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ import CanonicalPackageURL

final class ExtensionsTests: XCTestCase {

func test_allPackages() throws {
let records: [SwiftPackageIndexAPI.PackageRecord] = [
.init(.p1, [.p3]),
.init(.p2, [.p4]),
.init(.p3, [.p2, .p4, .p5]),
]
XCTAssertEqual(records.allPackages.sorted(by: { $0.canonicalPath < $1.canonicalPath }).map(\.path),
[CanonicalPackageURL.p1, .p2, .p3].map(\.path))
}

func test_allDependencies() throws {
let records: [SwiftPackageIndexAPI.PackageRecord] = [
.init(.p1, [.p3]),
Expand All @@ -47,7 +37,7 @@ final class ExtensionsTests: XCTestCase {
.init(.p1, []),
.init(.p2, [p1_prime, .p3]),
]
let missing = records.allDependencies.subtracting(records.allPackages)
let missing = records.allDependencies.subtracting(.init([.p1, .p2]))
XCTAssertEqual(missing.count, 1)
XCTAssertEqual(missing.first?.canonicalPath, CanonicalPackageURL.p3.canonicalPath)
}
Expand Down

0 comments on commit 9f5e3a4

Please sign in to comment.