diff options
author | Zeger-Jan van de Weg <zegerjan@gitlab.com> | 2016-04-08 15:28:21 +0200 |
---|---|---|
committer | Zeger-Jan van de Weg <zegerjan@gitlab.com> | 2016-04-13 14:22:45 +0200 |
commit | f460fe71fe0948150469cb8751fc10636f6b9f4f (patch) | |
tree | a096132c49edb059d244de2b6b7d268e0cab3bca | |
parent | 01cda13909803ff6a02d9d489da517f55163967e (diff) | |
download | gitlab-ce-add-raw-file-endpoint-to-api-3934.tar.gz |
Add docs [ci skip]add-raw-file-endpoint-to-api-3934
-rw-r--r-- | CHANGELOG | 1 | ||||
-rw-r--r-- | doc/api/repository_files.md | 24 | ||||
-rw-r--r-- | lib/api/files.rb | 93 | ||||
-rw-r--r-- | spec/requests/api/files_spec.rb | 1 |
4 files changed, 67 insertions, 52 deletions
diff --git a/CHANGELOG b/CHANGELOG index 54d79259b30..0de260e5d76 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -31,6 +31,7 @@ v 8.7.0 (unreleased) - API: Expose user location (Robert Schilling) - ClosingIssueExtractor regex now also works with colons. e.g. "Fixes: #1234" !3591 - Update number of Todos in the sidebar when it's marked as "Done". !3600 + - API: Add raw file endpoint to API v 8.6.5 - Fix importing from GitHub Enterprise. !3529 diff --git a/doc/api/repository_files.md b/doc/api/repository_files.md index 623063f357b..9962bf21c23 100644 --- a/doc/api/repository_files.md +++ b/doc/api/repository_files.md @@ -33,6 +33,30 @@ Parameters: - `file_path` (required) - Full path to new file. Ex. lib/class.rb - `ref` (required) - The name of branch, tag or commit +## Get raw file from repository + +Allows you to receive a file from the repository without any metadata. + +``` +GET /projects/:id/repository/files/raw +``` + +| Attribute | Type | Required | Description | +| --------- | ---- | -------- | ----------- | +| `id` | integer | yes | The ID of a project | +| `file_path` | string | yes | Full path to file. Ex. lib/class.rb | +| `ref` | string | yes | The name of branch, tag or commit | + +```bash +curl -H "PRIVATE-TOKEN: 9koXpg98eAheJpvBs5tK" --data "ref=master" --data "file_path=Gemfile" https://gitlab.example.com/api/v3/projects/5/repository/files/raw +``` + +Example response: + +``` +"source \"https://rubygems.org\"\n\ngem 'rails', '4.2.5.2'\ngem 'rails-deprecated_sanitizer', '~\u003e 1.0.3'\n\n# Responders respond_to and respond_with\ngem 'responders', '~\u003e 2.0'\n\n +``` + ## Create new file in repository ``` diff --git a/lib/api/files.rb b/lib/api/files.rb index c86d4661909..309e48ad688 100644 --- a/lib/api/files.rb +++ b/lib/api/files.rb @@ -21,6 +21,43 @@ module API branch_name: attrs[:branch_name], } end + + def send_file(raw:) + required_attributes! [:file_path, :ref] + + ref = params[:ref] + file_path = params[:file_path] + + commit = user_project.commit(ref) + not_found! 'Commit' unless commit + + repository = user_project.repository + blob = repository.blob_at(commit.sha, file_path) + not_found! 'File' unless blob + + blob.load_all_data!(repository) + if raw + content_type "application/octet-stream" + header['Content-Disposition'] = "attachment; filename=#{blob.name}" + env['api.format'] = :binary + + blob.data + else + status(200) + + { + file_name: blob.name, + file_path: blob.path, + size: blob.size, + encoding: 'base64', + content: Base64.strict_encode64(blob.data), + ref: ref, + blob_id: blob.id, + commit_id: commit.id, + last_commit_id: repository.last_commit_for_path(commit.sha, file_path).id + } + end + end end resource :projects do @@ -47,38 +84,10 @@ module API # "last_commit_id": "570e7b2abdd848b95f2f578043fc23bd6f6fd24d", # } # - get ":id/repository/files" do + get ':id/repository/files' do authorize! :download_code, user_project - required_attributes! [:file_path, :ref] - attrs = attributes_for_keys [:file_path, :ref] - ref = attrs.delete(:ref) - file_path = attrs.delete(:file_path) - - commit = user_project.commit(ref) - not_found! 'Commit' unless commit - - repo = user_project.repository - blob = repo.blob_at(commit.sha, file_path) - - if blob - blob.load_all_data!(repo) - status(200) - - { - file_name: blob.name, - file_path: blob.path, - size: blob.size, - encoding: "base64", - content: Base64.strict_encode64(blob.data), - ref: ref, - blob_id: blob.id, - commit_id: commit.id, - last_commit_id: repo.last_commit_for_path(commit.sha, file_path).id - } - else - not_found! 'File' - end + send_file(raw: false) end # Get file from repository @@ -91,30 +100,10 @@ module API # Example Request: # GET /projects/:id/repository/files/raw # - get ":id/repository/files/raw" do + get ':id/repository/files/raw' do authorize! :download_code, user_project - required_attributes! [:file_path, :ref] - attrs = attributes_for_keys [:file_path, :ref] - ref = attrs.delete(:ref) - file_path = attrs.delete(:file_path) - - commit = user_project.commit(ref) - not_found! 'Commit' unless commit - - repo = user_project.repository - blob = repo.blob_at(commit.sha, file_path) - - if blob - content_type "application/octet-stream" - header['Content-Disposition'] = "attachment; filename=#{blob.name}" - env['api.format'] = :binary - - blob.load_all_data!(repo) - blob.data - else - not_found! 'File' - end + send_file(raw: true) end # Create new file in repository diff --git a/spec/requests/api/files_spec.rb b/spec/requests/api/files_spec.rb index 78e269f4c07..af2aa6727d7 100644 --- a/spec/requests/api/files_spec.rb +++ b/spec/requests/api/files_spec.rb @@ -47,6 +47,7 @@ describe API::API, api: true do } get api("/projects/#{project.id}/repository/files/raw", user), params + expect(response.status).to eq(200) expect(response.body).to match /shell_commands\.md\)\n\z/ end |