Skip to content
Snippets Groups Projects

[BE] Provide script to run on console for bulk assignment

Merged Mohamed Hamda requested to merge 431619-script-draft into master
Files
2
+ 104
0
# frozen_string_literal: true
# Code Suggestions Bulk User Assignment Script | Documentation
# 1. Place the script in your Rails app, for example, code_suggestions_bulk_user_assignment.rb.
# 2. Open the Rails console with SAAS simulation disabled:
# ```bash
# GITLAB_SIMULATE_SAAS=0 rails c
# ```
# 3. Run the script using the Rails console:
# ```ruby
# load "#{Rails.root}/code_suggestions_bulk_user_assignment.rb"
# ```
# 4. Set the `file_path` variable to point to your CSV file, e.g., `file_path = 'users.csv'`.
# 5. Execute the bulk assignment:
# ```ruby
# CodeSuggestionsBulkUserAssignment.new(file_path).assign_seats
# ```
# Error Messages:
# - `User is not found`
# - `ERROR_NO_SEATS_AVAILABLE`: No more seats are available.
# - `ERROR_INVALID_USER_MEMBERSHIP`: User is not eligible for assignment due to being inactive, a bot, or a ghost.
# rubocop: disable Gitlab/NamespacedClass -- Script does not belong to the codebase
class CodeSuggestionsBulkUserAssignment
# rubocop: disable CodeReuse/ActiveRecord -- Script does not belong to the codebase
# rubocop: disable Rails/Pluck -- Script does not belong to the codebase
def initialize(file_path)
@usernames = read_usernames(file_path)
@add_on_purchase = find_code_suggestions_add_on_purchase
@success_assignments = []
@failed_assignments = []
end
def assign_seats
@usernames.each do |username|
user_to_be_assigned = find_user_by_id(username)
unless user_to_be_assigned
error_message = "User is not found: #{username}"
@failed_assignments << error_message
next
end
result = create_user_add_on_service(user_to_be_assigned)
if result[:errors].empty?
log_success_assignment(result)
else
log_failed_assignment(result, username)
end
end
display_results
end
private
def read_usernames(file_path)
CSV.read(file_path, headers: true).map { |row| row['username'] }
end
def find_code_suggestions_add_on_purchase
+1
GitlabSubscriptions::AddOnPurchase.find_by(add_on: GitlabSubscriptions::AddOn.code_suggestions.last)
end
def find_user_by_id(username)
User.find_by(username: username)
end
def create_user_add_on_service(user)
create_service = ::GitlabSubscriptions::UserAddOnAssignments::SelfManaged::CreateService.new(
add_on_purchase: @add_on_purchase,
user: user
).execute
if create_service.success?
{ add_on_purchase: @add_on_purchase, user: user, errors: create_service.errors }
else
{ errors: create_service.errors }
end
end
def log_success_assignment(result)
@success_assignments << "User assigned: #{result[:user].username}"
end
def log_failed_assignment(result, username)
@failed_assignments << "Failed to assign seat to user: #{username}, Errors: #{result[:errors]}"
end
def display_results
puts "\nSuccess Assignments:"
puts @success_assignments.join("\n")
puts "\nFailed Assignments:"
puts @failed_assignments.join("\n")
end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: enable Rails/Pluck
end
# rubocop: enable Gitlab/NamespacedClass
Loading