From bb968b0a32aa76b90f26a25c847df619a12126a3 Mon Sep 17 00:00:00 2001 From: Ucanbarlic Date: Wed, 18 Sep 2024 11:38:07 +0200 Subject: [PATCH 1/3] Update Fork to containt fallback url --- Sources/App/Commands/Ingest.swift | 2 +- .../080/UpdateRepositoryAddForkedFrom2.swift | 31 +++++++++++++++++++ Sources/App/Models/Repository.swift | 2 +- Sources/App/configure.swift | 3 ++ 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 Sources/App/Migrations/080/UpdateRepositoryAddForkedFrom2.swift diff --git a/Sources/App/Commands/Ingest.swift b/Sources/App/Commands/Ingest.swift index 2278d1337..311915592 100644 --- a/Sources/App/Commands/Ingest.swift +++ b/Sources/App/Commands/Ingest.swift @@ -223,7 +223,7 @@ func getFork(on database: Database, parent: Github.Metadata.Parent?) async -> Fo if let packageId = try? await Package.query(on: database) .filter(\.$url, .custom("ilike"), parentUrl) .first()?.id { - return .parentId(packageId) + return .parentId(id: packageId, fallbackURL: parentUrl) } else { return .parentURL(parentUrl) } diff --git a/Sources/App/Migrations/080/UpdateRepositoryAddForkedFrom2.swift b/Sources/App/Migrations/080/UpdateRepositoryAddForkedFrom2.swift new file mode 100644 index 000000000..2e84c1e82 --- /dev/null +++ b/Sources/App/Migrations/080/UpdateRepositoryAddForkedFrom2.swift @@ -0,0 +1,31 @@ +// Copyright Dave Verwer, Sven A. Schmidt, and other contributors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import Fluent +import SQLKit + + +struct UpdateRepositoryAddForkedFrom2: AsyncMigration { + func prepare(on database: Database) async throws { + guard let db = database as? SQLDatabase else { + fatalError("Database must be an SQLDatabase ('as? SQLDatabase' must succeed)") + } + + try await db.raw(#"UPDATE "repositories" SET forked_from = NULL"#).run() + } + + func revert(on database: Database) async throws { + // There's nothing we can do to restore the previous state + } +} diff --git a/Sources/App/Models/Repository.swift b/Sources/App/Models/Repository.swift index 20ab78a28..22338a08d 100644 --- a/Sources/App/Models/Repository.swift +++ b/Sources/App/Models/Repository.swift @@ -273,6 +273,6 @@ enum S3Readme: Codable, Equatable { } enum Fork: Codable, Equatable { - case parentId(Package.Id) + case parentId(id: Package.Id, fallbackURL: String) case parentURL(String) } diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 48f48c512..7784b038f 100644 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -337,6 +337,9 @@ public func configure(_ app: Application) async throws -> String { do { // Migration 079 - Add `forked_from` to `repositories` app.migrations.add(UpdateRepositoryAddForkedFrom()) } + do { // Migration 080 - Set`forkded_from` to NULL because of Fork model change in Repository + app.migrations.add(UpdateRepositoryAddForkedFrom2()) + } app.asyncCommands.use(Analyze.Command(), as: "analyze") app.asyncCommands.use(CreateRestfileCommand(), as: "create-restfile") From 56a39ac63604c21c6ad063fb431333ea85f2a110 Mon Sep 17 00:00:00 2001 From: Ucanbarlic Date: Wed, 18 Sep 2024 11:46:05 +0200 Subject: [PATCH 2/3] Fix tests --- Tests/AppTests/IngestorTests.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Tests/AppTests/IngestorTests.swift b/Tests/AppTests/IngestorTests.swift index 01378c213..0804fe49f 100644 --- a/Tests/AppTests/IngestorTests.swift +++ b/Tests/AppTests/IngestorTests.swift @@ -606,16 +606,16 @@ class IngestorTests: AppTestCase { // test lookup when package is in the index let fork = await getFork(on: app.db, parent: .init(url: "https://github.com/foo/parent.git")) - XCTAssertEqual(fork, .parentId(.id0)) - + XCTAssertEqual(fork, .parentId(id: .id0, fallbackURL: "https://github.com/foo/parent.git")) + // test lookup when package is in the index but with different case in URL let fork2 = await getFork(on: app.db, parent: .init(url: "https://github.com/Foo/Parent.git")) - XCTAssertEqual(fork2, .parentId(.id0)) - + XCTAssertEqual(fork2, .parentId(id: .id0, fallbackURL: "https://github.com/Foo/Parent.git")) + // test whem metadata repo url doesn't have `.git` at end let fork3 = await getFork(on: app.db, parent: .init(url: "https://github.com/Foo/Parent")) - XCTAssertEqual(fork3, .parentId(.id0)) - + XCTAssertEqual(fork3, .parentId(id: .id0, fallbackURL: "https://github.com/Foo/Parent.git")) + // test lookup when package is not in the index let fork4 = await getFork(on: app.db, parent: .init(url: "https://github.com/some/other.git")) XCTAssertEqual(fork4, .parentURL("https://github.com/some/other.git")) From 6052f57696885b5a7c8c797047b0717f5663c60d Mon Sep 17 00:00:00 2001 From: Ucanbarlic Date: Wed, 18 Sep 2024 12:15:06 +0200 Subject: [PATCH 3/3] Resolve PR comments --- ...orkedFrom2.swift => UpdateRepositoryResetForkedFrom.swift} | 2 +- Sources/App/configure.swift | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename Sources/App/Migrations/080/{UpdateRepositoryAddForkedFrom2.swift => UpdateRepositoryResetForkedFrom.swift} (95%) diff --git a/Sources/App/Migrations/080/UpdateRepositoryAddForkedFrom2.swift b/Sources/App/Migrations/080/UpdateRepositoryResetForkedFrom.swift similarity index 95% rename from Sources/App/Migrations/080/UpdateRepositoryAddForkedFrom2.swift rename to Sources/App/Migrations/080/UpdateRepositoryResetForkedFrom.swift index 2e84c1e82..1ac0b761e 100644 --- a/Sources/App/Migrations/080/UpdateRepositoryAddForkedFrom2.swift +++ b/Sources/App/Migrations/080/UpdateRepositoryResetForkedFrom.swift @@ -16,7 +16,7 @@ import Fluent import SQLKit -struct UpdateRepositoryAddForkedFrom2: AsyncMigration { +struct UpdateRepositoryResetForkedFrom: AsyncMigration { func prepare(on database: Database) async throws { guard let db = database as? SQLDatabase else { fatalError("Database must be an SQLDatabase ('as? SQLDatabase' must succeed)") diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 7784b038f..28cbd3fb6 100644 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -337,8 +337,8 @@ public func configure(_ app: Application) async throws -> String { do { // Migration 079 - Add `forked_from` to `repositories` app.migrations.add(UpdateRepositoryAddForkedFrom()) } - do { // Migration 080 - Set`forkded_from` to NULL because of Fork model change in Repository - app.migrations.add(UpdateRepositoryAddForkedFrom2()) + do { // Migration 080 - Set`forked_from` to NULL because of Fork model change in Repository + app.migrations.add(UpdateRepositoryResetForkedFrom()) } app.asyncCommands.use(Analyze.Command(), as: "analyze")