forked from prontolabs/pronto
-
Notifications
You must be signed in to change notification settings - Fork 0
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
9 changed files
with
183 additions
and
1 deletion.
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
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 GitlabFormatter | ||
def format(messages, repo) | ||
messages = messages.uniq { |message| [message.msg, message.line.new_lineno] } | ||
client = Gitlab.new repo | ||
|
||
commit_messages = messages.map do |message| | ||
create_comment(client, | ||
message.commit_sha, | ||
message.msg, | ||
message.path, | ||
message.line.commit_line.new_lineno) | ||
end | ||
|
||
"#{commit_messages.compact.count} Pronto messages posted to GitLab" | ||
end | ||
|
||
private | ||
|
||
def create_comment(client, sha, note, path, line) | ||
comment = Gitlab::Comment.new(sha, note, path, line) | ||
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,55 @@ | ||
module Pronto | ||
class Gitlab | ||
def initialize(repo) | ||
@repo = repo | ||
@comment_cache = {} | ||
end | ||
|
||
def commit_comments(sha) | ||
@comment_cache["#{sha}"] ||= begin | ||
client.commit_comments(slug, sha).map do |comment| | ||
Comment.new(sha, comment.note, comment.path, comment.line) | ||
end | ||
end | ||
end | ||
|
||
def create_commit_comment(comment) | ||
client.create_commit_comment(slug, comment.sha, comment.note, | ||
path: comment.path, line: comment.line, | ||
line_type: 'new') | ||
end | ||
|
||
private | ||
|
||
def slug | ||
@slug ||= begin | ||
host = URI.split(endpoint)[2, 2].compact.join(':') | ||
slug = @repo.remote_urls.map do |url| | ||
match = /.*#{host}(:|\/)(?<slug>.*).git/.match(url) | ||
match[:slug] if match | ||
end.compact.first | ||
URI.escape(slug, '/') if slug | ||
end | ||
end | ||
|
||
def client | ||
@client ||= ::Gitlab.client(endpoint: endpoint, private_token: private_token) | ||
end | ||
|
||
def private_token | ||
ENV['GITLAB_API_PRIVATE_TOKEN'] | ||
end | ||
|
||
def endpoint | ||
ENV['GITLAB_API_ENDPOINT'] | ||
end | ||
|
||
class Comment < Struct.new(:sha, :note, :path, :line) | ||
def ==(other) | ||
line == other.line && | ||
path == other.path && | ||
note == other.note | ||
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,61 @@ | ||
require 'spec_helper' | ||
|
||
module Pronto | ||
module Formatter | ||
describe GitlabFormatter do | ||
ENV['GITLAB_API_ENDPOINT'] = 'http://example.com/api/v3' | ||
ENV['GITLAB_API_PRIVATE_TOKEN'] = 'token' | ||
|
||
let(:gitlab_formatter) { GitlabFormatter.new } | ||
|
||
describe '#format' do | ||
subject { gitlab_formatter.format(messages, repository) } | ||
let(:messages) { [message, message] } | ||
let(:repository) { Git::Repository.new('.') } | ||
let(:message) { Message.new('path/to', line, :warning, 'crucial') } | ||
let(:line) { double(new_lineno: 1, commit_sha: '123', position: nil) } | ||
before { line.stub(:commit_line).and_return(line) } | ||
|
||
specify do | ||
::Gitlab::Client.any_instance | ||
.should_receive(:commit_comments) | ||
.once | ||
.and_return([]) | ||
|
||
::Gitlab::Client.any_instance | ||
.should_receive(:create_commit_comment) | ||
.once | ||
|
||
subject | ||
end | ||
end | ||
|
||
describe '#format without duplicates' do | ||
subject { gitlab_formatter.format(messages, repository) } | ||
let(:messages) { [message1, message2] } | ||
let(:repository) { Git::Repository.new('.') } | ||
let(:message1) { Message.new('path/to1', line1, :warning, 'crucial') } | ||
let(:message2) { Message.new('path/to2', line2, :warning, 'crucial') } | ||
let(:line1) { double(new_lineno: 1, commit_sha: '123', position: nil) } | ||
let(:line2) { double(new_lineno: 2, commit_sha: '123', position: nil) } | ||
before do | ||
line1.stub(:commit_line).and_return(line1) | ||
line2.stub(:commit_line).and_return(line2) | ||
end | ||
|
||
specify do | ||
::Gitlab::Client.any_instance | ||
.should_receive(:commit_comments) | ||
.once | ||
.and_return([]) | ||
|
||
::Gitlab::Client.any_instance | ||
.should_receive(:create_commit_comment) | ||
.twice | ||
|
||
subject | ||
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,31 @@ | ||
require 'spec_helper' | ||
|
||
module Pronto | ||
describe Gitlab do | ||
let(:gitlab) { Gitlab.new(repo) } | ||
|
||
describe '#commit_comments' do | ||
subject { gitlab.commit_comments(sha) } | ||
|
||
context 'three requests for same comments' do | ||
let(:repo) { double(remote_urls: ['[email protected]:mmozuras/pronto.git']) } | ||
let(:sha) { 'foobar' } | ||
|
||
specify do | ||
ENV['GITLAB_API_ENDPOINT'] = 'http://gitlab.example.com/api/v3' | ||
ENV['GITLAB_API_PRIVATE_TOKEN'] = 'token' | ||
|
||
::Gitlab::Client.any_instance | ||
.should_receive(:commit_comments) | ||
.with('mmozuras%2Fpronto', sha) | ||
.once | ||
.and_return([]) | ||
|
||
subject | ||
subject | ||
subject | ||
end | ||
end | ||
end | ||
end | ||
end |