From 775b720d657c454afa42be9a89764aca3518b666 Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Thu, 28 Jan 2016 18:37:52 +0000 Subject: [PATCH 1/2] adding posting comments to bitbucket - had to drop down to API v1 --- .../BitBucket/BitBucketComment.swift | 4 +-- .../BitBucket/BitBucketEndpoints.swift | 9 ++++++- .../BitBucket/BitBucketServer.swift | 25 +++++++++++++++++-- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/BuildaGitServer/BitBucket/BitBucketComment.swift b/BuildaGitServer/BitBucket/BitBucketComment.swift index 2a9e439..88f0e1c 100644 --- a/BuildaGitServer/BitBucket/BitBucketComment.swift +++ b/BuildaGitServer/BitBucket/BitBucketComment.swift @@ -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) } diff --git a/BuildaGitServer/BitBucket/BitBucketEndpoints.swift b/BuildaGitServer/BitBucket/BitBucketEndpoints.swift index 7a142e5..94b2c7d 100644 --- a/BuildaGitServer/BitBucket/BitBucketEndpoints.swift +++ b/BuildaGitServer/BitBucket/BitBucketEndpoints.swift @@ -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: diff --git a/BuildaGitServer/BitBucket/BitBucketServer.swift b/BuildaGitServer/BitBucket/BitBucketServer.swift index 8bd3f88..3826c31 100644 --- a/BuildaGitServer/BitBucket/BitBucketServer.swift +++ b/BuildaGitServer/BitBucket/BitBucketServer.swift @@ -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?) -> ()) { From 670a02f06fe65c845d23344158e917ef29074a80 Mon Sep 17 00:00:00 2001 From: Honza Dvorsky Date: Thu, 28 Jan 2016 18:43:50 +0000 Subject: [PATCH 2/2] removed unused code - we can't do these things for bitbucket :( --- .../BitBucket/BitBucketServer.swift | 28 ------------------- 1 file changed, 28 deletions(-) diff --git a/BuildaGitServer/BitBucket/BitBucketServer.swift b/BuildaGitServer/BitBucket/BitBucketServer.swift index 3826c31..741e67a 100644 --- a/BuildaGitServer/BitBucket/BitBucketServer.swift +++ b/BuildaGitServer/BitBucket/BitBucketServer.swift @@ -220,11 +220,6 @@ 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 { @@ -232,32 +227,9 @@ extension BitBucketServer { 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 {