Commit 909c8c345dff0851e15d81917efe7817c7f89e22
1 parent
2dc05192
Exists in
master
and in
4 other branches
Make project users API more RESTful
Showing
4 changed files
with
111 additions
and
8 deletions
Show diff stats
doc/api/projects.md
| @@ -106,12 +106,26 @@ Parameters: | @@ -106,12 +106,26 @@ Parameters: | ||
| 106 | Will return created project with status `201 Created` on success, or `404 Not | 106 | Will return created project with status `201 Created` on success, or `404 Not |
| 107 | found` on fail. | 107 | found` on fail. |
| 108 | 108 | ||
| 109 | +## Get project users | ||
| 110 | + | ||
| 111 | +Get users and access roles for existing project | ||
| 112 | + | ||
| 113 | +``` | ||
| 114 | +GET /projects/:id/users | ||
| 115 | +``` | ||
| 116 | + | ||
| 117 | +Parameters: | ||
| 118 | + | ||
| 119 | ++ `id` (required) - The ID or code name of a project | ||
| 120 | + | ||
| 121 | +Will return users and their access roles with status `200 OK` on success, or `404 Not found` on fail. | ||
| 122 | + | ||
| 109 | ## Add project users | 123 | ## Add project users |
| 110 | 124 | ||
| 111 | Add users to exiting project | 125 | Add users to exiting project |
| 112 | 126 | ||
| 113 | ``` | 127 | ``` |
| 114 | -PUT /projects/:id/add_users | 128 | +POST /projects/:id/users |
| 115 | ``` | 129 | ``` |
| 116 | 130 | ||
| 117 | Parameters: | 131 | Parameters: |
| @@ -120,7 +134,38 @@ Parameters: | @@ -120,7 +134,38 @@ Parameters: | ||
| 120 | + `user_ids` (required) - The ID list of users to add | 134 | + `user_ids` (required) - The ID list of users to add |
| 121 | + `project_access` (required) - Project access level | 135 | + `project_access` (required) - Project access level |
| 122 | 136 | ||
| 123 | -Will return updated project with status `200 OK` on success, or `404 Not found` on fail. | 137 | +Will return status `201 Created` on success, or `404 Not found` on fail. |
| 138 | + | ||
| 139 | +## Update project users access level | ||
| 140 | + | ||
| 141 | +Update existing users to specified access level | ||
| 142 | + | ||
| 143 | +``` | ||
| 144 | +PUT /projects/:id/users | ||
| 145 | +``` | ||
| 146 | + | ||
| 147 | +Parameters: | ||
| 148 | + | ||
| 149 | ++ `id` (required) - The ID or code name of a project | ||
| 150 | ++ `user_ids` (required) - The ID list of users to add | ||
| 151 | ++ `project_access` (required) - Project access level | ||
| 152 | + | ||
| 153 | +Will return status `200 OK` on success, or `404 Not found` on fail. | ||
| 154 | + | ||
| 155 | +## Delete project users | ||
| 156 | + | ||
| 157 | +Delete users from exiting project | ||
| 158 | + | ||
| 159 | +``` | ||
| 160 | +DELETE /projects/:id/users | ||
| 161 | +``` | ||
| 162 | + | ||
| 163 | +Parameters: | ||
| 164 | + | ||
| 165 | ++ `id` (required) - The ID or code name of a project | ||
| 166 | ++ `user_ids` (required) - The ID list of users to add | ||
| 167 | + | ||
| 168 | +Will return status `200 OK` on success, or `404 Not found` on fail. | ||
| 124 | 169 | ||
| 125 | ## Project repository branches | 170 | ## Project repository branches |
| 126 | 171 |
lib/api/entities.rb
| @@ -16,6 +16,11 @@ module Gitlab | @@ -16,6 +16,11 @@ module Gitlab | ||
| 16 | expose :issues_enabled, :merge_requests_enabled, :wall_enabled, :wiki_enabled, :created_at | 16 | expose :issues_enabled, :merge_requests_enabled, :wall_enabled, :wiki_enabled, :created_at |
| 17 | end | 17 | end |
| 18 | 18 | ||
| 19 | + class UsersProject < Grape::Entity | ||
| 20 | + expose :user, using: Entities::UserBasic | ||
| 21 | + expose :project_access | ||
| 22 | + end | ||
| 23 | + | ||
| 19 | class RepoObject < Grape::Entity | 24 | class RepoObject < Grape::Entity |
| 20 | expose :name, :commit | 25 | expose :name, :commit |
| 21 | end | 26 | end |
lib/api/projects.rb
| @@ -44,6 +44,17 @@ module Gitlab | @@ -44,6 +44,17 @@ module Gitlab | ||
| 44 | end | 44 | end |
| 45 | end | 45 | end |
| 46 | 46 | ||
| 47 | + # Get project users | ||
| 48 | + # | ||
| 49 | + # Parameters: | ||
| 50 | + # id (required) - The ID or code name of a project | ||
| 51 | + # Example Request: | ||
| 52 | + # GET /projects/:id/users | ||
| 53 | + get ":id/users" do | ||
| 54 | + @users_projects = paginate user_project.users_projects | ||
| 55 | + present @users_projects, with: Entities::UsersProject | ||
| 56 | + end | ||
| 57 | + | ||
| 47 | # Add users to project with specified access level | 58 | # Add users to project with specified access level |
| 48 | # | 59 | # |
| 49 | # Parameters: | 60 | # Parameters: |
| @@ -51,11 +62,34 @@ module Gitlab | @@ -51,11 +62,34 @@ module Gitlab | ||
| 51 | # user_ids (required) - The ID list of users to add | 62 | # user_ids (required) - The ID list of users to add |
| 52 | # project_access (required) - Project access level | 63 | # project_access (required) - Project access level |
| 53 | # Example Request: | 64 | # Example Request: |
| 54 | - # PUT /projects/:id/add_users | ||
| 55 | - put ":id/add_users" do | 65 | + # POST /projects/:id/users |
| 66 | + post ":id/users" do | ||
| 56 | user_project.add_users_ids_to_team(params[:user_ids].values, params[:project_access]) | 67 | user_project.add_users_ids_to_team(params[:user_ids].values, params[:project_access]) |
| 57 | end | 68 | end |
| 58 | 69 | ||
| 70 | + # Update users to specified access level | ||
| 71 | + # | ||
| 72 | + # Parameters: | ||
| 73 | + # id (required) - The ID or code name of a project | ||
| 74 | + # user_ids (required) - The ID list of users to add | ||
| 75 | + # project_access (required) - New project access level to | ||
| 76 | + # Example Request: | ||
| 77 | + # PUT /projects/:id/add_users | ||
| 78 | + put ":id/users" do | ||
| 79 | + user_project.update_users_ids_to_role(params[:user_ids].values, params[:project_access]) | ||
| 80 | + end | ||
| 81 | + | ||
| 82 | + # Delete project users | ||
| 83 | + # | ||
| 84 | + # Parameters: | ||
| 85 | + # id (required) - The ID or code name of a project | ||
| 86 | + # user_ids (required) - The ID list of users to delete | ||
| 87 | + # Example Request: | ||
| 88 | + # DELETE /projects/:id/users | ||
| 89 | + delete ":id/users" do | ||
| 90 | + user_project.delete_users_ids_from_team(params[:user_ids].values) | ||
| 91 | + end | ||
| 92 | + | ||
| 59 | # Get a project repository branches | 93 | # Get a project repository branches |
| 60 | # | 94 | # |
| 61 | # Parameters: | 95 | # Parameters: |
spec/requests/api/projects_spec.rb
| @@ -8,6 +8,7 @@ describe Gitlab::API do | @@ -8,6 +8,7 @@ describe Gitlab::API do | ||
| 8 | let(:user3) { Factory.create(:user) } | 8 | let(:user3) { Factory.create(:user) } |
| 9 | let!(:project) { Factory :project, owner: user } | 9 | let!(:project) { Factory :project, owner: user } |
| 10 | let!(:snippet) { Factory :snippet, author: user, project: project, title: 'example' } | 10 | let!(:snippet) { Factory :snippet, author: user, project: project, title: 'example' } |
| 11 | + let!(:users_project) { Factory :users_project, user: user, project: project } | ||
| 11 | before { project.add_access(user, :read) } | 12 | before { project.add_access(user, :read) } |
| 12 | 13 | ||
| 13 | describe "GET /projects" do | 14 | describe "GET /projects" do |
| @@ -64,12 +65,30 @@ describe Gitlab::API do | @@ -64,12 +65,30 @@ describe Gitlab::API do | ||
| 64 | end | 65 | end |
| 65 | end | 66 | end |
| 66 | 67 | ||
| 67 | - describe "PUT /projects/:id/add_users" do | ||
| 68 | - it "should add users to existing project" do | 68 | + describe "POST /projects/:id/users" do |
| 69 | + it "should add users to project" do | ||
| 69 | expect { | 70 | expect { |
| 70 | - put api("/projects/#{project.code}/add_users", user), | 71 | + post api("/projects/#{project.code}/users", user), |
| 71 | user_ids: {"0" => user2.id, "1" => user3.id}, project_access: UsersProject::DEVELOPER | 72 | user_ids: {"0" => user2.id, "1" => user3.id}, project_access: UsersProject::DEVELOPER |
| 72 | - }.to change {Project.last.users_projects.where(:project_access => UsersProject::DEVELOPER).count}.by(2) | 73 | + }.to change {project.users_projects.where(:project_access => UsersProject::DEVELOPER).count}.by(2) |
| 74 | + end | ||
| 75 | + end | ||
| 76 | + | ||
| 77 | + describe "PUT /projects/:id/users" do | ||
| 78 | + it "should update users to new access role" do | ||
| 79 | + expect { | ||
| 80 | + put api("/projects/#{project.code}/users", user), | ||
| 81 | + user_ids: {"0" => user}, project_access: UsersProject::DEVELOPER | ||
| 82 | + }.to change {project.users_projects.where(:project_access => UsersProject::DEVELOPER).count}.by(1) | ||
| 83 | + end | ||
| 84 | + end | ||
| 85 | + | ||
| 86 | + describe "DELETE /projects/:id/users" do | ||
| 87 | + it "should delete users from project" do | ||
| 88 | + expect { | ||
| 89 | + delete api("/projects/#{project.code}/delete", user), | ||
| 90 | + user_ids: {"0" => users_project.id} | ||
| 91 | + }.to change {project.users_projects.where(:project_access => UsersProject::DEVELOPER).count}.by(-1) | ||
| 73 | end | 92 | end |
| 74 | end | 93 | end |
| 75 | 94 |