From eb5285ce6e1e1d780c63a970a70bf2f4804bf75e Mon Sep 17 00:00:00 2001 From: Michael Kohl Date: Wed, 11 Mar 2020 22:33:26 +0700 Subject: [PATCH] Github: Fix error when repository has no README + refactor (#6580) [deploy] --- .../github_tag/github_readme_tag.rb | 41 ++++++++++--------- app/views/liquids/_github_readme.html.erb | 4 +- .../github_tag/github_readme_tag_spec.rb | 9 ++++ 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/app/liquid_tags/github_tag/github_readme_tag.rb b/app/liquid_tags/github_tag/github_readme_tag.rb index f7ef2c570..ff94f6698 100644 --- a/app/liquid_tags/github_tag/github_readme_tag.rb +++ b/app/liquid_tags/github_tag/github_readme_tag.rb @@ -4,38 +4,35 @@ class GithubTag class GithubReadmeTag PARTIAL = "liquids/github_readme".freeze + attr_reader :client, :content, :options, :readme_html + def initialize(link) - @link = parse_link(link) + parsed_link = parse_link(link) @options = parse_options(link) - @content = get_content(@link) + @client = Octokit::Client.new(access_token: token) + @content = client.repository(parsed_link) + @readme_html = fetch_readme(parsed_link) end def render ActionController::Base.new.render_to_string( partial: PARTIAL, locals: { - content: @content, - show_readme: show_readme?, - updated_html: @updated_html + content: content, + show_readme: show_readme? && readme_html.present?, + readme_html: readme_html }, ) end + private + def parse_link(link) link = sanitize_link(link) - link.split(" ").first.delete(" ") - end + parsed_link = link.split(" ").first.delete(" ") + raise_error if parsed_link.split("/").length > 2 - def get_content(link) - repo_details = link.split("/") - raise_error if repo_details.length > 2 - user_name = repo_details[0] - repo_name = repo_details[1] - client = Octokit::Client.new(access_token: token) - @readme_html = client.readme user_name + "/" + repo_name, accept: "application/vnd.github.html" - @readme = client.readme user_name + "/" + repo_name - @updated_html = clean_relative_path!(@readme_html, @readme.download_url) - client.repository(user_name + "/" + repo_name) + parsed_link end def valid_option(option) @@ -53,10 +50,16 @@ class GithubTag end def show_readme? - @options.none? "no-readme" + options.none? "no-readme" end - private + def fetch_readme(link) + readme_html = client.readme(link, accept: "application/vnd.github.html") + readme = client.readme(link) + clean_relative_path!(readme_html, readme.download_url) + rescue Octokit::NotFound => _e + nil + end def sanitize_link(link) link = ActionController::Base.helpers.strip_tags(link) diff --git a/app/views/liquids/_github_readme.html.erb b/app/views/liquids/_github_readme.html.erb index 111654075..1cb5cfbe7 100644 --- a/app/views/liquids/_github_readme.html.erb +++ b/app/views/liquids/_github_readme.html.erb @@ -1,7 +1,7 @@
<% if show_readme %>
-

<%= (HTML_Truncator.truncate updated_html, 150).html_safe %>

+

<%= (HTML_Truncator.truncate readme_html, 150).html_safe %>

<% end %> diff --git a/spec/liquid_tags/github_tag/github_readme_tag_spec.rb b/spec/liquid_tags/github_tag/github_readme_tag_spec.rb index a41d079b9..4e23a9936 100644 --- a/spec/liquid_tags/github_tag/github_readme_tag_spec.rb +++ b/spec/liquid_tags/github_tag/github_readme_tag_spec.rb @@ -43,5 +43,14 @@ RSpec.describe GithubTag::GithubReadmeTag, type: :liquid_tag, vcr: vcr_option do readme_class = "ltag-github-body" expect(template).not_to include(readme_class) end + + it "handles respositories with a missing README" do + allow(my_ocktokit_client).to receive(:readme).and_raise(Octokit::NotFound) + + template = generate_github_readme(path, "no-readme").render + readme_class = "ltag-github-body" + + expect(template).not_to include(readme_class) + end end end