From ec54b19bb1e302f28b1189b13531d45ecee84a4c Mon Sep 17 00:00:00 2001 From: "Mr. Outis" Date: Mon, 1 Oct 2018 14:31:42 -0500 Subject: [PATCH 1/2] Add method to upload a file to a project Close #266 --- lib/gitlab/client/projects.rb | 15 +++++++++++++++ spec/fixtures/upload_file.json | 5 +++++ spec/gitlab/client/projects_spec.rb | 20 ++++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 spec/fixtures/upload_file.json diff --git a/lib/gitlab/client/projects.rb b/lib/gitlab/client/projects.rb index b8e19e672..757b695c4 100644 --- a/lib/gitlab/client/projects.rb +++ b/lib/gitlab/client/projects.rb @@ -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, file)) + # 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 diff --git a/spec/fixtures/upload_file.json b/spec/fixtures/upload_file.json new file mode 100644 index 000000000..ede0afb37 --- /dev/null +++ b/spec/fixtures/upload_file.json @@ -0,0 +1,5 @@ +{ + "alt": "null", + "url": "/uploads/f22e67e35e1bcb339058212c54bb8772/null", + "markdown": "[null](/uploads/f22e67e35e1bcb339058212c54bb8772/null)" +} diff --git a/spec/gitlab/client/projects_spec.rb b/spec/gitlab/client/projects_spec.rb index 1dbc264f8..c3e5e1a50 100644 --- a/spec/gitlab/client/projects_spec.rb +++ b/spec/gitlab/client/projects_spec.rb @@ -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 From e748cef5beb22628e664eaddd122ba38f7303416 Mon Sep 17 00:00:00 2001 From: "Mr. Outis" Date: Mon, 1 Oct 2018 14:33:40 -0500 Subject: [PATCH 2/2] Update docs related to upload_file --- lib/gitlab/client/projects.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gitlab/client/projects.rb b/lib/gitlab/client/projects.rb index 757b695c4..868aeec38 100644 --- a/lib/gitlab/client/projects.rb +++ b/lib/gitlab/client/projects.rb @@ -517,7 +517,7 @@ def user_projects(user_id, options = {}) # @see https://docs.gitlab.com/ee/api/projects.html#upload-a-file # # @example - # Gitlab.upload_file(1, File.open(File::NULL, file)) + # 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.