PageRenderTime 49ms CodeModel.GetById 11ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/api/repositories.rb

https://gitlab.com/vicvega/gitlab-ce
Ruby | 136 lines | 70 code | 22 blank | 44 comment | 4 complexity | 780dc71fa59340c2b92d4c2f45d24cc9 MD5 | raw file
  1require 'mime/types'
  2
  3module API
  4  # Projects API
  5  class Repositories < Grape::API
  6    before { authenticate! }
  7    before { authorize! :download_code, user_project }
  8
  9    resource :projects do
 10      helpers do
 11        def handle_project_member_errors(errors)
 12          if errors[:project_access].any?
 13            error!(errors[:project_access], 422)
 14          end
 15          not_found!
 16        end
 17      end
 18
 19      # Get a project repository tree
 20      #
 21      # Parameters:
 22      #   id (required) - The ID of a project
 23      #   ref_name (optional) - The name of a repository branch or tag, if not given the default branch is used
 24      # Example Request:
 25      #   GET /projects/:id/repository/tree
 26      get ':id/repository/tree' do
 27        ref = params[:ref_name] || user_project.try(:default_branch) || 'master'
 28        path = params[:path] || nil
 29
 30        commit = user_project.commit(ref)
 31        not_found!('Tree') unless commit
 32
 33        tree = user_project.repository.tree(commit.id, path)
 34
 35        present tree.sorted_entries, with: Entities::RepoTreeObject
 36      end
 37
 38      # Get a raw file contents
 39      #
 40      # Parameters:
 41      #   id (required) - The ID of a project
 42      #   sha (required) - The commit or branch name
 43      #   filepath (required) - The path to the file to display
 44      # Example Request:
 45      #   GET /projects/:id/repository/blobs/:sha
 46      get [ ":id/repository/blobs/:sha", ":id/repository/commits/:sha/blob" ] do
 47        required_attributes! [:filepath]
 48
 49        ref = params[:sha]
 50
 51        repo = user_project.repository
 52
 53        commit = repo.commit(ref)
 54        not_found! "Commit" unless commit
 55
 56        blob = Gitlab::Git::Blob.find(repo, commit.id, params[:filepath])
 57        not_found! "File" unless blob
 58
 59        send_git_blob repo, blob
 60      end
 61
 62      # Get a raw blob contents by blob sha
 63      #
 64      # Parameters:
 65      #   id (required) - The ID of a project
 66      #   sha (required) - The blob's sha
 67      # Example Request:
 68      #   GET /projects/:id/repository/raw_blobs/:sha
 69      get ':id/repository/raw_blobs/:sha' do
 70        ref = params[:sha]
 71
 72        repo = user_project.repository
 73
 74        begin
 75          blob = Gitlab::Git::Blob.raw(repo, ref)
 76        rescue
 77          not_found! 'Blob'
 78        end
 79
 80        not_found! 'Blob' unless blob
 81
 82        send_git_blob repo, blob
 83      end
 84
 85      # Get a an archive of the repository
 86      #
 87      # Parameters:
 88      #   id (required) - The ID of a project
 89      #   sha (optional) - the commit sha to download defaults to the tip of the default branch
 90      # Example Request:
 91      #   GET /projects/:id/repository/archive
 92      get ':id/repository/archive',
 93          requirements: { format: Gitlab::Regex.archive_formats_regex } do
 94        authorize! :download_code, user_project
 95
 96        begin
 97          send_git_archive user_project.repository, ref: params[:sha], format: params[:format]
 98        rescue
 99          not_found!('File')
100        end
101      end
102
103      # Compare two branches, tags or commits
104      #
105      # Parameters:
106      #   id (required) - The ID of a project
107      #   from (required) - the commit sha or branch name
108      #   to (required) - the commit sha or branch name
109      # Example Request:
110      #   GET /projects/:id/repository/compare?from=master&to=feature
111      get ':id/repository/compare' do
112        authorize! :download_code, user_project
113        required_attributes! [:from, :to]
114        compare = Gitlab::Git::Compare.new(user_project.repository.raw_repository, params[:from], params[:to])
115        present compare, with: Entities::Compare
116      end
117
118      # Get repository contributors
119      #
120      # Parameters:
121      #   id (required) - The ID of a project
122      # Example Request:
123      #   GET /projects/:id/repository/contributors
124      get ':id/repository/contributors' do
125        authorize! :download_code, user_project
126
127        begin
128          present user_project.repository.contributors,
129                  with: Entities::Contributor
130        rescue
131          not_found!
132        end
133      end
134    end
135  end
136end