Skip to content

Commit

Permalink
Merge pull request #214 from czechboy0/develop_1.0
Browse files Browse the repository at this point in the history
Developing 1.0 - Iris
  • Loading branch information
czechboy0 committed Feb 3, 2016
2 parents b5317a5 + a1aa76a commit 729293e
Show file tree
Hide file tree
Showing 120 changed files with 5,042 additions and 986 deletions.
64 changes: 64 additions & 0 deletions BuildaGitServer/Base/Authentication.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// Authentication.swift
// Buildasaur
//
// Created by Honza Dvorsky on 1/26/16.
// Copyright © 2016 Honza Dvorsky. All rights reserved.
//

import Foundation
import BuildaUtils

public struct ProjectAuthenticator {

public enum AuthType: String {
case PersonalToken
case OAuthToken
}

public let service: GitService
public let username: String
public let type: AuthType
public let secret: String

public init(service: GitService, username: String, type: AuthType, secret: String) {
self.service = service
self.username = username
self.type = type
self.secret = secret
}
}

public protocol KeychainStringSerializable {
static func fromString(value: String) throws -> Self
func toString() -> String
}

extension ProjectAuthenticator: KeychainStringSerializable {

public static func fromString(value: String) throws -> ProjectAuthenticator {

let comps = value.componentsSeparatedByString(":")
guard comps.count >= 4 else { throw Error.withInfo("Corrupted keychain string") }
guard let service = GitService(rawValue: comps[0]) else {
throw Error.withInfo("Unsupported service: \(comps[0])")
}
guard let type = ProjectAuthenticator.AuthType(rawValue: comps[2]) else {
throw Error.withInfo("Unsupported auth type: \(comps[2])")
}
//join the rest back in case we have ":" in the token
let remaining = comps.dropFirst(3).joinWithSeparator(":")
let auth = ProjectAuthenticator(service: service, username: comps[1], type: type, secret: remaining)
return auth
}

public func toString() -> String {

return [
self.service.rawValue,
self.username,
self.type.rawValue,
self.secret
].joinWithSeparator(":")
}
}
114 changes: 114 additions & 0 deletions BuildaGitServer/Base/BaseTypes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//
// BaseTypes.swift
// Buildasaur
//
// Created by Honza Dvorsky on 10/16/15.
// Copyright © 2015 Honza Dvorsky. All rights reserved.
//

import Foundation
import ReactiveCocoa

public protocol BuildStatusCreator {
func createStatusFromState(state: BuildState, description: String?, targetUrl: String?) -> StatusType
}

public protocol SourceServerType: BuildStatusCreator {

func getBranchesOfRepo(repo: String, completion: (branches: [BranchType]?, error: ErrorType?) -> ())
func getOpenPullRequests(repo: String, completion: (prs: [PullRequestType]?, error: ErrorType?) -> ())
func getPullRequest(pullRequestNumber: Int, repo: String, completion: (pr: PullRequestType?, error: ErrorType?) -> ())
func getRepo(repo: String, completion: (repo: RepoType?, error: ErrorType?) -> ())
func getStatusOfCommit(commit: String, repo: String, completion: (status: StatusType?, error: ErrorType?) -> ())
func postStatusOfCommit(commit: String, status: StatusType, repo: String, completion: (status: StatusType?, error: ErrorType?) -> ())
func postCommentOnIssue(comment: String, issueNumber: Int, repo: String, completion: (comment: CommentType?, error: ErrorType?) -> ())
func getCommentsOfIssue(issueNumber: Int, repo: String, completion: (comments: [CommentType]?, error: ErrorType?) -> ())

func authChangedSignal() -> Signal<ProjectAuthenticator?, NoError>
}

public class SourceServerFactory {

public init() { }

public func createServer(service: GitService, auth: ProjectAuthenticator?) -> SourceServerType {

if let auth = auth {
precondition(service == auth.service)
}

return GitServerFactory.server(service, auth: auth)
}
}

public struct RepoPermissions {
public let read: Bool
public let write: Bool
public init(read: Bool, write: Bool) {
self.read = read
self.write = write
}
}

public protocol RateLimitType {

var report: String { get }
}

public protocol RepoType {

var permissions: RepoPermissions { get }
var originUrlSSH: String { get }
var latestRateLimitInfo: RateLimitType? { get }
}

