2021-05-26 17:41:35 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require_relative "../mixins/github_body"
|
2024-07-10 07:39:31 +08:00
|
|
|
require_relative "../mixins/github_auth_header"
|
2021-05-26 17:41:35 +08:00
|
|
|
|
|
|
|
module Onebox
|
|
|
|
module Engine
|
|
|
|
class GithubPullRequestOnebox
|
|
|
|
include Engine
|
|
|
|
include LayoutSupport
|
|
|
|
include JSON
|
|
|
|
include Onebox::Mixins::GithubBody
|
2024-07-10 07:39:31 +08:00
|
|
|
include Onebox::Mixins::GithubAuthHeader
|
2021-05-26 17:41:35 +08:00
|
|
|
|
|
|
|
GITHUB_COMMENT_REGEX = /(<!--.*?-->\r\n)/
|
|
|
|
|
|
|
|
matches_regexp(%r{^https?://(?:www\.)?(?:(?:\w)+\.)?(github)\.com(?:/)?(?:.)*/pull})
|
|
|
|
always_https
|
|
|
|
|
|
|
|
def url
|
2024-07-15 11:07:36 +08:00
|
|
|
"https://api.github.com/repos/#{match[:org]}/#{match[:repository]}/pulls/#{match[:number]}"
|
2021-05-26 17:41:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def match
|
|
|
|
@match ||=
|
2024-07-15 11:07:36 +08:00
|
|
|
@url.match(%r{github\.com/(?<org>[^/]+)/(?<repository>[^/]+)/pull/(?<number>[^/]+)})
|
2021-05-26 17:41:35 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def data
|
2024-07-15 11:07:36 +08:00
|
|
|
result = raw(github_auth_header(match[:org])).clone
|
2021-05-26 17:41:35 +08:00
|
|
|
result["link"] = link
|
|
|
|
|
|
|
|
created_at = Time.parse(result["created_at"])
|
|
|
|
result["created_at"] = created_at.strftime("%I:%M%p - %d %b %y %Z")
|
|
|
|
result["created_at_date"] = created_at.strftime("%F")
|
|
|
|
result["created_at_time"] = created_at.strftime("%T")
|
|
|
|
|
|
|
|
ulink = URI(link)
|
|
|
|
result["domain"] = "#{ulink.host}/#{ulink.path.split("/")[1]}/#{ulink.path.split("/")[2]}"
|
|
|
|
|
|
|
|
result["body"], result["excerpt"] = compute_body(result["body"])
|
|
|
|
|
2022-10-07 01:26:04 +08:00
|
|
|
if result["commit"] = load_commit(link)
|
2022-10-25 03:26:48 +08:00
|
|
|
result["body"], result["excerpt"] =
|
|
|
|
compute_body(result["commit"]["commit"]["message"].lines[1..].join)
|
2022-10-07 01:26:04 +08:00
|
|
|
elsif result["comment"] = load_comment(link)
|
|
|
|
result["body"], result["excerpt"] = compute_body(result["comment"]["body"])
|
|
|
|
elsif result["discussion"] = load_review(link)
|
|
|
|
result["body"], result["excerpt"] = compute_body(result["discussion"]["body"])
|
|
|
|
else
|
|
|
|
result["pr"] = true
|
|
|
|
end
|
2024-01-19 22:26:06 +08:00
|
|
|
result["i18n"] = i18n
|
|
|
|
result["i18n"]["pr_summary"] = I18n.t(
|
|
|
|
"onebox.github.pr_summary",
|
|
|
|
{
|
|
|
|
commits: result["commits"],
|
|
|
|
changed_files: result["changed_files"],
|
|
|
|
additions: result["additions"],
|
|
|
|
deletions: result["deletions"],
|
|
|
|
},
|
|
|
|
)
|
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
|
|
|
result["is_private"] = result.dig("base", "repo", "private")
|
2022-10-07 01:26:04 +08:00
|
|
|
|
2021-05-26 17:41:35 +08:00
|
|
|
result
|
|
|
|
end
|
2022-10-07 01:26:04 +08:00
|
|
|
|
2024-01-19 22:26:06 +08:00
|
|
|
def i18n
|
|
|
|
{
|
|
|
|
opened: I18n.t("onebox.github.opened"),
|
|
|
|
commit_by: I18n.t("onebox.github.commit_by"),
|
|
|
|
comment_by: I18n.t("onebox.github.comment_by"),
|
|
|
|
review_by: I18n.t("onebox.github.review_by"),
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-10-07 01:26:04 +08:00
|
|
|
def load_commit(link)
|
|
|
|
if commit_match = link.match(%r{commits/(\h+)})
|
|
|
|
load_json(
|
2024-07-15 11:07:36 +08:00
|
|
|
"https://api.github.com/repos/#{match[:org]}/#{match[:repository]}/commits/#{commit_match[1]}",
|
2022-10-07 01:26:04 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_comment(link)
|
|
|
|
if comment_match = link.match(/#issuecomment-(\d+)/)
|
|
|
|
load_json(
|
2024-07-15 11:07:36 +08:00
|
|
|
"https://api.github.com/repos/#{match[:org]}/#{match[:repository]}/issues/comments/#{comment_match[1]}",
|
2022-10-07 01:26:04 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_review(link)
|
|
|
|
if review_match = link.match(/#discussion_r(\d+)/)
|
|
|
|
load_json(
|
2024-07-15 11:07:36 +08:00
|
|
|
"https://api.github.com/repos/#{match[:org]}/#{match[:repository]}/pulls/comments/#{review_match[1]}",
|
2022-10-07 01:26:04 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_json(url)
|
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
|
|
|
::MultiJson.load(
|
|
|
|
URI.parse(url).open({ read_timeout: timeout }.merge(github_auth_header(match[:org]))),
|
|
|
|
)
|
2022-10-07 01:26:04 +08:00
|
|
|
end
|
2021-05-26 17:41:35 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|