A platform for community discussion. Free, open, simple.
FIX: Only count commits from configured repos for GitHub badges
Régis Hanol73c5d6744742
diff --git a/plugins/discourse-github/app/lib/github_badges.rb b/plugins/discourse-github/app/lib/github_badges.rbindex 73d90ab953f..0aea47b4e3d 100644--- a/plugins/discourse-github/app/lib/github_badges.rb+++ b/plugins/discourse-github/app/lib/github_badges.rb@@ -76,10 +76,7 @@ module DiscourseGithubPluginenddef self.grant_committer_badges!- emails =- GithubCommit.where(merge_commit: false, role_id: CommitsPopulator::ROLES[:committer]).pluck(- :email,- )+ emails = configured_commits.where(role_id: CommitsPopulator::ROLES[:committer]).pluck(:email)bronze, silver, gold = committer_badges@@ -92,10 +89,7 @@ module DiscourseGithubPlugindef self.grant_contributor_badges!emails =- GithubCommit.where(- merge_commit: false,- role_id: CommitsPopulator::ROLES[:contributor],- ).pluck(:email)+ configured_commits.where(role_id: CommitsPopulator::ROLES[:contributor]).pluck(:email)bronze, silver, gold = contributor_badges@@ -110,6 +104,13 @@ module DiscourseGithubPlugingranter.grant!end+ def self.configured_commits+ GithubCommit.where(+ merge_commit: false,+ repo_id: GithubRepo.where(name: GithubRepo.names).select(:id),+ )+ end+def self.ensure_badge(name, attrs)badge = Badge.find_by("name ILIKE ?", name)diff --git a/plugins/discourse-github/app/models/github_repo.rb b/plugins/discourse-github/app/models/github_repo.rbindex b6c0d4cd35b..6ee1080f8c2 100644--- a/plugins/discourse-github/app/models/github_repo.rb+++ b/plugins/discourse-github/app/models/github_repo.rb@@ -8,27 +8,17 @@ module DiscourseGithubPluginhas_many :commits, foreign_key: :repo_id, class_name: :GithubCommit, dependent: :destroydef self.repos- repos = []+ names.map { |name| find_or_create_by!(name:) }+ end++ def self.namesSiteSetting.github_badges_repos.split("|")- .each do |link|- name = match_name_from_setting(link)- next if name.blank?- name.gsub!(/\.git$/, "")- name.gsub!(%r{/$}, "") # Remove trailing '/'- repos << find_or_create_by!(name: name)+ .filter_map do |link|+ name = link[VALID_URL_BASED_REPO_REGEX, 1] || link[VALID_USER_BASED_REPO_REGEX]+ name&.delete_suffix(".git")&.delete_suffix("/").presenceend- repos- end-- def self.match_name_from_setting(repo)- if repo =~ VALID_URL_BASED_REPO_REGEX- Regexp.last_match[1]- elsif repo =~ VALID_USER_BASED_REPO_REGEX- repo_name = Regexp.last_match[0]- repo.match(/https?:/).blank? ? repo_name : nil- endendendenddiff --git a/plugins/discourse-github/spec/lib/github_badges_spec.rb b/plugins/discourse-github/spec/lib/github_badges_spec.rbindex c24487e90f1..bade4178d30 100644--- a/plugins/discourse-github/spec/lib/github_badges_spec.rb+++ b/plugins/discourse-github/spec/lib/github_badges_spec.rb@@ -131,6 +131,16 @@ describe DiscourseGithubPlugin::GithubBadges doexpect(staged_user.badges.first).to eq(nil)end+ it "only counts commits from the configured repos" do+ DiscourseGithubPlugin::GithubBadges.committer_badges.each { it.update!(enabled: true) }+ SiteSetting.github_badges_repos = "https://github.com/org/repo1.git"++ DiscourseGithubPlugin::GithubBadges.grant!++ expect(bronze_user.reload.badges).to be_present+ expect(bronze_user_repo_2.reload.badges).to be_empty+ end+it "does not update user title if badge is not allowed to be used as a title" doDiscourseGithubPlugin::GithubBadges.grant!silver_committer_badge =diff --git a/plugins/discourse-github/spec/models/github_repo_spec.rb b/plugins/discourse-github/spec/models/github_repo_spec.rbindex 9b09ffd85fc..a9ba25fdea4 100644--- a/plugins/discourse-github/spec/models/github_repo_spec.rb+++ b/plugins/discourse-github/spec/models/github_repo_spec.rb@@ -3,25 +3,24 @@describe DiscourseGithubPlugin::GithubRepo dobefore { enable_current_plugin }- it "strips .git from url" do- SiteSetting.github_badges_repos = "https://github.com/discourse/discourse.git"- repo = DiscourseGithubPlugin::GithubRepo.repos.first- expect(repo.name).to eq("discourse/discourse")- end+ describe ".names" do+ it "extracts the repo names from the badges repos setting" do+ SiteSetting.github_badges_repos =+ "https://github.com/discourse/discourse.git|https://github.com/discourse/onebox/|discourse/somerepo_with-numbers7"- it "strips trailing slash from url" do- SiteSetting.github_badges_repos = "https://github.com/discourse/discourse/"- repo = DiscourseGithubPlugin::GithubRepo.repos.first- expect(repo.name).to eq("discourse/discourse")+ expect(described_class.names).to eq(+ %w[discourse/discourse discourse/onebox discourse/somerepo_with-numbers7],+ )+ expect(described_class.count).to eq(0)+ endend- it "doesn't raise an error when the site setting follows the user/repo format" do- SiteSetting.github_badges_repos = "discourse/discourse-github"- repo = DiscourseGithubPlugin::GithubRepo.repos.first- expect(repo.name).to eq("discourse/discourse-github")+ describe ".repos" do+ it "finds or creates a repo for each name" do+ SiteSetting.github_badges_repos = "discourse/discourse"- SiteSetting.github_badges_repos = "discourse/somerepo_with-numbers7"- repo = DiscourseGithubPlugin::GithubRepo.repos.first- expect(repo.name).to eq("discourse/somerepo_with-numbers7")+ expect(described_class.repos.map(&:name)).to eq(%w[discourse/discourse])+ expect { described_class.repos }.not_to change { described_class.count }+ endendend