-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add --clean option to the style checker
Closes #8
- Loading branch information
Emanuel Evans
committed
May 3, 2016
1 parent
2bb402a
commit 44a2345
Showing
11 changed files
with
169 additions
and
17 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
test: | ||
pre: | ||
- bundle exec exe/circlemator cancel-old | ||
- bundle exec exe/circlemator style-check --base-branch=master | ||
- bundle exec exe/circlemator style-check --base-branch=master --clean |
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,36 @@ | ||
# frozen_string_literal: true | ||
require 'circlemator/github_repo' | ||
|
||
module Circlemator | ||
class PrCleaner | ||
def initialize(github_repo:, pr_url:) | ||
@github_repo = github_repo | ||
@pr_url = pr_url | ||
get_user_id | ||
end | ||
|
||
def clean! | ||
response = @github_repo.get("#{@pr_url}/comments") | ||
if response.code != 200 | ||
raise ::Circlemator::GithubRepo::BadResponseError, response | ||
end | ||
|
||
comments = JSON.parse(response.body) | ||
comments.each do |comment| | ||
if comment.dig('user', 'id') == @user_id | ||
@github_repo.delete("#{@pr_url}/comments/#{comment.fetch('id')}") | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def get_user_id | ||
response = @github_repo.get_current_user | ||
if response.code != 200 | ||
raise ::Circlemator::GithubRepo::BadResponseError, response | ||
end | ||
@user_id = JSON.parse(response.body).fetch('id') | ||
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
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,75 @@ | ||
# frozen_string_literal: true | ||
require 'circlemator/pr_cleaner' | ||
require 'circlemator/github_repo' | ||
require 'json' | ||
|
||
RSpec.describe Circlemator::PrCleaner do | ||
describe '#clean!' do | ||
let(:github_repo) do | ||
Circlemator::GithubRepo.new(user: 'octocat', repo: 'Hello-World', github_auth_token: 'abc123') | ||
end | ||
let(:pr_url) { 'https://api.github.com/repos/octocat/Hello-World/pulls/1347' } | ||
let(:cleaner) do | ||
described_class.new github_repo: github_repo, | ||
pr_url: pr_url | ||
end | ||
let(:user_id) { 7 } | ||
let(:user_response) do | ||
{ | ||
'login' => 'octocat', | ||
'id' => user_id, | ||
}.to_json | ||
end | ||
let(:comments_response) do | ||
[ | ||
{ | ||
'id' => 1, | ||
'user' => { | ||
'id' => user_id, | ||
}, | ||
}, | ||
{ | ||
'id' => 2, | ||
'user' => { | ||
'id' => user_id + 1, | ||
}, | ||
}, | ||
{ | ||
'id' => 3, | ||
'user' => { | ||
'id' => user_id, | ||
}, | ||
}, | ||
].to_json | ||
end | ||
|
||
subject { cleaner.clean! } | ||
|
||
before do | ||
allow(github_repo) | ||
.to receive(:get_current_user).and_return double(code: 200, body: user_response) | ||
allow(github_repo) | ||
.to receive(:get).with("#{pr_url}/comments").and_return double(code: 200, body: comments_response) | ||
end | ||
|
||
context 'with a successful response' do | ||
it 'deletes all the comments for the PR for the authenticated user' do | ||
expect(github_repo).to receive(:delete).with(%r(comments/1)).once | ||
expect(github_repo).to receive(:delete).with(%r(comments/3)).once | ||
expect(github_repo).to_not receive(:delete).with(%r(comments/2)) | ||
|
||
subject | ||
end | ||
end | ||
|
||
context 'with an unsuccessful response' do | ||
before do | ||
allow(github_repo).to receive(:get).and_return double(code: 500, body: 'BAD') | ||
end | ||
|
||
it 'raises an error' do | ||
expect { subject }.to raise_error Circlemator::GithubRepo::BadResponseError | ||
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