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
Set title and description in dev #104
Conversation
@@ -16,9 +16,8 @@ def munge! | |||
# This is the good stuff. | |||
site.config["github"] = github_namespace | |||
|
|||
return unless should_add_fallbacks? | |||
add_url_and_baseurl_fallbacks! | |||
add_title_and_description_fallbacks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should bail early if:
- No auth token is present (if they rebuild frequently, they'll start getting 403 Rate Limit Exceeded errors which are confusing)
- There is no internet connectivity
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Both are a new feature in themselves, and out of scope for this PR, but 👍 to tackling those in a subsequent pass.
spec/site_github_munger_spec.rb
Outdated
@@ -83,6 +84,10 @@ | |||
before(:each) do | |||
Jekyll::GitHubMetadata.client = Jekyll::GitHubMetadata::Client.new({ :access_token => "" }) | |||
end | |||
before do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not before(:each)
? IIRC before
is discouraged because of the random ordering of each test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not related to this PR, so I'll pull it out (debugging artifact).
@@ -9,7 +9,7 @@ | |||
</head> | |||
<body> | |||
<div id="output"></div> | |||
<script src="renderjson.js" type="text/javascript" charset="utf-8"></script> | |||
<script src="{{ "renderjson.js" | absolute_url }}" type="text/javascript" charset="utf-8"></script> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this to ensure the internals that use url
and baseurl
don't blow up when either is a Proc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hooray for Value!
I'd give this a good whirl with your personal site then ship it. |
Also, to memorialize things here publicly, chatting with @parkr, I didn't like the idea that we'd be making API calls for every build, even if the user doesn't use |
Thanks! If you give the proc an arity of 2, it'll pass you the right repo too! First argument is the Client, second is the Repository 😄 |
@parkr this could use a second set of 👀 now, but otherwise should be good to go. Per our discussion:
As a result, when called locally, by default, the plugin should make no external API calls. Only if, for example, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work! Thank you for this. I have just a few suggestions & requests. 😸
lib/jekyll-github-metadata.rb
Outdated
@@ -61,9 +62,18 @@ def client | |||
@client ||= Client.new | |||
end | |||
|
|||
def repository | |||
@repository ||= GitHubMetadata::Repository.new(nwo).tap do |repo| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly what I was looking for, nice 👍
lib/jekyll-github-metadata.rb
Outdated
end | ||
|
||
def nwo | ||
@nwo ||= GitHubMetadata::RepositoryFinder.nwo |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For anything state-related, it would be nice to use an instance of RepositoryFinder here. It'd be much the same, but the state is thrown away once the nwo is discovered.
@nwo ||= Jekyll::GitHubMetadata::RepositoryFinder.new(site).nwo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we did that, JekyllGitHubMetadata would have to know (or be passed) the site variable (which you seemed to indicate the opposite below).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My struggle is that it's too easy for this data to get all out of sync. I want the behavior that if GitHubMetadata.site
changes, that GitHubMetadata.repository
changes with it.
module GitHubMetadata
def self.site
repository_finder.site
end
def self.site=(new_site)
reset!
@repository_finder = GitHubMetadata::RepositoryFinder.new(new_site)
end
def self.repository
@repository ||= GitHubMetadata::Repository.new(repository_finder.nwo).tap do |repo|
Jekyll::GitHubMetadata.log :debug, "Generating for #{repo.nwo}"
end
end
private
attr_reader :repository_finder
end
end
This enforces a safe top-level environment: when site
is set to some other value, it resets everything and creates a new repository_finder
instance with the new site.
module Jekyll | ||
module GitHubMetadata | ||
class RepositoryFinder | ||
class << self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use an instance here. Might be even easier to test that way since the site
would just be an instance variable; easier to remember to pass a required parameter to .new
than it is to set GitHubMetadata.site
before the test executes.
attr_reader :site | ||
extend Forwardable | ||
|
||
def_delegators :"Jekyll::GitHubMetadata", :site, :repository |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do these need to be public?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
site
was already public, but made repo private via 66f4b4a.
@@ -35,13 +36,14 @@ def github_namespace | |||
end | |||
|
|||
def drop | |||
@drop ||= MetadataDrop.new(site) | |||
@drop ||= MetadataDrop.new |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes me feel woozy. I wonder if keeping the site
on the global GitHubMetadata is a bad idea? I'm ok with client
and repository
at that level, but the whole site
object scares me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thoughts on a better solution? We need site in order to derive the nwo to build repository.
end | ||
|
||
# Set `site.url` and `site.baseurl` if unset. | ||
def add_url_and_baseurl_fallbacks! | ||
site.config["url"] ||= repository.url_without_path | ||
site.config["baseurl"] = repository.baseurl if should_set_baseurl? | ||
site.config["url"] ||= Value.new("url", proc { |_c, r| r.url_without_path }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😍
lib/jekyll-github-metadata/value.rb
Outdated
@@ -3,6 +3,9 @@ | |||
module Jekyll | |||
module GitHubMetadata | |||
class Value | |||
extend Forwardable | |||
def_delegators :render, :+, :to_s, :to_json, :eql? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clever. Can you memoize render
so it doesn't render on each call? Or use :value
instead of :render
.
I'd also like to have a test for each of these – it can be really basic, but I just want to ensure they don't break.
Why #+
?
Per Object#hash
, if you specify #eql?
you must also specify #hash
:
Generates an Integer hash value for this object. This function must have the property that
a.eql?(b)
impliesa.hash == b.hash
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Memoized via 7027532.
Why
#+
?
Workaround so we don't need to backport jekyll/jekyll#6253.
Per Object#hash, if you specify #eql? you must also specify #hash:
Done via f0662b0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd also like to have a test for each of these – it can be really basic, but I just want to ensure they don't break.
added via 4ea6404
render.to_s | ||
end | ||
|
||
def to_json(*) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, we can't just alias this method to #render
. to_json
must be able to accept a state
parameter. We do that in Jekyll::Drops::Drop to great effect. (See how state
affects things in DocumentDrop#hash_for_json
.)
def to_json(state = nil)
require "json"
JSON.generate(value, state)
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it's def_delegators
(plural), I believe it will delegate Value#to_json
to calling to_json
on the result of #render
, meaning it should accept the second argument so long as the result of #render
does.
@jekyllbot: merge +minor |
A follow up to #101, the title and description were only being set in production, making local previewing difficult.
This PR updates the logic to set the title and description in dev by moving the conditional statement.