Skip to content

Commit 2daabc5

Browse files
committed
Move Current.githubToken → github.token
1 parent a2a95cd commit 2daabc5

File tree

6 files changed

+27
-34
lines changed

6 files changed

+27
-34
lines changed

Sources/App/Core/AppEnvironment.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ struct AppEnvironment: Sendable {
2727
var fileManager: FileManager
2828
var getStatusCount: @Sendable (_ client: Client, _ status: Gitlab.Builder.Status) async throws -> Int
2929
var git: Git
30-
var githubToken: @Sendable () -> String?
3130
var gitlabApiToken: @Sendable () -> String?
3231
var gitlabPipelineToken: @Sendable () -> String?
3332
var gitlabPipelineLimit: @Sendable () -> Int
@@ -73,7 +72,6 @@ extension AppEnvironment {
7372
maxPageCount: 5)
7473
},
7574
git: .live,
76-
githubToken: { Environment.get("GITHUB_TOKEN") },
7775
gitlabApiToken: { Environment.get("GITLAB_API_TOKEN") },
7876
gitlabPipelineToken: { Environment.get("GITLAB_PIPELINE_TOKEN") },
7977
gitlabPipelineLimit: {

Sources/App/Core/Dependencies/GithubClient.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import Dependencies
1717
import IssueReporting
18+
import Vapor
1819

1920

2021
// We currently cannot use @DependencyClient here due to
@@ -24,6 +25,7 @@ struct GithubClient {
2425
var fetchLicense: @Sendable (_ owner: String, _ repository: String) async -> Github.License?
2526
var fetchMetadata: @Sendable (_ owner: String, _ repository: String) async throws(Github.Error) -> Github.Metadata = { _,_ in reportIssue("fetchMetadata"); return .init() }
2627
var fetchReadme: @Sendable (_ owner: String, _ repository: String) async -> Github.Readme?
28+
var token: @Sendable () -> String?
2729
}
2830

2931

@@ -32,7 +34,8 @@ extension GithubClient: DependencyKey {
3234
.init(
3335
fetchLicense: { owner, repo in await Github.fetchLicense(owner: owner, repository: repo) },
3436
fetchMetadata: { owner, repo throws(Github.Error) in try await Github.fetchMetadata(owner: owner, repository: repo) },
35-
fetchReadme: { owner, repo in await Github.fetchReadme(owner: owner, repository: repo) }
37+
fetchReadme: { owner, repo in await Github.fetchReadme(owner: owner, repository: repo) },
38+
token: { Environment.get("GITHUB_TOKEN") }
3639
)
3740
}
3841
}
@@ -43,7 +46,8 @@ extension GithubClient: TestDependencyKey {
4346
.init(
4447
fetchLicense: { _, _ in unimplemented("fetchLicense"); return nil },
4548
fetchMetadata: { _, _ in unimplemented("fetchMetadata"); return .init() },
46-
fetchReadme: { _, _ in unimplemented("fetchReadme"); return nil }
49+
fetchReadme: { _, _ in unimplemented("fetchReadme"); return nil },
50+
token: { unimplemented("token"); return nil }
4751
)
4852
}
4953
}

Sources/App/Core/Github.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,13 @@ extension Github {
8787
}
8888

