Github: Fix error when repository has no README + refactor (#6580) [deploy]

This commit is contained in:
Michael Kohl 2020-03-11 22:33:26 +07:00 committed by GitHub
parent 2d008c58f0
commit eb5285ce6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 21 deletions

View file

@ -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)

View file

@ -1,7 +1,7 @@
<div class="ltag-github-readme-tag">
<div class="readme-overview">
<h2>
<img src="<%= ActionController::Base.helpers.asset_path('github-logo.svg') %>" alt="GitHub logo">
<img src="<%= ActionController::Base.helpers.asset_path("github-logo.svg") %>" alt="GitHub logo">
<a href="https://github.com/<%= content.owner.login %>">
<%= content.owner.login %>
</a> / <a style="font-weight: 600;" href="<%= content.html_url %>">
@ -14,7 +14,7 @@
</div>
<% if show_readme %>
<div class="ltag-github-body">
<p><%= (HTML_Truncator.truncate updated_html, 150).html_safe %></p>
<p><%= (HTML_Truncator.truncate readme_html, 150).html_safe %></p>
</div>
<div class="gh-btn-container"><a class="gh-btn" href="<%= content.html_url %>">View on GitHub</a></div>
<% end %>

View file

@ -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