From 626808e1009ffc2e5a49d596de6c40edd74c7e2a Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 9 May 2019 14:51:29 +0800 Subject: [PATCH] Fix incorrect condition in `groups:grant_badge` rake task. `#find` raises an error if the id given to it is invalid. As a result, the conditional to check whether a `group` or `badge` is `present?` will not be executed if any of the ids are invalid. Follow up to https://github.com/discourse/discourse/commit/6ba914033cea726ddced866f57115b511fb9ef95. --- lib/tasks/groups.rake | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/tasks/groups.rake b/lib/tasks/groups.rake index b0d6bf11fc1..6dbfac4a9d9 100644 --- a/lib/tasks/groups.rake +++ b/lib/tasks/groups.rake @@ -8,21 +8,30 @@ task "groups:grant_badge", [:group_id, :badge_id] => [:environment] do |_, args| exit 1 end - group = Group.find(group_id) - badge = Badge.find(badge_id) + group = Group.find_by(id: group_id) - if group.present? && badge.present? - puts "Granting badge '#{badge.name}' to all users in group '#{group.name}'..." + unless group + puts "ERROR: `group_id` is invalid" + exit 1 + end - count = 0 - group.users.each do |user| - begin - BadgeGranter.grant(badge, user) - rescue => e - puts "", "Failed to grant badge to #{user.username}", e, e.backtrace.join("\n") - end - putc "." if (count += 1) % 5 == 0 + badge = Badge.find_by(id: badge_id) + + unless badge + puts "ERROR: `badge_id` is invalid" + exit 1 + end + + puts "Granting badge '#{badge.name}' to all users in group '#{group.name}'..." + + count = 0 + group.users.each do |user| + begin + BadgeGranter.grant(badge, user) + rescue => e + puts "", "Failed to grant badge to #{user.username}", e, e.backtrace.join("\n") end + putc "." if (count += 1) % 5 == 0 end puts "", "Done! Badge granted to #{count} members.", ""