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

Use html_url from Pages endpoint (behind preview env flag) #60

Merged
merged 6 commits into from May 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -2,5 +2,5 @@ source 'https://rubygems.org'
gemspec

group :test do
gem "webmock"
gem "webmock", "~> 2.0"
end
35 changes: 27 additions & 8 deletions lib/jekyll-github-metadata.rb
@@ -1,4 +1,6 @@
require 'octokit'
require 'logger'

if defined?(Jekyll) && Jekyll.respond_to?(:env) && Jekyll.env == 'development'
begin
require 'dotenv'
Expand All @@ -17,21 +19,38 @@ module Errors
module GitHubMetadata
NoRepositoryError = Class.new(Jekyll::Errors::FatalException)

autoload :Client, 'jekyll-github-metadata/client'
autoload :Pages, 'jekyll-github-metadata/pages'
autoload :Repository, 'jekyll-github-metadata/repository'
autoload :Sanitizer, 'jekyll-github-metadata/sanitizer'
autoload :Value, 'jekyll-github-metadata/value'
autoload :VERSION, 'jekyll-github-metadata/version'
autoload :Client, 'jekyll-github-metadata/client'
autoload :Pages, 'jekyll-github-metadata/pages'
autoload :Repository, 'jekyll-github-metadata/repository'
autoload :RepositoryCompat, 'jekyll-github-metadata/repository_compat'
autoload :Sanitizer, 'jekyll-github-metadata/sanitizer'
autoload :Value, 'jekyll-github-metadata/value'
autoload :VERSION, 'jekyll-github-metadata/version'

class << self
attr_accessor :repository
attr_writer :client
attr_writer :client, :logger

def environment
Jekyll.respond_to?(:env) ? Jekyll.env : (Pages.env || 'development')
end

def logger
@logger ||= if Jekyll.respond_to?(:logger)
Jekyll.logger
else
Logger.new($stdout)
end
end

def log(severity, message)
if logger.method(severity).arity.abs >= 2
logger.public_send(severity, "GitHub Metadata:", message.to_s)
else
logger.public_send(severity, "GitHub Metadata: #{message}")
end
end

def client
@client ||= Client.new
end
Expand Down Expand Up @@ -103,7 +122,7 @@ def init!
register_value('is_user_page', proc { |_,r| r.user_page? })
register_value('is_project_page', proc { |_,r| r.project_page? })
register_value('show_downloads', proc { |_,r| r.show_downloads? })
register_value('url', proc { |_,r| r.pages_url })
register_value('url', proc { |_,r| r.html_url })
register_value('contributors', proc { |_,r| r.contributors })
register_value('releases', proc { |_,r| r.releases })

Expand Down
4 changes: 2 additions & 2 deletions lib/jekyll-github-metadata/client.rb
Expand Up @@ -48,7 +48,7 @@ def method_missing(method_name, *args, &block)
method = method_name.to_s
if accepts_client_method?(method_name)
key = cache_key(method_name, args)
Jekyll.logger.debug "GitHub Metadata:", "Calling @client.#{method}(#{args.map(&:inspect).join(", ")})"
Jekyll::GitHubMetadata.log :debug, "Calling @client.#{method}(#{args.map(&:inspect).join(", ")})"
cache[key] ||= save_from_errors { @client.public_send(method_name, *args, &block) }
elsif @client.respond_to?(method_name)
raise InvalidMethodError, "#{method_name} is not whitelisted on #{inspect}"
Expand Down Expand Up @@ -90,7 +90,7 @@ def pluck_auth_method
elsif !ENV['NO_NETRC'] && File.exist?(File.join(ENV['HOME'], '.netrc')) && safe_require('netrc')
{ :netrc => true }
else
Jekyll.logger.warn "GitHub Metadata:", "No GitHub API authentication could be found." +
Jekyll::GitHubMetadata.log :warn, "No GitHub API authentication could be found." \
" Some fields may be missing or have incorrect data."
{}.freeze
end
Expand Down
4 changes: 2 additions & 2 deletions lib/jekyll-github-metadata/ghp_metadata_generator.rb
Expand Up @@ -4,9 +4,9 @@ class GHPMetadataGenerator < Jekyll::Generator
safe true

def generate(site)
Jekyll.logger.debug "Generator:", "Calling GHPMetadataGenerator"
Jekyll::GitHubMetadata.log :debug, "Calling GHPMetadataGenerator"
initialize_repo! nwo(site)
Jekyll.logger.debug "GitHub Metadata:", "Generating for #{GitHubMetadata.repository.nwo}"
Jekyll::GitHubMetadata.log :debug, "Generating for #{GitHubMetadata.repository.nwo}"

