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 5 commits
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
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ gemspec

gem "climate_control", "~> 1.0.1"
gem "rake", "~> 13.0"
gem "rspec", "~> 3.0"
gem "rspec", "~> 3.10"
gem "rspec-mocks", "~> 3.10"
gem "simplecov", "~> 0.21.2"
gem "standard", "~> 1.6"
gem "vcr", "~> 6.0.0"
gem "webmock", "~> 3.13"
15 changes: 13 additions & 2 deletions 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 @@ -16,6 +16,8 @@ GEM
ast (2.4.2)
climate_control (1.0.1)
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 +48,7 @@ GEM
faraday-patron (1.0.0)
faraday-rack (1.0.0)
faraday-retry (1.0.3)
hashdiff (1.0.1)
multipart-post (2.1.1)
octokit (4.22.0)
faraday (>= 0.9)
Expand Down Expand Up @@ -117,6 +120,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 @@ -126,9 +134,12 @@ DEPENDENCIES
climate_control (~> 1.0.1)
end_of_life!
rake (~> 13.0)
rspec (~> 3.0)
rspec (~> 3.10)
rspec-mocks (~> 3.10)
simplecov (~> 0.21.2)
standard (~> 1.6)
vcr (~> 6.0.0)
webmock (~> 3.13)

BUNDLED WITH
2.3.4
4 changes: 2 additions & 2 deletions end_of_life.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ 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"

Expand Down
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
15 changes: 15 additions & 0 deletions spec/end_of_life/repository_integration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require "climate_control"
require "ostruct"

RSpec.describe EndOfLife::Repository do
describe "#fetch" do
it "fetches all 200 repositories from an account despite exceeding page size" do
repositories = VCR.use_cassette("many_repositories") do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is not using the VCR cassette. Make sure to commit the cassettes if you want CI to use them.

Be sure that the cassettes don't leak your GITHUB_TOKEN. If they do, we can just rely on mocks, instead.

Copy link
Contributor Author

@duncan-bayne duncan-bayne Feb 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! Got it. Amazing what a few hours of good sleep can do ...

It was using the cassette, and the cassette was committed, but the check for the presence of GITHUB_TOKEN happens way before the cassette is read. The fix is to set the environment variable in the spec.

Thanks for the warning re. leaking real tokens. I had already taken care of this with the following VCR configuration:

config.filter_sensitive_data("REDACTED") { ENV["GITHUB_TOKEN"] }

EndOfLife::Repository.fetch(language: "ruby", user: nil, organizations: nil, repository: nil)
end
expect(repositories.value!.count).to eq(200)
end
end
end
58 changes: 44 additions & 14 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,34 @@
end
end

describe "#fetch" do
it "calls the GitHub API once" do
client = build_client
allow(Octokit::Client).to receive(:new).and_return(client)

repositories = with_env GITHUB_TOKEN: "FOO" do
EndOfLife::Repository.fetch(language: "ruby", user: "thoughtbot", organizations: nil, repository: nil)
end

expect(client).to have_received(:search_repositories).once
end

it "returns the search results" do
client = build_client(
search_results: [OpenStruct.new(:full_name => "thoughtbot/paperclip", :language => "ruby")]
)
allow(Octokit::Client).to receive(:new).and_return(client)

repositories = with_env GITHUB_TOKEN: "FOO" do
EndOfLife::Repository.fetch(language: "ruby", user: "thoughtbot", organizations: nil, repository: nil)
end

results = repositories.value!
expect(results.count).to eq(1)
expect(results.first.full_name).to eq("thoughtbot/paperclip")
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,13 +271,16 @@

private

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

def build_client(repo:, contents:)
def build_client(repo: nil, contents: [], search_results: [])
client = Object.new

response = OpenStruct.new(
items: search_results,
incomplete_results: false
)
allow(client).to receive(:search_repositories).and_return(response)
allow(client).to receive(:auto_paginate=).with(true)

contents.each do |path, config|
config ||= {}
encoder = config[:encoding] == "base64" ? Base64.method(:encode64) : ->(x) { x }
Expand All @@ -259,14 +289,14 @@ def build_client(repo:, contents:)
allow(client).to receive(:contents).with(repo, path: path).and_raise(Octokit::NotFound)
else
allow(client).to receive(:contents).with(repo, path: path).and_return(
if config[:content]
OpenStruct.new(
content: encoder.call(config[:content]),
name: path,
encoding: config[:encoding]
)
end
)
if config[:content]
OpenStruct.new(
content: encoder.call(config[:content]),
name: path,
encoding: config[:encoding]
)
end
)
end
end

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

Large diffs are not rendered by default.

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

require "end_of_life"

require "climate_control"
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 +38,7 @@
config.filter_run_when_matching :focus
end
end

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