repositories.rb 4.4 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: API::PROJECT_ENDPOINT_REQUIREMENTS do
13 14 15 16 17
      helpers do
        def handle_project_member_errors(errors)
          if errors[:project_access].any?
            error!(errors[:project_access], 422)
          end
18

19 20
          not_found!
        end
21 22

        def assign_blob_vars!
23 24
          authorize! :download_code, user_project

25 26 27 28
          @repo = user_project.repository

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

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

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

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

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

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

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

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

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

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

R
Robert Schilling 已提交
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'
103
        optional :straight, type: Boolean, desc: 'Comparison method, `true` for direct comparison between `from` and `to` (`from`..`to`), `false` to compare using merge base (`from`...`to`)', default: false
R
Robert Schilling 已提交
104
      end
105
      get ':id/repository/compare' do
106
        compare = Gitlab::Git::Compare.new(user_project.repository.raw_repository, params[:from], params[:to], straight: params[:straight])
107 108
        present compare, with: Entities::Compare
      end
109

R
Robert Schilling 已提交
110 111 112
      desc 'Get repository contributors' do
        success Entities::Contributor
      end
113 114
      params do
        use :pagination
115 116
        optional :order_by, type: String, values: %w[email name commits], default: 'commits', desc: 'Return contributors ordered by `name` or `email` or `commits`'
        optional :sort, type: String, values: %w[asc desc], default: 'asc', desc: 'Sort by asc (ascending) or desc (descending)'
117
      end
118
      get ':id/repository/contributors' do
119
        begin
120
          contributors = ::Kaminari.paginate_array(user_project.repository.contributors(order_by: params[:order_by], sort: params[:sort]))
121
          present paginate(contributors), with: Entities::Contributor
122 123 124
        rescue
          not_found!
        end
125
      end
126 127 128
    end
  end
end