Skip to content
Open
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
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby 2.6.5
ruby 3.2.2
6 changes: 6 additions & 0 deletions lib/travis/api/v3/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ class Models::Organization < Model

has_preferences Models::OrganizationPreferences

scope :by_login, ->(login, provider) { where(
'lower(login) = ? and lower(vcs_type) = ?'.freeze,
login.downcase,
provider.downcase + 'organization'
).order("id DESC") }

after_initialize do
ensure_preferences
end
Expand Down
38 changes: 19 additions & 19 deletions lib/travis/api/v3/queries/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ class Queries::Organization < Query
def find
return Models::Organization.find_by_id(id) if id
return Models::Organization.find_by(vcs_id: github_id) || Models::Organization.find_by(github_id: github_id) if github_id
return Models::Organization.where(
'lower(login) = ? and lower(vcs_type) = ?'.freeze,
login.downcase,
provider.downcase + 'organization'
).order("id DESC").first if login
return Models::Organization.by_login(login, provider).first if login
raise WrongParams, 'missing organization.id or organization.login'.freeze
end

Expand All @@ -27,14 +23,14 @@ def active(value, from)

def suspend(value)
if params['vcs_type']
raise WrongParams, 'missing user ids'.freeze unless params['vcs_ids']&.size > 0
raise_missing_ids_unless('vcs_ids')

user_ids = Models::User.where("vcs_type = ? and vcs_id in (?)", vcs_type,params['vcs_ids']).all.map(&:id)
else
raise WrongParams, 'missing user ids'.freeze unless params['user_ids']&.size > 0
user_ids = Models::User.where(vcs_type: vcs_type, vcs_id: params['vcs_ids']).pluck(:id)
else
raise_missing_ids_unless('user_ids')

user_ids = params['user_ids']
end
user_ids = params['user_ids']
end

filtered_ids = filter_ids(user_ids)
Models::User.where("id in (?)", filtered_ids).update!(suspended: value, suspended_at: value ? Time.now.utc : nil)
Expand All @@ -49,18 +45,22 @@ def filter_ids(ids)
end

def vcs_type
@_vcs_type ||=
params['vcs_type'] ?
(
params['vcs_type'].end_with?('User') ?
params['vcs_type'] :
"#{params['vcs_type'].capitalize}User"
)
: 'GithubUser'
@_vcs_type ||= case
when params['vcs_type']&.end_with?('User')
params['vcs_type']
when params['vcs_type']
"#{params['vcs_type'].capitalize}User"
else
'GithubUser'
end
end

private

def raise_missing_ids_unless(key)
raise WrongParams, 'missing user ids'.freeze unless params[key]&.size > 0
end

def provider
params['provider'] || 'github'
end
Expand Down