Commit 7c0a1068bf337cd169d11cca764af6b0ceb986b2
Exists in
master
and in
4 other branches
Merge pull request #1363 from AlexDenisov/api_create_project_fixes
API create project fixes
Showing
4 changed files
with
58 additions
and
37 deletions
Show diff stats
doc/api/projects.md
| @@ -102,6 +102,12 @@ Parameters: | @@ -102,6 +102,12 @@ Parameters: | ||
| 102 | + `name` (required) - new project name | 102 | + `name` (required) - new project name |
| 103 | + `code` (optional) - new project code, uses project name if not set | 103 | + `code` (optional) - new project code, uses project name if not set |
| 104 | + `path` (optional) - new project path, uses project name if not set | 104 | + `path` (optional) - new project path, uses project name if not set |
| 105 | ++ `description (optional) - short project description | ||
| 106 | ++ `default_branch` (optional) - 'master' by default | ||
| 107 | ++ `issues_enabled` (optional) - enabled by default | ||
| 108 | ++ `wall_enabled` (optional) - enabled by default | ||
| 109 | ++ `merge_requests_enabled` (optional) - enabled by default | ||
| 110 | ++ `wiki_enabled` (optional) - enabled by default | ||
| 105 | 111 | ||
| 106 | Will return created project with status `201 Created` on success, or `404 Not | 112 | Will return created project with status `201 Created` on success, or `404 Not |
| 107 | found` on fail. | 113 | found` on fail. |
lib/api/projects.rb
| @@ -29,14 +29,24 @@ module Gitlab | @@ -29,14 +29,24 @@ module Gitlab | ||
| 29 | # name (required) - name for new project | 29 | # name (required) - name for new project |
| 30 | # code (optional) - code for new project, uses project name if not set | 30 | # code (optional) - code for new project, uses project name if not set |
| 31 | # path (optional) - path for new project, uses project name if not set | 31 | # path (optional) - path for new project, uses project name if not set |
| 32 | + # description (optional) - short project description | ||
| 33 | + # default_branch (optional) - 'master' by default | ||
| 34 | + # issues_enabled (optional) - enabled by default | ||
| 35 | + # wall_enabled (optional) - enabled by default | ||
| 36 | + # merge_requests_enabled (optional) - enabled by default | ||
| 37 | + # wiki_enabled (optional) - enabled by default | ||
| 32 | # Example Request | 38 | # Example Request |
| 33 | # POST /projects | 39 | # POST /projects |
| 34 | post do | 40 | post do |
| 35 | - project = {} | ||
| 36 | - project[:name] = params[:name] | ||
| 37 | - project[:code] = params[:code] || project[:name] | ||
| 38 | - project[:path] = params[:path] || project[:name] | ||
| 39 | - @project = Project.create_by_user(project, current_user) | 41 | + params[:code] ||= params[:name] |
| 42 | + params[:path] ||= params[:name] | ||
| 43 | + project_attrs = {} | ||
| 44 | + params.each_pair do |k ,v| | ||
| 45 | + if Project.attribute_names.include? k | ||
| 46 | + project_attrs[k] = v | ||
| 47 | + end | ||
| 48 | + end | ||
| 49 | + @project = Project.create_by_user(project_attrs, current_user) | ||
| 40 | if @project.saved? | 50 | if @project.saved? |
| 41 | present @project, with: Entities::Project | 51 | present @project, with: Entities::Project |
| 42 | else | 52 | else |
spec/factories.rb
| @@ -11,6 +11,9 @@ module Factory | @@ -11,6 +11,9 @@ module Factory | ||
| 11 | def self.new(type, *args) | 11 | def self.new(type, *args) |
| 12 | FactoryGirl.build(type, *args) | 12 | FactoryGirl.build(type, *args) |
| 13 | end | 13 | end |
| 14 | + def self.attributes(type, *args) | ||
| 15 | + FactoryGirl.attributes_for(type, *args) | ||
| 16 | + end | ||
| 14 | end | 17 | end |
| 15 | 18 | ||
| 16 | FactoryGirl.define do | 19 | FactoryGirl.define do |
spec/requests/api/projects_spec.rb
| @@ -27,38 +27,40 @@ describe Gitlab::API do | @@ -27,38 +27,40 @@ describe Gitlab::API do | ||
| 27 | 27 | ||
| 28 | describe "POST /projects" do | 28 | describe "POST /projects" do |
| 29 | it "should create new project without code and path" do | 29 | it "should create new project without code and path" do |
| 30 | - lambda { | ||
| 31 | - name = "foo" | ||
| 32 | - post api("/projects", user), { | ||
| 33 | - name: name | ||
| 34 | - } | ||
| 35 | - response.status.should == 201 | ||
| 36 | - json_response["name"].should == name | ||
| 37 | - json_response["code"].should == name | ||
| 38 | - json_response["path"].should == name | ||
| 39 | - }.should change{Project.count}.by(1) | ||
| 40 | - end | ||
| 41 | - it "should create new project" do | ||
| 42 | - lambda { | ||
| 43 | - name = "foo" | ||
| 44 | - path = "bar" | ||
| 45 | - code = "bazz" | ||
| 46 | - post api("/projects", user), { | ||
| 47 | - code: code, | ||
| 48 | - path: path, | ||
| 49 | - name: name | ||
| 50 | - } | ||
| 51 | - response.status.should == 201 | ||
| 52 | - json_response["name"].should == name | ||
| 53 | - json_response["path"].should == path | ||
| 54 | - json_response["code"].should == code | ||
| 55 | - }.should change{Project.count}.by(1) | ||
| 56 | - end | ||
| 57 | - it "should not create project without name" do | ||
| 58 | - lambda { | ||
| 59 | - post api("/projects", user) | ||
| 60 | - response.status.should == 404 | ||
| 61 | - }.should_not change{Project.count} | 30 | + expect { post api("/projects", user), name: 'foo' }.to change {Project.count}.by(1) |
| 31 | + end | ||
| 32 | + | ||
| 33 | + it "should not create new project without name" do | ||
| 34 | + expect { post api("/projects", user) }.to_not change {Project.count} | ||
| 35 | + end | ||
| 36 | + | ||
| 37 | + it "should respond with 201 on success" do | ||
| 38 | + post api("/projects", user), name: 'foo' | ||
| 39 | + response.status.should == 201 | ||
| 40 | + end | ||
| 41 | + | ||
| 42 | + it "should repsond with 404 on failure" do | ||
| 43 | + post api("/projects", user) | ||
| 44 | + response.status.should == 404 | ||
| 45 | + end | ||
| 46 | + | ||
| 47 | + it "should assign attributes to project" do | ||
| 48 | + project = Factory.attributes(:project, { | ||
| 49 | + path: 'path', | ||
| 50 | + code: 'code', | ||
| 51 | + description: Faker::Lorem.sentence, | ||
| 52 | + default_branch: 'stable', | ||
| 53 | + issues_enabled: false, | ||
| 54 | + wall_enabled: false, | ||
| 55 | + merge_requests_enabled: false, | ||
| 56 | + wiki_enabled: false | ||
| 57 | + }) | ||
| 58 | + | ||
| 59 | + post api("/projects", user), project | ||
| 60 | + | ||
| 61 | + project.each_pair do |k,v| | ||
| 62 | + json_response[k.to_s].should == v | ||
| 63 | + end | ||
| 62 | end | 64 | end |
| 63 | end | 65 | end |
| 64 | 66 |