Skip to content

Commit

Permalink
Merge pull request #223 from czechboy0/hd/bitbucket_posting_comments
Browse files Browse the repository at this point in the history
BitBucket posting comments
  • Loading branch information
czechboy0 committed Jan 28, 2016
2 parents 0fe5c99 + 670a02f commit 306d4ce
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
4 changes: 2 additions & 2 deletions BuildaGitServer/BitBucket/BitBucketComment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class BitBucketComment: BitBucketEntity, CommentType {
required init(json: NSDictionary) {

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

super.init(json: json)
}
Expand Down
9 changes: 8 additions & 1 deletion BuildaGitServer/BitBucket/BitBucketEndpoints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ class BitBucketEndpoints {
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"

if params?["method"] == "POST" {
let repo = params!["repo"]!
let pr = params!["pr"]!
return "/1.0/repositories/\(repo)/pullrequests/\(pr)/comments"
} else {
return "\(pr)/comments"
}

case .CommitStatuses:

Expand Down
53 changes: 23 additions & 30 deletions BuildaGitServer/BitBucket/BitBucketServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,29 @@ extension BitBucketServer: SourceServerType {

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"))
let params = [
"repo": repo,
"pr": issueNumber.description
]

let body = [
"content": comment
]

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

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

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

func getCommentsOfIssue(issueNumber: Int, repo: String, completion: (comments: [CommentType]?, error: ErrorType?) -> ()) {
Expand Down Expand Up @@ -199,44 +220,16 @@ extension BitBucketServer {

private func _sendRequest(request: NSMutableURLRequest, isRetry: Bool = false, completion: HTTP.Completion) {

// let cachedInfo = self.cache.getCachedInfoForRequest(request)
// if let etag = cachedInfo.etag {
// request.setValue(etag, forHTTPHeaderField: "If-None-Match")
// }

self.http.sendRequest(request) { (response, body, error) -> () in

if let error = error {
completion(response: response, body: body, error: error)
return
}

// if let response = response {
// let headers = response.allHeaderFields
//
// if
// let resetTime = (headers["X-RateLimit-Reset"] as? NSString)?.doubleValue,
// let limit = (headers["X-RateLimit-Limit"] as? NSString)?.integerValue,
// let remaining = (headers["X-RateLimit-Remaining"] as? NSString)?.integerValue {
//
// let rateLimitInfo = GitHubRateLimit(resetTime: resetTime, limit: limit, remaining: remaining)
// self.latestRateLimitInfo = rateLimitInfo
//
// } else {
// Log.error("No X-RateLimit info provided by GitHub in headers: \(headers), we're unable to detect the remaining number of allowed requests. GitHub might fail to return data any time now :(")
// }
// }

//error out on special HTTP status codes
let statusCode = response!.statusCode
switch statusCode {
// case 200...299: //good response, cache the returned data
// let responseInfo = ResponseInfo(response: response!, body: body)
// cachedInfo.update(responseInfo)
// case 304: //not modified, return the cached response
// let responseInfo = cachedInfo.responseInfo!
// completion(response: responseInfo.response, body: responseInfo.body, error: nil)
// return
case 401: //unauthorized, use refresh token to get a new access token
//only try to refresh token once
if !isRetry {
Expand Down

0 comments on commit 306d4ce

Please sign in to comment.