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

api/v1/versions endpoint #277

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions lib/gemstash/gem_source/private_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ def serve_versions
halt 403, "Not yet supported"
end

def serve_gem_versions(name)
authorization.protect(self) do
auth.check("fetch") if gemstash_env.config[:protected_fetch]
name.slice! ".json"
gem = fetch_gem(name)
halt 404 unless gem.exist?(:spec)
content_type "application/json;charset=UTF-8"
fetch_gem_versions(name).to_json
end
end

def serve_info(name)
halt 403, "Not yet supported"
end
Expand Down Expand Up @@ -118,6 +129,29 @@ def fetch_gem(gem_full_name)
halt 403, "That gem has been yanked" unless gem.properties[:indexed]
gem
end

def fetch_gem_versions(gem)
results = db["
SELECT rubygem.name,
version.number, version.platform
FROM rubygems rubygem
JOIN versions version
ON version.rubygem_id = rubygem.id
WHERE rubygem.name = ?
AND version.indexed = ?", gem.to_a, true].to_a
results.group_by {|r| r[:name] }.each do |gem, rows|
requirements = rows.group_by {|r| [r[:number], r[:platform]] }

value = requirements.map do |version, r|
Copy link
Member

@olleolleolle olleolleolle Nov 24, 2020

Choose a reason for hiding this comment

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

Would this do the right thing?

Suggested change
value = requirements.map do |version, r|
value = requirements.map do |(version_number, platform), r|

Well, version_number is perhaps a too-long name, could call it version, too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

agreed. I changed it in the following commit. eb619cd


{
:number => version.first,
:platform => version.last
}
end

yield(gem, value)
end
end
end
end
4 changes: 4 additions & 0 deletions lib/gemstash/gem_source/upstream_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ def serve_versions
redirect index_upstream.url("versions", request.query_string)
end

def serve_gem_versions(name)
redirect index_upstream.url("api/v1/versions/#{name}", request.query_string)
end

def serve_info(name)
redirect index_upstream.url("info/#{name}", request.query_string)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/gemstash/web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def http_client_for(server_url)
@gem_source.serve_dependencies_json
end

get "/api/v1/versions" do
@gem_source.serve_dependencies_json
get "/api/v1/versions/:name" do
@gem_source.serve_gem_versions(params[:name])
end

post "/api/v1/gems" do
Expand Down