mirror of
https://github.com/zhigang1992/GitHawk.git
synced 2026-01-12 22:47:34 +08:00
* [WIP] Refactor networking to shared lib * notification models as codable and tests * default request params * break out into files * fix tests * check notification values * add milestones * add milestone response * repo notifications * refactor v3 response * refactor to support passing response to response initializer * add release fetch and model * add examples from githawk * notifications working * almost all notifications requests migrated * finish notification migration, add graphql request/response * replace issue requests * replace milestones request * fetch assignees migrated * readme fetch migrated * migrate badge request * delete unused request from client * remove paging * nearly done w/ migration * local build green * refactor gql fetches, replace mutations * build green, slim down client * strip session manager from client * everything working
79 lines
2.3 KiB
Swift
79 lines
2.3 KiB
Swift
//
|
|
// GitHubClient+Repository.swift
|
|
// Freetime
|
|
//
|
|
// Created by Ryan Nystrom on 10/14/17.
|
|
// Copyright © 2017 Ryan Nystrom. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
extension GithubClient {
|
|
|
|
func fetchFiles(
|
|
owner: String,
|
|
repo: String,
|
|
branch: String,
|
|
path: String,
|
|
completion: @escaping (Result<[RepositoryFile]>) -> Void
|
|
) {
|
|
let query = RepoFilesQuery(owner: owner, name: repo, branchAndPath: "\(branch):\(path)")
|
|
|
|
client.query(query, result: { $0.repository?.object?.asTree?.entries }) { result in
|
|
switch result {
|
|
case .failure(let error):
|
|
completion(.error(error))
|
|
case .success(let models):
|
|
// trees A-Z first, then blobs A-Z
|
|
var trees = [RepositoryFile]()
|
|
var blobs = [RepositoryFile]()
|
|
for model in models {
|
|
let isTree = model.type == "tree"
|
|
let file = RepositoryFile(
|
|
name: model.name,
|
|
isDirectory: model.type == "tree"
|
|
)
|
|
if isTree {
|
|
trees.append(file)
|
|
} else {
|
|
blobs.append(file)
|
|
}
|
|
}
|
|
trees.sort { $0.name < $1.name }
|
|
blobs.sort { $0.name < $1.name }
|
|
completion(.success(trees + blobs))
|
|
}
|
|
}
|
|
}
|
|
|
|
// different result type so handling non-text is treated differently
|
|
enum FileResult {
|
|
case success(String)
|
|
case nonUTF8
|
|
case error(Error?)
|
|
}
|
|
|
|
func fetchFile(
|
|
owner: String,
|
|
repo: String,
|
|
branch: String,
|
|
path: String,
|
|
completion: @escaping (FileResult) -> Void
|
|
) {
|
|
let query = RepoFileQuery(owner: owner, name: repo, branchAndPath: "\(branch):\(path)")
|
|
client.query(query, result: { $0.repository?.object?.asBlob }) { result in
|
|
switch result {
|
|
case .failure(let error):
|
|
completion(.error(error))
|
|
case .success(let blob):
|
|
if let text = blob.text, !text.isEmpty {
|
|
completion(.success(text))
|
|
} else {
|
|
completion(.nonUTF8)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|