Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/gitlab/client/projects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -511,5 +511,20 @@ def unstar_project(id)
def user_projects(user_id, options = {})
get("/users/#{url_encode user_id}/projects", query: options)
end

# Uploads a file to the specified project to be used in an issue or
# merge request description, or a comment.
# @see https://docs.gitlab.com/ee/api/projects.html#upload-a-file
#
# @example
# Gitlab.upload_file(1, File.open(File::NULL, 'r'))
# File.open('myfile') { |file| Gitlab.upload_file(1, file) }
#
# @param [Integer, String] id The ID or path of a project.
# @param [File] The file you are interested to upload.
# @return [Gitlab::ObjectifiedHash]
def upload_file(id, file)
post("/projects/#{url_encode id}/uploads", body: { file: file })
end
end
end
5 changes: 5 additions & 0 deletions spec/fixtures/upload_file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"alt": "null",
"url": "/uploads/f22e67e35e1bcb339058212c54bb8772/null",
"markdown": "[null](/uploads/f22e67e35e1bcb339058212c54bb8772/null)"
}
20 changes: 20 additions & 0 deletions spec/gitlab/client/projects_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -676,4 +676,24 @@
expect(@user_projects.first.owner.id).to eq(user_id)
end
end

describe '.upload_file' do
let(:id) { 1 }
let(:file) { File.open(File::NULL, 'r') }

before do
stub_post("/projects/#{id}/uploads", 'upload_file')
@file = Gitlab.upload_file(id, file)
end

it 'gets the correct resource' do
expect(a_post("/projects/#{id}/uploads")).to have_been_made
end

it 'returns information about the uploaded file' do
expect(@file.alt).to eq('null')
expect(@file.url).to eq('/uploads/f22e67e35e1bcb339058212c54bb8772/null')
expect(@file.markdown).to eq('[null](/uploads/f22e67e35e1bcb339058212c54bb8772/null)')
end
end
end