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

Prevent loading all GH data on reset #245

Merged
merged 5 commits into from
Feb 17, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/jekyll-github-metadata/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ def owner_public_repositories
:accept => "application/vnd.github.mercy-preview+json",
}
memoize_value :@owner_public_repositories, Value.new("owner_public_repositories", proc do |c|
c.list_repos(owner, options).map do |r|
repos = c.list_repos(owner, options) || []
parkr marked this conversation as resolved.
Show resolved Hide resolved
repos.map do |r|
r[:releases] = Value.new("owner_public_repositories_releases", proc { c.releases(r[:full_name]) })
r[:contributors] = Value.new("owner_public_repositories_contributors", proc { c.contributors(r[:full_name]) })
r
Expand Down
15 changes: 12 additions & 3 deletions lib/jekyll-github-metadata/site_github_munger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class << self

def initialize(site)
Jekyll::GitHubMetadata.site = site
@original_config = site.config["github"]
end

def munge!
Expand All @@ -28,16 +29,20 @@ def inject_metadata!(payload)
payload.site["github"] = github_namespace
end

def uninject_metadata!(payload)
payload.site["github"] = @original_config
end

private

def github_namespace
case site.config["github"]
case @original_config
when nil
drop
when Hash
Jekyll::Utils.deep_merge_hashes(drop, site.config["github"])
drop.merge(@original_config)
else
site.config["github"]
@original_config
end
end

Expand Down Expand Up @@ -94,5 +99,9 @@ def should_warn_about_site_name?
Jekyll::Hooks.register :site, :pre_render do |_site, payload|
SiteGitHubMunger.global_munger.inject_metadata!(payload)
end

Jekyll::Hooks.register :site, :post_render do |_site, payload|
SiteGitHubMunger.global_munger.uninject_metadata!(payload)
end
end
end
39 changes: 39 additions & 0 deletions spec/site_github_munger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,43 @@
end.to raise_error(Jekyll::GitHubMetadata::Client::BadCredentialsError)
end
end

context "render the 'uninject' fixture test site" do
let(:source) { File.expand_path("test-site-uninject", __dir__) }
let(:dest) { File.expand_path("../tmp/test-site-uninject-build", __dir__) }
let(:config) { Jekyll::Configuration.from({ "source" => source, "destination" => dest }) }
let(:fixture_rendered) { File.expand_path("test-site-uninject-rendered", __dir__) }

it "process site twice (simulate reset), check API calls & rendered site" do
site = Jekyll::Site.new(config)
site.process
# These API calls are expected because we use the attributes in the
# fixture site.
expect_api_call "/repos/jekyll/github-metadata"
expect_api_call "/repos/jekyll/github-metadata/releases/latest"
expect_api_call "/orgs/jekyll"
site.process
# After processing the site again, we expect that these API calls were
# still only made once. We cache the results so we shouldn't be making the
# same API call more than once.
expect_api_call "/repos/jekyll/github-metadata"
dunkmann00 marked this conversation as resolved.
Show resolved Hide resolved
expect_api_call "/repos/jekyll/github-metadata/releases/latest"
expect_api_call "/orgs/jekyll"

# We do not expect these API calls to have been made since we do not use
# these attributes in the fixture site.
not_expect_api_call "/repos/jekyll/github-metadata/pages"
not_expect_api_call "/repos/jekyll/github-metadata/contributors?per_page=100"
not_expect_api_call "/orgs/jekyll/public_members?per_page=100"
not_expect_api_call "/users/jekyll/repos?per_page=100&type=public"

# Check to make sure the fixture site is rendered with the correct
# site.github values.
Dir.children(dest).each do |file|
rendered_file = File.join(dest, file)
fixture_file = File.join(fixture_rendered, file)
expect(File.read(rendered_file)).to eql(File.read(fixture_file))
end
end
end
end
5 changes: 5 additions & 0 deletions spec/spec_helpers/web_mock_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ def expect_api_call(path)
.with(:headers => request_headers).once
end

def not_expect_api_call(path)
expect(WebMock).to have_requested(:get, url(path))
.with(:headers => request_headers).times(0)
end

def request_headers
REQUEST_HEADERS.merge(
"Authorization" => "token #{ENV.fetch("JEKYLL_GITHUB_TOKEN", "1234abc")}"
Expand Down
13 changes: 13 additions & 0 deletions spec/test-site-uninject-rendered/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Page Title</title>
</head>
<body>
<h1>github-metadata</h1>
<h2>:octocat: `site.github`</h2>
<h2>Owner: jekyll</h2>
<h3>Latest Release: {"url"=>"https://api.github.com/repos/jekyll/github-metadata/releases/5198319", "assets_url"=>"https://api.github.com/repos/jekyll/github-metadata/releases/5198319/assets", "upload_url"=>"https://uploads.github.com/repos/jekyll/github-metadata/releases/5198319/assets{?name,label}", "html_url"=>"https://github.com/jekyll/github-metadata/releases/tag/v2.3.1", "id"=>5198319, "tag_name"=>"v2.3.1", "target_commitish"=>"master", "name"=>"v2.3.1", "draft"=>false, "author"=>{"login"=>"jekyllbot", "id"=>6166343, "avatar_url"=>"https://avatars.githubusercontent.com/u/6166343?v=3", "gravatar_id"=>"", "url"=>"https://api.github.com/users/jekyllbot", "html_url"=>"https://github.com/jekyllbot", "followers_url"=>"https://api.github.com/users/jekyllbot/followers", "following_url"=>"https://api.github.com/users/jekyllbot/following{/other_user}", "gists_url"=>"https://api.github.com/users/jekyllbot/gists{/gist_id}", "starred_url"=>"https://api.github.com/users/jekyllbot/starred{/owner}{/repo}", "subscriptions_url"=>"https://api.github.com/users/jekyllbot/subscriptions", "organizations_url"=>"https://api.github.com/users/jekyllbot/orgs", "repos_url"=>"https://api.github.com/users/jekyllbot/repos", "events_url"=>"https://api.github.com/users/jekyllbot/events{/privacy}", "received_events_url"=>"https://api.github.com/users/jekyllbot/received_events", "type"=>"User", "site_admin"=>false}, "prerelease"=>false, "created_at"=>2017-01-18 20:10:29 UTC, "published_at"=>2017-01-18 20:10:33 UTC, "assets"=>[], "tarball_url"=>"https://api.github.com/repos/jekyll/github-metadata/tarball/v2.3.1", "zipball_url"=>"https://api.github.com/repos/jekyll/github-metadata/zipball/v2.3.1", "body"=>"- Remove log on Octokit::NotFound (#86)\n"}</h3>
parkr marked this conversation as resolved.
Show resolved Hide resolved
</body>
</html>
1 change: 1 addition & 0 deletions spec/test-site-uninject-rendered/subvalues.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
owner: {"avatar_url"=>"https://avatars0.githubusercontent.com/u/3083652?v=4", "bio"=>nil, "blog"=>"https://jekyllrb.com", "collaborators"=>nil, "company"=>nil, "created_at"=>2012-12-19 19:37:35 UTC, "description"=>"Jekyll is a blog-aware, static site generator in Ruby.", "email"=>"", "followers"=>0, "following"=>0, "has_organization_projects"=>true, "has_repository_projects"=>true, "hireable"=>nil, "html_url"=>"https://github.com/jekyll", "id"=>3083652, "is_verified"=>true, "location"=>nil, "login"=>"jekyll", "name"=>"Jekyll", "node_id"=>"MDEyOk9yZ2FuaXphdGlvbjMwODM2NTI=", "public_gists"=>0, "public_repos"=>50, "type"=>"Organization", "updated_at"=>2019-01-27 15:27:32 UTC}
9 changes: 9 additions & 0 deletions spec/test-site-uninject/_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
repository: jekyll/github-metadata
github:
in_your_config:
setting_your: keyz
plugins:
- jekyll-github-metadata
# remove this once we drop support for Jekyll v3.4 and below
gems:
- jekyll-github-metadata
16 changes: 16 additions & 0 deletions spec/test-site-uninject/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
---

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Page Title</title>
</head>
<body>
<h1>{{ site.github.project_title }}</h1>
<h2>{{ site.github.project_tagline }}</h2>
<h2>Owner: {{ site.github.owner_name }}</h2>
<h3>Latest Release: {{ site.github.latest_release }}</h3>
</body>
</html>
4 changes: 4 additions & 0 deletions spec/test-site-uninject/subvalues.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
---

owner: {{ site.github.owner }}