Sign in or sign up before continuing. Don't have an account yet? Register now to get started.
Register now

Remove fallback to nil for `Current. organization&.id`

Summary

After the merge of #499081 (closed), we are left with some usages of Current.organization&.id.

Improvements

The safe navigation operator & should not be used because Current.organization should always return a valid organization. Using the safe navigation operator can cause errors that seem unrelated, making it more difficult to debug and fix issues during development.

Risks

Replacing Current.organization&.id by Current.organization.id can result in new errors, if the Current.organization is not set.

We can mitigate this by ensuring that an organization is always assigned.

Involved components

The output of `git grep "Current.organization&.id"` as of Fri April 4th:
app/controllers/admin/groups_controller.rb:42:      group_params.with_defaults(organization_id: Current.organization&.id)).execute
app/controllers/admin/topics_controller.rb:87:    ::Current.organization&.id
app/controllers/admin/users_controller.rb:215:    opts[:organization_id] ||= Current.organization&.id
app/controllers/groups_controller.rb:84:      group_params.merge(organization_id: Current.organization&.id)
app/controllers/import/base_controller.rb:115:      organization_id: Current.organization&.id,
app/controllers/import/bitbucket_server_controller.rb:47:      params.merge({ organization_id: Current.organization&.id })
app/controllers/import/fogbugz_controller.rb:61:      organization_id: Current.organization&.id
app/controllers/import/gitlab_groups_controller.rb:24:      .with_defaults(organization_id: Current.organization&.id)
app/controllers/oauth/authorizations_controller.rb:81:    params[:organization_id] = ::Current.organization&.id
app/controllers/omniauth_callbacks_controller.rb:230:    { organization_id: Current.organization&.id }
app/controllers/registrations_controller.rb:242:                                                              organization_id: Current.organization&.id }))
app/controllers/user_settings/personal_access_tokens_controller.rb:56:        organization_id: Current.organization&.id,
app/graphql/mutations/snippets/create.rb:88:        args[:organization_id] = Current.organization&.id
app/graphql/resolvers/topics_resolver.rb:20:      organization = authorized_find!(id: args[:organization_id] || ::Current.organization&.id)
ee/app/controllers/gitlab_subscriptions/groups_controller.rb:30:        current_user, name: name, path: path, organization_id: Current.organization&.id
ee/app/controllers/gitlab_subscriptions/subscriptions_controller.rb:189:        current_user, name: name, path: path, organization_id: Current.organization&.id
ee/app/controllers/gitlab_subscriptions/trials_controller.rb:126:      .with_defaults(organization_id: Current.organization&.id).to_h
ee/app/controllers/registrations/groups_controller.rb:118:      ).with_defaults(organization_id: Current.organization&.id)
ee/app/controllers/trial_registrations_controller.rb:53:      current_user, sign_up_params.merge(organization_id: Current.organization&.id)
ee/lib/api/group_service_accounts.rb:199:              current_user: current_user, target_user: user, organization_id: Current.organization&.id,
ee/lib/api/scim/instance_scim.rb:114:              parser.post_params.merge(organization_id: ::Current.organization&.id)
ee/lib/api/service_accounts.rb:32:          current_user, declared_params.merge(organization_id: Current.organization&.id)
lib/api/group_import.rb:70:        optional :organization_id, type: Integer, default: -> { Current.organization&.id }, desc: "The ID of the organization that the group will be part of. "
lib/api/groups.rb:261:        optional :organization_id, type: Integer, default: -> { Current.organization&.id },
lib/api/import_bitbucket.rb:37:        params.merge(organization_id: Current.organization&.id)
lib/api/internal/base.rb:289:            current_user: user, target_user: user, organization_id: Current.organization&.id, params: { name: params[:name], scopes: params[:scopes], expires_at: expires_at }
lib/api/snippets.rb:143:        attrs = process_create_params(declared_params(include_missing: false)).merge(organization_id: Current.organization&.id)
lib/api/topics.rb:32:      optional :organization_id, type: Integer, default: -> { ::Current.organization&.id },
lib/api/topics.rb:70:      optional :organization_id, type: Integer, default: -> { ::Current.organization&.id },
lib/api/users.rb:350:        params = declared_params(include_missing: false).merge(organization_id: Current.organization&.id)
lib/api/users.rb:1095:              current_user: current_user, target_user: target_user, organization_id: Current.organization&.id, params: declared_params(include_missing: false)
lib/api/users.rb:1556:            current_user: current_user, target_user: current_user, params: declared_params(include_missing: false), organization_id: Current.organization&.id
lib/gitlab/current_settings.rb:34:          return ::Organizations::OrganizationSetting.for(::Current.organization&.id).send(name, *args, **kwargs, &block)
lib/gitlab/current_settings.rb:48:        ::Organizations::OrganizationSetting.for(::Current.organization&.id).respond_to?(name, include_private)

Intended side effects

With the introduction of Organization, we will need to rely on the presence of a Current Organization. Making this change increases the confidence our code works with new Organization data structure.

Impact

git grep "Current.organization&.id" reveals 34 usages in 30 different files

Edited Apr 04, 2025 by Rutger Wessels
Assignee Loading
Time tracking Loading