Skip to content
Snippets Groups Projects
Commit f9237e2d authored by Thong Kuah's avatar Thong Kuah :speech_balloon:
Browse files

Fix CodeReuse/ActiveRecord offenses in workers

Use the allowed find_by_id method instead of find_by(id: 1).
parent fd23cb31
No related branches found
No related tags found
No related merge requests found
Showing
with 26 additions and 56 deletions
Loading
Loading
@@ -25,11 +25,9 @@ def self.bulk_perform_inline(args_list)
end
end
 
# rubocop: disable CodeReuse/ActiveRecord
def perform(user_id)
user = User.find_by(id: user_id)
user = User.find_by_id(user_id)
 
user&.refresh_authorized_projects(source: self.class.name)
end
# rubocop: enable CodeReuse/ActiveRecord
end
Loading
Loading
@@ -14,7 +14,7 @@ class BuildHooksWorker # rubocop:disable Scalability/IdempotentWorker
# rubocop: disable CodeReuse/ActiveRecord
def perform(build_id)
Ci::Build.includes({ runner: :tags })
.find_by(id: build_id)
.find_by_id(build_id)
.try(:execute_hooks)
end
# rubocop: enable CodeReuse/ActiveRecord
Loading
Loading
Loading
Loading
@@ -12,11 +12,9 @@ class BuildQueueWorker # rubocop:disable Scalability/IdempotentWorker
worker_resource_boundary :cpu
data_consistency :sticky
 
# rubocop: disable CodeReuse/ActiveRecord
def perform(build_id)
Ci::Build.find_by(id: build_id).try do |build|
Ci::Build.find_by_id(build_id).try do |build|
Ci::UpdateBuildQueueService.new.tick(build)
end
end
# rubocop: enable CodeReuse/ActiveRecord
end
Loading
Loading
@@ -11,13 +11,11 @@ class BuildSuccessWorker # rubocop:disable Scalability/IdempotentWorker
queue_namespace :pipeline_processing
urgency :high
 
# rubocop: disable CodeReuse/ActiveRecord
def perform(build_id)
Ci::Build.find_by(id: build_id).try do |build|
Ci::Build.find_by_id(build_id).try do |build|
stop_environment(build) if build.stops_environment?
end
end
# rubocop: enable CodeReuse/ActiveRecord
 
private
 
Loading
Loading
Loading
Loading
@@ -16,9 +16,8 @@ class ChatNotificationWorker # rubocop:disable Scalability/IdempotentWorker
RESCHEDULE_INTERVAL = 2.seconds
RESCHEDULE_TIMEOUT = 5.minutes
 
# rubocop: disable CodeReuse/ActiveRecord
def perform(build_id, reschedule_count = 0)
Ci::Build.find_by(id: build_id).try do |build|
Ci::Build.find_by_id(build_id).try do |build|
send_response(build)
end
rescue Gitlab::Chat::Output::MissingBuildSectionError
Loading
Loading
@@ -30,7 +29,6 @@ def perform(build_id, reschedule_count = 0)
# the job instead of producing an error.
self.class.perform_in(RESCHEDULE_INTERVAL, build_id, reschedule_count + 1)
end
# rubocop: enable CodeReuse/ActiveRecord
 
def send_response(build)
Gitlab::Chat::Responder.responder_for(build).try do |responder|
Loading
Loading
Loading
Loading
@@ -9,12 +9,10 @@ class ArchiveTraceWorker # rubocop:disable Scalability/IdempotentWorker
sidekiq_options retry: 3
include PipelineBackgroundQueue
 
# rubocop: disable CodeReuse/ActiveRecord
def perform(job_id)
Ci::Build.without_archived_trace.find_by(id: job_id).try do |job|
Ci::Build.without_archived_trace.find_by_id(job_id).try do |job|
Ci::ArchiveTraceService.new.execute(job, worker_name: self.class.name)
end
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
Loading
Loading
@@ -16,7 +16,7 @@ class BuildFinishedWorker # rubocop:disable Scalability/IdempotentWorker
ARCHIVE_TRACES_IN = 2.minutes.freeze
 
def perform(build_id)
return unless build = Ci::Build.find_by(id: build_id) # rubocop: disable CodeReuse/ActiveRecord
return unless build = Ci::Build.find_by_id(build_id)
return unless build.project
return if build.project.pending_delete?
 
Loading
Loading
Loading
Loading
@@ -13,12 +13,10 @@ class BuildTraceChunkFlushWorker
 
idempotent!
 
