From 8fd8943d628b2325fb7b06401246b7e6bcbbe407 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Tue, 10 Sep 2019 15:05:14 -0700 Subject: Support creating project from template via API This adds support for creating projects from project templates via the API. Note there isn't an API to look up the available project template names (e.g. rails, iosswift, etc.) yet. This is a backport of https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/16352. --- .../sh-create-project-from-template-api-ce.yml | 5 +++++ doc/api/projects.md | 4 ++++ lib/api/helpers/projects_helpers.rb | 8 ++++++++ lib/api/projects.rb | 4 +++- spec/requests/api/projects_spec.rb | 19 +++++++++++++++++++ 5 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 changelogs/unreleased/sh-create-project-from-template-api-ce.yml diff --git a/changelogs/unreleased/sh-create-project-from-template-api-ce.yml b/changelogs/unreleased/sh-create-project-from-template-api-ce.yml new file mode 100644 index 00000000000..ff6ae55e097 --- /dev/null +++ b/changelogs/unreleased/sh-create-project-from-template-api-ce.yml @@ -0,0 +1,5 @@ +--- +title: Support creating project from template via API +merge_request: 32906 +author: +type: added diff --git a/doc/api/projects.md b/doc/api/projects.md index 3ea1434d5ca..99153296bf7 100644 --- a/doc/api/projects.md +++ b/doc/api/projects.md @@ -991,6 +991,10 @@ POST /projects/user/:user_id | `external_authorization_classification_label` | string | no | **(PREMIUM)** The classification label for the project | | `mirror` | boolean | no | **(STARTER)** Enables pull mirroring in a project | | `mirror_trigger_builds` | boolean | no | **(STARTER)** Pull mirroring triggers builds | +| `template_name` | string | no | Name of built-in project template (e.g. rails, spring, express, iosswift, etc.). When used with `use_custom_template`, specifies the name of the project | + | +| `use_custom_template` | boolean | no | **(PREMIUM)** Use instance-level or group project templates. Specify `group_with_project_templates_id` for group templates | +| `group_with_project_templates_id` | integer | no | **(PREMIUM)** Specifies which group ID where all the custom project templates are sourced. Requires `use_custom_template` to be true. Leave empty for instance-level templates | NOTE: **Note:** If your HTTP repository is not publicly accessible, add authentication information to the URL: `https://username:password@gitlab.company.com/group/project.git` diff --git a/lib/api/helpers/projects_helpers.rb b/lib/api/helpers/projects_helpers.rb index c1e7af33235..d55803b07ac 100644 --- a/lib/api/helpers/projects_helpers.rb +++ b/lib/api/helpers/projects_helpers.rb @@ -56,6 +56,14 @@ module API use :optional_project_params_ee end + params :optional_create_project_params_ee do + end + + params :optional_create_project_params do + use :optional_project_params + use :optional_create_project_params_ee + end + params :optional_filter_params_ee do end diff --git a/lib/api/projects.rb b/lib/api/projects.rb index 63bfa8db61c..e4790d191d7 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -68,6 +68,8 @@ module API params :create_params do optional :namespace_id, type: Integer, desc: 'Namespace ID for the new project. Default to the user namespace.' optional :import_url, type: String, desc: 'URL from which the project is imported' + optional :template_name, type: String, desc: "Name of template from which to create project" + mutually_exclusive :import_url, :template_name end def load_projects @@ -155,7 +157,7 @@ module API optional :name, type: String, desc: 'The name of the project' optional :path, type: String, desc: 'The path of the repository' at_least_one_of :name, :path - use :optional_project_params + use :optional_create_project_params use :create_params end post do diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb index 550c7d135a6..571f6af9404 100644 --- a/spec/requests/api/projects_spec.rb +++ b/spec/requests/api/projects_spec.rb @@ -631,6 +631,25 @@ describe API::Projects do expect(project.project_feature.wiki_access_level).to eq(ProjectFeature::DISABLED) end + it 'creates a project using a template' do + expect { post api('/projects', user), params: { template_name: 'rails', name: 'rails-test' } } + .to change { Project.count }.by(1) + + expect(response).to have_gitlab_http_status(201) + + project = Project.last + expect(project).to be_saved + expect(project.import_type).to eq('gitlab_project') + end + + it 'disallows creating a project with an import_url and template' do + project_params = { import_url: 'http://example.com', template_name: 'rails', name: 'rails-test' } + expect { post api('/projects', user), params: project_params } + .not_to change { Project.count } + + expect(response).to have_gitlab_http_status(400) + end + it 'sets a project as public' do project = attributes_for(:project, visibility: 'public') -- cgit v1.2.1