Skip to content

Commit 98c96a5

Browse files
committed
Move Current.fetchPackageDenyList to @dependency
1 parent 24200dd commit 98c96a5

File tree

7 files changed

+38
-42
lines changed

7 files changed

+38
-42
lines changed

Sources/App/Commands/Reconcile.swift

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,11 @@ func reconcile(client: Client, database: Database) async throws {
6969
func reconcileMainPackageList(client: Client, database: Database) async throws -> [URL] {
7070
@Dependency(\.packageListRepository) var packageListRepository
7171

72-
async let sourcePackageList = try packageListRepository.fetchPackageList(client: client)
73-
async let sourcePackageDenyList = try Current.fetchPackageDenyList(client)
72+
let sourcePackageList = try await packageListRepository.fetchPackageList(client: client)
73+
let sourcePackageDenyList = try await packageListRepository.fetchPackageDenyList(client: client)
7474
async let currentList = try fetchCurrentPackageList(database)
7575

76-
let packageList = processPackageDenyList(packageList: try await sourcePackageList,
77-
denyList: try await sourcePackageDenyList)
76+
let packageList = processPackageDenyList(packageList: sourcePackageList, denyList: sourcePackageDenyList)
7877

7978
try await reconcileLists(db: database,
8079
source: packageList,
@@ -84,25 +83,6 @@ func reconcileMainPackageList(client: Client, database: Database) async throws -
8483
}
8584

8685

87-
@available(*, deprecated)
88-
func liveFetchPackageDenyList(_ client: Client) async throws -> [URL] {
89-
struct DeniedPackage: Decodable {
90-
var packageUrl: String
91-
92-
enum CodingKeys: String, CodingKey {
93-
case packageUrl = "package_url"
94-
}
95-
}
96-
97-
return try await client
98-
.get(Constants.packageDenyListUri)
99-
.content
100-
.decode([DeniedPackage].self, using: JSONDecoder())
101-
.map(\.packageUrl)
102-
.compactMap(URL.init(string:))
103-
}
104-
105-
10686
func fetchCurrentPackageList(_ db: Database) async throws -> [URL] {
10787
try await Package.query(on: db)
10888
.field(Package.self, \.$url)

Sources/App/Core/AppEnvironment.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ struct AppEnvironment: Sendable {
4343
var environment: @Sendable () -> Environment
4444
var fetchDocumentation: @Sendable (_ client: Client, _ url: URI) async throws -> ClientResponse
4545
var fetchHTTPStatusCode: @Sendable (_ url: String) async throws -> HTTPStatus
46-
var fetchPackageDenyList: @Sendable (_ client: Client) async throws -> [URL]
4746
var fetchLicense: @Sendable (_ client: Client, _ owner: String, _ repository: String) async -> Github.License?
4847
var fetchMetadata: @Sendable (_ client: Client, _ owner: String, _ repository: String) async throws -> Github.Metadata
4948
var fetchReadme: @Sendable (_ client: Client, _ owner: String, _ repository: String) async -> Github.Readme?
@@ -164,7 +163,6 @@ extension AppEnvironment {
164163
environment: { (try? Environment.detect()) ?? .development },
165164
fetchDocumentation: { client, url in try await client.get(url) },
166165
fetchHTTPStatusCode: { url in try await Networking.fetchHTTPStatusCode(url) },
167-
fetchPackageDenyList: { client in try await liveFetchPackageDenyList(client) },
168166
fetchLicense: { client, owner, repo in await Github.fetchLicense(client:client, owner: owner, repository: repo) },
169167
fetchMetadata: { client, owner, repo in try await Github.fetchMetadata(client:client, owner: owner, repository: repo) },
170168
fetchReadme: { client, owner, repo in await Github.fetchReadme(client:client, owner: owner, repository: repo) },

Sources/App/Core/Dependencies/PackageListRepositoryClient.swift

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,39 @@ import Vapor
1919

2020
@DependencyClient
2121
struct PackageListRepositoryClient {
22+
var fetchPackageList: @Sendable (_ client: Client) async throws -> [URL]
23+
var fetchPackageDenyList: @Sendable (_ client: Client) async throws -> [URL]
2224
var fetchCustomCollection: @Sendable (_ client: Client, _ url: URL) async throws -> [URL]
2325
var fetchCustomCollections: @Sendable (_ client: Client) async throws -> [CustomCollection.DTO]
24-
var fetchPackageList: @Sendable (_ client: Client) async throws -> [URL]
25-
// TODO: move other package list dependencies here
2626
}
2727

2828

2929
extension PackageListRepositoryClient: DependencyKey {
3030
static var liveValue: PackageListRepositoryClient {
3131
.init(
32+
fetchPackageList: { client in
33+
try await client
34+
.get(Constants.packageListUri)
35+
.content
36+
.decode([String].self, using: JSONDecoder())
37+
.compactMap(URL.init(string:))
38+
},
39+
fetchPackageDenyList: { client in
40+
struct DeniedPackage: Decodable {
41+
var packageUrl: String
42+
43+
enum CodingKeys: String, CodingKey {
44+
case packageUrl = "package_url"
45+
}
46+
}
47+
48+
return try await client
49+
.get(Constants.packageDenyListUri)
50+
.content
51+
.decode([DeniedPackage].self, using: JSONDecoder())
52+
.map(\.packageUrl)
53+
.compactMap(URL.init(string:))
54+
},
3255
fetchCustomCollection: { client, url in
3356
try await client
3457
.get(URI(string: url.absoluteString))
@@ -40,13 +63,6 @@ extension PackageListRepositoryClient: DependencyKey {
4063
.get(Constants.customCollectionsUri)
4164
.content
4265
.decode([CustomCollection.DTO].self, using: JSONDecoder())
43-
},
44-
fetchPackageList: { client in
45-
try await client
46-
.get(Constants.packageListUri)
47-
.content
48-
.decode([String].self, using: JSONDecoder())
49-
.compactMap(URL.init(string:))
5066
}
5167
)
5268
}

Tests/AppTests/MastodonTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ final class MastodonTests: AppTestCase {
5757

5858
try await withDependencies {
5959
$0.date.now = .now
60+
$0.packageListRepository.fetchPackageList = { @Sendable _ in [url.url] }
61+
$0.packageListRepository.fetchPackageDenyList = { @Sendable _ in [] }
6062
$0.packageListRepository.fetchCustomCollections = { @Sendable _ in [] }
6163
$0.packageListRepository.fetchCustomCollection = { @Sendable _, _ in [] }
62-
$0.packageListRepository.fetchPackageList = { @Sendable _ in [url.url] }
6364
} operation: {
6465
// run first two processing steps
6566
try await reconcile(client: app.client, database: app.db)

Tests/AppTests/Mocks/AppEnvironment+mock.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ extension AppEnvironment {
4242
environment: { .development },
4343
fetchDocumentation: { _, _ in .init(status: .ok) },
4444
fetchHTTPStatusCode: { _ in .ok },
45-
fetchPackageDenyList: { _ in
46-
["https://github.com/daveverwer/LeftPad"].asURLs
47-
},
4845
fetchLicense: { _, _, _ in .init(htmlUrl: "https://github.com/foo/bar/blob/main/LICENSE") },
4946
fetchMetadata: { _, _, _ in .mock },
5047
fetchReadme: { _, _, _ in .init(html: "readme html", htmlUrl: "readme html url", imagesToCache: []) },

Tests/AppTests/PipelineTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,10 @@ class PipelineTests: AppTestCase {
161161
let urls = ["1", "2", "3"].asGithubUrls
162162
try await withDependencies {
163163
$0.date.now = .now
164+
$0.packageListRepository.fetchPackageList = { @Sendable _ in urls.asURLs }
165+
$0.packageListRepository.fetchPackageDenyList = { @Sendable _ in [] }
164166
$0.packageListRepository.fetchCustomCollections = { @Sendable _ in [] }
165167
$0.packageListRepository.fetchCustomCollection = { @Sendable _, _ in [] }
166-
$0.packageListRepository.fetchPackageList = { @Sendable _ in urls.asURLs }
167168
} operation: {
168169
// Test pipeline pick-up end to end
169170
// setup

Tests/AppTests/ReconcilerTests.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class ReconcilerTests: AppTestCase {
3939
let urls = ["1", "2", "3"]
4040
try await withDependencies {
4141
$0.packageListRepository.fetchPackageList = { @Sendable _ in urls.asURLs }
42+
$0.packageListRepository.fetchPackageDenyList = { @Sendable _ in [] }
4243
} operation: {
4344
// MUT
4445
_ = try await reconcileMainPackageList(client: app.client, database: app.db)
@@ -67,6 +68,7 @@ class ReconcilerTests: AppTestCase {
6768

6869
try await withDependencies {
6970
$0.packageListRepository.fetchPackageList = { @Sendable _ in urls.asURLs }
71+
$0.packageListRepository.fetchPackageDenyList = { @Sendable _ in [] }
7072
} operation: {
7173
// MUT
7274
_ = try await reconcileMainPackageList(client: app.client, database: app.db)
@@ -88,10 +90,10 @@ class ReconcilerTests: AppTestCase {
8890

8991
// Deny list denies 2 and 4 (one existing and one new)
9092
let packageDenyList = ["2", "4"]
91-
Current.fetchPackageDenyList = { _ in packageDenyList.asURLs }
9293

9394
try await withDependencies {
9495
$0.packageListRepository.fetchPackageList = { @Sendable _ in packageList.asURLs }
96+
$0.packageListRepository.fetchPackageDenyList = { @Sendable _ in packageDenyList.asURLs }
9597
} operation: {
9698
// MUT
9799
_ = try await reconcileMainPackageList(client: app.client, database: app.db)
@@ -113,10 +115,10 @@ class ReconcilerTests: AppTestCase {
113115

114116
// Deny list denies one/one, but with incorrect casing.
115117
let packageDenyList = ["https://example.com/OnE/oNe"]
116-
Current.fetchPackageDenyList = { _ in packageDenyList.asURLs }
117118

118119
try await withDependencies {
119120
$0.packageListRepository.fetchPackageList = { @Sendable _ in packageList.asURLs }
121+
$0.packageListRepository.fetchPackageDenyList = { @Sendable _ in packageDenyList.asURLs }
120122
} operation: {
121123
// MUT
122124
_ = try await reconcileMainPackageList(client: app.client, database: app.db)
@@ -221,6 +223,8 @@ class ReconcilerTests: AppTestCase {
221223
struct TestError: Error { var message: String }
222224

223225
try await withDependencies {
226+
$0.packageListRepository.fetchPackageList = { @Sendable _ in fullPackageList }
227+
$0.packageListRepository.fetchPackageDenyList = { @Sendable _ in [] }
224228
$0.packageListRepository.fetchCustomCollection = { @Sendable _, url in
225229
if url == "collectionURL" {
226230
return [URL("2")]
@@ -231,7 +235,6 @@ class ReconcilerTests: AppTestCase {
231235
$0.packageListRepository.fetchCustomCollections = { @Sendable _ in
232236
[.init(name: "List", url: "collectionURL")]
233237
}
234-
$0.packageListRepository.fetchPackageList = { @Sendable _ in fullPackageList }
235238
} operation: {
236239
// MUT
237240
_ = try await reconcile(client: app.client, database: app.db)

0 commit comments

Comments
 (0)