Skip to content

Commit 79152ec

Browse files
authored
[Collections] Fix auth token lookup (#3461)
Motivation: In `AuthTokenType` `host` is the GitHub host (e.g., `github.com`), but when we look up auth token we use the GitHub API host (e.g., `api.github.com`). As a result the auth tokens are not used. Modification: Look up auth tokens with GitHub host by removing `api.` prefix. rdar://77293200
1 parent 5d84f0c commit 79152ec

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

Sources/PackageCollections/Providers/GitHubPackageMetadataProvider.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import PackageModel
1919
import TSCBasic
2020

2121
struct GitHubPackageMetadataProvider: PackageMetadataProvider {
22+
private static let apiHostPrefix = "api."
23+
2224
public var name: String = "GitHub"
2325

2426
var configuration: Configuration
@@ -179,7 +181,7 @@ struct GitHubPackageMetadataProvider: PackageMetadataProvider {
179181
let owner = String(url[ownerRange])
180182
let repo = String(url[repoRange])
181183

182-
return URL(string: "https://api.\(host)/repos/\(owner)/\(repo)")
184+
return URL(string: "https://\(Self.apiHostPrefix)\(host)/repos/\(owner)/\(repo)")
183185
}
184186
}
185187
return nil
@@ -194,7 +196,8 @@ struct GitHubPackageMetadataProvider: PackageMetadataProvider {
194196
options.validResponseCodes = validResponseCodes
195197
options.authorizationProvider = { url in
196198
url.host.flatMap { host in
197-
self.configuration.authTokens()?[.github(host)].flatMap { token in
199+
let host = host.hasPrefix(Self.apiHostPrefix) ? String(host.dropFirst(Self.apiHostPrefix.count)) : host
200+
return self.configuration.authTokens()?[.github(host)].flatMap { token in
198201
"token \(token)"
199202
}
200203
}

Tests/PackageCollectionsTests/GitHubPackageMetadataProviderTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ class GitHubPackageMetadataProviderTests: XCTestCase {
209209
try testWithTemporaryDirectory { tmpPath in
210210
let repoURL = "https://github.com/octocat/Hello-World.git"
211211
let apiURL = URL(string: "https://api.github.com/repos/octocat/Hello-World")!
212-
let authTokens = [AuthTokenType.github("api.github.com"): "foo"]
212+
let authTokens = [AuthTokenType.github("github.com"): "foo"]
213213

214214
let handler: HTTPClient.Handler = { request, _, completion in
215215
if request.headers.get("Authorization").first == "token \(authTokens.first!.value)" {
@@ -335,7 +335,7 @@ class GitHubPackageMetadataProviderTests: XCTestCase {
335335
httpClient.configuration.requestHeaders!.add(name: "Cache-Control", value: "no-cache")
336336
var configuration = GitHubPackageMetadataProvider.Configuration()
337337
if let token = ProcessEnv.vars["GITHUB_API_TOKEN"] {
338-
configuration.authTokens = { [.github("api.github.com"): token] }
338+
configuration.authTokens = { [.github("github.com"): token] }
339339
}
340340
configuration.apiLimitWarningThreshold = 50
341341
configuration.cacheTTLInSeconds = -1 // Disable cache so we hit the API

0 commit comments

Comments
 (0)