files.rb 3.92 KB
Newer Older
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 17 18 19
        }
      end

      def commit_response(attrs)
        {
          file_path: attrs[:file_path],
20
          branch: attrs[:branch]
21 22
        }
      end
Robert Schilling's avatar
Robert Schilling committed
23 24 25

      params :simple_file_params do
        requires :file_path, type: String, desc: 'The path to new file. Ex. lib/class.rb'
26
        requires :branch, type: String, desc: 'The name of branch'
Robert Schilling's avatar
Robert Schilling committed
27 28 29 30 31 32 33 34 35 36
        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
37 38
    end

Robert Schilling's avatar
Robert Schilling committed
39 40 41
    params do
      requires :id, type: String, desc: 'The project ID'
    end
42
    resource :projects do
Robert Schilling's avatar
Robert Schilling committed
43 44 45 46 47
      desc 'Get a file from repository'
      params do
        requires :file_path, type: String, desc: 'The path to the file. Ex. lib/class.rb'
        requires :ref, type: String, desc: 'The name of branch, tag, or commit'
      end
48
      get ":id/repository/files" do
49 50
        authorize! :download_code, user_project

Robert Schilling's avatar
Robert Schilling committed
51 52
        commit = user_project.commit(params[:ref])
        not_found!('Commit') unless commit
53

Jacob Vosmaer's avatar
Jacob Vosmaer committed
54
        repo = user_project.repository
Robert Schilling's avatar
Robert Schilling committed
55 56
        blob = repo.blob_at(commit.sha, params[:file_path])
        not_found!('File') unless blob
57

Robert Schilling's avatar
Robert Schilling committed
58 59
        blob.load_all_data!(repo)
        status(200)
60

Robert Schilling's avatar
Robert Schilling committed
61 62 63 64 65 66 67 68 69
        {
          file_name: blob.name,
          file_path: blob.path,
          size: blob.size,
          encoding: "base64",
          content: Base64.strict_encode64(blob.data),
          ref: params[:ref],
          blob_id: blob.id,
          commit_id: commit.id,
Hiroyuki Sato's avatar
Hiroyuki Sato committed
70
          last_commit_id: repo.last_commit_id_for_path(commit.sha, params[:file_path])
Robert Schilling's avatar
Robert Schilling committed
71
        }
72 73
      end

Robert Schilling's avatar
Robert Schilling committed
74 75 76 77
      desc 'Create new file in repository'
      params do
        use :extended_file_params
      end
78
      post ":id/repository/files" do
79 80
        authorize! :push_code, user_project

Robert Schilling's avatar
Robert Schilling committed
81 82
        file_params = declared_params(include_missing: false)
        result = ::Files::CreateService.new(user_project, current_user, commit_params(file_params)).execute
83 84 85

        if result[:status] == :success
          status(201)
Robert Schilling's avatar
Robert Schilling committed
86
          commit_response(file_params)
87
        else
88
          render_api_error!(result[:message], 400)
89 90
        end
      end
91

Robert Schilling's avatar
Robert Schilling committed
92 93 94 95
      desc 'Update existing file in repository'
      params do
        use :extended_file_params
      end
96
      put ":id/repository/files" do
97 98
        authorize! :push_code, user_project

Robert Schilling's avatar
Robert Schilling committed
99 100
        file_params = declared_params(include_missing: false)
        result = ::Files::UpdateService.new(user_project, current_user, commit_params(file_params)).execute
101 102 103

        if result[:status] == :success
          status(200)
Robert Schilling's avatar
Robert Schilling committed
104
          commit_response(file_params)
105
        else
106 107
          http_status = result[:http_status] || 400
          render_api_error!(result[:message], http_status)
108 109
        end
      end
110

Robert Schilling's avatar
Robert Schilling committed
111 112 113 114
      desc 'Delete an existing file in repository'
      params do
        use :simple_file_params
      end
115
      delete ":id/repository/files" do
116 117
        authorize! :push_code, user_project

Robert Schilling's avatar
Robert Schilling committed
118
        file_params = declared_params(include_missing: false)
119
        result = ::Files::DestroyService.new(user_project, current_user, commit_params(file_params)).execute
120

121
        if result[:status] != :success
122
          render_api_error!(result[:message], 400)
123 124
        end
      end
125 126 127
    end
  end
end