8989
static func fetch(url: String, headers: [(String, String)] = []) async throws -> (content: String, etag: String?) {
90-
guard let token = Current.githubToken() else {
90+
@Dependency(\.github) var github
91+
@Dependency(\.httpClient) var httpClient
92+
93+
guard let token = github.token() else {
9194
throw Error.missingToken
9295
}
9396

94-
@Dependency(\.httpClient) var httpClient
9597

9698
let response = try await httpClient.get(url: url, headers: defaultHeaders(with: token).adding(contentsOf: headers))
9799

@@ -114,7 +116,9 @@ extension Github {
114116
}
115117

116118
static func fetchResource<T: Decodable>(_ type: T.Type, url: String) async throws -> T {
117-
guard let token = Current.githubToken() else {
119+
@Dependency(\.github) var github
120+
121+
guard let token = github.token() else {
118122
throw Error.missingToken
119123
}
120124

@@ -176,7 +180,9 @@ extension Github {
176180
}
177181

178182
static func fetchResource<T: Decodable>(_ type: T.Type, query: GraphQLQuery) async throws(Github.Error) -> T {
179-
guard let token = Current.githubToken() else {
183+
@Dependency(\.github) var github
184+
185+
guard let token = github.token() else {
180186
throw Error.missingToken
181187
}
182188

Tests/AppTests/GithubTests.swift

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ class GithubTests: AppTestCase {
103103
}
104104

105105
func test_fetchResource() async throws {
106-
Current.githubToken = { "secr3t" }
107106
struct Response: Decodable, Equatable {
108107
var data: Data
109108
struct Data: Decodable, Equatable {
@@ -116,6 +115,7 @@ class GithubTests: AppTestCase {
116115
let q = Github.GraphQLQuery(query: "query { viewer { login } }")
117116

118117
try await withDependencies {
118+
$0.github.token = { "secr3t" }
119119
$0.httpClient.post = { @Sendable _, _, _ in
120120
.ok(body: #"{"data":{"viewer":{"login":"finestructure"}}}"#)
121121
}
@@ -126,10 +126,10 @@ class GithubTests: AppTestCase {
126126
}
127127

128128
func test_fetchMetadata() async throws {
129-
Current.githubToken = { "secr3t" }
130129
let iso8601 = ISO8601DateFormatter()
131130

132131
try await withDependencies {
132+
$0.github.token = { "secr3t" }
133133
$0.httpClient.post = { @Sendable _, _, _ in
134134
try .ok(fixture: "github-graphql-resource.json")
135135
}
@@ -181,9 +181,8 @@ class GithubTests: AppTestCase {
181181
}
182182

183183
func test_fetchMetadata_badRequest() async throws {
184-
Current.githubToken = { "secr3t" }
185-
186184
await withDependencies {
185+
$0.github.token = { "secr3t" }
187186
$0.httpClient.post = { @Sendable _, _, _ in .badRequest }
188187
} operation: {
189188
do {
@@ -200,10 +199,8 @@ class GithubTests: AppTestCase {
200199
}
201200

202201
func test_fetchMetadata_badData() async throws {
203-
// setup
204-
Current.githubToken = { "secr3t" }
205-
206202
await withDependencies {
203+
$0.github.token = { "secr3t" }
207204
$0.httpClient.post = { @Sendable _, _, _ in .ok(body: "bad data") }
208205
} operation: {
209206
// MUT
@@ -225,10 +222,8 @@ class GithubTests: AppTestCase {
225222

226223
func test_fetchMetadata_rateLimiting_429() async throws {
227224
// Github doesn't actually send a 429 when you hit the rate limit
228-
// setup
229-
Current.githubToken = { "secr3t" }
230-
231225
await withDependencies {
226+
$0.github.token = { "secr3t" }
232227
$0.httpClient.post = { @Sendable _, _, _ in .tooManyRequests }
233228
} operation: {
234229
// MUT
@@ -278,10 +273,8 @@ class GithubTests: AppTestCase {
278273
// X-RateLimit-Limit: 60
279274
// X-RateLimit-Remaining: 56
280275
// Ensure we record it as a rate limit error and raise a Rollbar item
281-
// setup
282-
Current.githubToken = { "secr3t" }
283-
284276
await withDependencies {
277+
$0.github.token = { "secr3t" }
285278
$0.httpClient.post = { @Sendable _, _, _ in
286279
.init(status: .forbidden, headers: ["X-RateLimit-Remaining": "0"])
287280
}
@@ -313,10 +306,8 @@ class GithubTests: AppTestCase {
313306
}
314307

315308
func test_fetchLicense() async throws {
316-
// setup
317-
Current.githubToken = { "secr3t" }
318-
319309
await withDependencies {
310+
$0.github.token = { "secr3t" }
320311
$0.httpClient.get = { @Sendable _, _ in
321312
try .ok(fixture: "github-license-response.json")
322313
}
@@ -331,10 +322,8 @@ class GithubTests: AppTestCase {
331322

332323
func test_fetchLicense_notFound() async throws {
333324
// https://github.com/SwiftPackageIndex/SwiftPackageIndex-Server/issues/761
334-
// setup
335-
Current.githubToken = { "secr3t" }
336-
337325
await withDependencies {
326+
$0.github.token = { "secr3t" }
338327
$0.httpClient.get = { @Sendable _, _ in .notFound }
339328
} operation: {
340329
// MUT
@@ -346,10 +335,9 @@ class GithubTests: AppTestCase {
346335
}
347336

348337
func test_fetchReadme() async throws {
349-
// setup
350-
Current.githubToken = { "secr3t" }
351338
let requestCount = QueueIsolated(0)
352339
await withDependencies {
340+
$0.github.token = { "secr3t" }
353341
$0.httpClient.get = { @Sendable _, headers in
354342
requestCount.increment()
355343
switch headers[.accept] {
@@ -383,10 +371,8 @@ class GithubTests: AppTestCase {
383371
}
384372

385373
func test_fetchReadme_notFound() async throws {
386-
// setup
387-
Current.githubToken = { "secr3t" }
388-
389374
await withDependencies {
375+
$0.github.token = { "secr3t" }
390376
$0.httpClient.get = { @Sendable _, headers in .notFound }
391377
} operation: {
392378
// MUT

Tests/AppTests/IngestionTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@ class IngestionTests: AppTestCase {
640640
// use mock for metadata request which we're not interested in ...
641641
$0.github.fetchMetadata = { @Sendable _, _ in .init() }
642642
$0.github.fetchReadme = { @Sendable _, _ in nil }
643+
$0.github.token = { "token" }
643644
$0.httpClient.get = { @Sendable url, _ in
644645
if url.hasSuffix("/license") {
645646
return .notFound
@@ -653,7 +654,6 @@ class IngestionTests: AppTestCase {
653654
// setup
654655
let pkg = Package(url: "https://github.com/foo/1")
655656
try await pkg.save(on: app.db)
656-
Current.githubToken = { "token" }
657657

658658
// MUT
659659
let (_, license, _) = try await Ingestion.fetchMetadata(package: pkg, owner: "foo", repository: "1")

Tests/AppTests/Mocks/AppEnvironment+mock.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ extension AppEnvironment {
2626
fileManager: .mock,
2727
getStatusCount: { _, _ in 100 },
2828
git: .mock,
29-
githubToken: { nil },
3029
gitlabApiToken: { nil },
3130
gitlabPipelineToken: { nil },
3231
gitlabPipelineLimit: { Constants.defaultGitlabPipelineLimit },

0 commit comments

Comments
 (0)