site.config['github'] =
case site.config['github']
Expand Down
19 changes: 12 additions & 7 deletions lib/jekyll-github-metadata/pages.rb
Expand Up @@ -3,13 +3,14 @@ module GitHubMetadata
class Pages
class << self
DEFAULTS = {
'PAGES_ENV' => 'dotcom'.freeze,
'PAGES_API_URL' => 'https://api.github.com'.freeze,
'PAGES_HELP_URL' => 'https://help.github.com'.freeze,
'PAGES_GITHUB_HOSTNAME' => 'github.com'.freeze,
'PAGES_PAGES_HOSTNAME' => 'github.io'.freeze,
'SSL' => 'false'.freeze,
'SUBDOMAIN_ISOLATION' => 'false'.freeze
'PAGES_ENV' => 'dotcom'.freeze,
'PAGES_API_URL' => 'https://api.github.com'.freeze,
'PAGES_HELP_URL' => 'https://help.github.com'.freeze,
'PAGES_GITHUB_HOSTNAME' => 'github.com'.freeze,
'PAGES_PAGES_HOSTNAME' => 'github.io'.freeze,
'SSL' => 'false'.freeze,
'SUBDOMAIN_ISOLATION' => 'false'.freeze,
'PAGES_PREVIEW_HTML_URL' => nil
}.freeze

# Whether the GitHub instance supports HTTPS
Expand Down Expand Up @@ -40,6 +41,10 @@ def env
env_var 'PAGES_ENV'
end

def repo_pages_html_url_preview?
env_var "PAGES_PREVIEW_HTML_URL"
end

def github_url
if dotcom?
"https://github.com".freeze
Expand Down
91 changes: 34 additions & 57 deletions lib/jekyll-github-metadata/repository.rb
Expand Up @@ -8,14 +8,26 @@ def initialize(name_with_owner)
@name = nwo.split("/").last
end

def git_ref
user_page? ? 'master' : 'gh-pages'
def repo_compat
@repo_compat ||= Jekyll::GitHubMetadata::RepositoryCompat.new(self)
end

def repo_info
@repo_info ||= (Value.new(proc { |c| c.repository(nwo) }).render || Hash.new)
end

def repo_pages_info
@repo_pages_info ||= (Value.new(proc { |c| c.pages(nwo, repo_pages_info_opts) }).render || Hash.new)
end

def repo_pages_info_opts
if Pages.repo_pages_html_url_preview?
{ :accept => "application/vnd.github.mister-fantastic-preview+json" }
else
{}
end
end
Copy link
Member Author

Choose a reason for hiding this comment

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

@benbalter this is key.


def language
repo_info["language"]
end
Expand Down Expand Up @@ -86,6 +98,10 @@ def releases
memoize_value :@releases, Value.new(proc { |c| c.releases(nwo) })
end

def git_ref
user_page? ? 'master' : 'gh-pages'
end

def user_page?
primary?
end
Expand All @@ -106,18 +122,10 @@ def primary?
end
end

# In enterprise, the site's scheme will be the same as the instance's
# In dotcom, this will be `https` for GitHub-owned sites that end with
# `.github.com` and will be `http` for all other sites.
# Note: This is not the same as *instance*'s scheme, which may differ
def url_scheme
if Pages.enterprise?
Pages.scheme
elsif owner == 'github' && domain.end_with?('.github.com')
'https'
else
'http'
end
def user_page_domains
domains = [default_user_domain]
domains.push "#{owner}.github.com".downcase unless Pages.enterprise?
domains
end

def default_user_domain
Expand All @@ -130,56 +138,25 @@ def default_user_domain
end
end

def user_page_domains
domains = [default_user_domain]
domains.push "#{owner}.github.com".downcase unless Pages.enterprise?
domains
def cname
return nil unless Pages.custom_domains_enabled?
repo_pages_info["cname"]
end

def user_domain
domain = default_user_domain
user_page_domains.each do |user_repo|
candidate_nwo = "#{owner}/#{user_repo}"
next unless Value.new(proc { |client| client.repository? candidate_nwo }).render
domain = self.class.new(candidate_nwo).domain
end
domain
end

