From 25b745271a1db25fc709ae13737b04c1f94cdb95 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Fri, 21 Sep 2012 03:23:17 -0700 Subject: [PATCH] add API for project team members --- doc/api/projects.md | 65 +++++++++++++------- lib/api/entities.rb | 7 ++- lib/api/projects.rb | 95 +++++++++++++++++++----------- spec/requests/api/projects_spec.rb | 54 ++++++++++------- 4 files changed, 141 insertions(+), 80 deletions(-) diff --git a/doc/api/projects.md b/doc/api/projects.md index 53ee5d41..1e23df3a 100644 --- a/doc/api/projects.md +++ b/doc/api/projects.md @@ -112,66 +112,89 @@ Parameters: Will return created project with status `201 Created` on success, or `404 Not 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: + `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 - -Add users to exiting project +Get a project team member. ``` -POST /projects/:id/users +GET /projects/:id/members/:user_id ``` Parameters: + `id` (required) - The ID or code name of a project -+ `user_ids` (required) - The ID list of users to add -+ `project_access` (required) - Project access level ++ `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 +} +``` + +## Add project team member + +Add a user to a project team. + +``` +POST /projects/:id/members +``` + +Parameters: + ++ `id` (required) - The ID or code name of a project ++ `user_id` (required) - The ID of a user to add ++ `access_level` (required) - Project access level 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: + `id` (required) - The ID or code name of a project -+ `user_ids` (required) - The ID list of users to add -+ `project_access` (required) - Project access level ++ `user_id` (required) - The ID of a team member ++ `access_level` (required) - Project access level 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: + `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 @@ -344,7 +367,7 @@ Parameters: + `id` (required) - The ID or code name of a project + `sha` (required) - The commit or branch name -+ `filepath` (required) - The path the file ++ `filepath` (required) - The path the file Will return the raw file contents. diff --git a/lib/api/entities.rb b/lib/api/entities.rb index a8b786ae..9e572324 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -24,9 +24,10 @@ module Gitlab expose :issues_enabled, :merge_requests_enabled, :wall_enabled, :wiki_enabled, :created_at end - class UsersProject < Grape::Entity - expose :user, using: Entities::UserBasic - expose :project_access + class ProjectMember < UserBasic + expose :project_access, :as => :access_level do |user, options| + options[:project].users_projects.find_by_user_id(user.id).project_access + end end class RepoObject < Grape::Entity diff --git a/lib/api/projects.rb b/lib/api/projects.rb index 1d9004f8..456b8a77 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -40,14 +40,14 @@ module Gitlab post do params[:code] ||= params[:name] params[:path] ||= params[:name] - attrs = attributes_for_keys [:code, - :path, - :name, - :description, - :default_branch, - :issues_enabled, - :wall_enabled, - :merge_requests_enabled, + attrs = attributes_for_keys [:code, + :path, + :name, + :description, + :default_branch, + :issues_enabled, + :wall_enabled, + :merge_requests_enabled, :wiki_enabled] @project = Project.create_by_user(attrs, current_user) if @project.saved? @@ -57,56 +57,83 @@ module Gitlab end end - # Get project users + # Get a project team members # # Parameters: # id (required) - The ID or code name of a project # Example Request: - # GET /projects/:id/users - get ":id/users" do - @users_projects = paginate user_project.users_projects - present @users_projects, with: Entities::UsersProject + # GET /projects/:id/members + get ":id/members" do + @members = paginate user_project.users + present @members, with: Entities::ProjectMember, project: user_project end - # Add users to project with specified access level + # Get a project team members # # Parameters: # id (required) - The ID or code name of a project - # user_ids (required) - The ID list of users to add - # project_access (required) - Project access level + # user_id (required) - The ID of a user # Example Request: - # POST /projects/:id/users - post ":id/users" do - authorize! :admin_project, user_project - user_project.add_users_ids_to_team(params[:user_ids].values, params[:project_access]) - nil + # GET /projects/:id/members/:user_id + get ":id/members/:user_id" do + @member = user_project.users.find params[:user_id] + present @member, with: Entities::ProjectMember, project: user_project end - # Update users to specified access level + # Add a new project team member # # Parameters: # id (required) - The ID or code name of a project - # user_ids (required) - The ID list of users to add - # project_access (required) - New project access level to + # user_id (required) - The ID of a user + # access_level (required) - Project access level # Example Request: - # PUT /projects/:id/add_users - put ":id/users" do + # POST /projects/:id/members + post ":id/members" do authorize! :admin_project, user_project - user_project.update_users_ids_to_role(params[:user_ids].values, params[:project_access]) - nil + users_project = user_project.users_projects.new( + 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 - # Delete project users + # Update project team member # # Parameters: # 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 + # access_level (required) - Project access level # Example Request: - # DELETE /projects/:id/users - delete ":id/users" do + # PUT /projects/:id/members/:user_id + put ":id/members/:user_id" do authorize! :admin_project, user_project - user_project.delete_users_ids_from_team(params[:user_ids].values) - nil + users_project = user_project.users_projects.find_by_user_id params[:user_id] + + 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 + + # Remove a team member from project + # + # Parameters: + # id (required) - The ID or code name of a project + # user_id (required) - The ID of a team member + # Example Request: + # DELETE /projects/:id/members/:user_id + delete ":id/members/:user_id" do + authorize! :admin_project, user_project + users_project = user_project.users_projects.find_by_user_id params[:user_id] + users_project.destroy end # Get project hooks diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb index b46380b2..40a9147d 100644 --- a/spec/requests/api/projects_spec.rb +++ b/spec/requests/api/projects_spec.rb @@ -111,42 +111,52 @@ describe Gitlab::API do end end - describe "GET /projects/:id/users" do - it "should return project users" do - get api("/projects/#{project.code}/users", user) - + describe "GET /projects/:id/members" do + it "should return project team members" do + get api("/projects/#{project.code}/members", user) response.status.should == 200 - json_response.should be_an Array json_response.count.should == 2 - json_response.first['user']['id'].should == user.id + json_response.first['email'].should == user.email end end - describe "POST /projects/:id/users" do - it "should add users to project" do - expect { - post api("/projects/#{project.code}/users", user), - user_ids: {"0" => user2.id}, project_access: UsersProject::DEVELOPER - }.to change {project.users_projects.where(:project_access => UsersProject::DEVELOPER).count}.by(1) + describe "GET /projects/:id/members/:user_id" do + it "should return project team member" do + get api("/projects/#{project.code}/members/#{user.id}", user) + response.status.should == 200 + json_response['email'].should == user.email + json_response['access_level'].should == UsersProject::MASTER end end - describe "PUT /projects/:id/users" do - it "should update users to new access role" do + describe "POST /projects/:id/members" do + it "should add user to project team" do expect { - put api("/projects/#{project.code}/users", user), - user_ids: {"0" => user3.id}, project_access: UsersProject::MASTER - }.to change {project.users_projects.where(:project_access => UsersProject::MASTER).count}.by(1) + post api("/projects/#{project.code}/members", user), user_id: user2.id, + access_level: UsersProject::DEVELOPER + }.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 "DELETE /projects/:id/users" do - it "should delete users from project" do + 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 + + describe "DELETE /projects/:id/members/:user_id" do + it "should remove user from project team" do expect { - delete api("/projects/#{project.code}/users", user), - user_ids: {"0" => user3.id} - }.to change {project.users_projects.count}.by(-1) + delete api("/projects/#{project.code}/members/#{user3.id}", user) + }.to change { UsersProject.count }.by(-1) end end