BigW Consortium Gitlab

files.rb 5.07 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 14 15 16 17 18 19 20 21 22 23 24 25
          target_branch: attrs[:branch_name],
          commit_message: attrs[:commit_message],
          file_content: attrs[:content],
          file_content_encoding: attrs[:encoding]
        }
      end

      def commit_response(attrs)
        {
          file_path: attrs[:file_path],
          branch_name: attrs[:branch_name],
        }
      end
    end

26
    resource :projects do
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
      # Get file from repository
      # File content is Base64 encoded
      #
      # Parameters:
      #   file_path (required) - The path to the file. Ex. lib/class.rb
      #   ref (required) - The name of branch, tag or commit
      #
      # Example Request:
      #   GET /projects/:id/repository/files
      #
      # Example response:
      # {
      #   "file_name": "key.rb",
      #   "file_path": "app/models/key.rb",
      #   "size": 1476,
      #   "encoding": "base64",
      #   "content": "IyA9PSBTY2hlbWEgSW5mb3...",
      #   "ref": "master",
      #   "blob_id": "79f7bbd25901e8334750839545a9bd021f0e4c83",
46 47
      #   "commit_id": "d5a3ff139356ce33e37e73add446f16869741b50",
      #   "last_commit_id": "570e7b2abdd848b95f2f578043fc23bd6f6fd24d",
48 49 50
      # }
      #
      get ":id/repository/files" do
51 52
        authorize! :download_code, user_project

53 54 55 56 57
        required_attributes! [:file_path, :ref]
        attrs = attributes_for_keys [:file_path, :ref]
        ref = attrs.delete(:ref)
        file_path = attrs.delete(:file_path)

58
        commit = user_project.commit(ref)
59
        not_found! 'Commit' unless commit
60

Jacob Vosmaer committed
61 62
        repo = user_project.repository
        blob = repo.blob_at(commit.sha, file_path)
63 64

        if blob
Jacob Vosmaer committed
65
          blob.load_all_data!(repo)
66 67 68 69 70 71 72
          status(200)

          {
            file_name: blob.name,
            file_path: blob.path,
            size: blob.size,
            encoding: "base64",
73
            content: Base64.strict_encode64(blob.data),
74 75 76
            ref: ref,
            blob_id: blob.id,
            commit_id: commit.id,
Jacob Vosmaer committed
77
            last_commit_id: repo.last_commit_for_path(commit.sha, file_path).id
78 79
          }
        else
80
          not_found! 'File'
81 82 83
        end
      end

84 85 86
      # Create new file in repository
      #
      # Parameters:
87
      #   file_path (required) - The path to new file. Ex. lib/class.rb
88 89 90 91 92 93
      #   branch_name (required) - The name of branch
      #   content (required) - File content
      #   commit_message (required) - Commit message
      #
      # Example Request:
      #   POST /projects/:id/repository/files
94
      #
95
      post ":id/repository/files" do
96 97
        authorize! :push_code, user_project

98
        required_attributes! [:file_path, :branch_name, :content, :commit_message]
99
        attrs = attributes_for_keys [:file_path, :branch_name, :content, :commit_message, :encoding]
100
        result = ::Files::CreateService.new(user_project, current_user, commit_params(attrs)).execute
101 102 103

        if result[:status] == :success
          status(201)
104
          commit_response(attrs)
105
        else
106
          render_api_error!(result[:message], 400)
107 108
        end
      end
109 110 111 112

      # Update existing file in repository
      #
      # Parameters:
113
      #   file_path (optional) - The path to file. Ex. lib/class.rb
114 115 116 117 118 119 120 121
      #   branch_name (required) - The name of branch
      #   content (required) - File content
      #   commit_message (required) - Commit message
      #
      # Example Request:
      #   PUT /projects/:id/repository/files
      #
      put ":id/repository/files" do
122 123
        authorize! :push_code, user_project

124
        required_attributes! [:file_path, :branch_name, :content, :commit_message]
125
        attrs = attributes_for_keys [:file_path, :branch_name, :content, :commit_message, :encoding]
126
        result = ::Files::UpdateService.new(user_project, current_user, commit_params(attrs)).execute
127 128 129

        if result[:status] == :success
          status(200)
130
          commit_response(attrs)
131
        else
132 133
          http_status = result[:http_status] || 400
          render_api_error!(result[:message], http_status)
134 135
        end
      end
136 137 138 139 140 141 142 143 144 145 146 147 148

      # Delete existing file in repository
      #
      # Parameters:
      #   file_path (optional) - The path to file. Ex. lib/class.rb
      #   branch_name (required) - The name of branch
      #   content (required) - File content
      #   commit_message (required) - Commit message
      #
      # Example Request:
      #   DELETE /projects/:id/repository/files
      #
      delete ":id/repository/files" do
149 150
        authorize! :push_code, user_project

151 152
        required_attributes! [:file_path, :branch_name, :commit_message]
        attrs = attributes_for_keys [:file_path, :branch_name, :commit_message]
153
        result = ::Files::DeleteService.new(user_project, current_user, commit_params(attrs)).execute
154 155 156

        if result[:status] == :success
          status(200)
157
          commit_response(attrs)
158
        else
159
          render_api_error!(result[:message], 400)
160 161
        end
      end
162 163 164
    end
  end
end