Skip to content
Snippets Groups Projects
Commit f460fe71 authored by Zeger-Jan van de Weg's avatar Zeger-Jan van de Weg
Browse files

Add docs [ci skip]

parent 01cda139
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -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
Loading
Loading
Loading
Loading
@@ -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
 
```
Loading
Loading
Loading
Loading
@@ -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
Loading
Loading
@@ -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
Loading
Loading
@@ -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
Loading
Loading
Loading
Loading
@@ -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
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment