files.rb 4.6 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1 2
module API
  class Files < Grape::API
3 4 5 6
    helpers do
      def commit_params(attrs)
        {
          file_path: attrs[:file_path],
7 8
          start_branch: attrs[:branch],
          target_branch: attrs[:branch],
9 10
          commit_message: attrs[:commit_message],
          file_content: attrs[:content],
11 12 13
          file_content_encoding: attrs[:encoding],
          author_email: attrs[:author_email],
          author_name: attrs[:author_name]
14 15 16
        }
      end

17
      def assign_file_vars!
18 19
        authorize! :download_code, user_project

20 21 22
        @commit = user_project.commit(params[:ref])
        not_found!('Commit') unless @commit

23 24
        @repo = user_project.repository
        @blob = @repo.blob_at(@commit.sha, params[:file_path])
25 26 27 28 29

        not_found!('File') unless @blob
        @blob.load_all_data!(@repo)
      end

30 31 32
      def commit_response(attrs)
        {
          file_path: attrs[:file_path],
33
          branch: attrs[:branch]
34 35
        }
      end
R
Robert Schilling 已提交
36 37

      params :simple_file_params do
38
        requires :file_path, type: String, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
39
        requires :branch, type: String, desc: 'The name of branch'
R
Robert Schilling 已提交
40 41 42 43 44 45 46 47 48 49
        requires :commit_message, type: String, desc: 'Commit Message'
        optional :author_email, type: String, desc: 'The email of the author'
        optional :author_name, type: String, desc: 'The name of the author'
      end

      params :extended_file_params do
        use :simple_file_params
        requires :content, type: String, desc: 'File content'
        optional :encoding, type: String, values: %w[base64], desc: 'File encoding'
      end
50 51
    end

R
Robert Schilling 已提交
52 53 54
    params do
      requires :id, type: String, desc: 'The project ID'
    end
D
Dmitriy Zaporozhets 已提交
55
    resource :projects do
56
      desc 'Get raw file contents from the repository'
R
Robert Schilling 已提交
57
      params do
58 59
        requires :file_path, type: String, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
        requires :ref, type: String, desc: 'The name of branch, tag commit'
R
Robert Schilling 已提交
60
      end
61 62 63 64 65 66
      get ":id/repository/files/:file_path/raw" do
        assign_file_vars!

        send_git_blob @repo, @blob
      end

67
      desc 'Get a file from the repository'
68
      params do
69 70
        requires :file_path, type: String, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
        requires :ref, type: String, desc: 'The name of branch, tag or commit'
71
      end
72
      get ":id/repository/files/:file_path", requirements: { file_path: /.+/ } do
73
        assign_file_vars!
D
Dmitriy Zaporozhets 已提交
74

R
Robert Schilling 已提交
75
        {
76 77 78
          file_name: @blob.name,
          file_path: @blob.path,
          size: @blob.size,
R
Robert Schilling 已提交
79
          encoding: "base64",
80
          content: Base64.strict_encode64(@blob.data),
R
Robert Schilling 已提交
81
          ref: params[:ref],
82 83
          blob_id: @blob.id,
          commit_id: @commit.id,
84
          last_commit_id: @repo.last_commit_id_for_path(@commit.sha, params[:file_path])
R
Robert Schilling 已提交
85
        }
D
Dmitriy Zaporozhets 已提交
86 87
      end

R
Robert Schilling 已提交
88 89 90 91
      desc 'Create new file in repository'
      params do
        use :extended_file_params
      end
92
      post ":id/repository/files/:file_path", requirements: { file_path: /.+/ } do
N
Nihad Abbasov 已提交
93 94
        authorize! :push_code, user_project

R
Robert Schilling 已提交
95 96
        file_params = declared_params(include_missing: false)
        result = ::Files::CreateService.new(user_project, current_user, commit_params(file_params)).execute
D
Dmitriy Zaporozhets 已提交
97 98 99

        if result[:status] == :success
          status(201)
R
Robert Schilling 已提交
100
          commit_response(file_params)
D
Dmitriy Zaporozhets 已提交
101
        else
102
          render_api_error!(result[:message], 400)
D
Dmitriy Zaporozhets 已提交
103 104
        end
      end
D
Dmitriy Zaporozhets 已提交
105

R
Robert Schilling 已提交
106 107 108 109
      desc 'Update existing file in repository'
      params do
        use :extended_file_params
      end
110
      put ":id/repository/files/:file_path", requirements: { file_path: /.+/ } do
N
Nihad Abbasov 已提交
111 112
        authorize! :push_code, user_project

R
Robert Schilling 已提交
113 114
        file_params = declared_params(include_missing: false)
        result = ::Files::UpdateService.new(user_project, current_user, commit_params(file_params)).execute
D
Dmitriy Zaporozhets 已提交
115 116 117

        if result[:status] == :success
          status(200)
R
Robert Schilling 已提交
118
          commit_response(file_params)
D
Dmitriy Zaporozhets 已提交
119
        else
120 121
          http_status = result[:http_status] || 400
          render_api_error!(result[:message], http_status)
D
Dmitriy Zaporozhets 已提交
122 123
        end
      end
124

R
Robert Schilling 已提交
125 126 127 128
      desc 'Delete an existing file in repository'
      params do
        use :simple_file_params
      end
129
      delete ":id/repository/files/:file_path", requirements: { file_path: /.+/ } do
N
Nihad Abbasov 已提交
130 131
        authorize! :push_code, user_project

R
Robert Schilling 已提交
132
        file_params = declared_params(include_missing: false)
133
        result = ::Files::DestroyService.new(user_project, current_user, commit_params(file_params)).execute
134

135
        if result[:status] != :success
136
          render_api_error!(result[:message], 400)
137 138
        end
      end
D
Dmitriy Zaporozhets 已提交
139 140 141
    end
  end
end