Skip to content

Commit

Permalink
Merge pull request #249 from czechboy0/hd/deleted_repo_fix
Browse files Browse the repository at this point in the history
Updated Swift version, crash fix
  • Loading branch information
czechboy0 committed Mar 2, 2016
2 parents 483126a + 79bee57 commit e067259
Show file tree
Hide file tree
Showing 21 changed files with 128 additions and 99 deletions.
6 changes: 3 additions & 3 deletions BuildaGitServer/GitHub/GitHubBranch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class GitHubBranch : GitHubEntity {
let name: String
let commit: GitHubCommit

required init(json: NSDictionary) {
required init(json: NSDictionary) throws {

self.name = json.stringForKey("name")
self.commit = GitHubCommit(json: json.dictionaryForKey("commit"))
super.init(json: json)
self.commit = try GitHubCommit(json: json.dictionaryForKey("commit"))
try super.init(json: json)
}
}

Expand Down
6 changes: 3 additions & 3 deletions BuildaGitServer/GitHub/GitHubComment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class GitHubComment : GitHubEntity {
let body: String
let author: GitHubUser

required init(json: NSDictionary) {
required init(json: NSDictionary) throws {

self.body = json.stringForKey("body")
self.author = GitHubUser(json: json.dictionaryForKey("user"))
self.author = try GitHubUser(json: json.dictionaryForKey("user"))

super.init(json: json)
try super.init(json: json)
}
}

Expand Down
4 changes: 2 additions & 2 deletions BuildaGitServer/GitHub/GitHubCommit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class GitHubCommit : GitHubEntity {

let sha: String

required init(json: NSDictionary) {
required init(json: NSDictionary) throws {

self.sha = json.stringForKey("sha")

super.init(json: json)
try super.init(json: json)
}
}

Expand Down
16 changes: 8 additions & 8 deletions BuildaGitServer/GitHub/GitHubEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

protocol GitHubType {
init(json: NSDictionary)
init(json: NSDictionary) throws
}

class GitHubEntity : GitHubType {
Expand All @@ -19,7 +19,7 @@ class GitHubEntity : GitHubType {
let id: Int?

//initializer which takes a dictionary and fills in values for recognized keys
required init(json: NSDictionary) {
required init(json: NSDictionary) throws {

self.htmlUrl = json.optionalStringForKey("html_url")
self.url = json.optionalStringForKey("url")
Expand All @@ -37,22 +37,22 @@ class GitHubEntity : GitHubType {
return NSDictionary()
}

class func optional<T: GitHubEntity>(json: NSDictionary?) -> T? {
class func optional<T: GitHubEntity>(json: NSDictionary?) throws -> T? {
if let json = json {
return T(json: json)
return try T(json: json)
}
return nil
}

}

//parse an array of dictionaries into an array of parsed entities
func GitHubArray<T where T: GitHubType>(jsonArray: NSArray!) -> [T] {
func GitHubArray<T where T: GitHubType>(jsonArray: NSArray!) throws -> [T] {

let array = jsonArray as! [NSDictionary]!
let parsed = array.map {
let array = jsonArray as! [NSDictionary]
let parsed = try array.map {
(json: NSDictionary) -> (T) in
return T(json: json)
return try T(json: json)
}
return parsed
}
Expand Down
4 changes: 2 additions & 2 deletions BuildaGitServer/GitHub/GitHubIssue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ class GitHubIssue : GitHubEntity {
let body: String
var title: String

required init(json: NSDictionary) {
required init(json: NSDictionary) throws {

self.number = json.intForKey("number")
self.body = json.stringForKey("body")
self.title = json.stringForKey("title")

super.init(json: json)
try super.init(json: json)
}
}

Expand Down
8 changes: 4 additions & 4 deletions BuildaGitServer/GitHub/GitHubPullRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class GitHubPullRequest : GitHubIssue, PullRequestType {
let head: GitHubPullRequestBranch
let base: GitHubPullRequestBranch

required init(json: NSDictionary) {
required init(json: NSDictionary) throws {

self.head = GitHubPullRequestBranch(json: json.dictionaryForKey("head"))
self.base = GitHubPullRequestBranch(json: json.dictionaryForKey("base"))
self.head = try GitHubPullRequestBranch(json: json.dictionaryForKey("head"))
self.base = try GitHubPullRequestBranch(json: json.dictionaryForKey("base"))

super.init(json: json)
try super.init(json: json)
}

var headName: String {
Expand Down
10 changes: 7 additions & 3 deletions BuildaGitServer/GitHub/GitHubPullRequestBranch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Foundation
import BuildaUtils

//PullRequestBranch is a special type of a branch - it also includes repo info (bc PRs can be cross repos)
//normal branches include less information
Expand All @@ -16,13 +17,16 @@ class GitHubPullRequestBranch : GitHubEntity {
let sha: String
let repo: GitHubRepo

required init(json: NSDictionary) {
required init(json: NSDictionary) throws {

self.ref = json.stringForKey("ref")
self.sha = json.stringForKey("sha")
self.repo = GitHubRepo(json: json.dictionaryForKey("repo"))
guard let repo = json.optionalDictionaryForKey("repo") else {
throw Error.withInfo("PR missing information about its repository")
}
self.repo = try GitHubRepo(json: repo)

super.init(json: json)
try super.init(json: json)
}
}

4 changes: 2 additions & 2 deletions BuildaGitServer/GitHub/GitHubRepo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class GitHubRepo : GitHubEntity {

var latestRateLimitInfo: RateLimitType?

required init(json: NSDictionary) {
required init(json: NSDictionary) throws {

self.name = json.stringForKey("name")
self.fullName = json.stringForKey("full_name")
Expand All @@ -31,7 +31,7 @@ class GitHubRepo : GitHubEntity {
self.permissionsDict = NSDictionary()
}

super.init(json: json)
try super.init(json: json)
}
}

Expand Down
76 changes: 50 additions & 26 deletions BuildaGitServer/GitHub/GitHubServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,9 @@ extension GitHubServer {
return
}

if let body = body as? NSArray {
let prs: [GitHubPullRequest] = GitHubArray(body)
if
let body = body as? NSArray,
let prs: [GitHubPullRequest] = try? GitHubArray(body) {
completion(prs: prs, error: nil)
} else {
completion(prs: nil, error: Error.withInfo("Wrong body \(body)"))
Expand All @@ -310,8 +311,10 @@ extension GitHubServer {
return
}

if let body = body as? NSDictionary {
let pr = GitHubPullRequest(json: body)
if
let body = body as? NSDictionary,
let pr = try? GitHubPullRequest(json: body)
{
completion(pr: pr, error: nil)
} else {
completion(pr: nil, error: Error.withInfo("Wrong body \(body)"))
Expand All @@ -334,8 +337,9 @@ extension GitHubServer {
return
}

if let body = body as? NSArray {
let issues: [GitHubIssue] = GitHubArray(body)
if
let body = body as? NSArray,
let issues: [GitHubIssue] = try? GitHubArray(body) {
completion(issues: issues, error: nil)
} else {
completion(issues: nil, error: Error.withInfo("Wrong body \(body)"))
Expand All @@ -360,8 +364,10 @@ extension GitHubServer {
return
}

if let body = body as? NSDictionary {
let issue = GitHubIssue(json: body)
if
let body = body as? NSDictionary,
let issue = try? GitHubIssue(json: body)
{
completion(issue: issue, error: nil)
} else {
completion(issue: nil, error: Error.withInfo("Wrong body \(body)"))
Expand Down Expand Up @@ -390,8 +396,10 @@ extension GitHubServer {
return
}

if let body = body as? NSDictionary {
let issue = GitHubIssue(json: body)
if
let body = body as? NSDictionary,
let issue = try? GitHubIssue(json: body)
{
completion(issue: issue, error: nil)
} else {
completion(issue: nil, error: Error.withInfo("Wrong body \(body)"))
Expand Down Expand Up @@ -420,8 +428,10 @@ extension GitHubServer {
return
}

if let body = body as? NSDictionary {
let issue = GitHubIssue(json: body)
if
let body = body as? NSDictionary,
let issue = try? GitHubIssue(json: body)
{
completion(issue: issue, error: nil)
} else {
completion(issue: nil, error: Error.withInfo("Wrong body \(body)"))
Expand All @@ -446,8 +456,10 @@ extension GitHubServer {
return
}

if let body = body as? NSArray {
let statuses: [GitHubStatus] = GitHubArray(body)
if
let body = body as? NSArray,
let statuses: [GitHubStatus] = try? GitHubArray(body)
{
//sort them by creation date
let mostRecentStatus = statuses.sort({ return $0.created! > $1.created! }).first
completion(status: mostRecentStatus, error: nil)
Expand Down Expand Up @@ -475,8 +487,10 @@ extension GitHubServer {
return
}

if let body = body as? NSDictionary {
let status = GitHubStatus(json: body)
if
let body = body as? NSDictionary,
let status = try? GitHubStatus(json: body)
{
completion(status: status, error: nil)
} else {
completion(status: nil, error: Error.withInfo("Wrong body \(body)"))
Expand All @@ -503,8 +517,10 @@ extension GitHubServer {
return
}

if let body = body as? NSArray {
let comments: [GitHubComment] = GitHubArray(body)
if
let body = body as? NSArray,
let comments: [GitHubComment] = try? GitHubArray(body)
{
completion(comments: comments, error: nil)
} else {
completion(comments: nil, error: Error.withInfo("Wrong body \(body)"))
Expand Down Expand Up @@ -533,8 +549,10 @@ extension GitHubServer {
return
}

if let body = body as? NSDictionary {
let comment = GitHubComment(json: body)
if
let body = body as? NSDictionary,
let comment = try? GitHubComment(json: body)
{
completion(comment: comment, error: nil)
} else {
completion(comment: nil, error: Error.withInfo("Wrong body \(body)"))
Expand Down Expand Up @@ -563,8 +581,10 @@ extension GitHubServer {
return
}

if let body = body as? NSDictionary {
let comment = GitHubComment(json: body)
if
let body = body as? NSDictionary,
let comment = try? GitHubComment(json: body)
{
completion(comment: comment, error: nil)
} else {
completion(comment: nil, error: Error.withInfo("Wrong body \(body)"))
Expand Down Expand Up @@ -641,8 +661,10 @@ extension GitHubServer {
return
}

if let body = body as? NSArray {
let branches: [GitHubBranch] = GitHubArray(body)
if
let body = body as? NSArray,
let branches: [GitHubBranch] = try? GitHubArray(body)
{
completion(branches: branches, error: nil)
} else {
completion(branches: nil, error: Error.withInfo("Wrong body \(body)"))
Expand All @@ -667,8 +689,10 @@ extension GitHubServer {
return
}

if let body = body as? NSDictionary {
let repository: GitHubRepo = GitHubRepo(json: body)
if
let body = body as? NSDictionary,
let repository: GitHubRepo = try? GitHubRepo(json: body)
{
completion(repo: repository, error: nil)
} else {
completion(repo: nil, error: Error.withInfo("Wrong body \(body)"))
Expand Down
6 changes: 3 additions & 3 deletions BuildaGitServer/GitHub/GitHubStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,20 @@ class GitHubStatus : GitHubEntity {
let created: String?
let creator: GitHubUser?

required init(json: NSDictionary) {
required init(json: NSDictionary) throws {

self.githubState = GitHubState(rawValue: json.stringForKey("state"))!
self.description = json.optionalStringForKey("description")
self.targetUrl = json.optionalStringForKey("target_url")
self.context = json.optionalStringForKey("context")
self.created = json.optionalStringForKey("created_at")
if let creator = json.optionalDictionaryForKey("creator") {
self.creator = GitHubUser(json: creator)
self.creator = try GitHubUser(json: creator)
} else {
self.creator = nil
}

super.init(json: json)
try super.init(json: json)
}

init(state: GitHubState, description: String?, targetUrl: String?, context: String?) {
Expand Down
4 changes: 2 additions & 2 deletions BuildaGitServer/GitHub/GitHubUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ class GitHubUser : GitHubEntity {
let realName: String?
let avatarUrl: String?

required init(json: NSDictionary) {
required init(json: NSDictionary) throws {

self.userName = json.stringForKey("login")
self.realName = json.optionalStringForKey("name")
self.avatarUrl = json.stringForKey("avatar_url")

super.init(json: json)
try super.init(json: json)
}
}

Loading

0 comments on commit e067259

Please sign in to comment.