Commit cc03600b22342a541ab1b4617eaf4d38d6d2d1da
Exists in
master
and in
4 other branches
Merge pull request #1347 from AlexDenisov/api_project_creation
API for new project creation
Showing
3 changed files
with
76 additions
and
0 deletions
Show diff stats
doc/api/projects.md
@@ -89,6 +89,24 @@ Parameters: | @@ -89,6 +89,24 @@ Parameters: | ||
89 | } | 89 | } |
90 | ``` | 90 | ``` |
91 | 91 | ||
92 | +## Create project | ||
93 | + | ||
94 | +Create new project owned by user | ||
95 | + | ||
96 | +``` | ||
97 | +POST /projects | ||
98 | +``` | ||
99 | + | ||
100 | +Parameters: | ||
101 | + | ||
102 | ++ `name` (required) - new project name | ||
103 | ++ `code` (optional) - new project code, uses project name if not set | ||
104 | ++ `path` (optional) - new project path, uses project name if not set | ||
105 | + | ||
106 | +Will return created project with status `201 Created` on success, or `404 Not | ||
107 | +found` on fail. | ||
108 | + | ||
109 | + | ||
92 | ## Project repository branches | 110 | ## Project repository branches |
93 | 111 | ||
94 | Get a list of project repository branches sorted by name alphabetically. | 112 | Get a list of project repository branches sorted by name alphabetically. |
lib/api/projects.rb
@@ -23,6 +23,27 @@ module Gitlab | @@ -23,6 +23,27 @@ module Gitlab | ||
23 | present user_project, with: Entities::Project | 23 | present user_project, with: Entities::Project |
24 | end | 24 | end |
25 | 25 | ||
26 | + # Create new project | ||
27 | + # | ||
28 | + # Parameters: | ||
29 | + # name (required) - name for new project | ||
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 | ||
32 | + # Example Request | ||
33 | + # POST /projects | ||
34 | + 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) | ||
40 | + if @project.saved? | ||
41 | + present @project, with: Entities::Project | ||
42 | + else | ||
43 | + error!({'message' => '404 Not found'}, 404) | ||
44 | + end | ||
45 | + end | ||
46 | + | ||
26 | # Get a project repository branches | 47 | # Get a project repository branches |
27 | # | 48 | # |
28 | # Parameters: | 49 | # Parameters: |
spec/requests/api/projects_spec.rb
@@ -25,6 +25,43 @@ describe Gitlab::API do | @@ -25,6 +25,43 @@ describe Gitlab::API do | ||
25 | end | 25 | end |
26 | end | 26 | end |
27 | 27 | ||
28 | + describe "POST /projects" 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} | ||
62 | + end | ||
63 | + end | ||
64 | + | ||
28 | describe "GET /projects/:id" do | 65 | describe "GET /projects/:id" do |
29 | it "should return a project by id" do | 66 | it "should return a project by id" do |
30 | get api("/projects/#{project.id}", user) | 67 | get api("/projects/#{project.id}", user) |