A platform for community discussion. Free, open, simple.
FIX: Skip GitHub linkback when topic is already linked (#37633)
Régis Hanol318eb49a30d7
diff --git a/plugins/discourse-github/app/lib/github_linkback.rb b/plugins/discourse-github/app/lib/github_linkback.rbindex 02df7fed2bd..b3d022112d6 100644--- a/plugins/discourse-github/app/lib/github_linkback.rb+++ b/plugins/discourse-github/app/lib/github_linkback.rb@@ -135,6 +135,9 @@ class GithubLinkbackdef 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 =@@ -148,6 +151,8 @@ class GithubLinkbackenddef 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 =@@ -160,6 +165,61 @@ class GithubLinkbackExcon.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",diff --git a/plugins/discourse-github/spec/lib/github_linkback_spec.rb b/plugins/discourse-github/spec/lib/github_linkback_spec.rbindex 5cd781123fd..108220e8cf9 100644--- a/plugins/discourse-github/spec/lib/github_linkback_spec.rb+++ b/plugins/discourse-github/spec/lib/github_linkback_spec.rb@@ -200,6 +200,15 @@ describe GithubLinkback doenddescribe "#create" do+ let(:github_api_headers) do+ {+ "Authorization" => "token abcdef",+ "Content-Type" => "application/json",+ "Host" => "api.github.com",+ "User-Agent" => "Discourse-Github-Linkback",+ }+ end+before { SiteSetting.github_linkback_projects = "discourse/discourse|discourse/*" }it "returns an empty array without an access token" do@@ -207,13 +216,11 @@ describe GithubLinkback doendcontext "with an access token" do- let(:headers) do- {- "Authorization" => "token abcdef",- "Content-Type" => "application/json",- "Host" => "api.github.com",- "User-Agent" => "Discourse-Github-Linkback",- }+ let(:empty_json_response) do+ { status: 200, body: "[]", headers: { "Content-Type" => "application/json" } }+ end+ let(:empty_body_json_response) do+ { status: 200, body: '{"body":""}', headers: { "Content-Type" => "application/json" } }endbefore do@@ -222,22 +229,34 @@ describe GithubLinkback dostub_request(:post,"https://api.github.com/repos/discourse/discourse/commits/76981605fa10975e2e7af457e2f6a31909e0c811/comments",- ).with(headers: headers).to_return(status: 200, body: "", headers: {})+ ).with(headers: github_api_headers).to_return(status: 200, body: "", headers: {})stub_request(:post,"https://api.github.com/repos/discourse/discourse/issues/123/comments",- ).with(headers: headers).to_return(status: 200, body: "", headers: {})+ ).with(headers: github_api_headers).to_return(status: 200, body: "", headers: {})stub_request(:post,"https://api.github.com/repos/discourse/discourse/issues/701/comments",- ).with(headers: headers).to_return(status: 200, body: "", headers: {})+ ).with(headers: github_api_headers).to_return(status: 200, body: "", headers: {})stub_request(:post,"https://api.github.com/repos/discourse/discourse-github-linkback/issues/3/comments",- ).with(headers: headers).to_return(status: 200, body: "", headers: {})+ ).with(headers: github_api_headers).to_return(status: 200, body: "", headers: {})++ stub_request(:get, %r{https://api\.github\.com/repos/.+/commits/.+/comments}).to_return(+ empty_json_response,+ )++ stub_request(:get, %r{https://api\.github\.com/repos/.+/issues/\d+$}).to_return(+ empty_body_json_response,+ )++ stub_request(:get, %r{https://api\.github\.com/repos/.+/issues/\d+/comments}).to_return(+ empty_json_response,+ )endit "returns the URLs it linked to and creates custom fields" do@@ -282,5 +301,197 @@ describe GithubLinkback doexpect(links.size).to eq(1)endend++ context "when topic is already linked on GitHub" do+ let(:pr_post) { Fabricate(:post, raw: github_pr_link) }+ let(:issue_post) { Fabricate(:post, raw: github_issue_link) }+ let(:commit_post) { Fabricate(:post, raw: github_commit_link) }++ before { SiteSetting.github_linkback_access_token = "abcdef" }++ it "skips linkback when the PR description already contains a link to the topic" do+ topic_url = "#{Discourse.base_url}/t/#{pr_post.topic.slug}/#{pr_post.topic_id}"++ stub_request(:get, "https://api.github.com/repos/discourse/discourse/issues/701").with(+ headers: github_api_headers,+ ).to_return(+ status: 200,+ body: { body: "This PR implements #{topic_url}" }.to_json,+ headers: {+ "Content-Type" => "application/json",+ },+ )++ stub_request(+ :get,+ "https://api.github.com/repos/discourse/discourse/issues/701/comments?per_page=100",+ ).with(headers: github_api_headers).to_return(+ status: 200,+ body: [].to_json,+ headers: {+ "Content-Type" => "application/json",+ },+ )++ links = GithubLinkback.new(pr_post).create+ expect(links.size).to eq(1)++ expect(+ a_request(:post, "https://api.github.com/repos/discourse/discourse/issues/701/comments"),+ ).not_to have_been_made+ end++ it "skips linkback when an existing comment contains a link to the topic" do+ topic_url = "#{Discourse.base_url}/t/#{issue_post.topic_id}/3"++ stub_request(:get, "https://api.github.com/repos/discourse/discourse/issues/123").with(+ headers: github_api_headers,+ ).to_return(+ status: 200,+ body: { body: "Some unrelated description" }.to_json,+ headers: {+ "Content-Type" => "application/json",+ },+ )++ stub_request(+ :get,+ "https://api.github.com/repos/discourse/discourse/issues/123/comments?per_page=100",+ ).with(headers: github_api_headers).to_return(+ status: 200,+ body: [{ body: "Already discussed at #{topic_url}" }].to_json,+ headers: {+ "Content-Type" => "application/json",+ },+ )++ links = GithubLinkback.new(issue_post).create+ expect(links.size).to eq(1)++ expect(+ a_request(:post, "https://api.github.com/repos/discourse/discourse/issues/123/comments"),+ ).not_to have_been_made+ end++ it "skips linkback for commits when an existing comment links to the topic" do+ topic_url = "#{Discourse.base_url}/t/#{commit_post.topic_id}"++ stub_request(+ :get,+ "https://api.github.com/repos/discourse/discourse/commits/76981605fa10975e2e7af457e2f6a31909e0c811/comments?per_page=100",+ ).with(headers: github_api_headers).to_return(+ status: 200,+ body: [{ body: "Discussed in #{topic_url}" }].to_json,+ headers: {+ "Content-Type" => "application/json",+ },+ )++ links = GithubLinkback.new(commit_post).create+ expect(links.size).to eq(1)++ expect(+ a_request(+ :post,+ "https://api.github.com/repos/discourse/discourse/commits/76981605fa10975e2e7af457e2f6a31909e0c811/comments",+ ),+ ).not_to have_been_made+ end+ end++ context "when topic is not already linked on GitHub" do+ let(:pr_post) { Fabricate(:post, raw: github_pr_link) }+ let(:commit_post) { Fabricate(:post, raw: github_commit_link) }++ before { SiteSetting.github_linkback_access_token = "abcdef" }++ it "posts linkback when existing links point to a different topic" do+ different_topic_url = "#{Discourse.base_url}/t/some-other-topic/999999"++ stub_request(:get, "https://api.github.com/repos/discourse/discourse/issues/701").with(+ headers: github_api_headers,+ ).to_return(+ status: 200,+ body: { body: "See #{different_topic_url}" }.to_json,+ headers: {+ "Content-Type" => "application/json",+ },+ )++ stub_request(+ :get,+ "https://api.github.com/repos/discourse/discourse/issues/701/comments?per_page=100",+ ).with(headers: github_api_headers).to_return(+ status: 200,+ body: [].to_json,+ headers: {+ "Content-Type" => "application/json",+ },+ )++ stub_request(+ :post,+ "https://api.github.com/repos/discourse/discourse/issues/701/comments",+ ).with(headers: github_api_headers).to_return(status: 200, body: "", headers: {})++ links = GithubLinkback.new(pr_post).create+ expect(links.size).to eq(1)++ expect(+ a_request(:post, "https://api.github.com/repos/discourse/discourse/issues/701/comments"),+ ).to have_been_made.once+ end++ it "posts linkback when the GitHub API check fails (fail open)" do+ stub_request(:get, "https://api.github.com/repos/discourse/discourse/issues/701").with(+ headers: github_api_headers,+ ).to_return(status: 404, body: "", headers: {})++ stub_request(+ :get,+ "https://api.github.com/repos/discourse/discourse/issues/701/comments?per_page=100",+ ).with(headers: github_api_headers).to_return(status: 404, body: "", headers: {})++ stub_request(+ :post,+ "https://api.github.com/repos/discourse/discourse/issues/701/comments",+ ).with(headers: github_api_headers).to_return(status: 200, body: "", headers: {})++ links = GithubLinkback.new(pr_post).create+ expect(links.size).to eq(1)++ expect(+ a_request(:post, "https://api.github.com/repos/discourse/discourse/issues/701/comments"),+ ).to have_been_made.once+ end++ it "posts linkback for commits when no existing comment links to the topic" do+ stub_request(+ :get,+ "https://api.github.com/repos/discourse/discourse/commits/76981605fa10975e2e7af457e2f6a31909e0c811/comments?per_page=100",+ ).with(headers: github_api_headers).to_return(+ status: 200,+ body: [{ body: "Some unrelated comment" }].to_json,+ headers: {+ "Content-Type" => "application/json",+ },+ )++ stub_request(+ :post,+ "https://api.github.com/repos/discourse/discourse/commits/76981605fa10975e2e7af457e2f6a31909e0c811/comments",+ ).with(headers: github_api_headers).to_return(status: 200, body: "", headers: {})++ links = GithubLinkback.new(commit_post).create+ expect(links.size).to eq(1)++ expect(+ a_request(+ :post,+ "https://api.github.com/repos/discourse/discourse/commits/76981605fa10975e2e7af457e2f6a31909e0c811/comments",+ ),+ ).to have_been_made.once+ end+ endendend