Skip to content

Commit

Permalink
wip reconcile
Browse files Browse the repository at this point in the history
  • Loading branch information
finestructure committed Oct 9, 2024
1 parent 60a22ca commit d456643
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 9 deletions.
61 changes: 53 additions & 8 deletions Sources/App/Commands/Reconcile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import Dependencies
import Fluent
import Vapor

Expand Down Expand Up @@ -48,16 +49,24 @@ struct ReconcileCommand: AsyncCommand {
func reconcile(client: Client, database: Database) async throws {
let start = DispatchTime.now().uptimeNanoseconds
defer { AppMetrics.reconcileDurationSeconds?.time(since: start) }
async let sourcePackageList = try Current.fetchPackageList(client)
async let sourcePackageDenyList = try Current.fetchPackageDenyList(client)
async let currentList = try fetchCurrentPackageList(database)

let packageList = processPackageDenyList(packageList: try await sourcePackageList,
denyList: try await sourcePackageDenyList)
do { // reconcile main package list
async let sourcePackageList = try Current.fetchPackageList(client)
async let sourcePackageDenyList = try Current.fetchPackageDenyList(client)
async let currentList = try fetchCurrentPackageList(database)

try await reconcileLists(db: database,
source: packageList,
target: currentList)
let packageList = processPackageDenyList(packageList: try await sourcePackageList,
denyList: try await sourcePackageDenyList)

try await reconcileLists(db: database,
source: packageList,
target: currentList)
}

do { // reconcile custom package collections
// - fetch custom-package-collections.json
// - for each entry: reconcileCustomCollection
}
}


Expand Down Expand Up @@ -115,6 +124,7 @@ func reconcileLists(db: Database, source: [URL], target: [URL]) async throws {
}
}


func processPackageDenyList(packageList: [URL], denyList: [URL]) -> [URL] {
// Note: If the implementation of this function ever changes, the `RemoveDenyList`
// command in the Validator will also need updating to match.
Expand All @@ -140,3 +150,38 @@ func processPackageDenyList(packageList: [URL], denyList: [URL]) -> [URL] {
.subtracting(Set(denyList.map(CaseInsensitiveURL.init)))
).map(\.url)
}


func reconcileCustomCollection(client: Client, database: Database, fullPackageList: [URL], collectionURL: URL) async throws {
@Dependency(\.packageListRepository) var packageListRepository

// - fetch list
let list = try await packageListRepository.fetchCustomCollection(client: client, url: collectionURL)

// impose a limit because we're dealing with an input outside our control
let urls = list.prefix(50)

// lookup packages
// current assumptions:
// - url must exist in the full list
// - url must be spelled exactly like it is in the full list
let packages = Package.query(on: database).filter(by: urls)

Check warning on line 168 in Sources/App/Commands/Reconcile.swift

View workflow job for this annotation

GitHub Actions / Test

initialization of immutable value 'packages' was never used; consider replacing with assignment to '_' or removing it

// - reconcile the list against its custom collection
// - load collection
guard let collection = try await CustomCollection.query(on: database)
.filter(\.$url == collectionURL.absoluteString)
.first() else {
throw Abort(.notFound)
}
try await collection.$packages.load(on: database)
let currentURLs = Set(collection.packages.map(\.url))
let incomingURLs = Set(urls.map(\.absoluteString))
let toAdd = incomingURLs.subtracting(currentURLs)
let toDelete = currentURLs.subtracting(incomingURLs)

// - add/remove as needed
for pkg in toAdd {

}
}
20 changes: 19 additions & 1 deletion Tests/AppTests/ReconcilerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import XCTest

@testable import App

import Dependencies
import Vapor
import XCTest


class ReconcilerTests: AppTestCase {
Expand Down Expand Up @@ -114,4 +116,20 @@ class ReconcilerTests: AppTestCase {
let packages = try await Package.query(on: app.db).all()
XCTAssertEqual(packages.map(\.url).sorted(), ["https://example.com/two/two"])
}

func test_reconcileCustomCollections() async throws {
let fullPackageList = [URL("a"), URL("b"), URL("c")]
try await withDependencies {
$0.packageListRepository.fetchCustomCollection = { @Sendable _, _ in
[URL("b")]
}
} operation: {
// MUT
// try await reconcileCustomCollection(client: app.client,
// database: app.db,
// fullPackageList: fullPackageList,
// collectionURL: URL("collection"))
}
}

}

0 comments on commit d456643

Please sign in to comment.