BigW Consortium Gitlab

files.rb 5.24 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 26 27
        }
      end
    end

28
    resource :projects do
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
      # 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",
48 49
      #   "commit_id": "d5a3ff139356ce33e37e73add446f16869741b50",
      #   "last_commit_id": "570e7b2abdd848b95f2f578043fc23bd6f6fd24d",
50 51 52
      # }
      #
      get ":id/repository/files" do
53 54
        authorize! :download_code, user_project

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

60
        commit = user_project.commit(ref)
61
        not_found! 'Commit' unless commit
62

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

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

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

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

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

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

      # Update existing file in repository
      #
      # Parameters:
115
      #   file_path (optional) - The path to file. Ex. lib/class.rb
116 117 118 119 120 121 122 123
      #   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
124 125
        authorize! :push_code, user_project

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

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

      # 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
151 152
        authorize! :push_code, user_project

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

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