FEATURE: Allow oneboxing private GitHub repo URLs and add private indicator to HTML (#27947)
Followup 560e8aff75e4bde67bb162e8fdea52e704a19f81
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.
2024-07-19 10:21:45 +08:00
|
|
|
# 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
|
|
|
|
|
2024-12-03 02:08:55 +08:00
|
|
|
GITHUB_COMMENT_REGEX = /(<!--.*?-->\r\n)/m
|
FEATURE: Allow oneboxing private GitHub repo URLs and add private indicator to HTML (#27947)
Followup 560e8aff75e4bde67bb162e8fdea52e704a19f81
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.
2024-07-19 10:21:45 +08:00
|
|
|
|
|
|
|
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
|