This repository has been archived by the owner on Feb 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add paging for retrieval of forks (#3)
- Loading branch information
Showing
8 changed files
with
294 additions
and
6 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
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,57 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
require 'timecop' | ||
|
||
RSpec.describe GithubBot::Github::CheckRun do | ||
let(:client) { double } | ||
|
||
subject do | ||
described_class.new( | ||
name: 'foo', | ||
repo: 'bar', | ||
sha: 'baz', | ||
client_api: client | ||
) | ||
end | ||
|
||
before :each do | ||
expect(client).to receive(:create_check_run) | ||
end | ||
|
||
it '#initialize' do | ||
expect(subject).not_to be_nil | ||
end | ||
|
||
it '#in_progress!' do | ||
time = Time.iso8601('2020-04-21T00:00:00Z') | ||
Timecop.freeze(time) do | ||
expect(subject).to receive(:update).with(status: 'in_progress', started_at: time) | ||
subject.in_progress! | ||
end | ||
end | ||
|
||
it '#complete!' do | ||
time = Time.iso8601('2020-04-21T00:00:00Z') | ||
Timecop.freeze(time) do | ||
expect(subject).to receive(:update).with( | ||
status: 'completed', | ||
conclusion: 'success', | ||
completed_at: time | ||
) | ||
subject.complete! | ||
end | ||
end | ||
|
||
it '#action_required!' do | ||
time = Time.iso8601('2020-04-21T00:00:00Z') | ||
Timecop.freeze(time) do | ||
expect(subject).to receive(:update).with( | ||
status: 'completed', | ||
conclusion: 'action_required', | ||
completed_at: time | ||
) | ||
subject.action_required! | ||
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,156 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe GithubBot::Github::Client do | ||
let(:request) { double } | ||
let(:client) { double } | ||
let(:removed_file) { double('Sawyer::Resource', type: 'file', status: 'removed') } | ||
let(:added_file) { double('Sawyer::Resource', type: 'file', status: 'added') } | ||
let(:files) { [removed_file, added_file] } | ||
let(:pull_request) { double } | ||
|
||
subject { described_class.initialize(request) } | ||
|
||
before :each do | ||
described_class.instance_variable_set(:@instance, nil) | ||
subject.instance_variable_set(:@client, client) | ||
end | ||
|
||
it '.initialize' do | ||
expect(described_class.initialize(request)).not_to be_nil | ||
end | ||
|
||
describe '.instance' do | ||
before :each do | ||
described_class.instance_variable_set(:@instance, nil) | ||
end | ||
|
||
context 'when no instance' do | ||
it 'raises error' do | ||
expect { described_class.instance }.to raise_error(StandardError) | ||
end | ||
end | ||
|
||
context 'when an instance exists' do | ||
let(:instance) { double } | ||
it 'returns the instance' do | ||
described_class.instance_variable_set(:@instance, instance) | ||
expect(described_class.instance).to eql(instance) | ||
end | ||
end | ||
end | ||
|
||
describe '#file_content' do | ||
let(:file) { double('Sawyer::Resource', type: 'file') } | ||
let(:filename) { 'foo' } | ||
|
||
before do | ||
allow(file).to receive(:filename).and_return(filename) | ||
allow(subject).to receive(:repository_full_name) | ||
allow(subject).to receive(:head_sha) | ||
end | ||
|
||
context 'when content not found' do | ||
it 'returns empty string' do | ||
expect(client).to receive(:contents).and_raise(Octokit::NotFound) | ||
expect(subject.file_content(file)).to be_empty | ||
end | ||
end | ||
|
||
context 'when content found' do | ||
context 'when small' do | ||
it 'decodes the response' do | ||
expect(client).to receive(:contents) | ||
expect { subject.file_content(file) }.not_to raise_error | ||
end | ||
end | ||
|
||
context 'when too_large' do | ||
let(:exception) { Octokit::Forbidden.new(body: { errors: [code: 'too_large'] }) } | ||
let(:uri_object) { double } | ||
let(:raw_url) { 'https://raw.github.com/foo/bar/main/file.rb'} | ||
let(:parsed_url) { double } | ||
|
||
it 'uses the raw url for retrieval' do | ||
expect(client).to receive(:contents).and_raise(exception) | ||
expect(file).to receive(:raw_url).and_return(raw_url) | ||
expect(URI).to receive(:parse).with(raw_url).and_return(parsed_url) | ||
expect(parsed_url).to receive(:open).and_return(uri_object) | ||
expect(uri_object).to receive(:read) | ||
expect { subject.file_content(file) }.not_to raise_error | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe '#modified_files' do | ||
it 'returns modified files' do | ||
allow(subject).to receive(:pull_request).and_return(pull_request) | ||
expect(subject).to receive(:repository_full_name) | ||
expect(subject).to receive(:pull_request_number) | ||
expect(client).to receive(:pull_request_files).and_return(files) | ||
expect(subject.modified_files).not_to be_empty | ||
expect(subject.modified_files).to include(added_file) | ||
expect(subject.modified_files).not_to include(removed_file) | ||
end | ||
end | ||
|
||
describe '#files' do | ||
it 'returns all files' do | ||
allow(subject).to receive(:pull_request).and_return(pull_request) | ||
expect(subject).to receive(:repository_full_name) | ||
expect(subject).to receive(:pull_request_number) | ||
expect(client).to receive(:pull_request_files).and_return(files) | ||
expect(subject.files).not_to be_empty | ||
expect(subject.files).to include(added_file) | ||
expect(subject.files).to include(removed_file) | ||
end | ||
end | ||
|
||
describe '#comment' do | ||
it 'adds a comment' do | ||
expect(subject).to receive(:repository_full_name) | ||
expect(subject).to receive(:pull_request_number) | ||
expect(client).to receive(:add_comment) | ||
subject.comment(message: 'foo') | ||
end | ||
end | ||
|
||
describe '#create_check_run' do | ||
it 'creates a check run instance' do | ||
expect(subject).to receive(:repository_full_name) | ||
expect(subject).to receive(:head_sha) | ||
expect(GithubBot::Github::CheckRun).to receive(:new) | ||
subject.create_check_run(name: 'foo') | ||
end | ||
end | ||
|
||
describe '#pull_request_details' do | ||
let(:repo_name) { 'foo-delivery/foo-config' } | ||
let(:pull_request_number) { 123 } | ||
let(:pull_request) { { number: pull_request_number } } | ||
let(:pull_request_response) { double } | ||
let(:mock_payload) do | ||
{ | ||
repository: { | ||
full_name: repo_name | ||
} | ||
} | ||
end | ||
|
||
it 'validates' do | ||
allow(subject).to receive(:payload).and_return(mock_payload) | ||
expect(subject).to receive(:pull_request).and_return(pull_request) | ||
expect(client).to receive(:pull_request).with( | ||
repo_name, pull_request_number | ||
).and_return(pull_request_response) | ||
expect(subject.pull_request_details).to eq(pull_request_response) | ||
|
||
# check set of instance variable | ||
expect(subject.instance_variable_get(:@pull_request_details)).to eq(pull_request_response) | ||
expect(client).not_to receive(:pull_request) | ||
expect(subject.pull_request_details).to eq(pull_request_response) | ||
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,54 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
class DummyTestClass | ||
include GithubBot::Github::Payload | ||
|
||
def repository; end | ||
end | ||
|
||
RSpec.describe GithubBot::Github::Payload do | ||
subject { DummyTestClass.new } | ||
|
||
describe '#repository_fork_urls' do | ||
let(:mock_payload) { double } | ||
let(:mock_repository) { double } | ||
let(:mock_parse) { double('URI::HTTP', query: []) } | ||
let(:mock_open) { double } | ||
let(:mock_data) do | ||
[].tap do |ar| | ||
ar << { | ||
updated_at: '2020-10-01T00:00:00Z', | ||
clone_url: 'http://foo.clone.com' | ||
}.with_indifferent_access | ||
|
||
ar << { | ||
updated_at: '2020-10-02T00:00:00Z', | ||
clone_url: 'http://bar.clone.com' | ||
}.with_indifferent_access | ||
end | ||
end | ||
|
||
before do | ||
end | ||
|
||
it 'returns fork urls' do | ||
allow(URI).to receive(:parse).and_return(mock_parse) | ||
allow(mock_parse).to receive(:query=) | ||
allow(mock_parse).to receive(:open).and_return(mock_open) | ||
allow(mock_open).to receive(:read) | ||
allow(subject).to receive(:repository).and_return(mock_repository) | ||
allow(subject).to receive(:payload).and_return(mock_payload) | ||
|
||
# first sequence expect to retrieve actual data, second time return empty response | ||
expect(JSON).to receive(:parse).ordered.and_return(mock_data) | ||
expect(JSON).to receive(:parse).ordered.and_return({}) | ||
|
||
expect(mock_repository).to receive(:[]).with(:forks_url) | ||
urls = subject.repository_fork_urls | ||
expect(urls.length).to be 2 | ||
expect(urls).to include('http://bar.clone.com') | ||
end | ||
end | ||
end |