discourse/plugins/discourse-github/app/lib/github_linkback.rb
Régis Hanol 318eb49a30
FIX: Skip GitHub linkback when topic is already linked (#37633)
The GitHub linkback feature posts comments on PRs/issues/commits when
they are mentioned in Discourse posts. However, it never checked whether
the GitHub PR/issue/commit already contained a link back to the same
Discourse topic (in its description or existing comments). This led to
redundant linkback comments when a PR already referenced the topic.

Before posting a linkback comment, we now fetch the PR/issue body and
existing comments (or commit comments) from the GitHub API and check
whether any of them already contain a URL pointing to the same Discourse
topic. URL matching uses Discourse.route_for — the same mechanism used
by the oneboxer, search, and TopicLink — to reliably recognize all topic
URL formats (/t/slug/id, /t/id, /t/id/post_number, etc.).

On any GitHub API error, the check fails open (still posts the linkback)
to preserve the pre-existing behavior — and because if the GET fails,
the subsequent POST will likely fail too.
2026-02-09 14:31:27 +01:00

230 lines
6.3 KiB
Ruby

# frozen_string_literal: true
require_dependency "pretty_text"
require "digest/sha1"
class GithubLinkback
class Link
attr_reader :url, :project, :type
attr_accessor :sha, :pr_number, :issue_number
def initialize(url, project, type)
@url = url
@project = project
@type = type
end
end
def initialize(post)
@post = post
end
def should_enqueue?
return false if ignored_category?
!!(
SiteSetting.github_linkback_enabled? && SiteSetting.enable_discourse_github_plugin? &&
@post.present? && @post.post_type == Post.types[:regular] && @post.raw =~ /github\.com/ &&
Guardian.new.can_see?(@post) && @post.topic.visible?
)
end
def enqueue
Jobs.enqueue(:create_github_linkback, post_id: @post.id) if should_enqueue?
end
def github_links
projects = SiteSetting.github_linkback_projects.split("|")
return [] if projects.blank?
result = {}
PrettyText
.extract_links(@post.cooked)
.map(&:url)
.each do |l|
if l =~ %r{https?://github\.com/([^/]+)/([^/]+)/commit/([0-9a-f]+)}
url, org, repo, sha = Regexp.last_match.to_a
project = "#{org}/#{repo}"
next if result[url]
next if @post.custom_fields[GithubLinkback.field_for(url)].present?
next unless is_allowed_project_link?(projects, project)
link = Link.new(url, project, :commit)
link.sha = sha
result[url] = link
elsif l =~ %r{https?://github.com/([^/]+)/([^/]+)/pull/(\d+)}
url, org, repo, pr_number = Regexp.last_match.to_a
project = "#{org}/#{repo}"
next if result[url]
next if @post.custom_fields[GithubLinkback.field_for(url)].present?
next unless is_allowed_project_link?(projects, project)
link = Link.new(url, project, :pr)
link.pr_number = pr_number.to_i
result[url] = link
elsif l =~ %r{https?://github.com/([^/]+)/([^/]+)/issues/(\d+)}
url, org, repo, issue_number = Regexp.last_match.to_a
project = "#{org}/#{repo}"
next if result[url]
next if @post.custom_fields[GithubLinkback.field_for(url)].present?
next unless is_allowed_project_link?(projects, project)
link = Link.new(url, project, :issue)
link.issue_number = issue_number.to_i
result[url] = link
end
end
result.values
end
def is_allowed_project_link?(projects, project)
return true if projects.include?(project)
check_user = project.split("/")[0]
projects.any? do |allowed_project|
allowed_user, allowed_all_projects = allowed_project.split("/")
(allowed_user == check_user) && (allowed_all_projects == "*")
end
end
def create
return [] if SiteSetting.github_linkback_access_token.blank?
links = []
DistributedMutex.synchronize("github_linkback_#{@post.id}") do
links = github_links
return [] if links.length() > SiteSetting.github_linkback_maximum_links
links.each do |link|
case link.type
when :commit
post_commit(link)
when :pr
post_pr_or_issue(link, :pr)
when :issue
post_pr_or_issue(link, :issue)
else
next
end
# Don't post the same link twice
@post.custom_fields[GithubLinkback.field_for(link.url)] = "true"
end
@post.save_custom_fields
end
links
end
def self.field_for(url)
"github-linkback:#{Digest::SHA1.hexdigest(url)[0..15]}"
end
private
def ignored_category?
return false if @post&.topic&.category_id.blank?
SiteSetting.github_linkback_ignored_categories_map.include?(@post.topic.category_id)
end
def post_pr_or_issue(link, type)
pr_or_issue_number = link.pr_number || link.issue_number
return if topic_already_linked_on_github?(link)
github_url =
"https://api.github.com/repos/#{link.project}/issues/#{pr_or_issue_number}/comments"
comment =
I18n.t(
type == :pr ? "github_linkback.pr_template" : "github_linkback.issue_template",
title: SiteSetting.title,
post_url: "#{Discourse.base_url}#{@post.url}",
)
Excon.post(github_url, body: { body: comment }.to_json, headers: headers)
end
def post_commit(link)
return if topic_already_linked_on_github?(link)
github_url = "https://api.github.com/repos/#{link.project}/commits/#{link.sha}/comments"
comment =
I18n.t(
"github_linkback.commit_template",
title: SiteSetting.title,
post_url: "#{Discourse.base_url}#{@post.url}",
)
Excon.post(github_url, body: { body: comment }.to_json, headers: headers)
end
def topic_already_linked_on_github?(link)
texts =
if link.type == :commit
fetch_commit_comment_texts(link.project, link.sha)
else
fetch_pr_or_issue_texts(link.project, link.pr_number || link.issue_number)
end
texts.any? { |text| text_links_to_topic?(text) }
rescue Excon::Error
false
end
def fetch_commit_comment_texts(project, sha)
response =
Excon.get(
"https://api.github.com/repos/#{project}/commits/#{sha}/comments?per_page=100",
headers:,
expects: [200],
)
JSON.parse(response.body).map { |comment| comment["body"].to_s }
end
def fetch_pr_or_issue_texts(project, number)
issue_response =
Excon.get(
"https://api.github.com/repos/#{project}/issues/#{number}",
headers:,
expects: [200],
)
comments_response =
Excon.get(
"https://api.github.com/repos/#{project}/issues/#{number}/comments?per_page=100",
headers:,
expects: [200],
)
[
JSON.parse(issue_response.body)["body"].to_s,
*JSON.parse(comments_response.body).map { |comment| comment["body"].to_s },
]
end
def text_links_to_topic?(text)
base_url = Discourse.base_url
text
.scan(%r{#{Regexp.escape(base_url)}/t/\S+})
.any? do |url|
route = Discourse.route_for(url)
route && route[:controller] == "topics" && route[:action] == "show" &&
(route[:id] || route[:topic_id]).to_i == @post.topic_id
end
end
def headers
{
"Content-Type" => "application/json",
"Authorization" => "token #{SiteSetting.github_linkback_access_token}",
"User-Agent" => "Discourse-Github-Linkback",
}
end
end