repositories.rb 5.3 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
require 'mime/types'

5 6
module API
  class Repositories < Grape::API
7 8
    include PaginationParams

9
    before { authorize! :download_code, user_project }
10

R
Robert Schilling 已提交
11 12 13
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
14
    resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
15 16 17 18 19
      helpers do
        def handle_project_member_errors(errors)
          if errors[:project_access].any?
            error!(errors[:project_access], 422)
          end
20

21 22
          not_found!
        end
23 24

        def assign_blob_vars!
25 26
          authorize! :download_code, user_project

27 28 29 30
          @repo = user_project.repository

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

          not_found! 'Blob' unless @blob
        end
38
      end
39

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

53
        commit = user_project.commit(ref)
54 55
        not_found!('Tree') unless commit

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

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

68 69
        send_git_blob @repo, @blob
      end
T
Thom Gerdes 已提交
70

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

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

R
Robert Schilling 已提交
86 87 88 89 90
      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
91
      get ':id/repository/archive', requirements: { format: Gitlab::PathRegex.archive_formats_regex } do
92 93
        not_acceptable! if Gitlab::HotlinkingDetector.intercept_hotlinking?(request)

N
Nick Thomas 已提交
94 95 96
        send_git_archive user_project.repository, ref: params[:sha], format: params[:format], append_sha: true
      rescue
        not_found!('File')
97
      end
98

R
Robert Schilling 已提交
99 100 101 102 103 104
      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'
105
        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 已提交
106
      end
107
      get ':id/repository/compare' do
108 109 110 111 112 113 114
        compare = CompareService.new(user_project, params[:to]).execute(user_project, params[:from], straight: params[:straight])

        if compare
          present compare, with: Entities::Compare
        else
          not_found!("Ref")
        end
115
      end
116

R
Robert Schilling 已提交
117 118 119
      desc 'Get repository contributors' do
        success Entities::Contributor
      end
120 121
      params do
        use :pagination
122 123
        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)'
124
      end
125
      get ':id/repository/contributors' do
N
Nick Thomas 已提交
126 127 128 129
        contributors = ::Kaminari.paginate_array(user_project.repository.contributors(order_by: params[:order_by], sort: params[:sort]))
        present paginate(contributors), with: Entities::Contributor
      rescue
        not_found!
130
      end
131 132 133 134 135 136 137 138 139 140

      desc 'Get the common ancestor between commits' do
        success Entities::Commit
      end
      params do
        requires :refs, type: Array[String]
      end
      get ':id/repository/merge_base' do
        refs = params[:refs]

141 142
        if refs.size < 2
          render_api_error!('Provide at least 2 refs', 400)
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
        end

        merge_base = Gitlab::Git::MergeBase.new(user_project.repository, refs)

        if merge_base.unknown_refs.any?
          ref_noun = 'ref'.pluralize(merge_base.unknown_refs.size)
          message = "Could not find #{ref_noun}: #{merge_base.unknown_refs.join(', ')}"
          render_api_error!(message, 400)
        end

        if merge_base.commit
          present merge_base.commit, with: Entities::Commit
        else
          not_found!("Merge Base")
        end
      end
159 160 161
    end
  end
end