Commit 13155362 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge pull request #1521 from NARKOZ/api

API for project team members
parents 10d3a30b 25b74527
...@@ -112,66 +112,89 @@ Parameters: ...@@ -112,66 +112,89 @@ Parameters:
Will return created project with status `201 Created` on success, or `404 Not Will return created project with status `201 Created` on success, or `404 Not
found` on fail. found` on fail.
## Get project users ## List project team members
Get users and access roles for existing project Get a list of project team members.
``` ```
GET /projects/:id/users GET /projects/:id/members
``` ```
Parameters: Parameters:
+ `id` (required) - The ID or code name of a project + `id` (required) - The ID or code name of a project
Will return users and their access roles with status `200 OK` on success, or `404 Not found` on fail. ## Get project team member
## Add project users Get a project team member.
Add users to exiting project ```
GET /projects/:id/members/:user_id
```
Parameters:
+ `id` (required) - The ID or code name of a project
+ `user_id` (required) - The ID of a user
```json
{
"id": 1,
"email": "john@example.com",
"name": "John Smith",
"blocked": false,
"created_at": "2012-05-23T08:00:58Z",
"access_level": 40
}
``` ```
POST /projects/:id/users
## Add project team member
Add a user to a project team.
```
POST /projects/:id/members
``` ```
Parameters: Parameters:
+ `id` (required) - The ID or code name of a project + `id` (required) - The ID or code name of a project
+ `user_ids` (required) - The ID list of users to add + `user_id` (required) - The ID of a user to add
+ `project_access` (required) - Project access level + `access_level` (required) - Project access level
Will return status `201 Created` on success, or `404 Not found` on fail. Will return status `201 Created` on success, or `404 Not found` on fail.
## Update project users access level ## Edit project team member
Update existing users to specified access level Update project team member to specified access level.
``` ```
PUT /projects/:id/users PUT /projects/:id/members/:user_id
``` ```
Parameters: Parameters:
+ `id` (required) - The ID or code name of a project + `id` (required) - The ID or code name of a project
+ `user_ids` (required) - The ID list of users to add + `user_id` (required) - The ID of a team member
+ `project_access` (required) - Project access level + `access_level` (required) - Project access level
Will return status `200 OK` on success, or `404 Not found` on fail. Will return status `200 OK` on success, or `404 Not found` on fail.
## Delete project users ## Remove project team member
Delete users from exiting project Removes user from project team.
``` ```
DELETE /projects/:id/users DELETE /projects/:id/members/:user_id
``` ```
Parameters: Parameters:
+ `id` (required) - The ID or code name of a project + `id` (required) - The ID or code name of a project
+ `user_ids` (required) - The ID list of users to add + `user_id` (required) - The ID of a team member
Will return status `200 OK` on success, or `404 Not found` on fail. Status code `200` will be returned on success.
## Get project hooks ## Get project hooks
......
...@@ -29,9 +29,10 @@ module Gitlab ...@@ -29,9 +29,10 @@ module Gitlab
expose :issues_enabled, :merge_requests_enabled, :wall_enabled, :wiki_enabled, :created_at expose :issues_enabled, :merge_requests_enabled, :wall_enabled, :wiki_enabled, :created_at
end end
class UsersProject < Grape::Entity class ProjectMember < UserBasic
expose :user, using: Entities::UserBasic expose :project_access, :as => :access_level do |user, options|
expose :project_access options[:project].users_projects.find_by_user_id(user.id).project_access
end
end end
class RepoObject < Grape::Entity class RepoObject < Grape::Entity
......
...@@ -57,56 +57,83 @@ module Gitlab ...@@ -57,56 +57,83 @@ module Gitlab
end end
end end
# Get project users # Get a project team members
# #
# Parameters: # Parameters:
# id (required) - The ID or code name of a project # id (required) - The ID or code name of a project
# Example Request: # Example Request:
# GET /projects/:id/users # GET /projects/:id/members
get ":id/users" do get ":id/members" do
@users_projects = paginate user_project.users_projects @members = paginate user_project.users
present @users_projects, with: Entities::UsersProject present @members, with: Entities::ProjectMember, project: user_project
end end
# Add users to project with specified access level # Get a project team members
# #
# Parameters: # Parameters:
# id (required) - The ID or code name of a project # id (required) - The ID or code name of a project
# user_ids (required) - The ID list of users to add # user_id (required) - The ID of a user
# project_access (required) - Project access level
# Example Request: # Example Request:
# POST /projects/:id/users # GET /projects/:id/members/:user_id
post ":id/users" do get ":id/members/:user_id" do
@member = user_project.users.find params[:user_id]
present @member, with: Entities::ProjectMember, project: user_project
end
# Add a new project team member
#
# Parameters:
# id (required) - The ID or code name of a project
# user_id (required) - The ID of a user
# access_level (required) - Project access level
# Example Request:
# POST /projects/:id/members
post ":id/members" do
authorize! :admin_project, user_project authorize! :admin_project, user_project
user_project.add_users_ids_to_team(params[:user_ids].values, params[:project_access]) users_project = user_project.users_projects.new(
nil user_id: params[:user_id],
project_access: params[:access_level]
)
if users_project.save
@member = users_project.user
present @member, with: Entities::ProjectMember, project: user_project
else
not_found!
end
end end
# Update users to specified access level # Update project team member
# #
# Parameters: # Parameters:
# id (required) - The ID or code name of a project # id (required) - The ID or code name of a project
# user_ids (required) - The ID list of users to add # user_id (required) - The ID of a team member
# project_access (required) - New project access level to # access_level (required) - Project access level
# Example Request: # Example Request:
# PUT /projects/:id/add_users # PUT /projects/:id/members/:user_id
put ":id/users" do put ":id/members/:user_id" do
authorize! :admin_project, user_project authorize! :admin_project, user_project
user_project.update_users_ids_to_role(params[:user_ids].values, params[:project_access]) users_project = user_project.users_projects.find_by_user_id params[:user_id]
nil
if users_project.update_attributes(project_access: params[:access_level])
@member = users_project.user
present @member, with: Entities::ProjectMember, project: user_project
else
not_found!
end
end end
# Delete project users # Remove a team member from project
# #
# Parameters: # Parameters:
# id (required) - The ID or code name of a project # id (required) - The ID or code name of a project
# user_ids (required) - The ID list of users to delete # user_id (required) - The ID of a team member
# Example Request: # Example Request:
# DELETE /projects/:id/users # DELETE /projects/:id/members/:user_id
delete ":id/users" do delete ":id/members/:user_id" do
authorize! :admin_project, user_project authorize! :admin_project, user_project
user_project.delete_users_ids_from_team(params[:user_ids].values) users_project = user_project.users_projects.find_by_user_id params[:user_id]
nil users_project.destroy
end end
# Get project hooks # Get project hooks
......
...@@ -111,42 +111,52 @@ describe Gitlab::API do ...@@ -111,42 +111,52 @@ describe Gitlab::API do
end end
end end
describe "GET /projects/:id/users" do describe "GET /projects/:id/members" do
it "should return project users" do it "should return project team members" do
get api("/projects/#{project.code}/users", user) get api("/projects/#{project.code}/members", user)
response.status.should == 200 response.status.should == 200
json_response.should be_an Array json_response.should be_an Array
json_response.count.should == 2 json_response.count.should == 2
json_response.first['user']['id'].should == user.id json_response.first['email'].should == user.email
end end
end end
describe "POST /projects/:id/users" do describe "GET /projects/:id/members/:user_id" do
it "should add users to project" do it "should return project team member" do
expect { get api("/projects/#{project.code}/members/#{user.id}", user)
post api("/projects/#{project.code}/users", user), response.status.should == 200
user_ids: {"0" => user2.id}, project_access: UsersProject::DEVELOPER json_response['email'].should == user.email
}.to change {project.users_projects.where(:project_access => UsersProject::DEVELOPER).count}.by(1) json_response['access_level'].should == UsersProject::MASTER
end end
end end
describe "PUT /projects/:id/users" do describe "POST /projects/:id/members" do
it "should update users to new access role" do it "should add user to project team" do
expect { expect {
put api("/projects/#{project.code}/users", user), post api("/projects/#{project.code}/members", user), user_id: user2.id,
user_ids: {"0" => user3.id}, project_access: UsersProject::MASTER access_level: UsersProject::DEVELOPER
}.to change {project.users_projects.where(:project_access => UsersProject::MASTER).count}.by(1) }.to change { UsersProject.count }.by(1)
response.status.should == 201
json_response['email'].should == user2.email
json_response['access_level'].should == UsersProject::DEVELOPER
end
end
describe "PUT /projects/:id/members/:user_id" do
it "should update project team member" do
put api("/projects/#{project.code}/members/#{user3.id}", user), access_level: UsersProject::MASTER
response.status.should == 200
json_response['email'].should == user3.email
json_response['access_level'].should == UsersProject::MASTER
end end
end end
describe "DELETE /projects/:id/users" do describe "DELETE /projects/:id/members/:user_id" do
it "should delete users from project" do it "should remove user from project team" do
expect { expect {
delete api("/projects/#{project.code}/users", user), delete api("/projects/#{project.code}/members/#{user3.id}", user)
user_ids: {"0" => user3.id} }.to change { UsersProject.count }.by(-1)
}.to change {project.users_projects.count}.by(-1)
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment