From 1b95c8bff351f6718ec31ac1de1e48c57bc95d44 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Wed, 25 Jul 2012 02:18:30 -0700 Subject: [PATCH 1/4] API: ability to get project by id --- lib/api/helpers.rb | 3 ++- spec/api/projects_spec.rb | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index e7b9a417..4dfe35f4 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -5,7 +5,8 @@ module Gitlab end def user_project - @project ||= current_user.projects.find_by_code(params[:id]) + @project ||= current_user.projects.find_by_id(params[:id]) || + current_user.projects.find_by_code(params[:id]) end def authenticate! diff --git a/spec/api/projects_spec.rb b/spec/api/projects_spec.rb index 9998ee50..92a25543 100644 --- a/spec/api/projects_spec.rb +++ b/spec/api/projects_spec.rb @@ -25,11 +25,17 @@ describe Gitlab::API do describe "GET /projects/:id" do it "should return a project by id" do - get "#{api_prefix}/projects/#{project.code}?private_token=#{user.private_token}" + get "#{api_prefix}/projects/#{project.id}?private_token=#{user.private_token}" response.status.should == 200 json_response['name'].should == project.name json_response['owner']['email'].should == user.email end + + it "should return a project by code name" do + get "#{api_prefix}/projects/#{project.code}?private_token=#{user.private_token}" + response.status.should == 200 + json_response['name'].should == project.name + end end describe "GET /projects/:id/repository/branches" do From 949b1df930bedace1dbd755aaa4a82e8c451a616 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Wed, 25 Jul 2012 02:35:41 -0700 Subject: [PATCH 2/4] API: update docs --- doc/api/issues.md | 10 +++++----- doc/api/projects.md | 22 +++++++++++----------- lib/api/issues.rb | 10 +++++----- lib/api/projects.rb | 16 ++++++++-------- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/doc/api/issues.md b/doc/api/issues.md index bad2d474..0ce14503 100644 --- a/doc/api/issues.md +++ b/doc/api/issues.md @@ -73,7 +73,7 @@ GET /projects/:id/issues Parameters: -+ `id` (required) - The code name of a project ++ `id` (required) - The ID or code name of a project ## Single issue @@ -85,7 +85,7 @@ GET /projects/:id/issues/:issue_id Parameters: -+ `id` (required) - The code name of a project ++ `id` (required) - The ID or code name of a project + `issue_id` (required) - The ID of a project issue ```json @@ -135,7 +135,7 @@ POST /projects/:id/issues Parameters: -+ `id` (required) - The code name of a project ++ `id` (required) - The ID or code name of a project + `title` (required) - The title of an issue + `description` (optional) - The description of an issue + `assignee_id` (optional) - The ID of a user to assign issue @@ -154,7 +154,7 @@ PUT /projects/:id/issues/:issue_id Parameters: -+ `id` (required) - The code name of a project ++ `id` (required) - The ID or code name of a project + `issue_id` (required) - The ID of a project's issue + `title` (optional) - The title of an issue + `description` (optional) - The description of an issue @@ -175,7 +175,7 @@ DELETE /projects/:id/issues/:issue_id Parameters: -+ `id` (required) - The code name of a project ++ `id` (required) - The ID or code name of a project + `issue_id` (required) - The ID of a project's issue Status code `200` will be returned on success. diff --git a/doc/api/projects.md b/doc/api/projects.md index c748745e..cd94cd42 100644 --- a/doc/api/projects.md +++ b/doc/api/projects.md @@ -1,6 +1,6 @@ ## List projects -Get a list of authenticated users' projects. +Get a list of authenticated user's projects. ``` GET /projects @@ -63,7 +63,7 @@ GET /projects/:id Parameters: -+ `id` (required) - The code name of a project ++ `id` (required) - The ID or code name of a project ```json { @@ -91,7 +91,7 @@ Parameters: ## Project repository branches -Get a list of project repository branches. +Get a list of project repository branches sorted by name alphabetically. ``` GET /projects/:id/repository/branches @@ -99,7 +99,7 @@ GET /projects/:id/repository/branches Parameters: -+ `id` (required) - The code name of a project ++ `id` (required) - The ID or code name of a project ```json [ @@ -131,7 +131,7 @@ Parameters: ## Project repository tags -Get a list of project repository tags. +Get a list of project repository tags sorted by name in reverse alphabetical order. ``` GET /projects/:id/repository/tags @@ -139,7 +139,7 @@ GET /projects/:id/repository/tags Parameters: -+ `id` (required) - The code name of a project ++ `id` (required) - The ID or code name of a project ```json [ @@ -183,7 +183,7 @@ GET /projects/:id/snippets/:snippet_id Parameters: -+ `id` (required) - The code name of a project ++ `id` (required) - The ID or code name of a project + `snippet_id` (required) - The ID of a project's snippet ```json @@ -214,7 +214,7 @@ GET /projects/:id/snippets/:snippet_id/raw Parameters: -+ `id` (required) - The code name of a project ++ `id` (required) - The ID or code name of a project + `snippet_id` (required) - The ID of a project's snippet ## New snippet @@ -227,7 +227,7 @@ POST /projects/:id/snippets Parameters: -+ `id` (required) - The code name of a project ++ `id` (required) - The ID or code name of a project + `title` (required) - The title of a snippet + `file_name` (required) - The name of a snippet file + `lifetime` (optional) - The expiration date of a snippet @@ -245,7 +245,7 @@ PUT /projects/:id/snippets/:snippet_id Parameters: -+ `id` (required) - The code name of a project ++ `id` (required) - The ID or code name of a project + `snippet_id` (required) - The ID of a project's snippet + `title` (optional) - The title of a snippet + `file_name` (optional) - The name of a snippet file @@ -264,7 +264,7 @@ DELETE /projects/:id/snippets/:snippet_id Parameters: -+ `id` (required) - The code name of a project ++ `id` (required) - The ID or code name of a project + `snippet_id` (required) - The ID of a project's snippet Status code `200` will be returned on success. diff --git a/lib/api/issues.rb b/lib/api/issues.rb index 2b6a1ac2..dcb6e535 100644 --- a/lib/api/issues.rb +++ b/lib/api/issues.rb @@ -17,7 +17,7 @@ module Gitlab # Get a list of project issues # # Parameters: - # id (required) - The code name of a project + # id (required) - The ID or code name of a project # Example Request: # GET /projects/:id/issues get ":id/issues" do @@ -27,7 +27,7 @@ module Gitlab # Get a single project issue # # Parameters: - # id (required) - The code name of a project + # id (required) - The ID or code name of a project # issue_id (required) - The ID of a project issue # Example Request: # GET /projects/:id/issues/:issue_id @@ -39,7 +39,7 @@ module Gitlab # Create a new project issue # # Parameters: - # id (required) - The code name of a project + # id (required) - The ID or code name of a project # title (required) - The title of an issue # description (optional) - The description of an issue # assignee_id (optional) - The ID of a user to assign issue @@ -67,7 +67,7 @@ module Gitlab # Update an existing issue # # Parameters: - # id (required) - The code name of a project + # id (required) - The ID or code name of a project # issue_id (required) - The ID of a project issue # title (optional) - The title of an issue # description (optional) - The description of an issue @@ -98,7 +98,7 @@ module Gitlab # Delete a project issue # # Parameters: - # id (required) - The code name of a project + # id (required) - The ID or code name of a project # issue_id (required) - The ID of a project issue # Example Request: # DELETE /projects/:id/issues/:issue_id diff --git a/lib/api/projects.rb b/lib/api/projects.rb index 34d1c236..8edfa481 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -16,7 +16,7 @@ module Gitlab # Get a single project # # Parameters: - # id (required) - The code of a project + # id (required) - The ID or code name of a project # Example Request: # GET /projects/:id get ":id" do @@ -26,7 +26,7 @@ module Gitlab # Get a project repository branches # # Parameters: - # id (required) - The code of a project + # id (required) - The ID or code name of a project # Example Request: # GET /projects/:id/repository/branches get ":id/repository/branches" do @@ -36,7 +36,7 @@ module Gitlab # Get a project repository tags # # Parameters: - # id (required) - The code of a project + # id (required) - The ID or code name of a project # Example Request: # GET /projects/:id/repository/tags get ":id/repository/tags" do @@ -46,7 +46,7 @@ module Gitlab # Get a project snippet # # Parameters: - # id (required) - The code of a project + # id (required) - The ID or code name of a project # snippet_id (required) - The ID of a project snippet # Example Request: # GET /projects/:id/snippets/:snippet_id @@ -58,7 +58,7 @@ module Gitlab # Create a new project snippet # # Parameters: - # id (required) - The code name of a project + # id (required) - The ID or code name of a project # title (required) - The title of a snippet # file_name (required) - The name of a snippet file # lifetime (optional) - The expiration date of a snippet @@ -84,7 +84,7 @@ module Gitlab # Update an existing project snippet # # Parameters: - # id (required) - The code name of a project + # id (required) - The ID or code name of a project # snippet_id (required) - The ID of a project snippet # title (optional) - The title of a snippet # file_name (optional) - The name of a snippet file @@ -111,7 +111,7 @@ module Gitlab # Delete a project snippet # # Parameters: - # id (required) - The code of a project + # id (required) - The ID or code name of a project # snippet_id (required) - The ID of a project snippet # Example Request: # DELETE /projects/:id/snippets/:snippet_id @@ -123,7 +123,7 @@ module Gitlab # Get a raw project snippet # # Parameters: - # id (required) - The code of a project + # id (required) - The ID or code name of a project # snippet_id (required) - The ID of a project snippet # Example Request: # GET /projects/:id/snippets/:snippet_id/raw From f7dd067490fe57505f7226c3b54d3127d2f7fd46 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Wed, 25 Jul 2012 04:22:21 -0700 Subject: [PATCH 3/4] API: expose issues project id --- doc/api/issues.md | 3 +++ lib/api/entities.rb | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/doc/api/issues.md b/doc/api/issues.md index 0ce14503..aaad3305 100644 --- a/doc/api/issues.md +++ b/doc/api/issues.md @@ -10,6 +10,7 @@ GET /issues [ { "id": 43, + "project_id": 8, "title": "4xx/5xx pages", "description": "", "labels": [ ], @@ -28,6 +29,7 @@ GET /issues }, { "id": 42, + "project_id": 8, "title": "Add user settings", "description": "", "labels": [ @@ -91,6 +93,7 @@ Parameters: ```json { "id": 42, + "project_id": 8, "title": "Add user settings", "description": "", "labels": [ diff --git a/lib/api/entities.rb b/lib/api/entities.rb index e5095f71..d86fb79c 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -31,7 +31,9 @@ module Gitlab end class Issue < Grape::Entity - expose :id, :title, :description + expose :id + expose (:project_id) {|issue| issue.project.id} + expose :title, :description expose :label_list, :as => :labels expose :milestone, :using => Entities::Milestone expose :assignee, :author, :using => Entities::UserBasic From cb32e0320ab538b465574bb73c7bd3d32db56d3c Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Wed, 25 Jul 2012 05:24:28 -0700 Subject: [PATCH 4/4] return 404 if project not found --- lib/api/helpers.rb | 9 +++++++-- spec/api/projects_spec.rb | 6 ++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index 4dfe35f4..c1ea0566 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -5,8 +5,13 @@ module Gitlab end def user_project - @project ||= current_user.projects.find_by_id(params[:id]) || - current_user.projects.find_by_code(params[:id]) + if @project ||= current_user.projects.find_by_id(params[:id]) || + current_user.projects.find_by_code(params[:id]) + else + error!({'message' => '404 Not found'}, 404) + end + + @project end def authenticate! diff --git a/spec/api/projects_spec.rb b/spec/api/projects_spec.rb index 92a25543..8852a0d3 100644 --- a/spec/api/projects_spec.rb +++ b/spec/api/projects_spec.rb @@ -36,6 +36,12 @@ describe Gitlab::API do response.status.should == 200 json_response['name'].should == project.name end + + it "should return a 404 error if not found" do + get "#{api_prefix}/projects/42?private_token=#{user.private_token}" + response.status.should == 404 + json_response['message'].should == '404 Not found' + end end describe "GET /projects/:id/repository/branches" do