module Gemdiff::RepoFinder
Constants
- GITHUB_REPO_REGEX
- PERMITTED_GEMSPEC_CLASSES
- REPO_EXCEPTIONS
rails builds several gems that are not individual projects some repos move and the old repo page still exists some repos are not mostly ruby so the github search doesn’t find them
Public Class Methods
github_url(gem_name)
click to toggle source
Try to get the homepage from the gemspec If not found, search github
# File lib/gemdiff/repo_finder.rb, line 105 def github_url(gem_name) gemspec_homepage(gem_name) || search(gem_name) end
Private Class Methods
access_token()
click to toggle source
# File lib/gemdiff/repo_finder.rb, line 139 def access_token ENV["GEMDIFF_GITHUB_TOKEN"] || ENV.fetch("GITHUB_TOKEN", nil) end
clean_url(url)
click to toggle source
return https URL with anchors stripped
# File lib/gemdiff/repo_finder.rb, line 124 def clean_url(url) url.sub(/\Ahttp:/, "https:").partition("#").first end
find_local_gemspec(name)
click to toggle source
# File lib/gemdiff/repo_finder.rb, line 153 def find_local_gemspec(name) `gem spec #{name}` end
find_remote_gemspec(name)
click to toggle source
# File lib/gemdiff/repo_finder.rb, line 157 def find_remote_gemspec(name) `gem spec -r #{name}` end
gemspec(name)
click to toggle source
# File lib/gemdiff/repo_finder.rb, line 147 def gemspec(name) yaml = find_local_gemspec(name) return yaml unless yaml.to_s.empty? find_remote_gemspec(name) end
gemspec_homepage(gem_name)
click to toggle source
# File lib/gemdiff/repo_finder.rb, line 111 def gemspec_homepage(gem_name) if (full_name = REPO_EXCEPTIONS[gem_name.to_sym]) return github_repo(full_name) end yaml = gemspec(gem_name) return if yaml.to_s.empty? spec = YAML.safe_load(yaml, permitted_classes: PERMITTED_GEMSPEC_CLASSES) return clean_url(spec.homepage) if GITHUB_REPO_REGEX.match?(spec.homepage) match = spec.description.to_s.match(GITHUB_REPO_REGEX) match && clean_url(match[0]) end
github_repo(full_name)
click to toggle source
# File lib/gemdiff/repo_finder.rb, line 135 def github_repo(full_name) "https://github.com/#{full_name}" end
octokit_client()
click to toggle source
# File lib/gemdiff/repo_finder.rb, line 143 def octokit_client Octokit::Client.new(access_token: access_token) end
search(gem_name)
click to toggle source
# File lib/gemdiff/repo_finder.rb, line 128 def search(gem_name) query = "#{gem_name} language:ruby in:name" result = octokit_client.search_repositories(query) return if result.items.empty? github_repo result.items.first.full_name end