-
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.
- Loading branch information
Showing
13 changed files
with
389 additions
and
2 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,93 @@ | ||
module Pronto | ||
class Bitbucket | ||
def initialize(repo) | ||
@repo = repo | ||
@config = Config.new | ||
@comment_cache = {} | ||
@pull_id_cache = {} | ||
end | ||
|
||
def pull_comments(sha) | ||
@comment_cache["#{pull_id}/#{sha}"] ||= begin | ||
client.pull_comments(slug, pull_id).map do |comment| | ||
Comment.new(sha, comment.content, comment.filename, comment.line_to) | ||
end | ||
end | ||
end | ||
|
||
def commit_comments(sha) | ||
@comment_cache[sha.to_s] ||= begin | ||
client.commit_comments(slug, sha).map do |comment| | ||
Comment.new(sha, comment.content, comment.filename, comment.line_to) | ||
end | ||
end | ||
end | ||
|
||
def create_commit_comment(comment) | ||
@config.logger.log("Creating commit comment on #{comment.sha}") | ||
client.create_commit_comment(slug, comment.sha, comment.body, | ||
comment.path, comment.position) | ||
end | ||
|
||
def create_pull_comment(comment) | ||
@config.logger.log("Creating pull request comment on #{pull_id}") | ||
client.create_pull_comment(slug, pull_id, comment.body, | ||
pull_sha || comment.sha, | ||
comment.path, comment.position) | ||
end | ||
|
||
private | ||
|
||
def slug | ||
return @config.bitbucket_slug if @config.bitbucket_slug | ||
@slug ||= begin | ||
@repo.remote_urls.map do |url| | ||
hostname = Regexp.escape(@config.bitbucket_hostname) | ||
match = /.*#{hostname}(:|\/)(?<slug>.*?)(?:\.git)?\z/.match(url) | ||
match[:slug] if match | ||
end.compact.first | ||
end | ||
end | ||
|
||
def client | ||
@client ||= BitbucketClient.new(@config.bitbucket_username, | ||
@config.bitbucket_password) | ||
end | ||
|
||
def pull_id | ||
pull ? pull.id.to_i : env_pull_id.to_i | ||
end | ||
|
||
def env_pull_id | ||
ENV['PULL_REQUEST_ID'] | ||
end | ||
|
||
def pull_sha | ||
pull.source['commit']['hash'] if pull | ||
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 { |pr| pr.branch['name'] == @repo.branch } | ||
end | ||
end | ||
|
||
def pull_requests | ||
@pull_requests ||= client.pull_requests(slug) | ||
end | ||
|
||
Comment = Struct.new(:sha, :body, :path, :position) do | ||
def ==(other) | ||
position == other.position && | ||
path == other.path && | ||
body == other.body | ||
end | ||
|
||
def to_s | ||
"[#{sha}] #{path}:#{position} - #{body}" | ||
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,51 @@ | ||
class BitbucketClient | ||
include HTTParty | ||
base_uri "https://api.bitbucket.org/1.0" | ||
|
||
def initialize(username, password) | ||
@credentials = { username: username, password: password } | ||
@headers = { basic_auth: @credentials } | ||
end | ||
|
||
def commit_comments(slug, sha, options={}) | ||
options.merge!(@headers) | ||
response = self.class.get("/repositories/#{slug}/changesets/#{sha}/comments", options) | ||
openstruct(response.parsed_response) | ||
end | ||
|
||
def create_commit_comment(slug, sha, body, path, position, runner = nil, commit_sha = nil, options={}) | ||
options.merge!(@headers) | ||
options[:body] = { | ||
content: body, | ||
line_to: position, | ||
filename: path | ||
} | ||
self.class.post("/repositories/#{slug}/changesets/#{sha}/comments", options) | ||
end | ||
|
||
def pull_comments(slug, pr_id, options={}) | ||
options.merge!(@headers) | ||
response = self.class.get("/repositories/#{slug}/pullrequests/#{pr_id}/comments", options) | ||
openstruct(response.parsed_response) | ||
end | ||
|
||
def pull_requests(slug, options={}) | ||
options.merge!(@headers) | ||
response = self.class.get("https://api.bitbucket.org/2.0/repositories/#{slug}/pullrequests?state=OPEN", options) | ||
openstruct(response.parsed_response['values']) | ||
end | ||
|
||
def create_pull_comment(slug, pull_id, body, sha, path, position, options={}) | ||
options.merge!(@headers) | ||
options[:body] = { | ||
content: body, | ||
line_to: position, | ||
filename: path | ||
} | ||
self.class.post("/repositories/#{slug}/pullrequests/#{pull_id}/comments", options) | ||
end | ||
|
||
def openstruct(response) | ||
response.map { |r| OpenStruct.new(r) } | ||
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
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,29 @@ | ||
module Pronto | ||
module Formatter | ||
class BitbucketFormatter | ||
def format(messages, repo, patches) | ||
client = Bitbucket.new(repo) | ||
|
||
commit_messages = messages.uniq.map do |message| | ||
sha = message.commit_sha | ||
body = message.msg | ||
path = message.path | ||
position = message.line.new_lineno | ||
|
||
create_comment(client, sha, body, path, position) | ||
end | ||
|
||
"#{commit_messages.compact.count} Pronto messages posted to BitBucket" | ||
end | ||
|
||
private | ||
|
||
def create_comment(client, sha, body, path, position) | ||
comment = Bitbucket::Comment.new(sha, body, path, position) | ||
comments = client.commit_comments(sha) | ||
existing = comments.any? { |c| comment == c } | ||
client.create_commit_comment(comment) unless existing | ||
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,29 @@ | ||
module Pronto | ||
module Formatter | ||
class BitbucketPullRequestFormatter | ||
def format(messages, repo, patches) | ||
client = Bitbucket.new(repo) | ||
head = repo.head_commit_sha | ||
|
||
commit_messages = messages.uniq.map do |message| | ||
body = message.msg | ||
path = message.path | ||
line = message.line.line.new_lineno | ||
|
||
create_comment(client, head, body, path, line) | ||
end | ||
|
||
"#{commit_messages.compact.count} Pronto messages posted to BitBucket" | ||
end | ||
|
||
private | ||
|
||
def create_comment(client, sha, body, path, position) | ||
comment = Bitbucket::Comment.new(sha, body, path, position) | ||
comments = client.pull_comments(sha) | ||
existing = comments.any? { |c| comment == c } | ||
client.create_pull_comment(comment) unless existing | ||
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
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,71 @@ | ||
module Pronto | ||
describe Bitbucket do | ||
let(:bitbucket) { described_class.new(repo) } | ||
|
||
let(:repo) do | ||
double(remote_urls: ['[email protected]:mmozuras/pronto.git'], branch: nil) | ||
end | ||
let(:sha) { '61e4bef' } | ||
let(:comment) { double(content: 'note', filename: 'path', line_to: 1, position: 1) } | ||
|
||
describe '#slug' do | ||
let(:repo) { double(remote_urls: ['[email protected]:mmozuras/pronto']) } | ||
subject { bitbucket.commit_comments(sha) } | ||
|
||
context 'git remote without .git suffix' do | ||
specify do | ||
BitbucketClient.any_instance | ||
.should_receive(:commit_comments) | ||
.with('mmozuras/pronto', sha) | ||
.once | ||
.and_return([comment]) | ||
|
||
subject | ||
end | ||
end | ||
end | ||
|
||
describe '#commit_comments' do | ||
subject { bitbucket.commit_comments(sha) } | ||
|
||
context 'three requests for same comments' do | ||
specify do | ||
BitbucketClient.any_instance | ||
.should_receive(:commit_comments) | ||
.with('mmozuras/pronto', sha) | ||
.once | ||
.and_return([comment]) | ||
|
||
subject | ||
subject | ||
subject | ||
end | ||
end | ||
end | ||
|
||
describe '#pull_comments' do | ||
subject { bitbucket.pull_comments(sha) } | ||
|
||
context 'three requests for same comments' do | ||
specify do | ||
BitbucketClient.any_instance | ||
.should_receive(:pull_requests) | ||
.once | ||
.and_return([]) | ||
|
||
BitbucketClient.any_instance | ||
.should_receive(:pull_comments) | ||
.with('mmozuras/pronto', 10) | ||
.once | ||
.and_return([comment]) | ||
|
||
ENV['PULL_REQUEST_ID'] = '10' | ||
|
||
subject | ||
subject | ||
subject | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.