Skip to content

Add option to use a custom field for GitHub user matching #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions app/lib/github_badges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ module GithubBadges
COMMITTER_BADGE_NAME_GOLD ||= "Amazing Committer"

class Granter
def initialize(emails)
def initialize(emails, custom_user_field)
@emails = emails
@badges = []
# The custom user field that stores the user's GitHub username, if
# specified.
@custom_user_field = custom_user_field
end

def add_badge(badge, as_title:, threshold:)
Expand Down Expand Up @@ -52,6 +55,18 @@ def grant!
.map { |row| [row.user, row.info["nickname"]] }
.to_h

# If we don't have any screen names, look in the custom user field, if
# specified.
if screen_names.empty? && @custom_user_field
screen_names =
UserCustomField
.where(name: @custom_user_field)
.where("value IN (?)", github_name_email.keys)
.includes(:user)
.map { |row| [row.user, row.value] }
.to_h
end

screen_names.each do |user, screen_name|
user_emails[user] ||= []
user_emails[user] << github_name_email[screen_name]
Expand Down Expand Up @@ -83,7 +98,7 @@ def self.grant_committer_badges!

bronze, silver, gold = committer_badges

granter = GithubBadges::Granter.new(emails)
granter = GithubBadges::Granter.new(emails, SiteSetting.github_user_custom_field)
granter.add_badge(bronze, as_title: false, threshold: 1)
granter.add_badge(silver, as_title: true, threshold: 25)
granter.add_badge(gold, as_title: true, threshold: 1000)
Expand All @@ -99,7 +114,7 @@ def self.grant_contributor_badges!

bronze, silver, gold = contributor_badges

granter = GithubBadges::Granter.new(emails)
granter = GithubBadges::Granter.new(emails, SiteSetting.github_user_custom_field)
granter.add_badge(bronze, as_title: false, threshold: 1)
granter.add_badge(
silver,
Expand Down
1 change: 1 addition & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ en:
github_badges_repos: "URLs of the GitHub repos to scan for contributions and commits"
github_silver_badge_min_commits: "Minumum number of commits for silver badge"
github_gold_badge_min_commits: "Minumum number of commits for gold badge"
github_user_custom_field: "Optional custom field that stores the GitHub username for verification"

errors:
invalid_badge_repo: "You must provide a GitHub URL or the repository name in the format github_user/repository_name"
Expand Down
2 changes: 2 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ discourse_github:
github_gold_badge_min_commits:
default: 250
min: 1
github_user_custom_field:
default: ""