BigW Consortium Gitlab

files.rb 4.06 KB
Newer Older
1 2 3 4 5
module API
  # Projects API
  class Files < Grape::API
    before { authenticate! }

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

      def commit_response(attrs)
        {
          file_path: attrs[:file_path],
23
          branch_name: attrs[:branch_name]
24 25
        }
      end
26 27 28 29 30 31 32 33 34 35 36 37 38 39

      params :simple_file_params do
        requires :file_path, type: String, desc: 'The path to new file. Ex. lib/class.rb'
        requires :branch_name, type: String, desc: 'The name of branch'
        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
40 41
    end

42 43 44
    params do
      requires :id, type: String, desc: 'The project ID'
    end
45
    resource :projects do
46 47 48 49 50
      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
51
      get ":id/repository/files" do
52 53
        authorize! :download_code, user_project

54 55
        commit = user_project.commit(params[:ref])
        not_found!('Commit') unless commit
56

Jacob Vosmaer committed
57
        repo = user_project.repository
58 59
        blob = repo.blob_at(commit.sha, params[:file_path])
        not_found!('File') unless blob
60

61 62
        blob.load_all_data!(repo)
        status(200)
63

64 65 66 67 68 69 70 71 72 73 74
        {
          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,
          last_commit_id: repo.last_commit_for_path(commit.sha, params[:file_path]).id
        }
75 76
      end

77 78 79 80
      desc 'Create new file in repository'
      params do
        use :extended_file_params
      end
81
      post ":id/repository/files" do
82 83
        authorize! :push_code, user_project

84 85
        file_params = declared_params(include_missing: false)
        result = ::Files::CreateService.new(user_project, current_user, commit_params(file_params)).execute
86 87 88

        if result[:status] == :success
          status(201)
89
          commit_response(file_params)
90
        else
91
          render_api_error!(result[:message], 400)
92 93
        end
      end
94

95 96 97 98
      desc 'Update existing file in repository'
      params do
        use :extended_file_params
      end
99
      put ":id/repository/files" do
100 101
        authorize! :push_code, user_project

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

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

114 115 116 117
      desc 'Delete an existing file in repository'
      params do
        use :simple_file_params
      end
118
      delete ":id/repository/files" do
119 120
        authorize! :push_code, user_project

121 122
        file_params = declared_params(include_missing: false)
        result = ::Files::DeleteService.new(user_project, current_user, commit_params(file_params)).execute
123 124 125

        if result[:status] == :success
          status(200)
126
          commit_response(file_params)
127
        else
128
          render_api_error!(result[:message], 400)
129 130
        end
      end
131 132 133
    end
  end
end