From 9053d78e7451d5358b0ec66788916a488ce66a00 Mon Sep 17 00:00:00 2001 From: Felipe Artur Date: Wed, 1 Mar 2017 17:22:29 -0300 Subject: Update API endpoints for raw files --- lib/api/files.rb | 60 ++++++++++++++++++++++++++++++++----------------- lib/api/repositories.rb | 45 ++++++++++++++++++++++++++++--------- 2 files changed, 74 insertions(+), 31 deletions(-) (limited to 'lib') diff --git a/lib/api/files.rb b/lib/api/files.rb index 9c4e43d77cc..5983845d007 100644 --- a/lib/api/files.rb +++ b/lib/api/files.rb @@ -14,6 +14,19 @@ module API } end + def assign_file_vars! + @commit = user_project.commit(params[:ref]) + not_found!('Commit') unless @commit + + @repo = user_project.repository + @file_path = params[:file_path] + @file_path = [params[:file_path], params[:format]].join('.') if params[:format].present? + @blob = @repo.blob_at(@commit.sha, @file_path) + + not_found!('File') unless @blob + @blob.load_all_data!(@repo) + end + def commit_response(attrs) { file_path: attrs[:file_path], @@ -22,7 +35,7 @@ module API end params :simple_file_params do - requires :file_path, type: String, desc: 'The path to new file. Ex. lib/class.rb' + requires :file_path, type: String, desc: 'The path to the file. Ex. lib/class.rb' requires :branch, type: String, desc: 'The name of branch' requires :commit_message, type: String, desc: 'Commit Message' optional :author_email, type: String, desc: 'The email of the author' @@ -40,34 +53,41 @@ module API requires :id, type: String, desc: 'The project ID' end resource :projects do - desc 'Get a file from repository' + desc 'Get a file from repository in raw format' params do - requires :file_path, type: String, desc: 'The path to the file. Ex. lib/class.rb' requires :ref, type: String, desc: 'The name of branch, tag, or commit' end - get ":id/repository/files" do + get ":id/repository/files/:file_path/raw" do authorize! :download_code, user_project - commit = user_project.commit(params[:ref]) - not_found!('Commit') unless commit + assign_file_vars! + + status(200) + + send_git_blob @repo, @blob + end + + desc 'Get a file from repository in base64 format' + params do + requires :ref, type: String, desc: 'The name of branch, tag, or commit' + end + get ":id/repository/files/:file_path" do + authorize! :download_code, user_project - repo = user_project.repository - blob = repo.blob_at(commit.sha, params[:file_path]) - not_found!('File') unless blob + assign_file_vars! - blob.load_all_data!(repo) status(200) { - file_name: blob.name, - file_path: blob.path, - size: blob.size, + file_name: @blob.name, + file_path: @blob.path, + size: @blob.size, encoding: "base64", - content: Base64.strict_encode64(blob.data), + content: Base64.strict_encode64(@blob.data), ref: params[:ref], - blob_id: blob.id, - commit_id: commit.id, - last_commit_id: repo.last_commit_id_for_path(commit.sha, params[:file_path]) + blob_id: @blob.id, + commit_id: @commit.id, + last_commit_id: @repo.last_commit_id_for_path(@commit.sha, @file_path) } end @@ -75,7 +95,7 @@ module API params do use :extended_file_params end - post ":id/repository/files" do + post ":id/repository/files/:file_path", requirements: { file_path: /.+/ } do authorize! :push_code, user_project file_params = declared_params(include_missing: false) @@ -93,7 +113,7 @@ module API params do use :extended_file_params end - put ":id/repository/files" do + put ":id/repository/files/:file_path", requirements: { file_path: /.+/ } do authorize! :push_code, user_project file_params = declared_params(include_missing: false) @@ -112,7 +132,7 @@ module API params do use :simple_file_params end - delete ":id/repository/files" do + delete ":id/repository/files/:file_path", requirements: { file_path: /.+/ } do authorize! :push_code, user_project file_params = declared_params(include_missing: false) diff --git a/lib/api/repositories.rb b/lib/api/repositories.rb index 36166780149..3932512d8da 100644 --- a/lib/api/repositories.rb +++ b/lib/api/repositories.rb @@ -17,6 +17,18 @@ module API end not_found! end + + def assign_blob_vars! + @repo = user_project.repository + + begin + @blob = Gitlab::Git::Blob.raw(@repo, params[:sha]) + rescue + not_found! 'Blob' + end + + not_found! 'Blob' unless @blob + end end desc 'Get a project repository tree' do @@ -45,7 +57,7 @@ module API requires :sha, type: String, desc: 'The commit, branch name, or tag name' requires :filepath, type: String, desc: 'The path to the file to display' end - get [":id/repository/blobs/:sha", ":id/repository/commits/:sha/blob"] do + get ":id/repository/commits/:sha/blob" do repo = user_project.repository commit = repo.commit(params[:sha]) @@ -57,22 +69,33 @@ module API send_git_blob repo, blob end - desc 'Get a raw blob contents by blob sha' + desc 'Get raw blob by sha' params do requires :sha, type: String, desc: 'The commit, branch name, or tag name' end - get ':id/repository/raw_blobs/:sha' do - repo = user_project.repository + get ':id/repository/blobs/:sha/:raw' do + assign_blob_vars! - begin - blob = Gitlab::Git::Blob.raw(repo, params[:sha]) - rescue - not_found! 'Blob' - end + status(200) - not_found! 'Blob' unless blob + send_git_blob @repo, @blob + end - send_git_blob repo, blob + desc 'Get blob base4 encoded content by sha' + params do + requires :sha, type: String, desc: 'The commit, branch name, or tag name' + end + get ':id/repository/blobs/:sha' do + assign_blob_vars! + + status(200) + + { + size: @blob.size, + encoding: "base64", + content: Base64.strict_encode64(@blob.data), + sha: @blob.id + } end desc 'Get an archive of the repository' -- cgit v1.2.3 From a61bb7cda3f31e2b32b53a26187079a6d6302845 Mon Sep 17 00:00:00 2001 From: Felipe Artur Date: Tue, 7 Mar 2017 21:14:33 -0300 Subject: Remove unecessary endpoint from repository, add compatibility endpoints for v3 and several improvements --- lib/api/files.rb | 32 +++++++++++---------------- lib/api/repositories.rb | 34 +++++++---------------------- lib/api/v3/repositories.rb | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 45 deletions(-) (limited to 'lib') diff --git a/lib/api/files.rb b/lib/api/files.rb index 5983845d007..bb8f5c3076d 100644 --- a/lib/api/files.rb +++ b/lib/api/files.rb @@ -15,13 +15,13 @@ module API end def assign_file_vars! + authorize! :download_code, user_project + @commit = user_project.commit(params[:ref]) not_found!('Commit') unless @commit - @repo = user_project.repository - @file_path = params[:file_path] - @file_path = [params[:file_path], params[:format]].join('.') if params[:format].present? - @blob = @repo.blob_at(@commit.sha, @file_path) + @repo = user_project.repository + @blob = @repo.blob_at(@commit.sha, params[:file_path]) not_found!('File') unless @blob @blob.load_all_data!(@repo) @@ -35,7 +35,7 @@ module API end params :simple_file_params do - requires :file_path, type: String, desc: 'The path to the file. Ex. lib/class.rb' + requires :file_path, type: String, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb' requires :branch, type: String, desc: 'The name of branch' requires :commit_message, type: String, desc: 'Commit Message' optional :author_email, type: String, desc: 'The email of the author' @@ -53,31 +53,25 @@ module API requires :id, type: String, desc: 'The project ID' end resource :projects do - desc 'Get a file from repository in raw format' + desc 'Get raw file contents from the repository' params do - requires :ref, type: String, desc: 'The name of branch, tag, or commit' + requires :file_path, type: String, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb' + requires :ref, type: String, desc: 'The name of branch, tag commit' end get ":id/repository/files/:file_path/raw" do - authorize! :download_code, user_project - assign_file_vars! - status(200) - send_git_blob @repo, @blob end - desc 'Get a file from repository in base64 format' + desc 'Get a file from the repository' params do - requires :ref, type: String, desc: 'The name of branch, tag, or commit' + requires :file_path, type: String, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb' + requires :ref, type: String, desc: 'The name of branch, tag or commit' end - get ":id/repository/files/:file_path" do - authorize! :download_code, user_project - + get ":id/repository/files/:file_path", requirements: { file_path: /.+/ } do assign_file_vars! - status(200) - { file_name: @blob.name, file_path: @blob.path, @@ -87,7 +81,7 @@ module API ref: params[:ref], blob_id: @blob.id, commit_id: @commit.id, - last_commit_id: @repo.last_commit_id_for_path(@commit.sha, @file_path) + last_commit_id: @repo.last_commit_id_for_path(@commit.sha, params[:file_path]) } end diff --git a/lib/api/repositories.rb b/lib/api/repositories.rb index 3932512d8da..531ef5a63ea 100644 --- a/lib/api/repositories.rb +++ b/lib/api/repositories.rb @@ -19,10 +19,13 @@ module API end def assign_blob_vars! + authorize! :download_code, user_project + @repo = user_project.repository begin @blob = Gitlab::Git::Blob.raw(@repo, params[:sha]) + @blob.load_all_data!(@repo) rescue not_found! 'Blob' end @@ -35,13 +38,13 @@ module API success Entities::RepoTreeObject end params do - optional :ref_name, type: String, desc: 'The name of a repository branch or tag, if not given the default branch is used' + optional :ref, type: String, desc: 'The name of a repository branch or tag, if not given the default branch is used' optional :path, type: String, desc: 'The path of the tree' optional :recursive, type: Boolean, default: false, desc: 'Used to get a recursive tree' use :pagination end get ':id/repository/tree' do - ref = params[:ref_name] || user_project.try(:default_branch) || 'master' + ref = params[:ref] || user_project.try(:default_branch) || 'master' path = params[:path] || nil commit = user_project.commit(ref) @@ -52,44 +55,23 @@ module API present paginate(entries), with: Entities::RepoTreeObject end - desc 'Get a raw file contents' + desc 'Get raw blob contents from the repository' params do requires :sha, type: String, desc: 'The commit, branch name, or tag name' - requires :filepath, type: String, desc: 'The path to the file to display' - end - get ":id/repository/commits/:sha/blob" do - repo = user_project.repository - - commit = repo.commit(params[:sha]) - not_found! "Commit" unless commit - - blob = Gitlab::Git::Blob.find(repo, commit.id, params[:filepath]) - not_found! "File" unless blob - - send_git_blob repo, blob end - - desc 'Get raw blob by sha' - params do - requires :sha, type: String, desc: 'The commit, branch name, or tag name' - end - get ':id/repository/blobs/:sha/:raw' do + get ':id/repository/blobs/:sha/raw' do assign_blob_vars! - status(200) - send_git_blob @repo, @blob end - desc 'Get blob base4 encoded content by sha' + desc 'Get a blob from the repository' params do requires :sha, type: String, desc: 'The commit, branch name, or tag name' end get ':id/repository/blobs/:sha' do assign_blob_vars! - status(200) - { size: @blob.size, encoding: "base64", diff --git a/lib/api/v3/repositories.rb b/lib/api/v3/repositories.rb index 3549ea225ef..44584e2eb70 100644 --- a/lib/api/v3/repositories.rb +++ b/lib/api/v3/repositories.rb @@ -38,6 +38,60 @@ module API present tree.sorted_entries, with: ::API::Entities::RepoTreeObject end + desc 'Get a raw file contents' + params do + requires :sha, type: String, desc: 'The commit, branch name, or tag name' + requires :filepath, type: String, desc: 'The path to the file to display' + end + get [":id/repository/blobs/:sha", ":id/repository/commits/:sha/blob"] do + repo = user_project.repository + commit = repo.commit(params[:sha]) + not_found! "Commit" unless commit + blob = Gitlab::Git::Blob.find(repo, commit.id, params[:filepath]) + not_found! "File" unless blob + send_git_blob repo, blob + end + + desc 'Get a raw blob contents by blob sha' + params do + requires :sha, type: String, desc: 'The commit, branch name, or tag name' + end + get ':id/repository/raw_blobs/:sha' do + repo = user_project.repository + begin + blob = Gitlab::Git::Blob.raw(repo, params[:sha]) + rescue + not_found! 'Blob' + end + not_found! 'Blob' unless blob + send_git_blob repo, blob + end + + desc 'Get an archive of the repository' + params do + optional :sha, type: String, desc: 'The commit sha of the archive to be downloaded' + optional :format, type: String, desc: 'The archive format' + end + get ':id/repository/archive', requirements: { format: Gitlab::Regex.archive_formats_regex } do + begin + send_git_archive user_project.repository, ref: params[:sha], format: params[:format] + rescue + not_found!('File') + end + end + + desc 'Compare two branches, tags, or commits' do + success ::API::Entities::Compare + end + params do + requires :from, type: String, desc: 'The commit, branch name, or tag name to start comparison' + requires :to, type: String, desc: 'The commit, branch name, or tag name to stop comparison' + end + get ':id/repository/compare' do + compare = Gitlab::Git::Compare.new(user_project.repository.raw_repository, params[:from], params[:to]) + present compare, with: ::API::Entities::Compare + end + desc 'Get repository contributors' do success ::API::Entities::Contributor end -- cgit v1.2.3