Skip to content

Commit

Permalink
Fixed a bug that prevented display of repositories without token.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdoe committed Jan 11, 2024
1 parent 8aacf78 commit b926d46
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,27 @@ class GitHubRepositoryList: ObservableObject {
}

private func fetchRepositories(owner: String, token: String?) async -> [GitHubRepository] {
let request1 = GitHubAPI.requestForRepositories(owner: owner, token: token)
let publicRepos = await fetchRepositories(request: request1)
if publicRepos.count > 0 && !publicRepos[0].isValid {
return publicRepos
let ownerRepoRequest = GitHubAPI.requestForRepositories(owner: owner, token: token)
var allRepos = await fetchRepositories(request: ownerRepoRequest)
if allRepos.count > 0 && !allRepos[0].isValid {
return allRepos
}

let request2 = GitHubAPI.requestForPrivateRepositories(token: token)
let privateRepos = (token != nil) ? await fetchRepositories(request: request2) : []
if privateRepos.count > 0 && !privateRepos[0].isValid {
return privateRepos
if let token = token, !token.isEmpty {
let privateRepoRequest = GitHubAPI.requestForPrivateRepositories(token: token)
let privateRepos = await fetchRepositories(request: privateRepoRequest)
if privateRepos.count > 0 && !privateRepos[0].isValid {
return privateRepos
}
allRepos.append(contentsOf: privateRepos)
}

var allRepos = (publicRepos + privateRepos).filter({ $0.owner?.login == owner })
allRepos = allRepos.filter({ $0.owner?.login == owner })
if allRepos.count == 0 {
allRepos = [GitHubRepository()]
}
allRepos.sort(by: { r1, r2 in r1.name.lowercased().compare(r2.name.lowercased()) == .orderedAscending })

return allRepos
}

Expand Down
4 changes: 2 additions & 2 deletions CCMenu/Source/Server Monitor/GitHubAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class GitHubAPI {
return makeRequest(path: path, params: queryParams, token: token)
}

static func requestForPrivateRepositories(token: String?) -> URLRequest {
static func requestForPrivateRepositories(token: String) -> URLRequest {
let path = String(format: "/user/repos")
let queryParams = [
"type": "private",
Expand Down Expand Up @@ -100,7 +100,7 @@ class GitHubAPI {
request.httpMethod = method
request.addValue("application/vnd.github+json", forHTTPHeaderField: "Accept")
request.addValue("2022-11-28", forHTTPHeaderField: "X-GitHub-Api-Version")
if let token = token {
if let token = token, !token.isEmpty {
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
}
return request
Expand Down

0 comments on commit b926d46

Please sign in to comment.