public protocol BranchType {

var name: String { get }
var commitSHA: String { get }
}

public protocol IssueType {

var number: Int { get }
}

public protocol PullRequestType: IssueType {

var headName: String { get }
var headCommitSHA: String { get }
var headRepo: RepoType { get }

var baseName: String { get }

var title: String { get }
}

public enum BuildState {
case NoState
case Pending
case Success
case Error
case Failure
}

public protocol StatusType {

var state: BuildState { get }
var description: String? { get }
var targetUrl: String? { get }
}

extension StatusType {

public func isEqual(rhs: StatusType) -> Bool {
let lhs = self
return lhs.state == rhs.state && lhs.description == rhs.description
}
}

public protocol CommentType {

var body: String { get }
}

32 changes: 32 additions & 0 deletions BuildaGitServer/Base/GitServerFactory.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// GitServerFactory.swift
// Buildasaur
//
// Created by Honza Dvorsky on 13/12/2014.
// Copyright (c) 2014 Honza Dvorsky. All rights reserved.
//

import Foundation
import BuildaUtils

class GitServerFactory {

class func server(service: GitService, auth: ProjectAuthenticator?, http: HTTP? = nil) -> SourceServerType {

let server: SourceServerType

switch service {
case .GitHub:
let baseURL = "https://api.github.com"
let endpoints = GitHubEndpoints(baseURL: baseURL, auth: auth)
server = GitHubServer(endpoints: endpoints, http: http)
case .BitBucket:
let baseURL = "https://api.bitbucket.org"
let endpoints = BitBucketEndpoints(baseURL: baseURL, auth: auth)
server = BitBucketServer(endpoints: endpoints, http: http)
}

return server
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// GitHubServerExtensions.swift
// SourceServerExtensions.swift
// Buildasaur
//
// Created by Honza Dvorsky on 14/12/2014.
Expand All @@ -10,12 +10,12 @@ import Foundation
import BuildaUtils

//functions to make working with github easier - utility functions
public extension GitHubServer {
extension SourceServerType {

/**
* Get the latest status of a pull request.
*/
public func getStatusOfPullRequest(pullRequestNumber: Int, repo: String, completion: (status: Status?, error: NSError?) -> ()) {
func getStatusOfPullRequest(pullRequestNumber: Int, repo: String, completion: (status: StatusType?, error: ErrorType?) -> ()) {

self.getPullRequest(pullRequestNumber, repo: repo) { (pr, error) -> () in

Expand All @@ -26,7 +26,7 @@ public extension GitHubServer {

if let pr = pr {
//fetched PR, now take its head's sha - that's the commit we care about.
let sha = pr.head.sha
let sha = pr.headName
self.getStatusOfCommit(sha, repo: repo, completion: completion)
} else {
completion(status: nil, error: Error.withInfo("PR is nil and error is nil"))
Expand All @@ -35,7 +35,7 @@ public extension GitHubServer {
}

//TODO: support paging through all the comments. currently we only fetch the last ~30 comments.
public func findMatchingCommentInIssue(commentsToMatch: [String], issue: Int, repo: String, completion: (foundComments: [Comment]?, error: NSError?) -> ()) {
public func findMatchingCommentInIssue(commentsToMatch: [String], issue: Int, repo: String, completion: (foundComments: [CommentType]?, error: ErrorType?) -> ()) {

self.getCommentsOfIssue(issue, repo: repo) { (comments, error) -> () in

Expand All @@ -45,7 +45,7 @@ public extension GitHubServer {
}

if let comments = comments {
let filtered = comments.filter { (comment: Comment) -> Bool in
let filtered = comments.filter { (comment: CommentType) -> Bool in

let filteredSearch = commentsToMatch.filter {
(searchString: String) -> Bool in
Expand Down
25 changes: 25 additions & 0 deletions BuildaGitServer/BitBucket/BitBucketComment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// BitBucketComment.swift
// Buildasaur
//
// Created by Honza Dvorsky on 1/27/16.
// Copyright © 2016 Honza Dvorsky. All rights reserved.
//

import Foundation

class BitBucketComment: BitBucketEntity, CommentType {

let body: String

required init(json: NSDictionary) {

self.body = json
.optionalDictionaryForKey("content")?
.stringForKey("raw") ?? json.stringForKey("content")

super.init(json: json)
}


}
Loading

0 comments on commit 729293e

Please sign in to comment.