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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[#43692] Modify API to be able to handle file_links without a container.
Pavel Balashovd78dabbd2a6d
diff --git a/app/contracts/work_packages/create_contract.rb b/app/contracts/work_packages/create_contract.rbindex 4786eab80bd..461c291fe7a 100644--- a/app/contracts/work_packages/create_contract.rb+++ b/app/contracts/work_packages/create_contract.rb@@ -38,14 +38,20 @@ module WorkPackagesdefault_attribute_permission :add_work_packagesvalidate :user_allowed_to_add+ validate :user_allowed_to_manage_file_linksprivatedef user_allowed_to_addif (model.project && [email protected]_to?(:add_work_packages, model.project)) ||[email protected]_to_globally?(:add_work_packages)+ errors.add(:base, :error_unauthorized)+ end+ end- errors.add :base, :error_unauthorized+ def user_allowed_to_manage_file_links+ if model.file_links.present? && model.project.present? && !user.allowed_to?(:manage_file_links, model.project)+ errors.add(:base, :error_unauthorized)endenddiff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rbindex 905566821eb..5e56ca961dc 100644--- a/app/services/work_packages/set_attributes_service.rb+++ b/app/services/work_packages/set_attributes_service.rb@@ -26,12 +26,15 @@# See COPYRIGHT and LICENSE files for more details.#++-class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes+class WorkPackages::SetAttributesService < BaseServices::SetAttributesinclude Attachments::SetReplacementsprivatedef set_attributes(attributes)+ file_links_ids = attributes.delete(:file_links_ids)+ model.file_links = Storages::FileLink.where(id: file_links_ids) if file_links_ids+set_attachments_attributes(attributes)set_static_attributes(attributes)@@ -382,6 +385,7 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributesmax = max_child_datereturn unless max+days.duration(min_child_date, max_child_date)enddiff --git a/docs/api/apiv3/components/schemas/work_package_model.yml b/docs/api/apiv3/components/schemas/work_package_model.ymlindex 766c83a0c0a..ba93583b9af 100644--- a/docs/api/apiv3/components/schemas/work_package_model.yml+++ b/docs/api/apiv3/components/schemas/work_package_model.yml@@ -24,7 +24,7 @@ properties:readOnly: truedescription:allOf:- - "$ref": "./formattable.yml"+ - $ref: "./formattable.yml"- description: The work package descriptionscheduleManually:type: boolean@@ -120,7 +120,7 @@ properties:properties:addAttachment:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-Attach a file to the WP@@ -130,7 +130,7 @@ properties:readOnly: trueaddComment:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-Post comment to WP@@ -140,7 +140,7 @@ properties:readOnly: trueaddRelation:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-Adds a relation to this work package.@@ -150,7 +150,7 @@ properties:readOnly: trueaddWatcher:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-Add any user to WP watchers@@ -163,7 +163,7 @@ properties:readOnly: trueitems:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-A predefined action that can be applied to the work package.@@ -171,13 +171,13 @@ properties:readOnly: truepreviewMarkup:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: Post markup (in markdown) here to receive an HTML-renderedresponsereadOnly: trueremoveWatcher:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-Remove any user from WP watchers@@ -187,7 +187,7 @@ properties:readOnly: trueunwatch:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-Remove current user from WP watchers@@ -197,7 +197,7 @@ properties:readOnly: trueupdate:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-Form endpoint that aids in preparing and performing edits on a WP@@ -207,7 +207,7 @@ properties:readOnly: trueupdateImmediately:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-Directly perform edits on a work package@@ -217,7 +217,7 @@ properties:readOnly: truewatch:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-Add current user to WP watchers@@ -227,7 +227,7 @@ properties:readOnly: trueself:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-This work package@@ -235,7 +235,7 @@ properties:readOnly: trueschema:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-The schema of this work package@@ -246,7 +246,7 @@ properties:readOnly: trueitems:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-A visible ancestor work package of the current work package.@@ -258,14 +258,14 @@ properties:readOnly: trueattachments:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-The files attached to this work package**Resource**: Collectionauthor:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-The person that created the work package@@ -273,14 +273,14 @@ properties:readOnly: trueassignee:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-The person that is intended to work on the work package**Resource**: UseravailableWatchers:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-All users that can be added to the work package as watchers.@@ -292,7 +292,7 @@ properties:readOnly: truebudget:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-The budget this work package is associated to@@ -303,7 +303,7 @@ properties:**Permission** view cost objectscategory:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-The category of the work package@@ -313,7 +313,7 @@ properties:readOnly: trueitems:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-A visible child work package of the current work package.@@ -343,35 +343,35 @@ properties:**Permission**: view file linksparent:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-Parent work package**Resource**: WorkPackagepriority:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-The priority of the work package**Resource**: Priorityproject:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-The project to which the work package belongs**Resource**: Projectresponsible:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-The person that is responsible for the overall outcome**Resource**: Userrelations:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-Relations this work package is involved in@@ -383,7 +383,7 @@ properties:readOnly: truerevisions:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-Revisions that are referencing the work package@@ -395,14 +395,14 @@ properties:readOnly: truestatus:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-The current status of the work package**Resource**: StatustimeEntries:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-All time entries logged on the work package. Please note that this is a link to an HTML resource for now and as such, the link is subject to change.@@ -414,21 +414,21 @@ properties:readOnly: truetype:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-The type of the work package**Resource**: Typeversion:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-The version associated to the work package**Resource**: Versionwatchers:allOf:- - "$ref": "./link.yml"+ - $ref: "./link.yml"- description: |-All users that are currently watching this work packagediff --git a/docs/api/apiv3/paths/work_packages.yml b/docs/api/apiv3/paths/work_packages.ymlindex a318f155c93..f45f79f1821 100644--- a/docs/api/apiv3/paths/work_packages.yml+++ b/docs/api/apiv3/paths/work_packages.yml@@ -108,8 +108,7 @@ get:required: falseschema:type: string- - description: Indicates whether properties should be summed up if they support- it.+ - description: Indicates whether properties should be summed up if they support it.example: truein: queryname: showSums@@ -150,7 +149,7 @@ get:post:summary: Create Work Package- operationId: Create_Work_Package+ operationId: create_work_packagetags:- Work Packagesdescription: |-diff --git a/lib/api/v3/work_packages/work_package_payload_representer.rb b/lib/api/v3/work_packages/work_package_payload_representer.rbindex 204cb4b13d0..de053c62144 100644--- a/lib/api/v3/work_packages/work_package_payload_representer.rb+++ b/lib/api/v3/work_packages/work_package_payload_representer.rb@@ -35,6 +35,25 @@ module APIcached_representer disabled: true+ property :file_links,+ exec_context: :decorator,+ getter: ->(*) {},+ setter: ->(fragment:, **) do+ next unless fragment.is_a?(Array)++ ids = fragment.map do |link|+ ::API::Utilities::ResourceLinkParser.parse_id link['href'],+ property: :file_link,+ expected_version: '3',+ expected_namespace: :file_links+ end++ represented.file_links_ids = ids+ end,+ skip_render: ->(*) { true },+ linked_resource: true,+ uncacheable: true+def writable_attributessuper + %w[date]enddiff --git a/spec/requests/api/v3/work_packages/create_resource_spec.rb b/spec/requests/api/v3/work_packages/create_resource_spec.rbindex 34fa8509e2d..3fd9a20baef 100644--- a/spec/requests/api/v3/work_packages/create_resource_spec.rb+++ b/spec/requests/api/v3/work_packages/create_resource_spec.rb@@ -37,7 +37,8 @@ describe 'API v3 Work package resource',create(:project, identifier: 'test_project', public: false)endlet(:role) { create(:role, permissions:) }- let(:permissions) { %i[add_work_packages view_project view_work_packages] }+ let(:permissions) { %i[add_work_packages view_project view_work_packages] + extra_permissions }+ let(:extra_permissions) { [] }current_user docreate(:user, member_in_project: project, member_through_role: role)@@ -128,7 +129,7 @@ describe 'API v3 Work package resource',expect(WorkPackage.first.type).to eq(type)end- context 'no permissions' do+ context 'without any permissions' dolet(:current_user) { create(:user) }it 'hides the endpoint' do@@ -136,7 +137,7 @@ describe 'API v3 Work package resource',endend- context 'view_project permission' do+ context 'when view_project permission is enabled' do# Note that this just removes the add_work_packages permission# view_project is actually provided by being a member of the projectlet(:permissions) { [:view_project] }@@ -146,7 +147,7 @@ describe 'API v3 Work package resource',endend- context 'empty parameters' do+ context 'with empty parameters' dolet(:parameters) { {} }it_behaves_like 'multiple errors', 422@@ -156,7 +157,7 @@ describe 'API v3 Work package resource',endend- context 'bogus parameters' do+ context 'with bogus parameters' dolet(:parameters) do{bogus: 'bogus',@@ -180,7 +181,7 @@ describe 'API v3 Work package resource',endend- context 'schedule manually' do+ context 'when scheduled manually' dolet(:work_package) { WorkPackage.first }context 'with true' do@@ -207,7 +208,7 @@ describe 'API v3 Work package resource',endend- context 'invalid value' do+ context 'with invalid value' dolet(:parameters) do{subject: nil,@@ -231,7 +232,7 @@ describe 'API v3 Work package resource',endend- context 'claiming attachments' do+ context 'when attachments are being claimed' dolet(:attachment) { create(:attachment, container: nil, author: current_user) }let(:parameters) do{@@ -251,7 +252,7 @@ describe 'API v3 Work package resource',endit 'creates the work package and assigns the attachments' do- expect(WorkPackage.all.count).to eq(1)+ expect(WorkPackage.count).to eq(1)work_package = WorkPackage.last@@ -259,5 +260,59 @@ describe 'API v3 Work package resource',.to match_array(attachment)endend++ context 'when file links are being claimed' do+ let(:storage) { create(:storage) }+ let(:file_link) do+ create(:file_link,+ container_id: nil,+ container_type: nil,+ storage:,+ creator: current_user)+ end+ let(:parameters) do+ {+ subject: 'subject',+ _links: {+ type: {+ href: api_v3_paths.type(project.types.first.id)+ },+ project: {+ href: api_v3_paths.project(project.id)+ },+ fileLinks: [+ href: api_v3_paths.file_link(file_link.id)+ ]+ }+ }+ end+ let(:extra_permissions) do+ %i[view_file_links]+ end++ it 'does not create a work packages and responds with an error ' \+ 'when user is not allowed to manage file links', :aggregate_failtures do+ expect(WorkPackage.count).to eq(0)+ expect(last_response.body).to be_json_eql(+ 'urn:openproject-org:api:v3:errors:MissingPermission'.to_json+ ).at_path('errorIdentifier')+ end++ context 'when user is allowed to manage file links' do+ let(:extra_permissions) do+ %i[view_file_links manage_file_links]+ end++ it 'creates a work package and assigns the file links', :aggregate_failtures do+ expect(WorkPackage.count).to eq(1)+ work_package = WorkPackage.first+ expect(work_package.file_links).to eq([file_link])+ expect(last_response.body).to be_json_eql(+ api_v3_paths.file_links(work_package.id).to_json+ ).at_path('_links/fileLinks/href')+ expect(last_response.body).to be_json_eql("1").at_path('_embedded/fileLinks/total')+ end+ end+ endendend