# rubocop: disable CodeReuse/ActiveRecord
def perform(id)
::Ci::BuildTraceChunk.find_by(id: id).try do |chunk|
::Ci::BuildTraceChunk.find_by_id(id).try do |chunk|
chunk.persist_data!
end
end
# rubocop: enable CodeReuse/ActiveRecord
end
end
Loading
Loading
@@ -26,16 +26,14 @@ def perform(app_name, app_id, project_id, scheduled_time)
 
private
 
# rubocop: disable CodeReuse/ActiveRecord
def execute(app_name, app_id, project_id, scheduled_time)
project = Project.find_by(id: project_id)
project = Project.find_by_id(project_id)
return unless project
 
find_application(app_name, app_id) do |app|
update_prometheus(app, scheduled_time, project)
end
end
# rubocop: enable CodeReuse/ActiveRecord
 
def update_prometheus(app, scheduled_time, project)
return unless app.managed_prometheus?
Loading
Loading
Loading
Loading
@@ -8,9 +8,8 @@ module ReschedulingMethods
# project_id - The ID of the GitLab project to import the note into.
# hash - A Hash containing the details of the GitHub object to import.
# notify_key - The Redis key to notify upon completion, if any.
# rubocop: disable CodeReuse/ActiveRecord
def perform(project_id, hash, notify_key = nil)
project = Project.find_by(id: project_id)
project = Project.find_by_id(project_id)
 
return notify_waiter(notify_key) unless project
 
Loading
Loading
@@ -25,7 +24,6 @@ def perform(project_id, hash, notify_key = nil)
.perform_in(client.rate_limit_resets_in, project.id, hash, notify_key)
end
end
# rubocop: enable CodeReuse/ActiveRecord
 
def try_import(*args)
import(*args)
Loading
Loading
Loading
Loading
@@ -33,13 +33,13 @@ def try_import(client, project)
self.class.perform_in(client.rate_limit_resets_in, project.id)
end
 
# rubocop: disable CodeReuse/ActiveRecord
def find_project(id)
# If the project has been marked as failed we want to bail out
# automatically.
Project.joins_import_state.where(import_state: { status: :started }).find_by(id: id)
# rubocop: disable CodeReuse/ActiveRecord
Project.joins_import_state.where(import_state: { status: :started }).find_by_id(id)
# rubocop: enable CodeReuse/ActiveRecord
end
# rubocop: enable CodeReuse/ActiveRecord
 
def abort_on_failure
false
Loading
Loading
Loading
Loading
@@ -14,7 +14,7 @@ module ImportWorker
end
 
def perform(project_id)
project = Project.find_by(id: project_id) # rubocop: disable CodeReuse/ActiveRecord
project = Project.find_by_id(project_id)
 
return unless can_import?(project)
 
Loading
Loading
Loading
Loading
@@ -10,21 +10,17 @@ def objects_found?(issuable_id, user_id)
user && issuable
end
 
# rubocop: disable CodeReuse/ActiveRecord
def set_user(user_id)
@user = User.find_by(id: user_id) # rubocop:disable Gitlab/ModuleWithInstanceVariables
@user = User.find_by_id(user_id) # rubocop:disable Gitlab/ModuleWithInstanceVariables
 
log_error(User, user_id) unless @user # rubocop:disable Gitlab/ModuleWithInstanceVariables
end
# rubocop: enable CodeReuse/ActiveRecord
 
# rubocop: disable CodeReuse/ActiveRecord
def set_issuable(issuable_id)
@issuable = issuable_class.find_by(id: issuable_id) # rubocop:disable Gitlab/ModuleWithInstanceVariables
@issuable = issuable_class.find_by_id(issuable_id) # rubocop:disable Gitlab/ModuleWithInstanceVariables
 
log_error(issuable_class, issuable_id) unless @issuable # rubocop:disable Gitlab/ModuleWithInstanceVariables
end
# rubocop: enable CodeReuse/ActiveRecord
 
def log_error(record_class, record_id)
Gitlab::AppLogger.error("#{self.class}: couldn't find #{record_class} with ID=#{record_id}, skipping job")
Loading
Loading
Loading
Loading
@@ -12,7 +12,6 @@ class CreateCommitSignatureWorker
idempotent!
loggable_arguments 0
 
# rubocop: disable CodeReuse/ActiveRecord
def perform(commit_shas, project_id)
# Older versions of Git::BranchPushService may push a single commit ID on
# the stack. We need this to be backwards compatible.
Loading
Loading
@@ -20,7 +19,7 @@ def perform(commit_shas, project_id)
 
return if commit_shas.empty?
 
project = Project.find_by(id: project_id)
project = Project.find_by_id(project_id)
return unless project
 
