Skip to content

Commit

Permalink
Merge pull request #221 from czechboy0/hd/bitbucket_entities
Browse files Browse the repository at this point in the history
Most BitBucket entities
  • Loading branch information
czechboy0 committed Jan 27, 2016
2 parents 7cdfc72 + 148fcbd commit 6056488
Show file tree
Hide file tree
Showing 13 changed files with 938 additions and 19 deletions.
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
.dictionaryForKey("content")
.stringForKey("raw")

super.init(json: json)
}


}
36 changes: 33 additions & 3 deletions BuildaGitServer/BitBucket/BitBucketEndpoints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class BitBucketEndpoints {
enum Endpoint {
case Repos
case PullRequests
case PullRequestComments
case CommitStatuses
}

private let baseURL: String
Expand Down Expand Up @@ -46,8 +48,31 @@ class BitBucketEndpoints {
} else {
return "\(repo)/pullrequests"
}


case .PullRequestComments:

assert(params?["repo"] != nil, "A repo must be specified")
assert(params?["pr"] != nil, "A PR must be specified")
let pr = self.endpointURL(.PullRequests, params: params)
return "\(pr)/comments"

case .CommitStatuses:

assert(params?["repo"] != nil, "A repo must be specified")
assert(params?["sha"] != nil, "A commit sha must be specified")
let repo = self.endpointURL(.Repos, params: params)
let sha = params!["sha"]!

let build = "\(repo)/commit/\(sha)/statuses/build"

if let key = params?["status_key"] {
return "\(build)/\(key)"
}

return build

}

}

