repositories.rb 3.8 KB
Newer Older
1 2
require 'mime/types'

3 4
module API
  class Repositories < Grape::API
5 6
    include PaginationParams

7
    before { authorize! :download_code, user_project }
8

R
Robert Schilling 已提交
9 10 11
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
12
    resource :projects, requirements: { id: %r{[^/]+} } do
13 14 15 16 17 18 19
      helpers do
        def handle_project_member_errors(errors)
          if errors[:project_access].any?
            error!(errors[:project_access], 422)
          end
          not_found!
        end
20 21

        def assign_blob_vars!
22 23
          authorize! :download_code, user_project

24 25 26 27
          @repo = user_project.repository

          begin
            @blob = Gitlab::Git::Blob.raw(@repo, params[:sha])
28
            @blob.load_all_data!(@repo)
29 30 31 32 33 34
          rescue
            not_found! 'Blob'
          end

          not_found! 'Blob' unless @blob
        end
35
      end
36

R
Robert Schilling 已提交
37 38 39 40
      desc 'Get a project repository tree' do
        success Entities::RepoTreeObject
      end
      params do
41
        optional :ref, type: String, desc: 'The name of a repository branch or tag, if not given the default branch is used'
R
Robert Schilling 已提交
42 43
        optional :path, type: String, desc: 'The path of the tree'
        optional :recursive, type: Boolean, default: false, desc: 'Used to get a recursive tree'
44
        use :pagination
R
Robert Schilling 已提交
45
      end
46
      get ':id/repository/tree' do
47
        ref = params[:ref] || user_project.try(:default_branch) || 'master'
48 49
        path = params[:path] || nil

50
        commit = user_project.commit(ref)
51 52
        not_found!('Tree') unless commit

R
Robert Schilling 已提交
53
        tree = user_project.repository.tree(commit.id, path, recursive: params[:recursive])
54 55
        entries = ::Kaminari.paginate_array(tree.sorted_entries)
        present paginate(entries), with: Entities::RepoTreeObject
56 57
      end

58
      desc 'Get raw blob contents from the repository'
R
Robert Schilling 已提交
59 60
      params do
        requires :sha, type: String, desc: 'The commit, branch name, or tag name'
T
Thom Gerdes 已提交
61
      end
62
      get ':id/repository/blobs/:sha/raw' do
63
        assign_blob_vars!
T
Thom Gerdes 已提交
64

65 66
        send_git_blob @repo, @blob
      end
T
Thom Gerdes 已提交
67

68
      desc 'Get a blob from the repository'
69 70 71 72 73 74 75 76 77 78 79 80
      params do
        requires :sha, type: String, desc: 'The commit, branch name, or tag name'
      end
      get ':id/repository/blobs/:sha' do
        assign_blob_vars!

        {
          size: @blob.size,
          encoding: "base64",
          content: Base64.strict_encode64(@blob.data),
          sha: @blob.id
        }
81
      end
82

R
Robert Schilling 已提交
83 84 85 86 87
      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
88
      get ':id/repository/archive', requirements: { format: Gitlab::PathRegex.archive_formats_regex } do
89
        begin
90
          send_git_archive user_project.repository, ref: params[:sha], format: params[:format]
91 92 93
        rescue
          not_found!('File')
        end
94
      end
95

R
Robert Schilling 已提交
96 97 98 99 100 101 102
      desc 'Compare two branches, tags, or commits' do
        success 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
103
      get ':id/repository/compare' do
104
        compare = Gitlab::Git::Compare.new(user_project.repository.raw_repository, params[:from], params[:to])
105 106
        present compare, with: Entities::Compare
      end
107

R
Robert Schilling 已提交
108 109 110
      desc 'Get repository contributors' do
        success Entities::Contributor
      end
111 112 113
      params do
        use :pagination
      end
114
      get ':id/repository/contributors' do
115
        begin
116 117
          contributors = ::Kaminari.paginate_array(user_project.repository.contributors)
          present paginate(contributors), with: Entities::Contributor
118 119 120
        rescue
          not_found!
        end
121
      end
122 123 124
    end
  end
end