Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Updates the CopyJob and tests to rely on GoodJob::Batch

Marcello Rocha3e32aa880e7e
diff --git a/app/services/projects/enqueue_copy_service.rb b/app/services/projects/enqueue_copy_service.rb
index 7fc088d4027..705e0c7f724 100644
--- a/app/services/projects/enqueue_copy_service.rb
+++ b/app/services/projects/enqueue_copy_service.rb
@@ -60,11 +60,15 @@ module Projects
##
# Schedule the project copy job
def schedule_copy_job(params)
- CopyProjectJob.perform_later(user_id: user.id,
- source_project_id: source.id,
- target_project_params: params[:target_project_params],
- associations_to_copy: params[:only].to_a,
- send_mails: ActiveRecord::Type::Boolean.new.cast(params[:send_notifications]))
+ job = nil
+ GoodJob::Batch.enqueue(user:) do
+ job = CopyProjectJob.perform_later(user_id: user.id,
+ source_project_id: source.id,
+ target_project_params: params[:target_project_params],
+ associations_to_copy: params[:only].to_a,
+ send_mails: ActiveRecord::Type::Boolean.new.cast(params[:send_notifications]))
+ end
+ job
end
end
end
diff --git a/app/workers/copy_project_job.rb b/app/workers/copy_project_job.rb
index b983393537d..cefe8158fab 100644
--- a/app/workers/copy_project_job.rb
+++ b/app/workers/copy_project_job.rb
@@ -29,15 +29,19 @@
class CopyProjectJob < ApplicationJob
queue_with_priority :above_normal
include OpenProject::LocaleHelper
+ include GoodJob::ActiveJobExtensions::Batches
- attr_reader :user_id,
- :source_project_id,
- :target_project_params,
- :target_project_name,
- :target_project,
- :errors,
- :associations_to_copy,
- :send_mails
+ queue_with_priority :above_normal
+
+ attr_reader :errors, :target_project, :target_project_params
+
+ # attr_reader :source_project_id,
+ # :target_project_params,
+ # :target_project_name,
+ # :target_project,
+ # :errors,
+ # :associations_to_copy,
+ # :send_mails
def perform(user_id:,
source_project_id:,
@@ -46,11 +50,10 @@ class CopyProjectJob < ApplicationJob
send_mails: false)
# Needs refactoring after moving to activejob
- @user_id = user_id
- @source_project_id = source_project_id
+ @source_project_id = source_project_id
@target_project_params = target_project_params.with_indifferent_access
- @associations_to_copy = associations_to_copy
- @send_mails = send_mails
+ @associations_to_copy = associations_to_copy
+ @send_mails = send_mails
User.current = user
@target_project_name = target_project_params[:name]
@@ -59,18 +62,21 @@ class CopyProjectJob < ApplicationJob
create_project_copy
end
+ batch.properties.merge!(target_project:, errors:)
+ batch.save
+
if target_project
successful_status_update
ProjectMailer.copy_project_succeeded(user, source_project, target_project, errors).deliver_later
else
failure_status_update
- ProjectMailer.copy_project_failed(user, source_project, target_project_name, errors).deliver_later
+ ProjectMailer.copy_project_failed(user, source_project, @target_project_name, errors).deliver_later
end
rescue StandardError => e
logger.error { "Failed to finish copy project job: #{e} #{e.message}" }
errors = [I18n.t("copy_project.failed_internal")]
failure_status_update
- ProjectMailer.copy_project_failed(user, source_project, target_project_name, errors).deliver_later
+ ProjectMailer.copy_project_failed(user, source_project, @target_project_name, errors).deliver_later
end
def store_status?
@@ -124,17 +130,17 @@ class CopyProjectJob < ApplicationJob
end
def user
- @user ||= User.find user_id
+ @user ||= batch.properties[:user]
end
def source_project
- @source_project ||= Project.find source_project_id
+ @source_project ||= Project.find @source_project_id
end
def create_project_copy
errors = []
- ProjectMailer.with_deliveries(send_mails) do
+ ProjectMailer.with_deliveries(@send_mails) do
service_call = copy_project
target_project = service_call.result
errors = service_call.errors.full_messages
@@ -149,13 +155,15 @@ class CopyProjectJob < ApplicationJob
end
rescue ActiveRecord::RecordNotFound => e
logger.error("Entity missing: #{e.message} #{e.backtrace.join("\n")}")
+ raise e
rescue StandardError => e
logger.error("Encountered an error when trying to copy project " \
- "'#{source_project_id}' : #{e.message} #{e.backtrace.join("\n")}")
+ "'#{@source_project_id}' : #{e.message} #{e.backtrace.join("\n")}")
+ raise e
ensure
unless errors.empty?
logger.error("Encountered an errors while trying to copy related objects for " \
- "project '#{source_project_id}': #{errors.inspect}")
+ "project '#{@source_project_id}': #{errors.inspect}")
end
end
@@ -166,8 +174,8 @@ class CopyProjectJob < ApplicationJob
end
def copy_project_params
- params = { target_project_params:, send_notifications: send_mails }
- params[:only] = associations_to_copy if associations_to_copy.present?
+ params = { target_project_params:, send_notifications: @send_mails }
+ params[:only] = @associations_to_copy if @associations_to_copy.present?
params
end
diff --git a/spec/requests/api/v3/projects/copy/copy_resource_spec.rb b/spec/requests/api/v3/projects/copy/copy_resource_spec.rb
index 388df496080..37ab48d5b10 100644
--- a/spec/requests/api/v3/projects/copy/copy_resource_spec.rb
+++ b/spec/requests/api/v3/projects/copy/copy_resource_spec.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2024 the OpenProject GmbH
@@ -28,7 +30,7 @@
require "spec_helper"
require "rack/test"
-RSpec.describe API::V3::Projects::Copy::CopyAPI, content_type: :json do
+RSpec.describe "API::V3::Projects::Copy::CopyAPI", content_type: :json, with_good_job_batches: [CopyProjectJob] do
include Rack::Test::Methods
include API::V3::Utilities::PathHelper
@@ -69,6 +71,8 @@ RSpec.describe API::V3::Projects::Copy::CopyAPI, content_type: :json do
subject(:response) { last_response }
+ # rubocop:disable RSpec/Rails/HaveHttpStatus
+ # those are mock responses that don't deal well with the rails helpers
describe "#POST /api/v3/projects/:id/copy" do
describe "with empty params" do
it "returns 422", :aggregate_failures do
@@ -104,17 +108,17 @@ RSpec.describe API::V3::Projects::Copy::CopyAPI, content_type: :json do
get response.location
- expect(last_response.status).to eq 200
+ expect(last_response.status).to eq(200)
expect(last_response.body)
.to be_json_eql("in_queue".to_json)
.at_path("status")
- perform_enqueued_jobs
+ GoodJob.perform_inline
get response.location
- expect(last_response.status).to eq 200
+ expect(last_response.status).to eq(200)
expect(last_response.body)
.to be_json_eql("success".to_json)
@@ -143,7 +147,7 @@ RSpec.describe API::V3::Projects::Copy::CopyAPI, content_type: :json do
end
it "does not copy the wiki" do
- perform_enqueued_jobs
+ GoodJob.perform_inline
project = Project.find_by(identifier: "my-copied-project")
expect(project).to be_present
@@ -169,10 +173,10 @@ RSpec.describe API::V3::Projects::Copy::CopyAPI, content_type: :json do
let(:sendNotifications) { false }
it "queues the job without notifications" do
- expect(CopyProjectJob)
- .to have_been_enqueued.with do |args|
- expect(args[:send_mails]).to be false
- end
+ job = GoodJob::Job.where(job_class: "CopyProjectJob").last
+ enqueue_params = job.serialized_params["arguments"][0]
+
+ expect(enqueue_params["send_mails"]).to be_falsey
end
end
@@ -180,10 +184,10 @@ RSpec.describe API::V3::Projects::Copy::CopyAPI, content_type: :json do
let(:sendNotifications) { true }
it "queues the job with notifications" do
- expect(CopyProjectJob)
- .to have_been_enqueued.with do |args|
- expect(args[:send_mails]).to be true
- end
+ job = GoodJob::Job.where(job_class: "CopyProjectJob").last
+ enqueue_params = job.serialized_params["arguments"][0]
+
+ expect(enqueue_params["send_mails"]).to be_truthy
end
end
end
@@ -199,4 +203,5 @@ RSpec.describe API::V3::Projects::Copy::CopyAPI, content_type: :json do
end
end
end
+ # rubocop:enable RSpec/Rails/HaveHttpStatus
end
diff --git a/spec/workers/copy_project_job_spec.rb b/spec/workers/copy_project_job_spec.rb
index 16673761b35..1f2308e4e27 100644
--- a/spec/workers/copy_project_job_spec.rb
+++ b/spec/workers/copy_project_job_spec.rb
@@ -28,50 +28,24 @@
require "spec_helper"
-RSpec.describe CopyProjectJob, type: :model do
+RSpec.describe CopyProjectJob, type: :model, with_good_job_batches: [CopyProjectJob] do
let(:params) { { name: "Copy", identifier: "copy" } }
- let(:maildouble) { double("Mail::Message", deliver: true) }
+ let(:user_de) { create(:admin, language: :de) }
+ let(:mail_double) { double("Mail::Message", deliver: true) } # rubocop:disable RSpec/VerifiedDoubles
- before do
- allow(maildouble).to receive(:deliver_later)
- end
-
- describe "copy localizes error message" do
- let(:user_de) { create(:admin, language: :de) }
- let(:source_project) { create(:project) }
- let(:target_project) { create(:project) }
-
- let(:copy_job) do
- described_class.new
- end
-
- it "sets locale correctly" do
- expect(copy_job)
- .to receive(:create_project_copy)
- .and_wrap_original do |m, *args, &block|
- expect(I18n.locale).to eq(:de)
- m.call(*args, &block)
- end
-
- copy_job.perform user_id: user_de.id,
- source_project_id: source_project.id,
- target_project_params: {},
- associations_to_copy: []
- end
- end
+ before { allow(mail_double).to receive(:deliver_later) }
describe "copy project succeeds with errors" do
let(:admin) { create(:admin) }
let(:source_project) { create(:project, types: [type]) }
+
let!(:work_package) { create(:work_package, project: source_project, type:) }
+
let(:type) { create(:type_bug) }
let(:custom_field) do
- create(:work_package_custom_field,
- name: "required_field",
- field_format: "text",
- is_required: true,
- is_for_all: true)
+ create(:work_package_custom_field, name: "required_field", field_format: "text", is_required: true, is_for_all: true)
end
+
let(:job_args) do
{
user_id: admin.id,
@@ -80,40 +54,50 @@ RSpec.describe CopyProjectJob, type: :model do
associations_to_copy: [:work_packages]
}
end
- let(:copy_job) do
- described_class.new(**job_args).tap(&:perform_now)
- end
let(:params) { { name: "Copy", identifier: "copy", type_ids: [type.id], work_package_custom_field_ids: [custom_field.id] } }
let(:expected_error_message) do
"#{WorkPackage.model_name.human} '#{work_package.type.name} ##{work_package.id}: #{work_package.subject}': #{custom_field.name} #{I18n.t('errors.messages.blank')}."
end
- # rubocop:disable RSpec/InstanceVariable
before do
source_project.work_package_custom_fields << custom_field
type.custom_fields << custom_field
-
- allow(User).to receive(:current).and_return(admin)
-
- @copied_project = copy_job.target_project
- @errors = copy_job.errors
end
it "copies the project", :aggregate_failures do
- expect(Project.find_by(identifier: params[:identifier])).to eq(@copied_project)
- expect(@errors.first).to eq(expected_error_message)
+ copy_job = nil
+ batch = GoodJob::Batch.enqueue(user: admin) do
+ copy_job = described_class.perform_later(**job_args)
+ end
+ GoodJob.perform_inline
+ batch.reload
+
+ copied_project = Project.find_by(identifier: params[:identifier])
+
+ expect(copied_project).to eq(batch.properties[:target_project])
+ expect(batch.properties[:errors].first).to eq(expected_error_message)
# expect to create a status
expect(copy_job.job_status).to be_present
expect(copy_job.job_status[:status]).to eq "success"
expect(copy_job.job_status[:payload]["redirect"]).to include "/projects/copy"
- expected_link = { "href" => "/api/v3/projects/#{@copied_project.id}", "title" => @copied_project.name }
+ expected_link = { "href" => "/api/v3/projects/#{copied_project.id}", "title" => copied_project.name }
expect(copy_job.job_status[:payload]["_links"]["project"]).to eq(expected_link)
end
+
+ it "ensures that error messages are correctly localized" do
+ batch = GoodJob::Batch.enqueue(user: user_de) do
+ described_class.perform_later(**job_args)
+ end
+ GoodJob.perform_inline
+ batch.reload
+
+ msg = /Arbeitspaket 'Bug #\d+: WorkPackage No. \d+': required_field muss ausgefüllt werden\./
+ expect(batch.properties[:errors].first).to match(msg)
+ end
end
- # rubocop:enable RSpec/InstanceVariable
describe "project has an invalid repository" do
let(:admin) { create(:admin) }
@@ -143,8 +127,18 @@ RSpec.describe CopyProjectJob, type: :model do
it "saves without the repository" do
expect(source_project).not_to be_valid
- copied_project = copy_job.target_project
- errors = copy_job.errors
+ batch = GoodJob::Batch.enqueue(user: admin) do
+ described_class.perform_later(user_id: nil,
+ source_project_id: source_project.id,
+ target_project_params: params,
+ associations_to_copy: [:work_packages])
+ end
+
+ GoodJob.perform_inline
+ batch.reload
+
+ copied_project = batch.properties[:target_project]
+ errors = batch.properties[:errors]
expect(errors).to be_empty
expect(copied_project).to be_valid
@@ -156,15 +150,6 @@ RSpec.describe CopyProjectJob, type: :model do
describe "copy project fails with internal error" do
let(:admin) { create(:admin) }
let(:source_project) { create(:project) }
- let(:copy_job) do
- described_class.new.tap do |job|
- job.perform user_id: admin.id,
- source_project_id: source_project.id,
- target_project_params: params,
- associations_to_copy: [:work_packages]
- end
- end
-
let(:params) { { name: "Copy", identifier: "copy" } }
before do
@@ -173,12 +158,20 @@ RSpec.describe CopyProjectJob, type: :model do
end
it "renders a error when unexpected errors occur" do
+ copy_job = nil
+ GoodJob::Batch.enqueue(user: admin) do
+ copy_job = described_class.perform_later(user_id: admin.id,
+ source_project_id: source_project.id,
+ target_project_params: params,
+ associations_to_copy: [:work_packages])
+ end
+
expect(ProjectMailer)
.to receive(:copy_project_failed)
.with(admin, source_project, "Copy", [I18n.t("copy_project.failed_internal")])
- .and_return maildouble
+ .and_return mail_double
- expect { copy_job }.not_to raise_error
+ GoodJob.perform_inline
# expect to create a status
expect(copy_job.job_status).to be_present
@@ -211,34 +204,39 @@ RSpec.describe CopyProjectJob, type: :model do
.call(%i[estimated_hours remaining_hours ignore_non_working_days])
end
- it "copies the project without any errord (Bug #52384)" do
+ it "copies the project without any errors (Bug #52384)" do
allow(OpenProject.logger).to receive(:error)
- copy_job = described_class.new
- copy_job.perform user_id: admin.id,
- source_project_id: source_project.id,
- target_project_params: params,
- associations_to_copy: [:work_packages]
+ copy_job = nil
+ batch = GoodJob::Batch.enqueue(user: admin) do
+ copy_job = described_class.perform_later(user_id: nil,
+ source_project_id: source_project.id,
+ target_project_params: params,
+ associations_to_copy: [:work_packages])
+ end
+ GoodJob.perform_inline
+ batch.reload
expect(copy_job.job_status.status).to eq "success"
- expect(copy_job.errors).to be_empty
+ expect(batch.properties[:errors]).to be_empty
expect(OpenProject.logger).not_to have_received(:error)
end
end
- describe "perform" do
+ describe "#perform" do
let(:project) { create(:project, public: false) }
let(:user) { create(:user) }
let(:role) { create(:project_role, permissions: [:copy_projects]) }
- shared_context "copy project" do
+ shared_context "on copy project" do
before do
- described_class.new.tap do |job|
- job.perform user_id: user.id,
- source_project_id: project_to_copy.id,
- target_project_params: params,
- associations_to_copy: [:members]
+ GoodJob::Batch.enqueue(user:) do
+ described_class.perform_later user_id: user.id,
+ source_project_id: project_to_copy.id,
+ target_project_params: params,
+ associations_to_copy: [:members]
end
+ GoodJob.perform_inline
end
end
@@ -261,7 +259,7 @@ RSpec.describe CopyProjectJob, type: :model do
subject { Project.find_by(identifier: "copy") }
describe "user without add_subprojects permission in parent" do
- include_context "copy project" do
+ include_context "on copy project" do
let(:project_to_copy) { subproject }
end
@@ -287,7 +285,7 @@ RSpec.describe CopyProjectJob, type: :model do
describe "user without add_subprojects permission in parent and when explicitly setting that parent" do
let(:params) { { name: "Copy", identifier: "copy", parent_id: project.id } }
- include_context "copy project" do
+ include_context "on copy project" do
let(:project_to_copy) { subproject }
end
@@ -318,7 +316,7 @@ RSpec.describe CopyProjectJob, type: :model do
member_add_subproject
end
- include_context "copy project" do
+ include_context "on copy project" do
let(:project_to_copy) { subproject }
end