Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More throwing, less trapping, more fixes #281

Merged
merged 1 commit into from
Jun 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions BuildaGitServer/BitBucket/BitBucketComment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ class BitBucketComment: BitBucketEntity, CommentType {

let body: String

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

self.body = json
if let body = try json
.optionalDictionaryForKey("content")?
.stringForKey("raw") ?? json.stringForKey("content")
.stringForKey("raw") {
self.body = body
} else {
self.body = try json.stringForKey("content")
}

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


}
14 changes: 7 additions & 7 deletions BuildaGitServer/BitBucket/BitBucketEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import Foundation

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

class BitBucketEntity : BitBucketType {

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

//add any common keys to be parsed here
}
Expand All @@ -29,21 +29,21 @@ class BitBucketEntity : BitBucketType {
return NSDictionary()
}

class func optional<T: BitBucketEntity>(json: NSDictionary?) -> T? {
class func optional<T: BitBucketEntity>(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 BitBucketArray<T where T: BitBucketType>(jsonArray: [NSDictionary]) -> [T] {
func BitBucketArray<T where T: BitBucketType>(jsonArray: [NSDictionary]) throws -> [T] {

let parsed = jsonArray.map {
let parsed = try jsonArray.map {
(json: NSDictionary) -> (T) in
return T(json: json)
return try T(json: json)
}
return parsed
}
6 changes: 3 additions & 3 deletions BuildaGitServer/BitBucket/BitBucketIssue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class BitBucketIssue: BitBucketEntity, IssueType {

let number: Int

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

self.number = json.intForKey("id")
self.number = try json.intForKey("id")

super.init(json: json)
try super.init(json: json)
}
}
10 changes: 5 additions & 5 deletions BuildaGitServer/BitBucket/BitBucketPullRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ class BitBucketPullRequest: BitBucketIssue, PullRequestType {
let source: BitBucketPullRequestBranch
let destination: BitBucketPullRequestBranch

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

self.title = json.stringForKey("title")
self.title = try json.stringForKey("title")

self.source = BitBucketPullRequestBranch(json: json.dictionaryForKey("source"))
self.destination = BitBucketPullRequestBranch(json: json.dictionaryForKey("destination"))
self.source = try BitBucketPullRequestBranch(json: try json.dictionaryForKey("source"))
self.destination = try BitBucketPullRequestBranch(json: try json.dictionaryForKey("destination"))

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

var headName: String {
Expand Down
10 changes: 5 additions & 5 deletions BuildaGitServer/BitBucket/BitBucketPullRequestBranch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class BitBucketPullRequestBranch : BitBucketEntity {
let commit: String
let repo: BitBucketRepo

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

self.branch = json.dictionaryForKey("branch").stringForKey("name")
self.commit = json.dictionaryForKey("commit").stringForKey("hash")
self.repo = BitBucketRepo(json: json.dictionaryForKey("repository"))
self.branch = try json.dictionaryForKey("branch").stringForKey("name")
self.commit = try json.dictionaryForKey("commit").stringForKey("hash")
self.repo = try BitBucketRepo(json: try json.dictionaryForKey("repository"))

super.init(json: json)
try super.init(json: json)
}
}
6 changes: 3 additions & 3 deletions BuildaGitServer/BitBucket/BitBucketRepo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class BitBucketRepo: BitBucketEntity, RepoType {
let latestRateLimitInfo: RateLimitType? = BitBucketRateLimit()
let originUrlSSH: String

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

//split with forward slash, the last two comps are the repo
//create a proper ssh url for bitbucket here
let repoName = json
let repoName = try json
.dictionaryForKey("links")
.dictionaryForKey("self")
.stringForKey("href")
Expand All @@ -28,6 +28,6 @@ class BitBucketRepo: BitBucketEntity, RepoType {
.joinWithSeparator("/")
self.originUrlSSH = "[email protected]:\(repoName).git"

super.init(json: json)
try super.init(json: json)
}
}
62 changes: 40 additions & 22 deletions BuildaGitServer/BitBucket/BitBucketServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ extension BitBucketServer: SourceServerType {
}

if let body = body as? [NSDictionary] {
let prs: [BitBucketPullRequest] = BitBucketArray(body)
completion(prs: prs.map { $0 as PullRequestType }, error: nil)
let (result, error): ([BitBucketPullRequest]?, NSError?) = unthrow {
return try BitBucketArray(body)
}
completion(prs: result?.map { $0 as PullRequestType }, error: error)
} else {
completion(prs: nil, error: Error.withInfo("Wrong body \(body)"))
}
Expand All @@ -81,8 +83,10 @@ extension BitBucketServer: SourceServerType {
}

if let body = body as? NSDictionary {
let pr = BitBucketPullRequest(json: body)
completion(pr: pr, error: nil)
let (result, error): (BitBucketPullRequest?, NSError?) = unthrow {
return try BitBucketPullRequest(json: body)
}
completion(pr: result, error: error)
} else {
completion(pr: nil, error: Error.withInfo("Wrong body \(body)"))
}
Expand All @@ -104,8 +108,10 @@ extension BitBucketServer: SourceServerType {
}

if let body = body as? NSDictionary {
let repository = BitBucketRepo(json: body)
completion(repo: repository, error: nil)
let (result, error): (BitBucketRepo?, NSError?) = unthrow {
return try BitBucketRepo(json: body)
}
completion(repo: result, error: error)
} else {
completion(repo: nil, error: Error.withInfo("Wrong body \(body)"))
}
Expand Down Expand Up @@ -134,8 +140,10 @@ extension BitBucketServer: SourceServerType {
}

if let body = body as? NSDictionary {
let status = BitBucketStatus(json: body)
completion(status: status, error: nil)
let (result, error): (BitBucketStatus?, NSError?) = unthrow {
return try BitBucketStatus(json: body)
}
completion(status: result, error: error)
} else {
completion(status: nil, error: Error.withInfo("Wrong body \(body)"))
}
Expand All @@ -158,8 +166,10 @@ extension BitBucketServer: SourceServerType {
}

if let body = body as? NSDictionary {
let status = BitBucketStatus(json: body)
completion(status: status, error: nil)
let (result, error): (BitBucketStatus?, NSError?) = unthrow {
return try BitBucketStatus(json: body)
}
completion(status: result, error: error)
} else {
completion(status: nil, error: Error.withInfo("Wrong body \(body)"))
}
Expand All @@ -185,8 +195,10 @@ extension BitBucketServer: SourceServerType {
}

if let body = body as? NSDictionary {
let comment = BitBucketComment(json: body)
completion(comment: comment, error: nil)
let (result, error): (BitBucketComment?, NSError?) = unthrow {
return try BitBucketComment(json: body)
}
completion(comment: result, error: error)
} else {
completion(comment: nil, error: Error.withInfo("Wrong body \(body)"))
}
Expand All @@ -208,8 +220,10 @@ extension BitBucketServer: SourceServerType {
}

if let body = body as? [NSDictionary] {
let comments: [BitBucketComment] = BitBucketArray(body)
completion(comments: comments.map { $0 as CommentType }, error: nil)
let (result, error): ([BitBucketComment]?, NSError?) = unthrow {
return try BitBucketArray(body)
}
completion(comments: result?.map { $0 as CommentType }, error: error)
} else {
completion(comments: nil, error: Error.withInfo("Wrong body \(body)"))
}
Expand Down Expand Up @@ -296,14 +310,18 @@ extension BitBucketServer {
return
}

let payload = body as! NSDictionary
let accessToken = payload.stringForKey("access_token")
let refreshToken = payload.stringForKey("refresh_token")
let secret = [refreshToken, accessToken].joinWithSeparator(":")

let newAuth = ProjectAuthenticator(service: .BitBucket, username: "GIT", type: .OAuthToken, secret: secret)
self.endpoints.auth.value = newAuth
completion(nil)
do {
let payload = body as! NSDictionary
let accessToken = try payload.stringForKey("access_token")
let refreshToken = try payload.stringForKey("refresh_token")
let secret = [refreshToken, accessToken].joinWithSeparator(":")

let newAuth = ProjectAuthenticator(service: .BitBucket, username: "GIT", type: .OAuthToken, secret: secret)
self.endpoints.auth.value = newAuth
completion(nil)
} catch {
completion(error as NSError)
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions BuildaGitServer/BitBucket/BitBucketStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class BitBucketStatus: BitBucketEntity, StatusType {
let description: String?
let targetUrl: String?

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

self.bbState = BitBucketState(rawValue: json.stringForKey("state"))!
self.key = json.stringForKey("key")
self.bbState = BitBucketState(rawValue: try json.stringForKey("state"))!
self.key = try json.stringForKey("key")
self.name = json.optionalStringForKey("name")
self.description = json.optionalStringForKey("description")
self.targetUrl = json.stringForKey("url")
self.targetUrl = try json.stringForKey("url")

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

init(state: BitBucketState, key: String, name: String?, description: String?, url: String) {
Expand Down
2 changes: 1 addition & 1 deletion BuildaGitServer/GitHub/GitHubBranch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GitHubBranch : GitHubEntity {

required init(json: NSDictionary) throws {

self.name = json.stringForKey("name")
self.name = try json.stringForKey("name")
self.commit = try GitHubCommit(json: json.dictionaryForKey("commit"))
try super.init(json: json)
}
Expand Down
2 changes: 1 addition & 1 deletion BuildaGitServer/GitHub/GitHubComment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GitHubComment : GitHubEntity {

required init(json: NSDictionary) throws {

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

try super.init(json: json)
Expand Down
2 changes: 1 addition & 1 deletion BuildaGitServer/GitHub/GitHubCommit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GitHubCommit : GitHubEntity {

required init(json: NSDictionary) throws {

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

try super.init(json: json)
}
Expand Down
6 changes: 3 additions & 3 deletions BuildaGitServer/GitHub/GitHubIssue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class GitHubIssue : GitHubEntity {

required init(json: NSDictionary) throws {

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

try super.init(json: json)
}
Expand Down
4 changes: 2 additions & 2 deletions BuildaGitServer/GitHub/GitHubPullRequestBranch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class GitHubPullRequestBranch : GitHubEntity {

required init(json: NSDictionary) throws {

self.ref = json.stringForKey("ref")
self.sha = json.stringForKey("sha")
self.ref = try json.stringForKey("ref")
self.sha = try json.stringForKey("sha")
guard let repo = json.optionalDictionaryForKey("repo") else {
throw Error.withInfo("PR missing information about its repository")
}
Expand Down
8 changes: 4 additions & 4 deletions BuildaGitServer/GitHub/GitHubRepo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class GitHubRepo : GitHubEntity {

required init(json: NSDictionary) throws {

self.name = json.stringForKey("name")
self.fullName = json.stringForKey("full_name")
self.repoUrlHTTPS = json.stringForKey("clone_url")
self.repoUrlSSH = json.stringForKey("ssh_url")
self.name = try json.stringForKey("name")
self.fullName = try json.stringForKey("full_name")
self.repoUrlHTTPS = try json.stringForKey("clone_url")
self.repoUrlSSH = try json.stringForKey("ssh_url")

if let permissions = json.optionalDictionaryForKey("permissions") {
self.permissionsDict = permissions
Expand Down
2 changes: 1 addition & 1 deletion BuildaGitServer/GitHub/GitHubStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class GitHubStatus : GitHubEntity {

required init(json: NSDictionary) throws {

self.githubState = GitHubState(rawValue: json.stringForKey("state"))!
self.githubState = GitHubState(rawValue: try json.stringForKey("state"))!
self.description = json.optionalStringForKey("description")
self.targetUrl = json.optionalStringForKey("target_url")
self.context = json.optionalStringForKey("context")
Expand Down
4 changes: 2 additions & 2 deletions BuildaGitServer/GitHub/GitHubUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class GitHubUser : GitHubEntity {

required init(json: NSDictionary) throws {

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

try super.init(json: json)
}
Expand Down
12 changes: 6 additions & 6 deletions BuildaKit/BuildTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public struct BuildTemplate: JSONSerializable {

self.id = json.optionalStringForKey(kKeyId) ?? Ref.new()
self.projectName = json.optionalStringForKey(kKeyProjectName)
self.name = json.stringForKey(kKeyName)
self.scheme = json.stringForKey(kKeyScheme)
self.name = try json.stringForKey(kKeyName)
self.scheme = try json.stringForKey(kKeyScheme)
if let scheduleDict = json.optionalDictionaryForKey(kKeySchedule) {
self.schedule = BotSchedule(json: scheduleDict)
self.schedule = try BotSchedule(json: scheduleDict)
} else {
self.schedule = BotSchedule.manualBotSchedule()
}
Expand All @@ -88,9 +88,9 @@ public struct BuildTemplate: JSONSerializable {
self.triggers = []
}

self.shouldAnalyze = json.boolForKey(kKeyShouldAnalyze)
self.shouldTest = json.boolForKey(kKeyShouldTest)
self.shouldArchive = json.boolForKey(kKeyShouldArchive)
self.shouldAnalyze = try json.boolForKey(kKeyShouldAnalyze)
self.shouldTest = try json.boolForKey(kKeyShouldTest)
self.shouldArchive = try json.boolForKey(kKeyShouldArchive)

self.testingDeviceIds = json.optionalArrayForKey(kKeyTestingDevices) as? [String] ?? []

Expand Down
Loading