commits = project.commits_by(oids: commit_shas)
Loading
Loading
@@ -44,5 +43,4 @@ def perform(commit_shas, project_id)
Gitlab::AppLogger.error("Failed to create signature for commit #{commit.id}. Error: #{e.message}")
end
end
# rubocop: enable CodeReuse/ActiveRecord
end
Loading
Loading
@@ -15,10 +15,9 @@ class DeleteContainerRepositoryWorker # rubocop:disable Scalability/IdempotentWo
 
attr_reader :container_repository
 
# rubocop: disable CodeReuse/ActiveRecord
def perform(current_user_id, container_repository_id)
current_user = User.find_by(id: current_user_id)
@container_repository = ContainerRepository.find_by(id: container_repository_id)
current_user = User.find_by_id(current_user_id)
@container_repository = ContainerRepository.find_by_id(container_repository_id)
project = container_repository&.project
 
return unless current_user && container_repository && project
Loading
Loading
@@ -29,7 +28,6 @@ def perform(current_user_id, container_repository_id)
Projects::ContainerRepository::DestroyService.new(project, current_user).execute(container_repository)
end
end
# rubocop: enable CodeReuse/ActiveRecord
 
# For ExclusiveLeaseGuard concern
def lease_key
Loading
Loading
Loading
Loading
@@ -14,16 +14,14 @@ class DetectRepositoryLanguagesWorker # rubocop:disable Scalability/IdempotentWo
 
attr_reader :project
 
# rubocop: disable CodeReuse/ActiveRecord
def perform(project_id, user_id = nil)
@project = Project.find_by(id: project_id)
@project = Project.find_by_id(project_id)
return unless project
 
try_obtain_lease do
::Projects::DetectRepositoryLanguagesService.new(project).execute
end
end
# rubocop: enable CodeReuse/ActiveRecord
 
private
 
Loading
Loading
Loading
Loading
@@ -9,17 +9,17 @@ class ExpireBuildInstanceArtifactsWorker # rubocop:disable Scalability/Idempoten
 
feature_category :build_artifacts
 
# rubocop: disable CodeReuse/ActiveRecord
def perform(build_id)
# rubocop: disable CodeReuse/ActiveRecord
build = Ci::Build
.with_expired_artifacts
.reorder(nil)
.find_by(id: build_id)
.find_by_id(build_id)
# rubocop: enable CodeReuse/ActiveRecord
 
return unless build&.project && !build.project.pending_delete
 
Gitlab::AppLogger.info("Removing artifacts for build #{build.id}...")
build.erase_erasable_artifacts!
end
# rubocop: enable CodeReuse/ActiveRecord
end
Loading
Loading
@@ -14,9 +14,8 @@ class ExpireJobCacheWorker # rubocop:disable Scalability/IdempotentWorker
deduplicate :until_executing, including_scheduled: true
idempotent!
 
# rubocop: disable CodeReuse/ActiveRecord
def perform(job_id)
job = CommitStatus.preload(:pipeline, :project).find_by(id: job_id)
job = CommitStatus.preload(:pipeline, :project).find_by_id(job_id) # rubocop: disable CodeReuse/ActiveRecord
return unless job
 
pipeline = job.pipeline
Loading
Loading
@@ -25,7 +24,6 @@ def perform(job_id)
Gitlab::EtagCaching::Store.new.touch(project_job_path(project, job))
ExpirePipelineCacheWorker.perform_async(pipeline.id)
end
# rubocop: enable CodeReuse/ActiveRecord
 
private
 
Loading
Loading
Loading
Loading
@@ -17,13 +17,11 @@ class ExpirePipelineCacheWorker
# Uncomment once https://gitlab.com/gitlab-org/gitlab/-/issues/325291 is resolved
# idempotent!
 
# rubocop: disable CodeReuse/ActiveRecord
def perform(pipeline_id)
pipeline = Ci::Pipeline.find_by(id: pipeline_id)
pipeline = Ci::Pipeline.find_by_id(pipeline_id)
return unless pipeline
 
Ci::ExpirePipelineCacheService.new.execute(pipeline)
end
# rubocop: enable CodeReuse/ActiveRecord
end
# rubocop:enable Scalability/IdempotentWorker
Loading
Loading
@@ -16,7 +16,7 @@ class StartImportWorker # rubocop:disable Scalability/IdempotentWorker
attr_reader :project
 
def perform(project_id)
@project = Project.find_by(id: project_id) # rubocop: disable CodeReuse/ActiveRecord
@project = Project.find_by_id(project_id)
 
return unless start_import
 
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment