Skip to content

Commit

Permalink
Add Bitbucket server pull requests formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-skat committed Mar 22, 2017
1 parent 9bc0bfa commit bac17ea
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/pronto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
require 'pronto/config'

require 'pronto/clients/bitbucket_client'
require 'pronto/clients/bitbucket_server_client'

require 'pronto/git/repository'
require 'pronto/git/patches'
Expand All @@ -30,6 +31,7 @@
require 'pronto/github'
require 'pronto/gitlab'
require 'pronto/bitbucket'
require 'pronto/bitbucket_server'

require 'pronto/formatter/colorizable'
require 'pronto/formatter/base'
Expand All @@ -44,6 +46,7 @@
require 'pronto/formatter/gitlab_formatter'
require 'pronto/formatter/bitbucket_formatter'
require 'pronto/formatter/bitbucket_pull_request_formatter'
require 'pronto/formatter/bitbucket_server_pull_request_formatter'
require 'pronto/formatter/checkstyle_formatter'
require 'pronto/formatter/null_formatter'
require 'pronto/formatter/formatter'
Expand Down
34 changes: 34 additions & 0 deletions lib/pronto/bitbucket_server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Pronto
class BitbucketServer < Bitbucket
def pull_comments(sha)
@comment_cache["#{pull_id}/#{sha}"] ||= begin
client.pull_comments(slug, pull_id).map do |comment|
if comment['commentAnchor']
Comment.new(sha, comment['comment']['text'], comment['commentAnchor']['path'], comment['commentAnchor']['line'])
else
Comment.new(sha, comment['comment']['text'])
end
end
end
end

private

def client
@client ||= BitbucketServerClient.new(@config.bitbucket_username,
@config.bitbucket_password,
@config.bitbucket_api_endpoint)
end

def pull
@pull ||=
if env_pull_id
pull_requests.find { |pr| pr.id.to_i == env_pull_id.to_i }
elsif @repo.branch
pull_requests.find do |pr|
pr['fromRef']['displayId'] == @repo.branch
end
end
end
end
end
64 changes: 64 additions & 0 deletions lib/pronto/clients/bitbucket_server_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
class BitbucketServerClient
include HTTParty

def initialize(username, password, endpoint)
self.class.base_uri(endpoint)
self.class.basic_auth(username, password)
@headers = { 'Content-Type' => 'application/json' }
end

def pull_comments(slug, pr_id)
project_key, repository_key = slug.split('/')
url = "/projects/#{project_key}/repos/#{repository_key}/pull-requests/#{pr_id}/activities"
response = paged_request(url)
response.select { |activity| activity.action == 'COMMENTED' }
end

def pull_requests(slug)
project_key, repository_key = slug.split('/')
url = "/projects/#{project_key}/repos/#{repository_key}/pull-requests"
paged_request(url, state: 'OPEN')
end

def create_pull_comment(slug, pull_id, body, path, position)
project_key, repository_key = slug.split('/')
url = "/projects/#{project_key}/repos/#{repository_key}/pull-requests/#{pull_id}/comments"
post(url, body, path, position)
end

private

def openstruct(response)
response.map { |r| OpenStruct.new(r) }
end

def post(url, body, path, position)
body = {
text: body,
anchor: {
line: position,
lineType: 'ADDED',
path: path,
srcPath: path
}
}
self.class.post(url, body: body.to_json, headers: @headers)
end

def paged_request(url, query = {})
Enumerator.new do |y|
next_page_start = 0
loop do
response = self.class.get(url, query.merge(start: next_page_start)).parsed_response
break if response['values'].nil?

response['values'].each do |item|
y << OpenStruct.new(item)
end

next_page_start = response['nextPageStart']
break unless next_page_start
end
end
end
end
1 change: 1 addition & 0 deletions lib/pronto/config_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ConfigFile
'slug' => nil,
'username' => nil,
'password' => nil,
'api_endpoint' => nil,
'web_endpoint' => 'https://bitbucket.org/'
},
'text' => {
Expand Down
17 changes: 17 additions & 0 deletions lib/pronto/formatter/bitbucket_server_pull_request_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Pronto
module Formatter
class BitbucketServerPullRequestFormatter < PullRequestFormatter
def client_module
BitbucketServer
end

def pretty_name
'BitBucket Server'
end

def line_number(message, _)
message.line.line.new_lineno if message.line
end
end
end
end
1 change: 1 addition & 0 deletions lib/pronto/formatter/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def self.names
'gitlab' => GitlabFormatter,
'bitbucket' => BitbucketFormatter,
'bitbucket_pr' => BitbucketPullRequestFormatter,
'bitbucket_server_pr' => BitbucketServerPullRequestFormatter,
'json' => JsonFormatter,
'checkstyle' => CheckstyleFormatter,
'text' => TextFormatter,
Expand Down

0 comments on commit bac17ea

Please sign in to comment.