Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Report GitHub token and repo problems on the admin dashboard

Régis Hanolec4cea91ea81
diff --git a/plugins/discourse-github/app/lib/commits_populator.rb b/plugins/discourse-github/app/lib/commits_populator.rb
index b3656fa693b..0e94b48346f 100644
--- a/plugins/discourse-github/app/lib/commits_populator.rb
+++ b/plugins/discourse-github/app/lib/commits_populator.rb
@@ -4,7 +4,7 @@ module DiscourseGithubPlugin
class CommitsPopulator
MERGE_COMMIT_REGEX = /^Merge pull request/
HISTORY_COMPLETE = "history-complete"
- class GraphQLError < StandardError
+ class GraphQLError < Discourse::GithubApi::Error
end
ROLES = { committer: 0, contributor: 1 }
@@ -100,9 +100,8 @@ module DiscourseGithubPlugin
end
def populate!
- return unless SiteSetting.github_badges_enabled?
return if @client.backing_off?
- return if @client.get("/repos/#{@repo.name}/branches").blank?
+ return if @client.get("/repos/#{@repo.name}/branches", per_page: 1).blank?
if @repo.commits.size == 0
build_history!
@@ -135,31 +134,6 @@ module DiscourseGithubPlugin
Discourse.redis.del(front_commit_redis_key)
end
end
- rescue Discourse::GithubApi::NotFound
- disable_github_badges_and_inform_admin(
- title: I18n.t("github_commits_populator.errors.repository_not_found_pm_title"),
- raw:
- I18n.t(
- "github_commits_populator.errors.repository_not_found_pm",
- repo_name: @repo.name,
- base_path: Discourse.base_path,
- ),
- )
- Rails.logger.warn(
- "Disabled github_badges_enabled site setting due to repository Not Found error ",
- )
- rescue Discourse::GithubApi::Unauthorized
- disable_github_badges_and_inform_admin(
- title: I18n.t("github_commits_populator.errors.invalid_octokit_credentials_pm_title"),
- raw:
- I18n.t(
- "github_commits_populator.errors.invalid_octokit_credentials_pm",
- base_path: Discourse.base_path,
- ),
- )
- Rails.logger.warn(
- "Disabled github_badges_enabled site setting due to invalid GitHub authentication credentials via github_linkback_access_token.",
- )
rescue Discourse::GithubApi::Error => err
Rails.logger.warn("#{err.class}: #{err.message}")
end
@@ -277,19 +251,5 @@ module DiscourseGithubPlugin
# e.g., if we got rate-limited by github
"discourse-github-back-cursor-#{@repo.name}"
end
-
- def disable_github_badges_and_inform_admin(title:, raw:)
- SiteSetting.github_badges_enabled = false
- site_admin_usernames =
- User.where(admin: true).human_users.order("last_seen_at DESC").limit(10).pluck(:username)
- PostCreator.create!(
- Discourse.system_user,
- title: title,
- raw: raw,
- archetype: Archetype.private_message,
- target_usernames: site_admin_usernames,
- skip_validations: true,
- )
- end
end
end
diff --git a/plugins/discourse-github/app/lib/github_linkback_access_token_setting_validator.rb b/plugins/discourse-github/app/lib/github_linkback_access_token_setting_validator.rb
deleted file mode 100644
index f9e5b3c566e..00000000000
--- a/plugins/discourse-github/app/lib/github_linkback_access_token_setting_validator.rb
+++ /dev/null
@@ -1,30 +0,0 @@
-# frozen_string_literal: true
-
-class GithubLinkbackAccessTokenSettingValidator
- def initialize(opts = {})
- @opts = opts
- end
-
- def valid_value?(val)
- return true if val.blank?
- client = Discourse::GithubApi.new(val)
- DiscourseGithubPlugin::GithubRepo.repos.each do |repo|
- @failing_repo = repo.name
- client.get("/repos/#{repo.name}/branches")
- end
- true
- rescue Discourse::GithubApi::Unauthorized, Discourse::GithubApi::NotFound
- false
- end
-
- def error_message
- if @failing_repo.present?
- I18n.t(
- "site_settings.errors.invalid_github_linkback_access_token_for_repo",
- repo: @failing_repo,
- )
- else
- I18n.t("site_settings.errors.invalid_github_linkback_access_token")
- end
- end
-end
diff --git a/plugins/discourse-github/app/services/problem_check/github_badges_repo.rb b/plugins/discourse-github/app/services/problem_check/github_badges_repo.rb
new file mode 100644
index 00000000000..b62f41637b3
--- /dev/null
+++ b/plugins/discourse-github/app/services/problem_check/github_badges_repo.rb
@@ -0,0 +1,39 @@
+# frozen_string_literal: true
+
+class ProblemCheck::GithubBadgesRepo < ProblemCheck
+ self.priority = "high"
+ self.perform_every = 4.hours
+ self.targets = -> { DiscourseGithubPlugin::GithubRepo.names }
+
+ def self.enabled?
+ SiteSetting.github_badges_enabled? && SiteSetting.github_linkback_access_token.present? &&
+ DiscourseGithubPlugin::GithubRepo.names.any?
+ end
+
+ def call
+ full_name =
+ Discourse::GithubApi
+ .for(token: SiteSetting.github_linkback_access_token)
+ .get("/repos/#{target}")
+ &.dig("full_name")
+ return no_problem if full_name.blank? || full_name.casecmp?(target)
+
+ problem(
+ target,
+ override_key: "dashboard.problem.github_badges_repo_moved",
+ override_data: {
+ new_name: full_name,
+ },
+ )
+ rescue Discourse::GithubApi::NotFound
+ problem(target, override_key: "dashboard.problem.github_badges_repo_not_found")
+ rescue Discourse::GithubApi::Unauthorized
+ no_problem
+ end
+
+ private
+
+ def translation_data(repo)
+ { repo: }
+ end
+end
diff --git a/plugins/discourse-github/app/services/problem_check/github_linkback_access_token_invalid.rb b/plugins/discourse-github/app/services/problem_check/github_linkback_access_token_invalid.rb
new file mode 100644
index 00000000000..25ef09e5d74
--- /dev/null
+++ b/plugins/discourse-github/app/services/problem_check/github_linkback_access_token_invalid.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+class ProblemCheck::GithubLinkbackAccessTokenInvalid < ProblemCheck
+ self.priority = "high"
+ self.perform_every = 1.hour
+
+ def self.enabled?
+ SiteSetting.github_linkback_access_token.present?
+ end
+
+ def call
+ Discourse::GithubApi.for(token: SiteSetting.github_linkback_access_token).raw_get("/rate_limit")
+ no_problem
+ rescue Discourse::GithubApi::Unauthorized
+ problem
+ end
+end
diff --git a/plugins/discourse-github/config/locales/server.en.yml b/plugins/discourse-github/config/locales/server.en.yml
index 5597592c213..2c37e414020 100644
--- a/plugins/discourse-github/config/locales/server.en.yml
+++ b/plugins/discourse-github/config/locales/server.en.yml
@@ -17,8 +17,6 @@ en:
errors:
invalid_badge_repo: "You must provide a GitHub URL or the repository name in the format github_user/repository_name"
invalid_badge_repo_value: "'%{repo}' is not a valid GitHub URL or repository name. Use the format github_user/repository_name."
- invalid_github_linkback_access_token: "You must provide a valid GitHub linkback access token which has access to the badge repositories you have provided."
- invalid_github_linkback_access_token_for_repo: "The GitHub linkback access token could not access the '%{repo}' repository. Make sure the token is valid and that the repository name is correct and accessible."
github_linkback:
commit_template: |
@@ -33,31 +31,8 @@ en:
This issue has been mentioned on **%{title}**. There might be relevant details there:
%{post_url}
- github_commits_populator:
- errors:
- repository_identifier_invalid_pm_title: "Action required for discourse-github plugin"
- repository_identifier_invalid_pm: |
- A repository specified in the discourse-github plugin is invalid:
- %{repo_name}
-
- They should be specified as `user/repo`.
-
- Badges will not be awarded until the name is corrected in
- <a href="%{base_path}/admin/site_settings/category/plugins?filter=GitHub">discourse-github Site Settings</a>
- and "github_badges_enabled" is turned back on.
- repository_not_found_pm_title: "Action required for discourse-github plugin"
- repository_not_found_pm: |
- A repository specified in the discourse-github plugin returned a "Not Found"
- error: %{repo_name}
-
- Badges will not be awarded until the name is corrected in
- <a href="%{base_path}/admin/site_settings/category/plugins?filter=GitHub">discourse-github Site Settings</a>
- and "github_badges_enabled" is turned back on.
- invalid_octokit_credentials_pm_title: "Action required for discourse-github plugin"
- invalid_octokit_credentials_pm: |
- The GitHub credentials provided to the discourse-github plugin are invalid. The "github badges enabled\ site
- setting has been disabled, and commits will no longer be populated until the issue is resolved.
-
- Check your "github linkback access token" site setting to ensure the token is correct and has permission to all
- repos via
- <a href="%{base_path}/admin/site_settings/category/plugins?filter=GitHub">discourse-github Site Settings</a>.
+ dashboard:
+ problem:
+ github_linkback_access_token_invalid: "GitHub rejected the {{setting:github_linkback_access_token}}. Generate a new token and update the setting."
+ github_badges_repo_not_found: "The GitHub repository <code>%{repo}</code> in {{setting:github_badges_repos}} doesn't exist or the {{setting:github_linkback_access_token}} can't access it, so its contributions aren't counted for badges."
+ github_badges_repo_moved: "The GitHub repository <code>%{repo}</code> has moved to <code>%{new_name}</code>. Update {{setting:github_badges_repos}} to use the new name."
diff --git a/plugins/discourse-github/config/settings.yml b/plugins/discourse-github/config/settings.yml
index ae085850db0..f49e19f96a7 100644
--- a/plugins/discourse-github/config/settings.yml
+++ b/plugins/discourse-github/config/settings.yml
@@ -9,7 +9,6 @@ discourse_github:
github_linkback_access_token:
default: ""
secret: true
- validator: "GithubLinkbackAccessTokenSettingValidator"
github_linkback_maximum_links:
default: 25
min: 1
diff --git a/plugins/discourse-github/plugin.rb b/plugins/discourse-github/plugin.rb
index 4a7f024a19b..2cd3546d29b 100644
--- a/plugins/discourse-github/plugin.rb
+++ b/plugins/discourse-github/plugin.rb
@@ -9,7 +9,6 @@
# Site setting validators must be loaded before initialize
require_relative "app/lib/github_badges_repo_setting_validator"
-require_relative "app/lib/github_linkback_access_token_setting_validator"
enabled_site_setting :enable_discourse_github_plugin
@@ -34,8 +33,13 @@ after_initialize do
../app/jobs/regular/create_github_linkback.rb
../app/jobs/scheduled/grant_github_badges.rb
../app/jobs/regular/replace_github_non_permalinks.rb
+ ../app/services/problem_check/github_linkback_access_token_invalid.rb
+ ../app/services/problem_check/github_badges_repo.rb
].each { |path| require File.expand_path(path, __FILE__) }
+ register_problem_check ProblemCheck::GithubLinkbackAccessTokenInvalid
+ register_problem_check ProblemCheck::GithubBadgesRepo
+
on(:post_created) do |post|
if SiteSetting.github_linkback_enabled? && SiteSetting.enable_discourse_github_plugin?
GithubLinkback.new(post).enqueue
diff --git a/plugins/discourse-github/spec/lib/commits_populator_spec.rb b/plugins/discourse-github/spec/lib/commits_populator_spec.rb
index 9ed14b6f4ab..57bd7a12b01 100644
--- a/plugins/discourse-github/spec/lib/commits_populator_spec.rb
+++ b/plugins/discourse-github/spec/lib/commits_populator_spec.rb
@@ -4,100 +4,27 @@ describe DiscourseGithubPlugin::CommitsPopulator do
subject(:populator) { described_class.new(repo) }
let(:repo) { DiscourseGithubPlugin::GithubRepo.new(name: "discourse/discourse") }
- let!(:site_admin1) { Fabricate(:admin) }
- let!(:site_admin2) { Fabricate(:admin) }
- let(:branches_url) { "https://api.github.com/repos/discourse/discourse/branches" }
+ let(:branches_url) { "https://api.github.com/repos/discourse/discourse/branches?per_page=1" }
- before do
- enable_current_plugin
- SiteSetting.github_badges_enabled = true
- end
-
- def last_pm
- Post
- .joins(:topic)
- .includes(:topic)
- .where("topics.archetype = ?", Archetype.private_message)
- .last
- end
-
- context "when invalid credentials have been provided (401)" do
- before { stub_request(:get, branches_url).to_return(status: 401) }
-
- it "disables github badges and sends a PM to the admin of the site to inform them" do
- populator.populate!
- expect(SiteSetting.github_badges_enabled).to eq(false)
- expect(last_pm.topic.allowed_users).to include(site_admin1, site_admin2)
- expect(last_pm.topic.title).to eq(
- I18n.t("github_commits_populator.errors.invalid_octokit_credentials_pm_title"),
- )
- expect(last_pm.raw).to eq(
- I18n.t(
- "github_commits_populator.errors.invalid_octokit_credentials_pm",
- base_path: Discourse.base_path,
- ).strip,
- )
- end
- end
-
- context "when the repository is not found (404)" do
- before { stub_request(:get, branches_url).to_return(status: 404) }
+ before { enable_current_plugin }
- it "disables github badges and sends a PM to the admin of the site to inform them" do
- populator.populate!
- expect(SiteSetting.github_badges_enabled).to eq(false)
- expect(last_pm.topic.allowed_users).to include(site_admin1, site_admin2)
- expect(last_pm.topic.title).to eq(
- I18n.t("github_commits_populator.errors.repository_not_found_pm_title"),
- )
- expect(last_pm.raw).to eq(
- I18n.t(
- "github_commits_populator.errors.repository_not_found_pm",
- repo_name: repo.name,
- base_path: Discourse.base_path,
- ).strip,
- )
- end
- end
+ it "logs GitHub API errors" do
+ stub_request(:get, branches_url).to_return(status: 404)
- context "if some other GitHub error is raised (500)" do
- before { stub_request(:get, branches_url).to_return(status: 500) }
+ logger = track_log_messages { populator.populate! }
- it "simply logs the error and does nothing else" do
- populator.populate!
- expect(SiteSetting.github_badges_enabled).to eq(true)
- end
+ expect(logger.warnings).to include(/NotFound/)
end
- context "if GraphQL returns no data" do
- before do
- stub_request(:get, branches_url).to_return(
- status: 200,
- body: [{ "name" => "main", "commit" => { "sha" => "abc" } }].to_json,
- headers: {
- "Content-Type" => "application/json",
- },
- )
- stub_request(:post, "https://api.github.com/graphql").to_return(
- status: 200,
- body: { "message" => "Bad credentials" }.to_json,
- headers: {
- "Content-Type" => "application/json",
- },
- )
- end
-
- it "raises a GraphQLError" do
- expect { populator.populate! }.to raise_error(described_class::GraphQLError)
- end
- end
+ it "logs when GraphQL returns no data" do
+ stub_request(:get, branches_url).to_return(status: 200, body: [{ name: "main" }].to_json)
+ stub_request(:post, "https://api.github.com/graphql").to_return(
+ status: 200,
+ body: { message: "Bad credentials" }.to_json,
+ )
- context "if github_badges_enabled is false" do
- before { SiteSetting.github_badges_enabled = false }
+ logger = track_log_messages { populator.populate! }
- it "early returns before making any GitHub request" do
- populator.populate!
- expect(a_request(:get, branches_url)).not_to have_been_made
- end
+ expect(logger.warnings).to include(/GraphQLError/)
end
end
diff --git a/plugins/discourse-github/spec/lib/github_linkback_access_token_setting_validator_spec.rb b/plugins/discourse-github/spec/lib/github_linkback_access_token_setting_validator_spec.rb
deleted file mode 100644
index c111fe91977..00000000000
--- a/plugins/discourse-github/spec/lib/github_linkback_access_token_setting_validator_spec.rb
+++ /dev/null
@@ -1,102 +0,0 @@
-# frozen_string_literal: true
-
-describe GithubLinkbackAccessTokenSettingValidator do
- subject(:validator) { described_class.new }
-
- let(:value) { SecureRandom.hex(10) }
-
- before { enable_current_plugin }
-
- describe "#valid_value?" do
- context "when the token cannot access a repo (401)" do
- before do
- setup_repos
- stub_request(:get, "https://api.github.com/repos/discourse/discourse/branches").to_return(
- status: 401,
- )
- end
-
- it "rejects the token" do
- expect(validator.valid_value?(value)).to eq(false)
- end
- end
-
- context "when a repo is missing or private (404)" do
- before do
- setup_repos
- stub_request(:get, "https://api.github.com/repos/discourse/discourse/branches").to_return(
- status: 404,
- )
- end
-
- it "rejects the token" do
- expect(validator.valid_value?(value)).to eq(false)
- end
- end
-
- context "when the token is accepted" do
- it "accepts the token when no repositories are defined" do
- expect(validator.valid_value?(value)).to eq(true)
- end
-
- context "when there are repos defined" do
- before do
- setup_repos
- stub_request(:get, "https://api.github.com/repos/discourse/discourse/branches").to_return(
- status: 200,
- body: "[]",
- )
- end
-
- it "accepts the token when all repositories are accessible" do
- expect(validator.valid_value?(value)).to eq(true)
- end
- end
- end
- end
-
- describe "#error_message" do
- it "returns the generic message when no specific repo failed" do
- expect(validator.error_message).to eq(
- I18n.t("site_settings.errors.invalid_github_linkback_access_token"),
- )
- end
-
- it "names the failing repo when access is unauthorized (401)" do
- setup_repos("discourse/discourse|acme/private-repo|foo/bar")
- stub_branches("discourse/discourse", status: 200)
- stub_branches("acme/private-repo", status: 401)
-
- expect(validator.valid_value?(value)).to eq(false)
- expect(validator.error_message).to eq(
- I18n.t(
- "site_settings.errors.invalid_github_linkback_access_token_for_repo",
- repo: "acme/private-repo",
- ),
- )
- end
-
- it "names the failing repo when it is missing or private (404)" do
- setup_repos("discourse/discourse|acme/typo-repo")
- stub_branches("discourse/discourse", status: 200)
- stub_branches("acme/typo-repo", status: 404)
-
- expect(validator.valid_value?(value)).to eq(false)
- expect(validator.error_message).to eq(
- I18n.t(
- "site_settings.errors.invalid_github_linkback_access_token_for_repo",
- repo: "acme/typo-repo",
- ),
- )
- end
- end
-
- def setup_repos(repos = "discourse/discourse")
- SiteSetting.github_badges_repos = repos
- DiscourseGithubPlugin::GithubRepo.repos
- end
-
- def stub_branches(repo, status:)
- stub_request(:get, "https://api.github.com/repos/#{repo}/branches").to_return(status:)
- end
-end
diff --git a/plugins/discourse-github/spec/services/problem_check/github_badges_repo_spec.rb b/plugins/discourse-github/spec/services/problem_check/github_badges_repo_spec.rb
new file mode 100644
index 00000000000..e9f50db5763
--- /dev/null
+++ b/plugins/discourse-github/spec/services/problem_check/github_badges_repo_spec.rb
@@ -0,0 +1,68 @@
+# frozen_string_literal: true
+
+describe ProblemCheck::GithubBadgesRepo do
+ subject(:check) { described_class.new("discourse/discourse") }
+
+ before do
+ enable_current_plugin
+ SiteSetting.github_badges_enabled = true
+ SiteSetting.github_linkback_access_token = "token"
+ SiteSetting.github_badges_repos = "discourse/discourse"
+ end
+
+ def stub_repo(status: 200, full_name: "discourse/discourse")
+ stub_request(:get, "https://api.github.com/repos/discourse/discourse").to_return(
+ status:,
+ body: { full_name: }.to_json,
+ )
+ end
+
+ it "is only enabled when badges can be populated" do
+ expect(described_class).to be_enabled
+
+ SiteSetting.github_linkback_access_token = ""
+ expect(described_class).not_to be_enabled
+
+ SiteSetting.github_linkback_access_token = "token"
+ SiteSetting.github_badges_repos = ""
+ expect(described_class).not_to be_enabled
+ end
+
+ it "reports a repo that has moved" do
+ stub_repo(full_name: "discourse/renamed")
+
+ expect(check).to have_a_problem.with_message(
+ I18n.t(
+ "dashboard.problem.github_badges_repo_moved",
+ repo: "discourse/discourse",
+ new_name: "discourse/renamed",
+ ),
+ )
+ end
+
+ it "reports a repo that is not found" do
+ stub_repo(status: 404)
+
+ expect(check).to have_a_problem.with_message(
+ I18n.t("dashboard.problem.github_badges_repo_not_found", repo: "discourse/discourse"),
+ )
+ end
+
+ it "is chill about a repo whose name only differs in case" do
+ stub_repo(full_name: "Discourse/Discourse")
+
+ expect(check).to be_chill_about_it
+ end
+
+ it "is chill about a rejected token, which has its own check" do
+ stub_repo(status: 401)
+
+ expect(check).to be_chill_about_it
+ end
+
+ it "lets other errors through so an existing notice is kept" do
+ stub_repo(status: 500)
+
+ expect { check.call }.to raise_error(Discourse::GithubApi::Error)
+ end
+end
diff --git a/plugins/discourse-github/spec/services/problem_check/github_linkback_access_token_invalid_spec.rb b/plugins/discourse-github/spec/services/problem_check/github_linkback_access_token_invalid_spec.rb
new file mode 100644
index 00000000000..533efb428a5
--- /dev/null
+++ b/plugins/discourse-github/spec/services/problem_check/github_linkback_access_token_invalid_spec.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+describe ProblemCheck::GithubLinkbackAccessTokenInvalid do
+ subject(:check) { described_class.new }
+
+ before do
+ enable_current_plugin
+ SiteSetting.github_linkback_access_token = "token"
+ end
+
+ def stub_rate_limit(status:)
+ stub_request(:get, "https://api.github.com/rate_limit").to_return(status:, body: "{}")
+ end
+
+ it "is only enabled when a token is configured" do
+ expect(described_class).to be_enabled
+
+ SiteSetting.github_linkback_access_token = ""
+ expect(described_class).not_to be_enabled
+ end
+
+ it "reports a token GitHub rejects" do
+ stub_rate_limit(status: 401)
+ expect(check).to have_a_problem.with_priority("high")
+ end
+
+ it "is chill about a token GitHub accepts" do
+ stub_rate_limit(status: 200)
+ expect(check).to be_chill_about_it
+ end
+
+ it "lets other errors through so an existing notice is kept" do
+ stub_rate_limit(status: 500)
+ expect { check.call }.to raise_error(Discourse::GithubApi::Error)
+ end
+end