func createRequest(method: HTTP.Method, endpoint: Endpoint, params: [String : String]? = nil, query: [String : String]? = nil, body: NSDictionary? = nil) throws -> NSMutableURLRequest {
Expand All @@ -64,15 +89,20 @@ class BitBucketEndpoints {
if let auth = self.auth {

switch auth.type {
case .PersonalToken, .OAuthToken:
request.setValue("token \(auth.secret)", forHTTPHeaderField:"Authorization")
case .OAuthToken:
let tokens = auth.secret.componentsSeparatedByString(":")
//first is refresh token, second access token
request.setValue("Bearer \(tokens[1])", forHTTPHeaderField:"Authorization")
default:
fatalError("This kind of authentication is not supported for BitBucket")
}
}

if let body = body {

let data = try NSJSONSerialization.dataWithJSONObject(body, options: NSJSONWritingOptions())
request.HTTPBody = data
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
}

return request
Expand Down
113 changes: 111 additions & 2 deletions BuildaGitServer/BitBucket/BitBucketServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ extension BitBucketServer: SourceServerType {

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

//TODO: replace with bitbucket types
return GitHubStatus(state: GitHubStatus.GitHubState.NoState, description: nil, targetUrl: nil, context: nil)
let bbState = BitBucketStatus.BitBucketState.fromBuildState(state)
let key = "Buildasaur"
let url = targetUrl ?? "https://github.com/czechboy0/Buildasaur"
return BitBucketStatus(state: bbState, key: key, name: key, description: description, url: url)
}

func getBranchesOfRepo(repo: String, completion: (branches: [BranchType]?, error: ErrorType?) -> ()) {

//TODO: start returning branches
completion(branches: [], error: nil)
}

func getOpenPullRequests(repo: String, completion: (prs: [PullRequestType]?, error: ErrorType?) -> ()) {
Expand All @@ -57,26 +61,131 @@ extension BitBucketServer: SourceServerType {

func getPullRequest(pullRequestNumber: Int, repo: String, completion: (pr: PullRequestType?, error: ErrorType?) -> ()) {

let params = [
"repo": repo,
"pr": pullRequestNumber.description
]

self._sendRequestWithMethod(.GET, endpoint: .PullRequests, params: params, query: nil, body: nil) { (response, body, error) -> () in

if error != nil {
completion(pr: nil, error: error)
return
}

if let body = body as? NSDictionary {
let pr = BitBucketPullRequest(json: body)
completion(pr: pr, error: nil)
} else {
completion(pr: nil, error: Error.withInfo("Wrong body \(body)"))
}
}
}

func getRepo(repo: String, completion: (repo: RepoType?, error: ErrorType?) -> ()) {

let params = [
"repo": repo
]

self._sendRequestWithMethod(.GET, endpoint: .Repos, params: params, query: nil, body: nil) {
(response, body, error) -> () in

if error != nil {
completion(repo: nil, error: error)
return
}

if let body = body as? NSDictionary {
let repository = BitBucketRepo(json: body)
completion(repo: repository, error: nil)
} else {
completion(repo: nil, error: Error.withInfo("Wrong body \(body)"))
}
}
}

func getStatusOfCommit(commit: String, repo: String, completion: (status: StatusType?, error: ErrorType?) -> ()) {

let params = [
"repo": repo,
"sha": commit,
"status_key": "Buildasaur"
]

self._sendRequestWithMethod(.GET, endpoint: .CommitStatuses, params: params, query: nil, body: nil) { (response, body, error) -> () in

if response?.statusCode == 404 {
//no status yet, just pass nil but OK
completion(status: nil, error: nil)
return
}

if error != nil {
completion(status: nil, error: error)
return
}

if let body = body as? NSDictionary {
let status = BitBucketStatus(json: body)
completion(status: status, error: nil)
} else {
completion(status: nil, error: Error.withInfo("Wrong body \(body)"))
}
}
}

func postStatusOfCommit(commit: String, status: StatusType, repo: String, completion: (status: StatusType?, error: ErrorType?) -> ()) {

let params = [
"repo": repo,
"sha": commit
]

let body = (status as! BitBucketStatus).dictionarify()
self._sendRequestWithMethod(.POST, endpoint: .CommitStatuses, params: params, query: nil, body: body) { (response, body, error) -> () in

if error != nil {
completion(status: nil, error: error)
return
}

if let body = body as? NSDictionary {
let status = BitBucketStatus(json: body)
completion(status: status, error: nil)
} else {
completion(status: nil, error: Error.withInfo("Wrong body \(body)"))
}
}
}

func postCommentOnIssue(comment: String, issueNumber: Int, repo: String, completion: (comment: CommentType?, error: ErrorType?) -> ()) {

//TODO
completion(comment: nil, error: Error.withInfo("Posting comments on BitBucket not yet supported"))
}

func getCommentsOfIssue(issueNumber: Int, repo: String, completion: (comments: [CommentType]?, error: ErrorType?) -> ()) {

let params = [
"repo": repo,
"pr": issueNumber.description
]

self._sendRequestWithMethod(.GET, endpoint: .PullRequestComments, params: params, query: nil, body: nil) { (response, body, error) -> () in

if error != nil {
completion(comments: nil, error: error)
return
}

if let body = body as? NSDictionary {
let comments: [BitBucketComment] = BitBucketArray(body)
completion(comments: comments.map { $0 as CommentType }, error: nil)
} else {
completion(comments: nil, error: Error.withInfo("Wrong body \(body)"))
}
}
}
}

Expand Down
82 changes: 82 additions & 0 deletions BuildaGitServer/BitBucket/BitBucketStatus.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//
// BitBucketStatus.swift
// Buildasaur
//
// Created by Honza Dvorsky on 1/27/16.
// Copyright © 2016 Honza Dvorsky. All rights reserved.
//

import Foundation

class BitBucketStatus: BitBucketEntity, StatusType {

enum BitBucketState: String {
case InProgress = "INPROGRESS"
case Success = "SUCCESSFUL"
case Failed = "FAILED"
}

let bbState: BitBucketState
let key: String
let name: String?
let description: String?
let targetUrl: String?

required init(json: NSDictionary) {

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

super.init(json: json)
}

init(state: BitBucketState, key: String, name: String?, description: String?, url: String) {

self.bbState = state
self.key = key
self.name = name
self.description = description
self.targetUrl = url

super.init()
}

var state: BuildState {
return self.bbState.toBuildState()
}

override func dictionarify() -> NSDictionary {

let dictionary = NSMutableDictionary()

dictionary["state"] = self.bbState.rawValue
dictionary["key"] = self.key
dictionary.optionallyAddValueForKey(self.description, key: "description")
dictionary.optionallyAddValueForKey(self.name, key: "name")
dictionary.optionallyAddValueForKey(self.targetUrl, key: "url")

return dictionary.copy() as! NSDictionary
}
}

extension BitBucketStatus.BitBucketState {

static func fromBuildState(state: BuildState) -> BitBucketStatus.BitBucketState {
switch state {
case .Success, .NoState: return .Success
case .Pending: return .InProgress
case .Error, .Failure: return .Failed
}
}

func toBuildState() -> BuildState {
switch self {
case .Success: return .Success
case .InProgress: return .Pending
case .Failed: return .Failure
}
}
}
13 changes: 0 additions & 13 deletions BuildaGitServer/GitHub/GitHubStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,6 @@ class GitHubStatus : GitHubEntity {
}
}

//for sending statuses upstream
extension GitHubStatus {

class func toDict(state: GitHubState, description: String? = nil, targetUrl: String? = nil, context: String? = nil) -> [String: String] {
return [
"state" : state.rawValue,
"target_url" : targetUrl ?? "",
"description" : description ?? "",
"context" : context ?? ""
]
}
}

extension GitHubStatus: StatusType {

var state: BuildState {
Expand Down
Loading

0 comments on commit 6056488

Please sign in to comment.