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

add projects update form end point to api

ulferts106f62474e51
diff --git a/docs/api/apiv3/endpoints/projects.apib b/docs/api/apiv3/endpoints/projects.apib
index 430091bfe01..e9fc2c924f8 100644
--- a/docs/api/apiv3/endpoints/projects.apib
+++ b/docs/api/apiv3/endpoints/projects.apib
@@ -721,6 +721,300 @@ For more details and all possible responses see the general specification of [Fo
"message": "You are not authorized to access this resource."
}
+## Project update form [/api/v3/projects/{id}/form]
+
+This endpoint returns a form allowing a guided modification of existing projects.
+The returned payload part of the form will be contain a merge between the already existing properties' values (as long as they are writeable) and the provided values with the later having precedence.
+
+For more details and all possible responses see the general specification of [Forms](#forms).
+
+## Project update form [POST]
+
++ Request Update project form
+
+ + Body
+
+ {
+ "identifier": "new_project_identifier",
+ "name": "New project name",
+ "customField35": "Text custom field value"
+ }
+
++ Response 200 (application/hal+json)
+
+ + Body
+
+ {
+ "_type": "Form",
+ "_embedded": {
+ "payload": {
+ "identifier": "new_project_identifier",
+ "name": "New project name",
+ "status": "active",
+ "public": false,
+ "description": {
+ "format": "markdown",
+ "raw": null,
+ "html": ""
+ },
+ "customField30": null,
+ "customField34": null,
+ "customField35": "Text custom field value",
+ "customField41": {
+ "format": "markdown",
+ "raw": "",
+ "html": ""
+ },
+ "customField42": null,
+ "_links": {
+ "customField26": {
+ "href": null,
+ "title": null
+ },
+ "customField31": {
+ "href": null,
+ "title": null
+ }
+ }
+ },
+ "schema": {
+ "_type": "Schema",
+ "_dependencies": [],
+ "id": {
+ "type": "Integer",
+ "name": "ID",
+ "required": true,
+ "hasDefault": false,
+ "writable": false
+ },
+ "name": {
+ "type": "String",
+ "name": "Name",
+ "required": true,
+ "hasDefault": false,
+ "writable": true,
+ "minLength": 1,
+ "maxLength": 255
+ },
+ "identifier": {
+ "type": "String",
+ "name": "Identifier",
+ "required": true,
+ "hasDefault": false,
+ "writable": true,
+ "minLength": 1,
+ "maxLength": 100
+ },
+ "description": {
+ "type": "Formattable",
+ "name": "Description",
+ "required": false,
+ "hasDefault": false,
+ "writable": true
+ },
+ "public": {
+ "type": "Boolean",
+ "name": "Public",
+ "required": true,
+ "hasDefault": false,
+ "writable": true
+ },
+ "status": {
+ "type": "String",
+ "name": "Status",
+ "required": true,
+ "hasDefault": false,
+ "writable": true,
+ "visibility": "default",
+ "_embedded": {
+ "allowedValues": [
+ "active",
+ "archived"
+ ]
+ },
+ "_links": {}
+ },
+ "createdAt": {
+ "type": "DateTime",
+ "name": "Created on",
+ "required": true,
+ "hasDefault": false,
+ "writable": false
+ },
+ "updatedAt": {
+ "type": "DateTime",
+ "name": "Updated on",
+ "required": true,
+ "hasDefault": false,
+ "writable": false
+ },
+ "customField26": {
+ "type": "User",
+ "name": "Project user",
+ "required": false,
+ "hasDefault": false,
+ "writable": true,
+ "visibility": "default",
+ "_links": {
+ "allowedValues": {
+ "href": "/api/v3/principals?filters=%5B%7B%22status%22%3A%7B%22operator%22%3A%22%21%22%2C%22values%22%3A%5B%220%22%2C%223%22%5D%7D%7D%2C%7B%22type%22%3A%7B%22operator%22%3A%22%3D%22%2C%22values%22%3A%5B%22User%22%5D%7D%7D%2C%7B%22member%22%3A%7B%22operator%22%3A%22%3D%22%2C%22values%22%3A%5B%22%22%5D%7D%7D%5D&pageSize=0"
+ }
+ }
+ },
+ "customField30": {
+ "type": "Integer",
+ "name": "some project cf",
+ "required": false,
+ "hasDefault": false,
+ "writable": true,
+ "visibility": "default"
+ },
+ "customField31": {
+ "type": "CustomOption",
+ "name": "list project cf",
+ "required": false,
+ "hasDefault": false,
+ "writable": true,
+ "visibility": "default",
+ "_embedded": {
+ "allowedValues": [
+ {
+ "_type": "CustomOption",
+ "id": 513,
+ "value": "abc",
+ "_links": {
+ "self": {
+ "href": "/api/v3/custom_options/513",
+ "title": "abc"
+ }
+ }
+ },
+ {
+ "_type": "CustomOption",
+ "id": 514,
+ "value": "def",
+ "_links": {
+ "self": {
+ "href": "/api/v3/custom_options/514",
+ "title": "def"
+ }
+ }
+ },
+ {
+ "_type": "CustomOption",
+ "id": 515,
+ "value": "ghi",
+ "_links": {
+ "self": {
+ "href": "/api/v3/custom_options/515",
+ "title": "ghi"
+ }
+ }
+ }
+ ]
+ },
+ "_links": {
+ "allowedValues": [
+ {
+ "href": "/api/v3/custom_options/513",
+ "title": "abc"
+ },
+ {
+ "href": "/api/v3/custom_options/514",
+ "title": "def"
+ },
+ {
+ "href": "/api/v3/custom_options/515",
+ "title": "ghi"
+ }
+ ]
+ }
+ },
+ "customField34": {
+ "type": "Boolean",
+ "name": "Bool Project CF",
+ "required": false,
+ "hasDefault": false,
+ "writable": true,
+ "visibility": "default"
+ },
+ "customField35": {
+ "type": "String",
+ "name": "text project cf",
+ "required": true,
+ "hasDefault": false,
+ "writable": true,
+ "visibility": "default"
+ },
+ "customField41": {
+ "type": "Formattable",
+ "name": "Long text project cf",
+ "required": false,
+ "hasDefault": false,
+ "writable": true,
+ "visibility": "default"
+ },
+ "customField42": {
+ "type": "Date",
+ "name": "Date project cf",
+ "required": false,
+ "hasDefault": false,
+ "writable": true,
+ "visibility": "default"
+ },
+ "_links": {}
+ },
+ "validationErrors": {
+ "identifier": {
+ "_type": "Error",
+ "errorIdentifier": "urn:openproject-org:api:v3:errors:PropertyConstraintViolation",
+ "message": "Identifier has already been taken.",
+ "_embedded": {
+ "details": {
+ "attribute": "identifier"
+ }
+ }
+ }
+ }
+ },
+ "_links": {
+ "self": {
+ "href": "/api/v3/projects/5/form",
+ "method": "post"
+ },
+ "validate": {
+ "href": "/api/v3/projects/5/form",
+ "method": "post"
+ }
+ }
+ }
+
++ Response 400 (application/hal+json)
+
+ Occurs when the client did not send a valid JSON object in the request body.
+
+ + Body
+
+ {
+ "_type": "Error",
+ "errorIdentifier": "urn:openproject-org:api:v3:errors:InvalidRequestBody",
+ "message": "The request body was not a single JSON object."
+ }
+
++ Response 403 (application/hal+json)
+
+ Returned if the client does not have sufficient permissions.
+
+ **Required permission:** edit projects in the project
+
+ + Body
+
+ {
+ "_type": "Error",
+ "errorIdentifier": "urn:openproject-org:api:v3:errors:MissingPermission",
+ "message": "You are not authorized to access this resource."
+ }
+
## Projects by version [/api/v3/versions/{id}/projects]
+ Model
diff --git a/lib/api/v3/projects/projects_api.rb b/lib/api/v3/projects/projects_api.rb
index a55fcaffd71..b22a5b5ab0b 100644
--- a/lib/api/v3/projects/projects_api.rb
+++ b/lib/api/v3/projects/projects_api.rb
@@ -57,6 +57,8 @@ module API
get &::API::V3::Utilities::Endpoints::Show.new(model: Project).mount
+ mount ::API::V3::Projects::UpdateFormAPI
+
mount API::V3::Projects::AvailableAssigneesAPI
mount API::V3::Projects::AvailableResponsiblesAPI
mount API::V3::WorkPackages::WorkPackagesByProjectAPI
diff --git a/lib/api/v3/projects/update_form_api.rb b/lib/api/v3/projects/update_form_api.rb
new file mode 100644
index 00000000000..efad143efd9
--- /dev/null
+++ b/lib/api/v3/projects/update_form_api.rb
@@ -0,0 +1,44 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module API
+ module V3
+ module Projects
+ class UpdateFormAPI < ::API::OpenProjectAPI
+ resource :form do
+ after_validation do
+ authorize :edit_project, context: @project
+ end
+
+ post &::API::V3::Utilities::Endpoints::UpdateForm.new(model: Project)
+ .mount
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/projects/update_form_representer.rb b/lib/api/v3/projects/update_form_representer.rb
new file mode 100644
index 00000000000..101727f1456
--- /dev/null
+++ b/lib/api/v3/projects/update_form_representer.rb
@@ -0,0 +1,39 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 the OpenProject Foundation (OPF)
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module API
+ module V3
+ module Projects
+ class UpdateFormRepresenter < FormRepresenter
+ include API::Decorators::UpdateForm
+ end
+ end
+ end
+end
diff --git a/spec/requests/api/v3/projects/update_form_resource_spec.rb b/spec/requests/api/v3/projects/update_form_resource_spec.rb
new file mode 100644
index 00000000000..6026c95ea20
--- /dev/null
+++ b/spec/requests/api/v3/projects/update_form_resource_spec.rb
@@ -0,0 +1,195 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See doc/COPYRIGHT.rdoc for more details.
+
+require 'spec_helper'
+require 'rack/test'
+
+describe ::API::V3::Projects::UpdateFormAPI, content_type: :json do
+ include Rack::Test::Methods
+ include API::V3::Utilities::PathHelper
+
+ let(:project) do
+ FactoryBot.create(:project,
+ "custom_field_#{text_custom_field.id}": "CF text",
+ "custom_field_#{list_custom_field.id}": list_custom_field.custom_options.first)
+ end
+ let(:current_user) do
+ FactoryBot.create(:user,
+ member_in_project: project,
+ member_with_permissions: permissions)
+ end
+ let(:text_custom_field) do
+ FactoryBot.create(:text_project_custom_field)
+ end
+ let(:list_custom_field) do
+ FactoryBot.create(:list_project_custom_field)
+ end
+ let(:permissions) { [:edit_project] }
+ let(:path) { api_v3_paths.project_form(project.id) }
+ let(:params) do
+ {
+ }
+ end
+
+ before do
+ login_as(current_user)
+
+ post path, params.to_json
+ end
+
+ subject(:response) { last_response }
+
+ describe '#POST /api/v3/projects/:id/form' do
+ it 'returns 200 OK' do
+ expect(response.status).to eq(200)
+ end
+
+ it 'returns a form' do
+ expect(response.body)
+ .to be_json_eql('Form'.to_json)
+ .at_path('_type')
+ end
+
+ context 'with empty parameters' do
+ it 'has no validation errors' do
+ expect(subject.body).to have_json_size(0).at_path('_embedded/validationErrors')
+ end
+
+ it 'returns the current project value`s in the payload' do
+ expect(subject.body)
+ .to be_json_eql(project.name.to_json)
+ .at_path('_embedded/payload/name')
+ end
+
+ it 'has a commit link' do
+ expect(subject.body)
+ .to have_json_path('_links/commit')
+ end
+ end
+
+ context 'with faulty parameters' do
+ let(:params) do
+ {
+ "name": nil
+ }
+ end
+
+ it 'has one validation errors' do
+ expect(subject.body).to have_json_size(1).at_path('_embedded/validationErrors')
+ end
+
+ it 'has a validation error on name' do
+ expect(subject.body).to have_json_path('_embedded/validationErrors/name')
+ end
+
+ it 'has no commit link' do
+ expect(subject.body)
+ .not_to have_json_path('_links/commit')
+ end
+
+ it 'does not alter the project' do
+ name_before = project.name
+
+ expect(project.reload.name)
+ .to eql name_before
+ end
+ end
+
+ context 'with valid parameters' do
+ let(:params) do
+ {
+ identifier: 'new_project_identifier',
+ name: 'Project name',
+ "customField#{text_custom_field.id}": {
+ "raw": "new CF text"
+ },
+ "_links": {
+ "customField#{list_custom_field.id}": {
+ "href": api_v3_paths.custom_option(list_custom_field.custom_options.last.id)
+ }
+ }
+ }
+ end
+
+ it 'has 0 validation errors' do
+ expect(subject.body).to have_json_size(0).at_path('_embedded/validationErrors')
+ end
+
+ it 'has the values prefilled in the payload' do
+ body = subject.body
+
+ expect(body)
+ .to be_json_eql('new_project_identifier'.to_json)
+ .at_path('_embedded/payload/identifier')
+
+ expect(body)
+ .to be_json_eql('Project name'.to_json)
+ .at_path('_embedded/payload/name')
+
+ expect(body)
+ .to be_json_eql('new CF text'.to_json)
+ .at_path("_embedded/payload/customField#{text_custom_field.id}/raw")
+
+ expect(body)
+ .to be_json_eql(api_v3_paths.custom_option(list_custom_field.custom_options.last.id).to_json)
+ .at_path("_embedded/payload/_links/customField#{list_custom_field.id}/href")
+ end
+
+ it 'has a commit link' do
+ expect(subject.body)
+ .to be_json_eql(api_v3_paths.project(project.id).to_json)
+ .at_path('_links/commit/href')
+ end
+
+ it 'does not alter the project' do
+ attributes_before = project.attributes
+
+ expect(project.reload.name)
+ .to eql attributes_before['name']
+
+ expect(project.identifier)
+ .to eql attributes_before['identifier']
+
+ expect(project.send("custom_field_#{text_custom_field.id}"))
+ .to eql 'CF text'
+
+ expect(project.send("custom_field_#{list_custom_field.id}"))
+ .to eql list_custom_field.custom_options.first.value
+ end
+ end
+
+ context 'without the necessary permission' do
+ let(:permissions) { [] }
+
+ it 'returns 403 Not Authorized' do
+ expect(response.status).to eq(403)
+ end
+ end
+ end
+end