diff --git a/lib/jekyll-github-metadata/repository.rb b/lib/jekyll-github-metadata/repository.rb index c8716f2..c2bc53b 100644 --- a/lib/jekyll-github-metadata/repository.rb +++ b/lib/jekyll-github-metadata/repository.rb @@ -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) || [] + 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 diff --git a/lib/jekyll-github-metadata/site_github_munger.rb b/lib/jekyll-github-metadata/site_github_munger.rb index 0fb1a3d..c723cc0 100644 --- a/lib/jekyll-github-metadata/site_github_munger.rb +++ b/lib/jekyll-github-metadata/site_github_munger.rb @@ -15,6 +15,7 @@ class << self def initialize(site) Jekyll::GitHubMetadata.site = site + @original_config = site.config["github"] end def munge! @@ -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 @@ -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 diff --git a/spec/site_github_munger_spec.rb b/spec/site_github_munger_spec.rb index 3bfa897..3870bdc 100644 --- a/spec/site_github_munger_spec.rb +++ b/spec/site_github_munger_spec.rb @@ -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" + 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 diff --git a/spec/spec_helpers/web_mock_helper.rb b/spec/spec_helpers/web_mock_helper.rb index ad465d1..41f42b7 100644 --- a/spec/spec_helpers/web_mock_helper.rb +++ b/spec/spec_helpers/web_mock_helper.rb @@ -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")}" diff --git a/spec/test-site-uninject-rendered/index.html b/spec/test-site-uninject-rendered/index.html new file mode 100644 index 0000000..9d780c5 --- /dev/null +++ b/spec/test-site-uninject-rendered/index.html @@ -0,0 +1,13 @@ + + +
+ +