BigW Consortium Gitlab

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

3 4
module API
  class Repositories < Grape::API
5
    before { authorize! :download_code, user_project }
6

7 8 9
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
10 11 12 13 14 15 16 17 18
    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
19

20 21 22 23 24 25 26 27
      desc 'Get a project repository tree' do
        success Entities::RepoTreeObject
      end
      params do
        optional :ref_name, type: String, desc: 'The name of a repository branch or tag, if not given the default branch is used'
        optional :path, type: String, desc: 'The path of the tree'
        optional :recursive, type: Boolean, default: false, desc: 'Used to get a recursive tree'
      end
28
      get ':id/repository/tree' do
29 30 31
        ref = params[:ref_name] || user_project.try(:default_branch) || 'master'
        path = params[:path] || nil

32
        commit = user_project.commit(ref)
33 34
        not_found!('Tree') unless commit

35
        tree = user_project.repository.tree(commit.id, path, recursive: params[:recursive])
36

37
        present tree.sorted_entries, with: Entities::RepoTreeObject
38 39
      end

40 41 42 43 44
      desc 'Get a raw file contents'
      params do
        requires :sha, type: String, desc: 'The commit, branch name, or tag name'
        requires :filepath, type: String, desc: 'The path to the file to display'
      end
45
      get [ ":id/repository/blobs/:sha", ":id/repository/commits/:sha/blob" ] do
46 47
        repo = user_project.repository

48
        commit = repo.commit(params[:sha])
49 50
        not_found! "Commit" unless commit

51 52
        blob = Gitlab::Git::Blob.find(repo, commit.id, params[:filepath])
        not_found! "File" unless blob
53

54
        send_git_blob repo, blob
55 56
      end

57 58 59 60
      desc 'Get a raw blob contents by blob sha'
      params do
        requires :sha, type: String, desc: 'The commit, branch name, or tag name'
      end
61
      get ':id/repository/raw_blobs/:sha' do
62 63
        repo = user_project.repository

64
        begin
65
          blob = Gitlab::Git::Blob.raw(repo, params[:sha])
66 67 68
        rescue
          not_found! 'Blob'
        end
69

70
        not_found! 'Blob' unless blob
71

72
        send_git_blob repo, blob
73
      end
74

75 76 77 78 79 80
      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
      get ':id/repository/archive', requirements: { format: Gitlab::Regex.archive_formats_regex } do
81
        begin
82
          send_git_archive user_project.repository, ref: params[:sha], format: params[:format]
83 84 85
        rescue
          not_found!('File')
        end
86
      end
87

88 89 90 91 92 93 94
      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
95
      get ':id/repository/compare' do
96
        compare = Gitlab::Git::Compare.new(user_project.repository.raw_repository, params[:from], params[:to])
97 98
        present compare, with: Entities::Compare
      end
99

100 101 102
      desc 'Get repository contributors' do
        success Entities::Contributor
      end
103
      get ':id/repository/contributors' do
104 105 106 107 108 109
        begin
          present user_project.repository.contributors,
                  with: Entities::Contributor
        rescue
          not_found!
        end
110
      end
111 112 113
    end
  end
end