-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Bitbucket server pull requests formatter
- Loading branch information
1 parent
9bc0bfa
commit bac17ea
Showing
6 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
lib/pronto/formatter/bitbucket_server_pull_request_formatter.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters