discourse/lib/onebox/engine/github_repo_onebox.rb
Régis Hanol 9d1bfb73f3
UX: Show contextual dates on GitHub PR oneboxes (#36637)
Previously, GitHub PR oneboxes always showed "opened on [date]"
regardless of the PR's status. This could be misleading for merged or
closed PRs where the relevant date is when that action occurred.

Now the date label and value match the PR status:
- open/draft: "opened/drafted on [created_at]"
- approved/changes_requested: "[status] on [review submitted_at]"
- merged/closed: "merged/closed on [merged_at/closed_at]"

Also removes an unused GITHUB_COMMENT_REGEX constant from
github_repo_onebox.rb.

**Here are a few examples**

<img width="743" height="1035" alt="CleanShot 2025-12-11 at 18 10 42"
src="https://github.com/user-attachments/assets/810893a5-2563-4a19-96a1-7bf876e2d27c"
/>
2025-12-11 20:10:24 +01:00

59 lines
1.7 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
matches_domain("github.com", "www.github.com")
always_https
def self.matches_path(path)
path.match?(%r{^/[^/]+/[^/]+/?$})
end
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