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

WIP - Processing PR: sync_single_file now private, representer with "next if" instead of unless

Frank Bergmanna16fa22b27a4
diff --git a/modules/storages/app/services/storages/file_link_sync_service.rb b/modules/storages/app/services/storages/file_link_sync_service.rb
index 9f0d760863f..4e21c91e7ff 100644
--- a/modules/storages/app/services/storages/file_link_sync_service.rb
+++ b/modules/storages/app/services/storages/file_link_sync_service.rb
@@ -52,37 +52,6 @@ class Storages::FileLinkSyncService
@service_result
end
- # Write the updated information from Nextcloud to a single file
- # @param storage Storage of the file
- # @param origin_file_id Nextcloud ID of the file
- # @param origin_file_info_hash Hash with updated information from Nextcloud
- # "24" => {
- # "id" : 24, # origin_file_id
- # "ctime" : 0, # Linux epoch file creation +overwrite
- # "mtime" : 1655301278, # Linux epoch file modification +overwrite
- # "mimetype" : "application/pdf", # +overwrite
- # "name" : "Nextcloud Manual.pdf", # "Canonical" name, could changed by owner +overwrite
- # "owner_id" : "admin", # ID at Nextcloud side +overwrite
- # "owner_name" : "admin", # Name at Nextcloud side +overwrite
- # "size" : 12706214, # Not used yet in OpenProject +overwrite
- # "status" : "OK", # Not used yet
- # "statuscode" : 200, # Not used yet
- # "trashed" : false # Exclude trashed files from result array of FileLinks
- # }
- # In case of permission errors (depending on the current user) we get:
- # "24" => { "status" => "Forbidden", "statuscode" => 403 }
- # In case of completely deleted file or other errors we might also get:
- # "24" = { "status" => "Not Found", "statuscode" => 404 }
- def sync_single_file(file_link, origin_file_info_hash)
- file_link.origin_mime_type = origin_file_info_hash["mimetype"]
- file_link.origin_created_by_name = origin_file_info_hash["owner_name"]
- file_link.origin_name = origin_file_info_hash["name"]
- file_link.origin_created_at = Time.zone.at(origin_file_info_hash["ctime"])
- file_link.origin_updated_at = Time.zone.at(origin_file_info_hash["mtime"])
-
- file_link
- end
-
private
# Get the OAuthClientToken that will authenticate us against Nextcloud
@@ -152,6 +121,37 @@ class Storages::FileLinkSyncService
end
end
+ # Write the updated information from Nextcloud to a single file
+ # @param storage Storage of the file
+ # @param origin_file_id Nextcloud ID of the file
+ # @param origin_file_info_hash Hash with updated information from Nextcloud
+ # "24" => {
+ # "id" : 24, # origin_file_id
+ # "ctime" : 0, # Linux epoch file creation +overwrite
+ # "mtime" : 1655301278, # Linux epoch file modification +overwrite
+ # "mimetype" : "application/pdf", # +overwrite
+ # "name" : "Nextcloud Manual.pdf", # "Canonical" name, could changed by owner +overwrite
+ # "owner_id" : "admin", # ID at Nextcloud side +overwrite
+ # "owner_name" : "admin", # Name at Nextcloud side +overwrite
+ # "size" : 12706214, # Not used yet in OpenProject +overwrite
+ # "status" : "OK", # Not used yet
+ # "statuscode" : 200, # Not used yet
+ # "trashed" : false # Exclude trashed files from result array of FileLinks
+ # }
+ # In case of permission errors (depending on the current user) we get:
+ # "24" => { "status" => "Forbidden", "statuscode" => 403 }
+ # In case of completely deleted file or other errors we might also get:
+ # "24" = { "status" => "Not Found", "statuscode" => 404 }
+ def sync_single_file(file_link, origin_file_info_hash)
+ file_link.origin_mime_type = origin_file_info_hash["mimetype"]
+ file_link.origin_created_by_name = origin_file_info_hash["owner_name"]
+ file_link.origin_name = origin_file_info_hash["name"]
+ file_link.origin_created_at = Time.zone.at(origin_file_info_hash["ctime"])
+ file_link.origin_updated_at = Time.zone.at(origin_file_info_hash["mtime"])
+
+ file_link
+ end
+
def set_error_for_file_links(storage_file_links)
@service_result.result += storage_file_links.each { |file_link| file_link.origin_permission = :error }
@service_result.success = false
diff --git a/modules/storages/lib/api/v3/file_links/file_link_representer.rb b/modules/storages/lib/api/v3/file_links/file_link_representer.rb
index bd6b9a6691d..06d137cafe6 100644
--- a/modules/storages/lib/api/v3/file_links/file_link_representer.rb
+++ b/modules/storages/lib/api/v3/file_links/file_link_representer.rb
@@ -87,7 +87,9 @@ module API
# Show a permission link only if we have actual permission information for a specific user
link :permission, uncacheable: true do
- PERMISSION_LINKS[represented.origin_permission] unless represented.origin_permission.nil?
+ next if represented.origin_permission.nil?
+
+ PERMISSION_LINKS[represented.origin_permission]
end
link :originOpen do
diff --git a/modules/storages/spec/services/storages/file_links/file_link_sync_service_spec.rb b/modules/storages/spec/services/storages/file_links/file_link_sync_service_spec.rb
index a0f50580593..e706debe013 100644
--- a/modules/storages/spec/services/storages/file_links/file_link_sync_service_spec.rb
+++ b/modules/storages/spec/services/storages/file_links/file_link_sync_service_spec.rb
@@ -76,19 +76,19 @@ describe ::Storages::FileLinkSyncService, type: :model do
let(:trashed) { false }
let(:file_links) { [file_link1] }
- # Nextcloud response part for valid file
+ # Nextcloud response for valid file, changing all origin_* attributes
let(:file_info1_s200) do
{
id: 24,
status: "OK",
statuscode: 200,
- name: "Nextcloud Manual.pdf",
- mtime: 1655301234,
- ctime: 1655334567,
- mimetype: "application/pdf",
- size: 12706214,
- owner_id: "admin",
- owner_name: "admin",
+ ctime: 1755334567, # -> origin_created_at
+ mtime: 1755301234, # -> origin_updated_at
+ mimetype: "application/text", # -> origin_mime_type
+ name: "Readme.txt", # -> origin_name
+ owner_id: "fraber_id",
+ owner_name: "fraber", # -> origin_created_by_name
+ size: 1270,
trashed:
}
end
@@ -127,11 +127,18 @@ describe ::Storages::FileLinkSyncService, type: :model do
.to_return(status: 200, headers: { 'Content-Type': 'application/json' }, body: response)
end
- it 'returns a successful ServiceResult with file_link1 and updated mtime' do
+ it 'updates all origin_* fields' do
expect(subject.success).to be_truthy
expect(subject.result.count).to be 1
+ expect(subject.result[0]).to be_a ::Storages::FileLink
+
+ # Check the detailed update result
expect(subject.result[0].origin_id).to eql '24'
- expect(subject.result[0].origin_updated_at).to eql Time.zone.at(1655301234) # updated
+ expect(subject.result[0].origin_created_at).to eql Time.zone.at(1755334567)
+ expect(subject.result[0].origin_updated_at).to eql Time.zone.at(1755301234)
+ expect(subject.result[0].origin_mime_type).to eql "application/text"
+ expect(subject.result[0].origin_name).to eql "Readme.txt"
+ expect(subject.result[0].origin_created_by_name).to eql "fraber"
end
end
@@ -216,7 +223,7 @@ describe ::Storages::FileLinkSyncService, type: :model do
it 'updates the file_link information' do
expect(subject).to be_a ServiceResult
expect(subject.success).to be_truthy
- expect(subject.result[0].origin_updated_at).to eql Time.zone.at(1655301234) # updated
+ expect(subject.result[0].origin_updated_at).to eql Time.zone.at(1755301234)
end
it 'updates the origin_permission' do
@@ -258,7 +265,7 @@ describe ::Storages::FileLinkSyncService, type: :model do
it 'updates the file_link information' do
expect(subject).to be_a ServiceResult
expect(subject.success).to be_truthy
- expect(subject.result[0].origin_updated_at).to eql Time.zone.at(1655301234) # updated
+ expect(subject.result[0].origin_updated_at).to eql Time.zone.at(1755301234)
expect(connection_manager).to have_received(:refresh_token).once
end
end
@@ -330,36 +337,4 @@ describe ::Storages::FileLinkSyncService, type: :model do
end
end
end
-
- # Test the update of fields of a single FileLink based on information returned from Nextcloud.
- describe '#sync_single_file' do
- let(:file_link1_all_modified_hash) do
- # Simulates Nextcloud update reply with strings ()and not symbols!) as Hash keys
- # We can simulate this using JSON.parse(response.to_json).
- JSON.parse({
- id: '24',
- ctime: 1755334567, # -> origin_created_at
- mtime: 1755301234, # -> origin_updated_at
- mimetype: "application/text", # -> origin_mime_type
- name: "Readme.txt", # -> origin_name
- owner_id: "fraber_id",
- owner_name: "fraber", # -> origin_created_by_name
- size: 1270,
- trashed: true
- }.to_json)
- end
-
- subject { instance.sync_single_file(file_link1, file_link1_all_modified_hash) }
-
- context 'with updated information for all FileLinks (happy path)' do
- it 'updates all relevant fields and leaves the list of file_links unchanged' do
- expect(subject).to be_a ::Storages::FileLink
- expect(subject.origin_created_at).to eql Time.zone.at(1755334567)
- expect(subject.origin_updated_at).to eql Time.zone.at(1755301234)
- expect(subject.origin_mime_type).to eql "application/text"
- expect(subject.origin_name).to eql "Readme.txt"
- expect(subject.origin_created_by_name).to eql "fraber"
- end
- end
- end
end