def pages_url
if !Pages.custom_domains_enabled?
path = user_page? ? owner : nwo
if Pages.subdomain_isolation?
URI.join("#{Pages.scheme}://#{Pages.pages_hostname}/", path).to_s
else
URI.join("#{Pages.github_url}/pages/", path).to_s
end
elsif cname || primary?
"#{url_scheme}://#{domain}"
else
URI.join("#{url_scheme}://#{domain}", name).to_s
end
def html_url
@html_url ||= (repo_pages_info["html_url"] || repo_compat.pages_url)
Copy link
Member Author

Choose a reason for hiding this comment

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

@benbalter as well as this. this is where it grabs the html_url from the pages endpoint if it exists, otherwise uses the old pages_url like before.

end

def cname
memoize_value :@cname, Value.new(proc { |c|
if Pages.custom_domains_enabled?
(c.pages(nwo) || {'cname' => nil})['cname']
end
})
def uri
@uri ||= URI(html_url)
end

def domain
@domain ||=
if !Pages.custom_domains_enabled?
Pages.github_hostname
elsif cname # explicit CNAME
cname
elsif primary? # user/org repo
default_user_domain
else # project repo
user_domain
end
uri.host
end

def url_scheme
uri.scheme
end

private
Expand Down
64 changes: 64 additions & 0 deletions lib/jekyll-github-metadata/repository_compat.rb
@@ -0,0 +1,64 @@
module Jekyll
module GitHubMetadata
class RepositoryCompat
attr_reader :repo

def initialize(repo)
@repo = repo
end

# In enterprise, the site's scheme will be the same as the instance's
# In dotcom, this will be `https` for GitHub-owned sites that end with
# `.github.com` and will be `http` for all other sites.
# Note: This is not the same as *instance*'s scheme, which may differ
def url_scheme
if Pages.enterprise?
Pages.scheme
elsif repo.owner == 'github' && domain.end_with?('.github.com')
'https'
else
'http'
end
end

def user_domain
domain = repo.default_user_domain
repo.user_page_domains.each do |user_repo|
candidate_nwo = "#{repo.owner}/#{user_repo}"
next unless Jekyll::GitHubMetadata.client.repository?(candidate_nwo)
domain = Jekyll::GitHubMetadata::Repository.new(candidate_nwo).repo_compat.domain
end
domain
end

def pages_url
if !Pages.custom_domains_enabled?
path = repo.user_page? ? repo.owner : repo.nwo
if Pages.subdomain_isolation?
URI.join("#{Pages.scheme}://#{Pages.pages_hostname}/", path).to_s
else
URI.join("#{Pages.github_url}/pages/", path).to_s
end
elsif repo.cname || repo.primary?
"#{url_scheme}://#{domain}"
else
URI.join("#{url_scheme}://#{domain}", repo.name).to_s
end
end
alias_method :html_url, :pages_url

def domain
@domain ||=
if !Pages.custom_domains_enabled?
Pages.github_hostname
elsif repo.cname # explicit CNAME
repo.cname
elsif repo.primary? # user/org repo
repo.default_user_domain
else # project repo
user_domain
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/jekyll-github-metadata/value.rb
Expand Up @@ -35,7 +35,7 @@ def render
end
@value = Sanitizer.sanitize(@value)
rescue RuntimeError, NameError => e
Jekyll.logger.error "GitHubMetadata:", "Error processing value '#{key}':"
Jekyll::GitHubMetadata.log :error, "Error processing value '#{key}':"
raise e
end

Expand Down
1 change: 1 addition & 0 deletions script/spec
14 changes: 2 additions & 12 deletions spec/integration_spec.rb
Expand Up @@ -10,16 +10,6 @@ def dest_dir(*files)
DEST_DIR.join(*files)
end

class ApiStub
attr_reader :path, :file
attr_accessor :stub

def initialize(path, file)
@path = path
@file = file
end
end

API_STUBS = {
"/users/jekyll/repos?per_page=100&type=public" => "owner_repos",
"/repos/jekyll/github-metadata" => "repo",
Expand Down Expand Up @@ -48,9 +38,9 @@ def initialize(path, file)
# Run Jekyll
Jekyll.logger.log_level = :error
Jekyll::Commands::Build.process({
"source" => SOURCE_DIR.to_s,
"source" => SOURCE_DIR.to_s,
"destination" => DEST_DIR.to_s,
"gems" => %w{jekyll-github-metadata}
"gems" => %w{jekyll-github-metadata}
})
end
subject { SafeYAML::load(dest_dir("rendered.txt").read) }
Expand Down