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 create form end point to api

ulferts7cca2631b03c
diff --git a/app/contracts/concerns/assignable_custom_field_values.rb b/app/contracts/concerns/assignable_custom_field_values.rb
new file mode 100644
index 00000000000..2ccc8b6e2ea
--- /dev/null
+++ b/app/contracts/concerns/assignable_custom_field_values.rb
@@ -0,0 +1,44 @@
+#-- 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 Concerns::AssignableCustomFieldValues
+ extend ActiveSupport::Concern
+
+ included do
+ def assignable_custom_field_values(custom_field)
+ case custom_field.field_format
+ when 'list'
+ custom_field.possible_values
+ when 'version'
+ assignable_versions
+ end
+ end
+ end
+end
diff --git a/app/contracts/assignable_values_contract.rb b/app/contracts/concerns/assignable_values_contract.rb
similarity index 80%
rename from app/contracts/assignable_values_contract.rb
rename to app/contracts/concerns/assignable_values_contract.rb
index 5a56b2dd59a..8a71e0a2d51 100644
--- a/app/contracts/assignable_values_contract.rb
+++ b/app/contracts/concerns/assignable_values_contract.rb
@@ -1,4 +1,4 @@
-module AssignableValuesContract
+module Concerns::AssignableValuesContract
def assignable_values(column, _user)
method_name = "assignable_#{column.to_s.pluralize}"
diff --git a/app/contracts/projects/base_contract.rb b/app/contracts/projects/base_contract.rb
index 78077b190b7..e36c95b9952 100644
--- a/app/contracts/projects/base_contract.rb
+++ b/app/contracts/projects/base_contract.rb
@@ -30,7 +30,8 @@
module Projects
class BaseContract < ::ModelContract
- include AssignableValuesContract
+ include Concerns::AssignableValuesContract
+ include Concerns::AssignableCustomFieldValues
attribute :name
attribute :identifier
@@ -60,10 +61,6 @@ module Projects
Project.statuses.keys
end
- def assignable_custom_field_values(custom_field)
- custom_field.possible_values
- end
-
def available_custom_fields
if user.admin?
model.available_custom_fields
@@ -72,6 +69,10 @@ module Projects
end
end
+ def assignable_versions
+ model.assignable_versions
+ end
+
private
def validate_status_not_nil
diff --git a/app/contracts/versions/base_contract.rb b/app/contracts/versions/base_contract.rb
index da6bbb4271a..1e2bf78a356 100644
--- a/app/contracts/versions/base_contract.rb
+++ b/app/contracts/versions/base_contract.rb
@@ -28,7 +28,7 @@
module Versions
class BaseContract < ::ModelContract
- include AssignableValuesContract
+ include Concerns::AssignableValuesContract
def self.model
Version
diff --git a/app/models/project.rb b/app/models/project.rb
index 322de9a2a1c..0d4399f1714 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -435,26 +435,19 @@ class Project < ActiveRecord::Base
# Returns a scope of the Versions on subprojects
def rolled_up_versions
- @rolled_up_versions ||=
- Version.includes(:project)
- .where(["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status = #{STATUS_ACTIVE}", lft, rgt])
- .references(:projects)
+ shared_versions_base_scope
+ .merge(Project.active)
+ .where(projects: { id: self_and_descendants.select(:id) })
end
# Returns a scope of the Versions used by the project
def shared_versions
@shared_versions ||= begin
- r = root? ? self : root
-
- Version.includes(:project)
- .where("#{Project.table_name}.id = #{id}" +
- " OR (#{Project.table_name}.status = #{Project::STATUS_ACTIVE} AND (" +
- " #{Version.table_name}.sharing = 'system'" +
- " OR (#{Project.table_name}.lft >= #{r.lft} AND #{Project.table_name}.rgt <= #{r.rgt} AND #{Version.table_name}.sharing = 'tree')" +
- " OR (#{Project.table_name}.lft < #{lft} AND #{Project.table_name}.rgt > #{rgt} AND #{Version.table_name}.sharing IN ('hierarchy', 'descendants'))" +
- " OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt} AND #{Version.table_name}.sharing = 'hierarchy')" +
- '))')
- .references(:projects)
+ if persisted?
+ shared_versions_on_persisted
+ else
+ shared_versions_by_system
+ end
end
end
@@ -805,4 +798,46 @@ class Project < ActiveRecord::Base
p
end
end
+
+ def shared_versions_on_persisted
+ shared_versions_base_scope
+ .where(projects: { id: id })
+ .or(shared_versions_by_system)
+ .or(shared_versions_by_tree)
+ .or(shared_versions_by_hierarchy_or_descendants)
+ .or(shared_versions_by_hierarchy)
+ end
+
+ def shared_versions_by_tree
+ r = root? ? self : root
+
+ shared_versions_base_scope
+ .merge(Project.active)
+ .where(projects: { id: r.self_and_descendants.select(:id) })
+ .where(sharing: 'tree')
+ end
+
+ def shared_versions_by_hierarchy_or_descendants
+ shared_versions_base_scope
+ .merge(Project.active)
+ .where(projects: { id: ancestors.select(:id) })
+ .where(sharing: %w(hierarchy descendants))
+ end
+
+ def shared_versions_by_hierarchy
+ rolled_up_versions
+ .where(sharing: 'hierarchy')
+ end
+
+ def shared_versions_by_system
+ shared_versions_base_scope
+ .merge(Project.active)
+ .where(sharing: 'system')
+ end
+
+ def shared_versions_base_scope
+ Version
+ .includes(:project)
+ .references(:projects)
+ end
end
diff --git a/docs/api/apiv3/endpoints/projects.apib b/docs/api/apiv3/endpoints/projects.apib
index 2917b04b1e2..430091bfe01 100644
--- a/docs/api/apiv3/endpoints/projects.apib
+++ b/docs/api/apiv3/endpoints/projects.apib
@@ -428,6 +428,299 @@ You can use the form and schema to be retrieve the valid attribute values and by
[Projects schema][]
+## Project create form [/api/v3/project/form]
+
+This endpoint returns a form allowing a guided creation of new projects.
+
+For more details and all possible responses see the general specification of [Forms](#forms).
+
+## Project create form [POST]
+
++ Request Create 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/form",
+ "method": "post"
+ },
+ "validate": {
+ "href": "/api/v3/projects/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:** add project which is a global permission
+
+ + 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/create_form_api.rb b/lib/api/v3/projects/create_form_api.rb
new file mode 100644
index 00000000000..5269eadda9b
--- /dev/null
+++ b/lib/api/v3/projects/create_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 CreateFormAPI < ::API::OpenProjectAPI
+ resource :form do
+ after_validation do
+ authorize :add_project, global: true
+ end
+
+ post &::API::V3::Utilities::Endpoints::CreateForm.new(model: Project)
+ .mount
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/projects/create_form_representer.rb b/lib/api/v3/projects/create_form_representer.rb
new file mode 100644
index 00000000000..4d40ad878ca
--- /dev/null
+++ b/lib/api/v3/projects/create_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 CreateFormRepresenter < FormRepresenter
+ include API::Decorators::CreateForm
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/projects/form_representer.rb b/lib/api/v3/projects/form_representer.rb
new file mode 100644
index 00000000000..3ab03f5dc9e
--- /dev/null
+++ b/lib/api/v3/projects/form_representer.rb
@@ -0,0 +1,41 @@
+#-- 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 FormRepresenter < ::API::Decorators::SimpleForm
+ def model
+ Project
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/projects/projects_api.rb b/lib/api/v3/projects/projects_api.rb
index bc14a9fe822..a55fcaffd71 100644
--- a/lib/api/v3/projects/projects_api.rb
+++ b/lib/api/v3/projects/projects_api.rb
@@ -41,6 +41,7 @@ module API
.mount
mount ::API::V3::Projects::Schemas::ProjectSchemaAPI
+ mount ::API::V3::Projects::CreateFormAPI
params do
requires :id, desc: 'Project id'
diff --git a/lib/api/v3/utilities/custom_field_injector.rb b/lib/api/v3/utilities/custom_field_injector.rb
index e5dfb9277e4..4ea6a3f8ba4 100644
--- a/lib/api/v3/utilities/custom_field_injector.rb
+++ b/lib/api/v3/utilities/custom_field_injector.rb
@@ -140,7 +140,7 @@ module API
def inject_version_schema(custom_field)
@class.schema_with_allowed_collection property_name(custom_field.id),
- type: 'Version',
+ type: resource_type(custom_field),
name_source: ->(*) { custom_field.name },
values_callback: ->(*) {
represented
@@ -158,10 +158,8 @@ module API
end
def inject_user_schema(custom_field)
- type = custom_field.multi_value? ? "[]User" : "User"
-
@class.schema_with_allowed_link property_name(custom_field.id),
- type: type,
+ type: resource_type(custom_field),
writable: true,
name_source: ->(*) { custom_field.name },
required: custom_field.is_required,
@@ -169,11 +167,9 @@ module API
end
def inject_list_schema(custom_field)
- type = custom_field.multi_value ? "[]CustomOption" : "CustomOption"
-
@class.schema_with_allowed_collection(
property_name(custom_field.id),
- type: type,
+ type: resource_type(custom_field),
name_source: ->(*) { custom_field.name },
values_callback: list_schemas_values_callback(custom_field),
value_representer: CustomOptions::CustomOptionRepresenter,
@@ -183,13 +179,13 @@ module API
)
end
- def inject_basic_schema(custom_field)
+ def inject_basic_schema(custom_field, writable: true)
@class.schema property_name(custom_field.id),
- type: TYPE_MAP[custom_field.field_format],
+ type: resource_type(custom_field),
name_source: ->(*) { custom_field.name },
required: custom_field.is_required,
has_default: custom_field.default_value.present?,
- writable: true,
+ writable: writable,
min_length: cf_min_length(custom_field),
max_length: cf_max_length(custom_field),
regular_expression: cf_regexp(custom_field)
@@ -301,11 +297,17 @@ module API
# for now we ASSUME that every customized that has a
# user custom field, will also define a project...
->(*) {
+ project_id_value = if represented.respond_to?(:model) && represented.model.is_a?(Project)
+ represented.id
+ else
+ represented.project_id.to_s
+ end
+
params = [{ status: { operator: '!',
values: [Principal::STATUSES[:builtin].to_s,
Principal::STATUSES[:locked].to_s] } },
{ type: { operator: '=', values: ['User'] } },
- { member: { operator: '=', values: [represented.project_id.to_s] } }]
+ { member: { operator: '=', values: [project_id_value.to_s] } }]
query = CGI.escape(::JSON.dump(params))
@@ -314,11 +316,11 @@ module API
end
def cf_min_length(custom_field)
- custom_field.min_length if custom_field.min_length > 0
+ custom_field.min_length if custom_field.min_length.positive?
end
def cf_max_length(custom_field)
- custom_field.max_length if custom_field.max_length > 0
+ custom_field.max_length if custom_field.max_length.positive?
end
def cf_regexp(custom_field)
@@ -343,6 +345,16 @@ module API
.constantize
end
+ def resource_type(custom_field)
+ type = TYPE_MAP[custom_field.field_format]
+
+ if custom_field.multi_value?
+ "[]#{type}"
+ else
+ type
+ end
+ end
+
module RepresenterClass
def custom_field_injector(config)
@custom_field_injector_config = config.reverse_merge custom_field_injector_config
diff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rb
index d38d5808636..38008569fbf 100644
--- a/lib/api/v3/utilities/path_helper.rb
+++ b/lib/api/v3/utilities/path_helper.rb
@@ -209,10 +209,7 @@ module API
alias :issue_priority :priority
end
- index :project
- show :project
- schema :project
-
+ resources :project
resources :query
def self.query_default
diff --git a/lib/api/v3/work_packages/schema/specific_work_package_schema.rb b/lib/api/v3/work_packages/schema/specific_work_package_schema.rb
index 042caf1e1d7..043da90e802 100644
--- a/lib/api/v3/work_packages/schema/specific_work_package_schema.rb
+++ b/lib/api/v3/work_packages/schema/specific_work_package_schema.rb
@@ -33,6 +33,8 @@ module API
module Schema
class SpecificWorkPackageSchema < BaseWorkPackageSchema
attr_reader :work_package
+ include ::Concerns::AssignableCustomFieldValues
+ include ::Concerns::AssignableValuesContract
def initialize(work_package:)
@work_package = work_package
@@ -46,34 +48,6 @@ module API
:available_custom_fields,
to: :@work_package
- def assignable_values(property, current_user)
- case property
- when :status
- assignable_statuses_for(current_user)
- when :type
- if project.nil?
- Type.includes(:color)
- else
- project.types.includes(:color)
- end
- when :version
- @work_package.try(:assignable_versions) if project
- when :priority
- IssuePriority.active
- when :category
- project.categories if project.respond_to?(:categories)
- end
- end
-
- def assignable_custom_field_values(custom_field)
- case custom_field.field_format
- when 'list'
- custom_field.possible_values
- when 'version'
- assignable_values(:version, nil)
- end
- end
-
def no_caching?
true
end
@@ -94,7 +68,27 @@ module API
end
end
- def assignable_statuses_for(user)
+ def assignable_categories
+ project.categories if project.respond_to?(:categories)
+ end
+
+ def assignable_priorities
+ IssuePriority.active
+ end
+
+ def assignable_versions
+ @work_package.try(:assignable_versions) if project
+ end
+
+ def assignable_types
+ if project.nil?
+ Type.includes(:color)
+ else
+ project.types.includes(:color)
+ end
+ end
+
+ def assignable_statuses
status_origin = @work_package
# do not allow to skip statuses without intermediately saving the work package
@@ -105,7 +99,7 @@ module API
status_origin.status = Status.find_by(id: @work_package.status_id_was)
end
- status_origin.new_statuses_allowed_to(user)
+ status_origin.new_statuses_allowed_to(User.current)
end
end
end
diff --git a/modules/grids/app/contracts/grids/base_contract.rb b/modules/grids/app/contracts/grids/base_contract.rb
index 6b31801a6b2..261d6537c83 100644
--- a/modules/grids/app/contracts/grids/base_contract.rb
+++ b/modules/grids/app/contracts/grids/base_contract.rb
@@ -33,7 +33,7 @@ require 'model_contract'
module Grids
class BaseContract < ::ModelContract
include OpenProject::StaticRouting::UrlHelpers
- include AssignableValuesContract
+ include Concerns::AssignableValuesContract
include ::Attachments::ValidateReplacements
attribute :row_count do
diff --git a/spec/contracts/projects/shared_contract_examples.rb b/spec/contracts/projects/shared_contract_examples.rb
index 419373e1abc..2ac89dc3ab4 100644
--- a/spec/contracts/projects/shared_contract_examples.rb
+++ b/spec/contracts/projects/shared_contract_examples.rb
@@ -142,7 +142,7 @@ shared_examples_for 'project contract' do
end
end
- context 'for a custom field' do
+ context 'for a list custom field' do
let(:custom_field) { FactoryBot.build_stubbed(:list_project_custom_field) }
it 'is the list of custom field values' do
@@ -150,6 +150,22 @@ shared_examples_for 'project contract' do
.to eql custom_field.possible_values
end
end
+
+ context 'for a version custom field' do
+ let(:custom_field) { FactoryBot.build_stubbed(:version_project_custom_field) }
+ let(:versions) { double('versions') }
+
+ before do
+ allow(project)
+ .to receive(:assignable_versions)
+ .and_return(versions)
+ end
+
+ it 'is the list of versions for the project' do
+ expect(subject.assignable_custom_field_values(custom_field))
+ .to eql versions
+ end
+ end
end
describe 'available_custom_fields' do
diff --git a/spec/lib/api/v3/utilities/path_helper_spec.rb b/spec/lib/api/v3/utilities/path_helper_spec.rb
index a5c43bd4011..dc51ebaea54 100644
--- a/spec/lib/api/v3/utilities/path_helper_spec.rb
+++ b/spec/lib/api/v3/utilities/path_helper_spec.rb
@@ -238,9 +238,7 @@ describe ::API::V3::Utilities::PathHelper do
end
describe 'projects paths' do
- it_behaves_like 'index', :project
- it_behaves_like 'show', :project
- it_behaves_like 'schema', :project
+ it_behaves_like 'resource', :project
end
describe 'query paths' do
diff --git a/spec/requests/api/v3/projects/create_form_resource_spec.rb b/spec/requests/api/v3/projects/create_form_resource_spec.rb
new file mode 100644
index 00000000000..f87fe767b0e
--- /dev/null
+++ b/spec/requests/api/v3/projects/create_form_resource_spec.rb
@@ -0,0 +1,156 @@
+#-- 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::CreateFormAPI, content_type: :json do
+ include Rack::Test::Methods
+ include API::V3::Utilities::PathHelper
+
+ let(:current_user) do
+ FactoryBot.create(:user).tap do |u|
+ u.global_roles << global_role
+ end
+ end
+ let(:global_role) do
+ FactoryBot.create(:global_role, 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) { [:add_project] }
+ let(:path) { api_v3_paths.create_project_form }
+ 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/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
+
+ it 'does not create a project' do
+ expect(Project.count)
+ .to eql 0
+ end
+
+ context 'with empty parameters' do
+ it 'has 2 validation errors' do
+ expect(subject.body).to have_json_size(2).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 a validation error on identifier' do
+ expect(subject.body).to have_json_path('_embedded/validationErrors/identifier')
+ end
+
+ it 'has no commit link' do
+ expect(subject.body)
+ .not_to have_json_path('_links/commit')
+ end
+ end
+
+ context 'with all parameters' do
+ let(:params) do
+ {
+ identifier: 'new_project_identifier',
+ name: 'Project name',
+ "customField#{text_custom_field.id}": {
+ "raw": "CF text"
+ },
+ "_links": {
+ "customField#{list_custom_field.id}": {
+ "href": api_v3_paths.custom_option(list_custom_field.custom_options.first.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('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.first.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.projects.to_json)
+ .at_path('_links/commit/href')
+ 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