mirror of
https://github.com/discourse/discourse.git
synced 2024-12-24 06:33:53 +08:00
f5cbc3e3b8
Followup 560e8aff75
The linked commit allowed oneboxing private GitHub PRs,
issues, commits, and so on, but it didn't actually allow
oneboxing the root repo e.g https://github.com/discourse/discourse-reactions
We didn't have an engine for this, we were relying on OpenGraph
tags on the HTML rendering of the page like we do with other
oneboxes.
To fix this, we needed a new github engine for repos specifically.
Also, this commit adds a `data-github-private-repo` attribute to
PR, issue, and repo onebox HTML so we have an indicator of
whether the repo was private, which can be used for theme components
and so on.
58 lines
1.8 KiB
Ruby
58 lines
1.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require_relative "../mixins/github_body"
|
|
require_relative "../mixins/github_auth_header"
|
|
|
|
module Onebox
|
|
module Engine
|
|
class GithubRepoOnebox
|
|
include Engine
|
|
include LayoutSupport
|
|
include JSON
|
|
include Onebox::Mixins::GithubAuthHeader
|
|
|
|
GITHUB_COMMENT_REGEX = /(<!--.*?-->\r\n)/
|
|
|
|
matches_regexp(%r{^https?:\/\/(?:www\.)?(?!gist\.)[^\/]*github\.com\/[^\/]+\/[^\/]+\/?$})
|
|
always_https
|
|
|
|
def url
|
|
"https://api.github.com/repos/#{match[:org]}/#{match[:repository]}"
|
|
end
|
|
|
|
private
|
|
|
|
def match
|
|
@match ||= @url.match(%r{github\.com/(?<org>[^/]+)/(?<repository>[^/]+)})
|
|
end
|
|
|
|
def data
|
|
result = raw(github_auth_header(match[:org])).clone
|
|
result["link"] = link
|
|
description = result["description"]
|
|
title = "GitHub - #{result["full_name"]}"
|
|
|
|
if description.blank?
|
|
description = I18n.t("onebox.github.no_description", repo: result["full_name"])
|
|
else
|
|
title += ": #{Onebox::Helpers.truncate(description)}"
|
|
end
|
|
|
|
result["description"] = description
|
|
result["title"] = title
|
|
result["is_private"] = result["private"]
|
|
|
|
# The SecureRandom part of this doesn't matter, it's just used for caching the
|
|
# repo thumbnail which is generated on the fly by GitHub. There isn't detail
|
|
# in https://github.blog/2021-06-22-framework-building-open-graph-images/,
|
|
# but this SO answer https://stackoverflow.com/a/69043743 suggests this is
|
|
# how it works and testing confirms it.
|
|
result[
|
|
"thumbnail"
|
|
] = "https://opengraph.githubassets.com/#{SecureRandom.hex}/#{match[:org]}/#{match[:repository]}"
|
|
result
|
|
end
|
|
end
|
|
end
|
|
end
|