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 GithubIssueOnebox
|
|
|
|
#Author Lidlanca 2014
|
|
|
|
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
|
|
|
|
|
|
|
matches_regexp(
|
|
|
|
%r{^https?://(?:www\.)?(?:(?:\w)+\.)?github\.com/(?<org>.+)/(?<repo>.+)/issues/([[:digit:]]+)},
|
|
|
|
)
|
|
|
|
always_https
|
|
|
|
|
|
|
|
def url
|
|
|
|
m = match
|
|
|
|
"https://api.github.com/repos/#{m["org"]}/#{m["repo"]}/issues/#{m["item_id"]}"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def match
|
|
|
|
@match ||=
|
|
|
|
@url.match(
|
|
|
|
%r{^http(?:s)?://(?:www\.)?(?:(?:\w)+\.)?github\.com/(?<org>.+)/(?<repo>.+)/(?<type>issues)/(?<item_id>[\d]+)},
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-01-19 22:26:06 +08:00
|
|
|
def i18n
|
|
|
|
{ opened: I18n.t("onebox.github.opened"), closed: I18n.t("onebox.github.closed") }
|
|
|
|
end
|
|
|
|
|
2021-05-26 17:41:35 +08:00
|
|
|
def data
|
2024-07-15 11:07:36 +08:00
|
|
|
result = raw(github_auth_header(match[:org])).clone
|
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
|
|
|
repo = load_repo
|
2024-07-10 07:39:31 +08:00
|
|
|
created_at = Time.parse(result["created_at"])
|
|
|
|
closed_at = Time.parse(result["closed_at"]) if result["closed_at"]
|
|
|
|
body, excerpt = compute_body(result["body"])
|
2021-05-26 17:41:35 +08:00
|
|
|
ulink = URI(link)
|
|
|
|
|
2024-01-23 02:25:29 +08:00
|
|
|
labels =
|
2024-07-10 07:39:31 +08:00
|
|
|
result["labels"].map do |l|
|
2024-01-23 02:25:29 +08:00
|
|
|
{ name: Emoji.codes_to_img(Onebox::Helpers.sanitize(l["name"])) }
|
|
|
|
end
|
2021-06-26 01:48:36 +08:00
|
|
|
|
2021-05-26 17:41:35 +08:00
|
|
|
{
|
|
|
|
link: @url,
|
2024-07-10 07:39:31 +08:00
|
|
|
title: result["title"],
|
2021-05-26 17:41:35 +08:00
|
|
|
body: body,
|
|
|
|
excerpt: excerpt,
|
2021-06-26 01:48:36 +08:00
|
|
|
labels: labels,
|
2024-07-10 07:39:31 +08:00
|
|
|
user: result["user"],
|
2021-06-26 01:48:36 +08:00
|
|
|
created_at: created_at.strftime("%I:%M%p - %d %b %y %Z"),
|
|
|
|
created_at_date: created_at.strftime("%F"),
|
|
|
|
created_at_time: created_at.strftime("%T"),
|
|
|
|
closed_at: closed_at&.strftime("%I:%M%p - %d %b %y %Z"),
|
|
|
|
closed_at_date: closed_at&.strftime("%F"),
|
|
|
|
closed_at_time: closed_at&.strftime("%T"),
|
2024-07-10 07:39:31 +08:00
|
|
|
closed_by: result["closed_by"],
|
|
|
|
avatar: "https://avatars1.githubusercontent.com/u/#{result["user"]["id"]}?v=2&s=96",
|
2021-05-26 17:41:35 +08:00
|
|
|
domain: "#{ulink.host}/#{ulink.path.split("/")[1]}/#{ulink.path.split("/")[2]}",
|
2024-01-19 22:26:06 +08:00
|
|
|
i18n: i18n,
|
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
|
|
|
is_private: repo["private"],
|
2021-05-26 17:41:35 +08:00
|
|
|
}
|
|
|
|
end
|
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
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def load_repo
|
|
|
|
load_json("https://api.github.com/repos/#{match[:org]}/#{match[:repo]}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_json(url)
|
|
|
|
::MultiJson.load(
|
|
|
|
URI.parse(url).open({ read_timeout: timeout }.merge(github_auth_header(match[:org]))),
|
|
|
|
)
|
|
|
|
end
|
2021-05-26 17:41:35 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|