Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement pagination using auto_paginate #13

Merged
merged 8 commits into from
Feb 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ PATH
specs:
end_of_life (0.2.0)
dry-monads (~> 1.3)
octokit (~> 4.0)
octokit (~> 4.22)
pastel (~> 0.8.0)
tty-spinner (~> 0.9.0)
tty-table (~> 0.12.0)
Expand All @@ -15,7 +15,10 @@ GEM
public_suffix (>= 2.0.2, < 5.0)
ast (2.4.2)
climate_control (1.0.1)
coderay (1.1.3)
concurrent-ruby (1.1.9)
crack (0.4.5)
rexml
diff-lcs (1.4.4)
docile (1.4.0)
dry-core (0.7.1)
Expand Down Expand Up @@ -46,6 +49,8 @@ GEM
faraday-patron (1.0.0)
faraday-rack (1.0.0)
faraday-retry (1.0.3)
hashdiff (1.0.1)
method_source (1.0.0)
multipart-post (2.1.1)
octokit (4.22.0)
faraday (>= 0.9)
Expand All @@ -55,6 +60,9 @@ GEM
ast (~> 2.4.1)
pastel (0.8.0)
tty-color (~> 0.5)
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
public_suffix (4.0.6)
rainbow (3.0.0)
rake (13.0.6)
Expand Down Expand Up @@ -117,6 +125,11 @@ GEM
tty-screen (~> 0.8)
unicode-display_width (2.1.0)
unicode_utils (1.4.0)
vcr (6.0.0)
webmock (3.13.0)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
x86_64-darwin-20
Expand All @@ -125,10 +138,14 @@ PLATFORMS
DEPENDENCIES
climate_control (~> 1.0.1)
end_of_life!
pry (~> 0.14)
rake (~> 13.0)
rspec (~> 3.0)
rspec-mocks (~> 3.10)
simplecov (~> 0.21.2)
standard (~> 1.6)
vcr (~> 6.0.0)
webmock (~> 3.13)

BUNDLED WITH
2.3.4
10 changes: 8 additions & 2 deletions end_of_life.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,18 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "octokit", "~> 4.0"
spec.add_dependency "pastel", "~> 0.8.0"
spec.add_dependency "dry-monads", "~> 1.3"
spec.add_dependency "octokit", "~> 4.22"
spec.add_dependency "pastel", "~> 0.8.0"
spec.add_dependency "tty-spinner", "~> 0.9.0"
spec.add_dependency "tty-table", "~> 0.12.0"

spec.add_development_dependency "pry", "~> 0.14"
MatheusRich marked this conversation as resolved.
Show resolved Hide resolved
spec.add_development_dependency "rspec", "~> 3.10"
spec.add_development_dependency "rspec-mocks", "~> 3.10"
spec.add_development_dependency "vcr", "~> 6.0.0"
spec.add_development_dependency "webmock", "~> 3.13"
MatheusRich marked this conversation as resolved.
Show resolved Hide resolved

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
end
8 changes: 4 additions & 4 deletions lib/end_of_life/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ class << self

def fetch(language:, user:, organizations:, repository:)
github_client.bind do |github|
github.auto_paginate = true
user ||= github.user.login
query = search_query_for(language: language, user: user, repository: repository, organizations: organizations)

response = github.search_repositories(query, per_page: 100)
warn "Incomplete results: we only search 100 repos at a time" if response.incomplete_results
query = search_query_for(language: language, user: user, repository: repository, organizations: organizations)
items = github.search_repositories(query).items

Success(
response.items.map do |repo|
items.map do |repo|
Repository.new(
full_name: repo.full_name,
url: repo.html_url,
Expand Down
20 changes: 20 additions & 0 deletions spec/end_of_life/repository_integration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require "climate_control"
require "ostruct"

RSpec.describe EndOfLife::Repository do
describe "#fetch" do
context "given an account with 200 end of life repositories" do
subject(:repositories) do
VCR.use_cassette("many_repositories") do
EndOfLife::Repository.fetch(language: "ruby", user: nil, organizations: nil, repository: nil)
end
end

it "returns Success with the collection of repositories" do
expect(repositories.value_or(nil).count).to eq(200)
MatheusRich marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
end
40 changes: 35 additions & 5 deletions spec/end_of_life/repository_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require "climate_control"
require "ostruct"

RSpec.describe EndOfLife::Repository do
Expand Down Expand Up @@ -108,6 +107,41 @@
end
end

describe "#fetch" do
let(:client) { instance_double(Octokit::Client, :user => user) }
MatheusRich marked this conversation as resolved.
Show resolved Hide resolved
let(:user) { OpenStruct.new(:login => "j-random-hacker") }

before do
allow(Octokit::Client).to receive(:new).and_return(client)
end

context "with complete results" do
let(:items) { [OpenStruct.new(:full_name => "j-random-hacker/ruby-foo", :language => "ruby")] }
let(:response) { OpenStruct.new(:items => items, incomplete_results: false) }

subject(:repositories) do
with_env GITHUB_TOKEN: "FOO" do
EndOfLife::Repository.fetch(language: "ruby", user: "j-random-hacker", organizations: nil, repository: nil)
end
end

before do
allow(client).to receive(:auto_paginate=).with(true)
allow(client).to receive(:user).and_return(user)
allow(client).to receive(:search_repositories).and_return(response)
end

it "calls the GitHub API once" do
repositories
expect(client).to have_received(:search_repositories).once
end

it "returns Success with the collection of repositories" do
expect(repositories.value_or(nil).count).to eq(1)
MatheusRich marked this conversation as resolved.
Show resolved Hide resolved
end
end
end

describe "#ruby_version" do
it "returns the minimum ruby version found in the repository" do
client = build_client(
Expand Down Expand Up @@ -244,10 +278,6 @@

private

def with_env(...)
ClimateControl.modify(...)
end

def build_client(repo:, contents:)
client = Object.new

Expand Down
249 changes: 249 additions & 0 deletions spec/fixtures/vcr_cassettes/many_repositories.yml

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@

require "end_of_life"

require "climate_control"
require "pry"
require "vcr"
require "webmock"

VCR.configure do |config|
config.cassette_library_dir = "spec/fixtures/vcr_cassettes"
config.hook_into :webmock
config.filter_sensitive_data("REDACTED") { ENV["GITHUB_TOKEN"] }
end

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"
Expand All @@ -28,3 +39,7 @@
config.filter_run_when_matching :focus
end
end

def with_env(...)
ClimateControl.modify(...)
end