diff options
Diffstat (limited to 'lib/api')
-rw-r--r-- | lib/api/api.rb | 3 | ||||
-rw-r--r-- | lib/api/commits.rb | 64 | ||||
-rw-r--r-- | lib/api/entities.rb | 27 | ||||
-rw-r--r-- | lib/api/files.rb | 53 | ||||
-rw-r--r-- | lib/api/internal.rb | 4 | ||||
-rw-r--r-- | lib/api/merge_requests.rb | 2 | ||||
-rw-r--r-- | lib/api/projects.rb | 14 | ||||
-rw-r--r-- | lib/api/repositories.rb | 61 |
8 files changed, 165 insertions, 63 deletions
diff --git a/lib/api/api.rb b/lib/api/api.rb index 283f7642f67..6bec8368b12 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -22,6 +22,8 @@ module API end format :json + content_type :txt, "text/plain" + helpers APIHelpers mount Groups @@ -40,6 +42,7 @@ module API mount ProjectHooks mount Services mount Files + mount Commits mount Namespaces end end diff --git a/lib/api/commits.rb b/lib/api/commits.rb new file mode 100644 index 00000000000..33b8b3d2244 --- /dev/null +++ b/lib/api/commits.rb @@ -0,0 +1,64 @@ +require 'mime/types' + +module API + # Projects API + class Commits < Grape::API + before { authenticate! } + before { authorize! :download_code, user_project } + + resource :projects do + helpers do + def handle_project_member_errors(errors) + if errors[:project_access].any? + error!(errors[:project_access], 422) + end + not_found! + end + end + + # Get a project repository commits + # + # Parameters: + # id (required) - The ID of a project + # ref_name (optional) - The name of a repository branch or tag, if not given the default branch is used + # Example Request: + # GET /projects/:id/repository/commits + get ":id/repository/commits" do + page = (params[:page] || 0).to_i + per_page = (params[:per_page] || 20).to_i + ref = params[:ref_name] || user_project.try(:default_branch) || 'master' + + commits = user_project.repository.commits(ref, nil, per_page, page * per_page) + present commits, with: Entities::RepoCommit + end + + # Get a specific commit of a project + # + # Parameters: + # id (required) - The ID of a project + # sha (required) - The commit hash or name of a repository branch or tag + # Example Request: + # GET /projects/:id/repository/commits/:sha + get ":id/repository/commits/:sha" do + sha = params[:sha] + commit = user_project.repository.commit(sha) + not_found! "Commit" unless commit + present commit, with: Entities::RepoCommitDetail + end + + # Get the diff for a specific commit of a project + # + # Parameters: + # id (required) - The ID of a project + # sha (required) - The commit or branch name + # Example Request: + # GET /projects/:id/repository/commits/:sha/diff + get ":id/repository/commits/:sha/diff" do + sha = params[:sha] + commit = user_project.repository.commit(sha) + not_found! "Commit" unless commit + commit.diffs + end + end + end +end diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 4f636fc54a3..8557fa074d4 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -6,6 +6,12 @@ module API expose :is_admin?, as: :is_admin expose :can_create_group?, as: :can_create_group expose :can_create_project?, as: :can_create_project + + expose :avatar_url do |user, options| + if user.avatar.present? + user.avatar.url + end + end end class UserSafe < Grape::Entity @@ -79,7 +85,16 @@ module API end class RepoObject < Grape::Entity - expose :name, :commit + expose :name + + expose :commit do |repo_obj, options| + if repo_obj.respond_to?(:commit) + repo_obj.commit + elsif options[:project] + options[:project].repository.commit(repo_obj.target) + end + end + expose :protected do |repo, options| if options[:project] options[:project].protected_branch? repo.name @@ -87,6 +102,16 @@ module API end end + class RepoTreeObject < Grape::Entity + expose :id, :name, :type + + expose :mode do |obj, options| + filemode = obj.mode.to_s(8) + filemode = "0" + filemode if filemode.length < 6 + filemode + end + end + class RepoCommit < Grape::Entity expose :id, :short_id, :title, :author_name, :author_email, :created_at end diff --git a/lib/api/files.rb b/lib/api/files.rb index 213604915a6..e0c46f92b84 100644 --- a/lib/api/files.rb +++ b/lib/api/files.rb @@ -5,10 +5,61 @@ module API before { authorize! :push_code, user_project } resource :projects do + # Get file from repository + # File content is Base64 encoded + # + # Parameters: + # file_path (required) - The path to the file. Ex. lib/class.rb + # ref (required) - The name of branch, tag or commit + # + # Example Request: + # GET /projects/:id/repository/files + # + # Example response: + # { + # "file_name": "key.rb", + # "file_path": "app/models/key.rb", + # "size": 1476, + # "encoding": "base64", + # "content": "IyA9PSBTY2hlbWEgSW5mb3...", + # "ref": "master", + # "blob_id": "79f7bbd25901e8334750839545a9bd021f0e4c83", + # "commit_id": "d5a3ff139356ce33e37e73add446f16869741b50" + # } + # + get ":id/repository/files" do + required_attributes! [:file_path, :ref] + attrs = attributes_for_keys [:file_path, :ref] + ref = attrs.delete(:ref) + file_path = attrs.delete(:file_path) + + commit = user_project.repository.commit(ref) + not_found! "Commit" unless commit + + blob = user_project.repository.blob_at(commit.sha, file_path) + + if blob + status(200) + + { + file_name: blob.name, + file_path: blob.path, + size: blob.size, + encoding: "base64", + content: Base64.encode64(blob.data), + ref: ref, + blob_id: blob.id, + commit_id: commit.id, + } + else + render_api_error!('File not found', 404) + end + end + # Create new file in repository # # Parameters: - # file_path (optional) - The path to new file. Ex. lib/class.rb + # file_path (required) - The path to new file. Ex. lib/class.rb # branch_name (required) - The name of branch # content (required) - File content # commit_message (required) - Commit message diff --git a/lib/api/internal.rb b/lib/api/internal.rb index ed6b50c3a6a..ebc9fef07b4 100644 --- a/lib/api/internal.rb +++ b/lib/api/internal.rb @@ -35,7 +35,9 @@ module API user = key.user return false if user.blocked? - return false if user.ldap_user? && Gitlab::LDAP::User.blocked?(user.extern_uid) + if Gitlab.config.ldap.enabled + return false if user.ldap_user? && Gitlab::LDAP::User.blocked?(user.extern_uid) + end action = case git_cmd when *DOWNLOAD_COMMANDS diff --git a/lib/api/merge_requests.rb b/lib/api/merge_requests.rb index 0f62cac9a0c..58d2f79faff 100644 --- a/lib/api/merge_requests.rb +++ b/lib/api/merge_requests.rb @@ -116,8 +116,6 @@ module API authorize! :modify_merge_request, merge_request if merge_request.update_attributes attrs - merge_request.reload_code - merge_request.mark_as_unchecked present merge_request, with: Entities::MergeRequest else handle_merge_request_errors! merge_request.errors diff --git a/lib/api/projects.rb b/lib/api/projects.rb index 888aa7e77d2..bcca69ff49a 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -11,7 +11,7 @@ module API end not_found! end - + def map_public_to_visibility_level(attrs) publik = attrs.delete(:public) publik = [ true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON' ].include?(publik) @@ -308,6 +308,18 @@ module API projects = Project.where("(id in (?) OR visibility_level in (?)) AND (name LIKE (?))", ids, visibility_levels, "%#{params[:query]}%") present paginate(projects), with: Entities::Project end + + + # Get a users list + # + # Example Request: + # GET /users + get ':id/users' do + @users = User.where(id: user_project.team.users.map(&:id)) + @users = @users.search(params[:search]) if params[:search].present? + @users = paginate @users + present @users, with: Entities::User + end end end end diff --git a/lib/api/repositories.rb b/lib/api/repositories.rb index 878929b8032..ba53bf9baa4 100644 --- a/lib/api/repositories.rb +++ b/lib/api/repositories.rb @@ -82,51 +82,7 @@ module API # Example Request: # GET /projects/:id/repository/tags get ":id/repository/tags" do - present user_project.repo.tags.sort_by(&:name).reverse, with: Entities::RepoObject - end - - # Get a project repository commits - # - # Parameters: - # id (required) - The ID of a project - # ref_name (optional) - The name of a repository branch or tag, if not given the default branch is used - # Example Request: - # GET /projects/:id/repository/commits - get ":id/repository/commits" do - page = (params[:page] || 0).to_i - per_page = (params[:per_page] || 20).to_i - ref = params[:ref_name] || user_project.try(:default_branch) || 'master' - - commits = user_project.repository.commits(ref, nil, per_page, page * per_page) - present commits, with: Entities::RepoCommit - end - - # Get a specific commit of a project - # - # Parameters: - # id (required) - The ID of a project - # sha (required) - The commit hash or name of a repository branch or tag - # Example Request: - # GET /projects/:id/repository/commits/:sha - get ":id/repository/commits/:sha" do - sha = params[:sha] - commit = user_project.repository.commit(sha) - not_found! "Commit" unless commit - present commit, with: Entities::RepoCommitDetail - end - - # Get the diff for a specific commit of a project - # - # Parameters: - # id (required) - The ID of a project - # sha (required) - The commit or branch name - # Example Request: - # GET /projects/:id/repository/commits/:sha/diff - get ":id/repository/commits/:sha/diff" do - sha = params[:sha] - commit = user_project.repository.commit(sha) - not_found! "Commit" unless commit - commit.diffs + present user_project.repo.tags.sort_by(&:name).reverse, with: Entities::RepoObject, project: user_project end # Get a project repository tree @@ -141,15 +97,9 @@ module API path = params[:path] || nil commit = user_project.repository.commit(ref) - tree = Tree.new(user_project.repository, commit.id, path) + tree = user_project.repository.tree(commit.id, path) - trees = [] - - %w(trees blobs submodules).each do |type| - trees += tree.send(type).map { |t| {name: t.name, type: type.singularize, mode: t.mode, id: t.id} } - end - - trees + present tree.sorted_entries, with: Entities::RepoTreeObject end # Get a raw file contents @@ -173,9 +123,7 @@ module API blob = Gitlab::Git::Blob.find(repo, commit.id, params[:filepath]) not_found! "File" unless blob - env['api.format'] = :txt - - content_type blob.mime_type + content_type 'text/plain' present blob.data end @@ -233,4 +181,3 @@ module API end end end - |