Commit 0f604e62fb453f2359ebc0438fe7dfaff8e55d10
1 parent
0a6b64e6
Exists in
master
and in
4 other branches
refactor projects API
Showing
3 changed files
with
13 additions
and
21 deletions
Show diff stats
lib/api/entities.rb
| ... | ... | @@ -16,11 +16,7 @@ module Gitlab |
| 16 | 16 | expose :issues_enabled, :merge_requests_enabled, :wall_enabled, :wiki_enabled, :created_at |
| 17 | 17 | end |
| 18 | 18 | |
| 19 | - class ProjectRepositoryBranches < Grape::Entity | |
| 20 | - expose :name, :commit | |
| 21 | - end | |
| 22 | - | |
| 23 | - class ProjectRepositoryTags < Grape::Entity | |
| 19 | + class RepoObject < Grape::Entity | |
| 24 | 20 | expose :name, :commit |
| 25 | 21 | end |
| 26 | 22 | ... | ... |
lib/api/helpers.rb
| ... | ... | @@ -4,6 +4,10 @@ module Gitlab |
| 4 | 4 | @current_user ||= User.find_by_authentication_token(params[:private_token]) |
| 5 | 5 | end |
| 6 | 6 | |
| 7 | + def user_project | |
| 8 | + @project ||= current_user.projects.find_by_code(params[:id]) | |
| 9 | + end | |
| 10 | + | |
| 7 | 11 | def authenticate! |
| 8 | 12 | error!({'message' => '401 Unauthorized'}, 401) unless current_user |
| 9 | 13 | end | ... | ... |
lib/api/projects.rb
| ... | ... | @@ -20,8 +20,7 @@ module Gitlab |
| 20 | 20 | # Example Request: |
| 21 | 21 | # GET /projects/:id |
| 22 | 22 | get ":id" do |
| 23 | - @project = current_user.projects.find_by_code(params[:id]) | |
| 24 | - present @project, :with => Entities::Project | |
| 23 | + present user_project, :with => Entities::Project | |
| 25 | 24 | end |
| 26 | 25 | |
| 27 | 26 | # Get a project repository branches |
| ... | ... | @@ -31,8 +30,7 @@ module Gitlab |
| 31 | 30 | # Example Request: |
| 32 | 31 | # GET /projects/:id/repository/branches |
| 33 | 32 | get ":id/repository/branches" do |
| 34 | - @project = current_user.projects.find_by_code(params[:id]) | |
| 35 | - present @project.repo.heads.sort_by(&:name), :with => Entities::ProjectRepositoryBranches | |
| 33 | + present user_project.repo.heads.sort_by(&:name), :with => Entities::RepoObject | |
| 36 | 34 | end |
| 37 | 35 | |
| 38 | 36 | # Get a project repository tags |
| ... | ... | @@ -42,8 +40,7 @@ module Gitlab |
| 42 | 40 | # Example Request: |
| 43 | 41 | # GET /projects/:id/repository/tags |
| 44 | 42 | get ":id/repository/tags" do |
| 45 | - @project = current_user.projects.find_by_code(params[:id]) | |
| 46 | - present @project.repo.tags.sort_by(&:name).reverse, :with => Entities::ProjectRepositoryTags | |
| 43 | + present user_project.repo.tags.sort_by(&:name).reverse, :with => Entities::RepoObject | |
| 47 | 44 | end |
| 48 | 45 | |
| 49 | 46 | # Get a project snippet |
| ... | ... | @@ -54,8 +51,7 @@ module Gitlab |
| 54 | 51 | # Example Request: |
| 55 | 52 | # GET /projects/:id/snippets/:snippet_id |
| 56 | 53 | get ":id/snippets/:snippet_id" do |
| 57 | - @project = current_user.projects.find_by_code(params[:id]) | |
| 58 | - @snippet = @project.snippets.find(params[:snippet_id]) | |
| 54 | + @snippet = user_project.snippets.find(params[:snippet_id]) | |
| 59 | 55 | present @snippet, :with => Entities::ProjectSnippet |
| 60 | 56 | end |
| 61 | 57 | |
| ... | ... | @@ -70,8 +66,7 @@ module Gitlab |
| 70 | 66 | # Example Request: |
| 71 | 67 | # POST /projects/:id/snippets |
| 72 | 68 | post ":id/snippets" do |
| 73 | - @project = current_user.projects.find_by_code(params[:id]) | |
| 74 | - @snippet = @project.snippets.new( | |
| 69 | + @snippet = user_project.snippets.new( | |
| 75 | 70 | :title => params[:title], |
| 76 | 71 | :file_name => params[:file_name], |
| 77 | 72 | :expires_at => params[:lifetime], |
| ... | ... | @@ -98,8 +93,7 @@ module Gitlab |
| 98 | 93 | # Example Request: |
| 99 | 94 | # PUT /projects/:id/snippets/:snippet_id |
| 100 | 95 | put ":id/snippets/:snippet_id" do |
| 101 | - @project = current_user.projects.find_by_code(params[:id]) | |
| 102 | - @snippet = @project.snippets.find(params[:snippet_id]) | |
| 96 | + @snippet = user_project.snippets.find(params[:snippet_id]) | |
| 103 | 97 | parameters = { |
| 104 | 98 | :title => (params[:title] || @snippet.title), |
| 105 | 99 | :file_name => (params[:file_name] || @snippet.file_name), |
| ... | ... | @@ -122,8 +116,7 @@ module Gitlab |
| 122 | 116 | # Example Request: |
| 123 | 117 | # DELETE /projects/:id/snippets/:snippet_id |
| 124 | 118 | delete ":id/snippets/:snippet_id" do |
| 125 | - @project = current_user.projects.find_by_code(params[:id]) | |
| 126 | - @snippet = @project.snippets.find(params[:snippet_id]) | |
| 119 | + @snippet = user_project.snippets.find(params[:snippet_id]) | |
| 127 | 120 | @snippet.destroy |
| 128 | 121 | end |
| 129 | 122 | |
| ... | ... | @@ -135,8 +128,7 @@ module Gitlab |
| 135 | 128 | # Example Request: |
| 136 | 129 | # GET /projects/:id/snippets/:snippet_id/raw |
| 137 | 130 | get ":id/snippets/:snippet_id/raw" do |
| 138 | - @project = current_user.projects.find_by_code(params[:id]) | |
| 139 | - @snippet = @project.snippets.find(params[:snippet_id]) | |
| 131 | + @snippet = user_project.snippets.find(params[:snippet_id]) | |
| 140 | 132 | present @snippet.content |
| 141 | 133 | end |
| 142 | 134 | end | ... | ... |