BigW Consortium Gitlab

files.rb 5.37 KB
Newer Older
1 2
module API
  class Files < Grape::API
3 4
    FILE_ENDPOINT_REQUIREMENTS = API::PROJECT_ENDPOINT_REQUIREMENTS.merge(file_path: API::NO_SLASH_URL_PART_REGEX)

5 6 7
    # Prevents returning plain/text responses for files with .txt extension
    after_validation { content_type "application/json" }

8 9 10 11
    helpers do
      def commit_params(attrs)
        {
          file_path: attrs[:file_path],
12
          start_branch: attrs[:start_branch] || attrs[:branch],
13
          branch_name: attrs[:branch],
14 15
          commit_message: attrs[:commit_message],
          file_content: attrs[:content],
16 17
          file_content_encoding: attrs[:encoding],
          author_email: attrs[:author_email],
18 19
          author_name: attrs[:author_name],
          last_commit_sha: attrs[:last_commit_id]
20 21 22
        }
      end

23
      def assign_file_vars!
24 25
        authorize! :download_code, user_project

26 27 28
        @commit = user_project.commit(params[:ref])
        not_found!('Commit') unless @commit

29 30
        @repo = user_project.repository
        @blob = @repo.blob_at(@commit.sha, params[:file_path])
31 32

        not_found!('File') unless @blob
33
        @blob.load_all_data!
34 35
      end

36 37 38
      def commit_response(attrs)
        {
          file_path: attrs[:file_path],
39
          branch: attrs[:branch]
40 41
        }
      end
42 43

      params :simple_file_params do
44
        requires :file_path, type: String, desc: 'The url encoded path to the file. Ex. lib%2Fclass%2Erb'
45 46 47
        requires :branch, type: String, desc: 'Name of the branch to commit into. To create a new branch, also provide `start_branch`.'
        requires :commit_message, type: String, desc: 'Commit message'
        optional :start_branch, type: String, desc: 'Name of the branch to start the new commit from'
48 49 50 51 52 53 54 55
        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'
56
        optional :last_commit_id, type: String, desc: 'Last known commit id for this file'
57
      end
58 59
    end

60 61 62
    params do
      requires :id, type: String, desc: 'The project ID'
    end
63
    resource :projects, requirements: FILE_ENDPOINT_REQUIREMENTS do
64
      desc 'Get raw file contents from the repository'
65
      params do
66 67
        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'
68
      end
69
      get ":id/repository/files/:file_path/raw", requirements: FILE_ENDPOINT_REQUIREMENTS do
70 71 72 73 74
        assign_file_vars!

        send_git_blob @repo, @blob
      end

75
      desc 'Get a file from the repository'
76
      params do
77 78
        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'
79
      end
80
      get ":id/repository/files/:file_path", requirements: FILE_ENDPOINT_REQUIREMENTS do
81
        assign_file_vars!
82

83
        {
84 85 86
          file_name: @blob.name,
          file_path: @blob.path,
          size: @blob.size,
87
          encoding: "base64",
88
          content: Base64.strict_encode64(@blob.data),
89
          ref: params[:ref],
90 91
          blob_id: @blob.id,
          commit_id: @commit.id,
92
          last_commit_id: @repo.last_commit_id_for_path(@commit.sha, params[:file_path])
93
        }
94 95
      end

96 97 98 99
      desc 'Create new file in repository'
      params do
        use :extended_file_params
      end
100
      post ":id/repository/files/:file_path", requirements: FILE_ENDPOINT_REQUIREMENTS do
101 102
        authorize! :push_code, user_project

103 104
        file_params = declared_params(include_missing: false)
        result = ::Files::CreateService.new(user_project, current_user, commit_params(file_params)).execute
105 106 107

        if result[:status] == :success
          status(201)
108
          commit_response(file_params)
109
        else
110
          render_api_error!(result[:message], 400)
111 112
        end
      end
113

114 115 116 117
      desc 'Update existing file in repository'
      params do
        use :extended_file_params
      end
118
      put ":id/repository/files/:file_path", requirements: FILE_ENDPOINT_REQUIREMENTS do
119 120
        authorize! :push_code, user_project

121
        file_params = declared_params(include_missing: false)
122 123 124 125 126 127

        begin
          result = ::Files::UpdateService.new(user_project, current_user, commit_params(file_params)).execute
        rescue ::Files::UpdateService::FileChangedError => e
          render_api_error!(e.message, 400)
        end
128 129 130

        if result[:status] == :success
          status(200)
131
          commit_response(file_params)
132
        else
133 134
          http_status = result[:http_status] || 400
          render_api_error!(result[:message], http_status)
135 136
        end
      end
137

138 139 140 141
      desc 'Delete an existing file in repository'
      params do
        use :simple_file_params
      end
142
      delete ":id/repository/files/:file_path", requirements: FILE_ENDPOINT_REQUIREMENTS do
143 144
        authorize! :push_code, user_project

145
        file_params = declared_params(include_missing: false)
146
        result = ::Files::DeleteService.new(user_project, current_user, commit_params(file_params)).execute
147

148
        if result[:status] != :success
149
          render_api_error!(result[:message], 400)
150 151
        end
      end
152 153 154
    end
  end
end