From 46eeb5673553942c548ff7eb9530df37e7cb141b Mon Sep 17 00:00:00 2001 From: Akash Srivastava Date: Sun, 7 Jun 2020 19:41:32 +0530 Subject: [PATCH 1/2] Added support for user snippets API --- lib/gitlab/client.rb | 1 + lib/gitlab/client/user_snippets.rb | 114 ++++++++++++++++ spec/fixtures/created_user_snippet.json | 21 +++ spec/fixtures/public_snippets.json | 39 ++++++ spec/fixtures/snippet_user_agent_details.json | 5 + spec/fixtures/updated_user_snippet.json | 21 +++ spec/fixtures/user_snippet.json | 21 +++ spec/fixtures/user_snippets.json | 42 ++++++ spec/gitlab/client/user_snippets_spec.rb | 129 ++++++++++++++++++ 9 files changed, 393 insertions(+) create mode 100644 lib/gitlab/client/user_snippets.rb create mode 100644 spec/fixtures/created_user_snippet.json create mode 100644 spec/fixtures/public_snippets.json create mode 100644 spec/fixtures/snippet_user_agent_details.json create mode 100644 spec/fixtures/updated_user_snippet.json create mode 100644 spec/fixtures/user_snippet.json create mode 100644 spec/fixtures/user_snippets.json create mode 100644 spec/gitlab/client/user_snippets_spec.rb diff --git a/lib/gitlab/client.rb b/lib/gitlab/client.rb index fac117484..18023ca84 100644 --- a/lib/gitlab/client.rb +++ b/lib/gitlab/client.rb @@ -62,6 +62,7 @@ class Client < API include Templates include Todos include Users + include UserSnippets include Versions include Wikis diff --git a/lib/gitlab/client/user_snippets.rb b/lib/gitlab/client/user_snippets.rb new file mode 100644 index 000000000..b51a3bf45 --- /dev/null +++ b/lib/gitlab/client/user_snippets.rb @@ -0,0 +1,114 @@ +# frozen_string_literal: true + +class Gitlab::Client + # Defines methods related to user snippets. + # @see https://docs.gitlab.com/ce/api/snippets.html + module UserSnippets + # Get a list of the current user’s snippets. + # + # @example + # Gitlab.user_snippets + # + # @return [Array] List of snippets of current user + def user_snippets + get("/snippets") + end + + # Get a single snippet. + # + # @example + # Gitlab.user_snippet(1) + # + # @param [Integer] id ID of snippet to retrieve. + # @return [Gitlab::ObjectifiedHash] Information about the user snippet + def user_snippet(id) + get("/snippets/#{id}") + end + + # Get a single snippet’s raw contents. + # + # @example + # Gitlab.user_snippet_raw(1) + # + # @param [Integer] id ID of snippet to retrieve. + # @return [String] User snippet text + def user_snippet_raw(id) + get("/snippets/#{id}/raw", + format: nil, + headers: { Accept: 'text/plain' }, + parser: ::Gitlab::Request::Parser) + end + + # Create a new snippet. + # + # @example + # Gitlab.create_user_snippet({ title: 'REST', file_name: 'api.rb', content: 'some code', description: 'Hello World snippet', visibility: 'public'}) + # + # @param [Hash] options A customizable set of options. + # @option options [String] :title (required) Title of a snippet. + # @option options [String] :file_name (required) Name of a snippet file. + # @option options [String] :content (required) Content of a snippet. + # @option options [String] :description (optional) Description of a snippet. + # @option options [String] :visibility (optional) Snippet’s visibility. + # @return [Gitlab::ObjectifiedHash] Information about created snippet. + def create_user_snippet(options = {}) + post("/snippets", body: options) + end + + # Update an existing snippet. + # + # @example + # Gitlab.edit_user_snippet(34, { file_name: 'README.txt' }) + # Gitlab.edit_user_snippet(34, { file_name: 'README.txt', title: 'New title' }) + # + # @param [Integer] id ID of snippet to update. + # @param [Hash] options A customizable set of options. + # @option options [String] :title (optional) Title of a snippet. + # @option options [String] :file_name (optional) Name of a snippet file. + # @option options [String] :content (optional) Content of a snippet. + # @option options [String] :description (optional) Description of a snippet. + # @option options [String] :visibility (optional) Snippet’s visibility. + # @return [Gitlab::ObjectifiedHash] Information about updated snippet. + def edit_user_snippet(id, options = {}) + put("/snippets/#{id}", body: options) + end + + # Delete an existing snippet. + # + # @example + # Gitlab.delete_user_snippet(14) + # + # @param [Integer] id ID of snippet to delete. + # @return [void] This API call returns an empty response body. + def delete_user_snippet(id) + delete("/snippets/#{id}") + end + + # List all public snippets. + # + # @example + # Gitlab.public_snippets + # Gitlab.public_snippets(per_page: 2, page: 1) + # + # @param [Hash] options A customizable set of options. + # @option options [String] :per_page (optional) Number of snippets to return per page. + # @option options [String] :page (optional) Page to retrieve. + # + # @return [Array] List of all public snippets + def public_snippets(options = {}) + get("/snippets/public", query: options) + end + + # Get user agent details for a snippet. + # + # @example + # Gitlab.snippet_user_agent_details(1) + # + # @param [Integer] id ID of snippet to delete. + # + # @return [Array] Details of the user agent + def snippet_user_agent_details(id) + get("/snippets/#{id}/user_agent_detail") + end + end +end diff --git a/spec/fixtures/created_user_snippet.json b/spec/fixtures/created_user_snippet.json new file mode 100644 index 000000000..7df31880a --- /dev/null +++ b/spec/fixtures/created_user_snippet.json @@ -0,0 +1,21 @@ +{ + "id": 1, + "title": "This is a snippet", + "file_name": "test.txt", + "description": "Hello World snippet", + "visibility": "internal", + "author": { + "id": 1, + "username": "john_smith", + "email": "john@example.com", + "name": "John Smith", + "state": "active", + "created_at": "2012-05-23T08:00:58Z" + }, + "expires_at": null, + "updated_at": "2012-06-28T10:52:04Z", + "created_at": "2012-06-28T10:52:04Z", + "project_id": null, + "web_url": "http://example.com/snippets/1", + "raw_url": "http://example.com/snippets/1/raw" +} diff --git a/spec/fixtures/public_snippets.json b/spec/fixtures/public_snippets.json new file mode 100644 index 000000000..cfee7e263 --- /dev/null +++ b/spec/fixtures/public_snippets.json @@ -0,0 +1,39 @@ +[ + { + "author": { + "avatar_url": "http://www.gravatar.com/avatar/edaf55a9e363ea263e3b981d09e0f7f7?s=80&d=identicon", + "id": 12, + "name": "Libby Rolfson", + "state": "active", + "username": "elton_wehner", + "web_url": "http://example.com/elton_wehner" + }, + "created_at": "2016-11-25T16:53:34.504Z", + "file_name": "oconnerrice.rb", + "id": 49, + "title": "Ratione cupiditate et laborum temporibus.", + "updated_at": "2016-11-25T16:53:34.504Z", + "project_id": null, + "web_url": "http://example.com/snippets/49", + "raw_url": "http://example.com/snippets/49/raw" + }, + { + "author": { + "avatar_url": "http://www.gravatar.com/avatar/36583b28626de71061e6e5a77972c3bd?s=80&d=identicon", + "id": 16, + "name": "Llewellyn Flatley", + "state": "active", + "username": "adaline", + "web_url": "http://example.com/adaline" + }, + "created_at": "2016-11-25T16:53:34.479Z", + "file_name": "muellershields.rb", + "id": 48, + "title": "Minus similique nesciunt vel fugiat qui ullam sunt.", + "updated_at": "2016-11-25T16:53:34.479Z", + "project_id": null, + "web_url": "http://example.com/snippets/48", + "raw_url": "http://example.com/snippets/49/raw", + "visibility": "public" + } +] diff --git a/spec/fixtures/snippet_user_agent_details.json b/spec/fixtures/snippet_user_agent_details.json new file mode 100644 index 000000000..78e12aa4a --- /dev/null +++ b/spec/fixtures/snippet_user_agent_details.json @@ -0,0 +1,5 @@ +{ + "user_agent": "AppleWebKit/537.36", + "ip_address": "127.0.0.1", + "akismet_submitted": false +} diff --git a/spec/fixtures/updated_user_snippet.json b/spec/fixtures/updated_user_snippet.json new file mode 100644 index 000000000..7e8327eac --- /dev/null +++ b/spec/fixtures/updated_user_snippet.json @@ -0,0 +1,21 @@ +{ + "id": 1, + "title": "test", + "file_name": "add.rb", + "description": "description of snippet", + "visibility": "internal", + "author": { + "id": 1, + "username": "john_smith", + "email": "john@example.com", + "name": "John Smith", + "state": "active", + "created_at": "2012-05-23T08:00:58Z" + }, + "expires_at": null, + "updated_at": "2012-06-28T10:52:04Z", + "created_at": "2012-06-28T10:52:04Z", + "project_id": null, + "web_url": "http://example.com/snippets/1", + "raw_url": "http://example.com/snippets/1/raw" +} diff --git a/spec/fixtures/user_snippet.json b/spec/fixtures/user_snippet.json new file mode 100644 index 000000000..1040f05dd --- /dev/null +++ b/spec/fixtures/user_snippet.json @@ -0,0 +1,21 @@ +{ + "id": 1, + "title": "test", + "file_name": "add.rb", + "description": "Ruby test snippet", + "visibility": "private", + "author": { + "id": 1, + "username": "john_smith", + "email": "john@example.com", + "name": "John Smith", + "state": "active", + "created_at": "2012-05-23T08:00:58Z" + }, + "expires_at": null, + "updated_at": "2012-06-28T10:52:04Z", + "created_at": "2012-06-28T10:52:04Z", + "project_id": null, + "web_url": "http://example.com/snippets/1", + "raw_url": "http://example.com/snippets/1/raw" +} diff --git a/spec/fixtures/user_snippets.json b/spec/fixtures/user_snippets.json new file mode 100644 index 000000000..5a9c6f88f --- /dev/null +++ b/spec/fixtures/user_snippets.json @@ -0,0 +1,42 @@ +[ + { + "id": 42, + "title": "Voluptatem iure ut qui aut et consequatur quaerat.", + "file_name": "mclaughlin.rb", + "description": null, + "visibility": "internal", + "author": { + "id": 22, + "name": "User 0", + "username": "user0", + "state": "active", + "avatar_url": "https://www.gravatar.com/avatar/52e4ce24a915fb7e51e1ad3b57f4b00a?s=80&d=identicon", + "web_url": "http://example.com/user0" + }, + "updated_at": "2018-09-18T01:12:26.383Z", + "created_at": "2018-09-18T01:12:26.383Z", + "project_id": null, + "web_url": "http://example.com/snippets/42", + "raw_url": "http://example.com/snippets/42/raw" + }, + { + "id": 41, + "title": "Ut praesentium non et atque.", + "file_name": "ondrickaemard.rb", + "description": null, + "visibility": "internal", + "author": { + "id": 22, + "name": "User 0", + "username": "user0", + "state": "active", + "avatar_url": "https://www.gravatar.com/avatar/52e4ce24a915fb7e51e1ad3b57f4b00a?s=80&d=identicon", + "web_url": "http://example.com/user0" + }, + "updated_at": "2018-09-18T01:12:26.360Z", + "created_at": "2018-09-18T01:12:26.360Z", + "project_id": 1, + "web_url": "http://example.com/gitlab-org/gitlab-test/snippets/41", + "raw_url": "http://example.com/gitlab-org/gitlab-test/snippets/41/raw" + } +] diff --git a/spec/gitlab/client/user_snippets_spec.rb b/spec/gitlab/client/user_snippets_spec.rb new file mode 100644 index 000000000..f164f04ea --- /dev/null +++ b/spec/gitlab/client/user_snippets_spec.rb @@ -0,0 +1,129 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Gitlab::Client do + describe '.user_snippets' do + before do + stub_get('/snippets', 'user_snippets') + @snippets = Gitlab.user_snippets + end + + it 'gets the correct resource' do + expect(a_get('/snippets')).to have_been_made + end + + it "returns a paginated response of user's snippets" do + expect(@snippets).to be_a Gitlab::PaginatedResponse + expect(@snippets.first.file_name).to eq('mclaughlin.rb') + end + end + + describe '.user_snippet' do + before do + stub_get('/snippets/1', 'user_snippet') + @snippet = Gitlab.user_snippet(1) + end + + it 'gets the correct resource' do + expect(a_get('/snippets/1')).to have_been_made + end + + it 'returns information about a snippet' do + expect(@snippet.file_name).to eq('add.rb') + expect(@snippet.author.name).to eq('John Smith') + end + end + + describe '.user_snippet_raw' do + before do + stub_get('/snippets/1/raw', 'snippet_content') + @snippet_content = Gitlab.user_snippet_raw(1) + end + + it 'gets the correct resource' do + expect(a_get('/snippets/1/raw')).to have_been_made + end + + it 'returns raw content of a user snippet' do + expect(@snippet_content).to eq("#!/usr/bin/env ruby\n\nputs \"Cool snippet!\"\n") + end + end + + describe '.create_user_snippet' do + before do + stub_post('/snippets', 'created_user_snippet') + @snippet = Gitlab.create_user_snippet(title: 'This is a snippet', content: 'Hello world', description: 'Hello World snippet', file_name: 'test.txt', visibility: 'internal') + end + + it 'gets the correct resource' do + body = { title: 'This is a snippet', content: 'Hello world', description: 'Hello World snippet', file_name: 'test.txt', visibility: 'internal' } + expect(a_post('/snippets').with(body: body)).to have_been_made + end + + it 'returns information about a new snippet' do + expect(@snippet.file_name).to eq('test.txt') + expect(@snippet.description).to eq('Hello World snippet') + expect(@snippet.author.name).to eq('John Smith') + end + end + + describe '.edit_user_snippet' do + before do + stub_put('/snippets/1', 'updated_user_snippet') + @snippet = Gitlab.edit_user_snippet(1, file_name: 'add.rb') + end + + it 'gets the correct resource' do + expect(a_put('/snippets/1') + .with(body: { file_name: 'add.rb' })).to have_been_made + end + + it 'returns information about an edited snippet' do + expect(@snippet.file_name).to eq('add.rb') + end + end + + describe '.delete_user_snippet' do + before do + stub_delete('/snippets/1', 'empty') + @snippet = Gitlab.delete_user_snippet(1) + end + + it 'gets the correct resource' do + expect(a_delete('/snippets/1')).to have_been_made + end + end + + describe '.public_snippets' do + before do + stub_get('/snippets/public', 'public_snippets').with(query: { per_page: 2, page: 1 }) + @snippets = Gitlab.public_snippets(per_page: 2, page: 1) + end + + it 'gets the correct resource' do + expect(a_get('/snippets/public') + .with(query: { per_page: 2, page: 1 })).to have_been_made + end + + it "returns a paginated response of public snippets" do + expect(@snippets).to be_a Gitlab::PaginatedResponse + expect(@snippets.last.visibility).to eq('public') + end + end + + describe '.snippet_user_agent_details' do + before do + stub_get('/snippets/1/user_agent_detail', 'snippet_user_agent_details') + @detail = Gitlab.snippet_user_agent_details(1) + end + + it 'gets the correct resource' do + expect(a_get('/snippets/1/user_agent_detail')).to have_been_made + end + + it 'returns user agent detail information about the snippet' do + expect(@detail.to_h.keys.sort).to eq(['akismet_submitted', 'ip_address', 'user_agent']) + end + end +end From d8faf6cea785928865a1068b3db40b42c095e470 Mon Sep 17 00:00:00 2001 From: Akash Srivastava Date: Sun, 7 Jun 2020 19:50:28 +0530 Subject: [PATCH 2/2] Fixed rubocop offences --- lib/gitlab/client/user_snippets.rb | 14 +++++++------- spec/gitlab/client/merge_request_approvals_spec.rb | 4 ++-- spec/gitlab/client/user_snippets_spec.rb | 4 ++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/gitlab/client/user_snippets.rb b/lib/gitlab/client/user_snippets.rb index b51a3bf45..5b68c5e59 100644 --- a/lib/gitlab/client/user_snippets.rb +++ b/lib/gitlab/client/user_snippets.rb @@ -4,14 +4,14 @@ class Gitlab::Client # Defines methods related to user snippets. # @see https://docs.gitlab.com/ce/api/snippets.html module UserSnippets - # Get a list of the current user’s snippets. + # Get a list of the snippets of the current user. # # @example # Gitlab.user_snippets # # @return [Array] List of snippets of current user def user_snippets - get("/snippets") + get('/snippets') end # Get a single snippet. @@ -25,7 +25,7 @@ def user_snippet(id) get("/snippets/#{id}") end - # Get a single snippet’s raw contents. + # Get raw contents of a single snippet. # # @example # Gitlab.user_snippet_raw(1) @@ -49,10 +49,10 @@ def user_snippet_raw(id) # @option options [String] :file_name (required) Name of a snippet file. # @option options [String] :content (required) Content of a snippet. # @option options [String] :description (optional) Description of a snippet. - # @option options [String] :visibility (optional) Snippet’s visibility. + # @option options [String] :visibility (optional) visibility of a snippet. # @return [Gitlab::ObjectifiedHash] Information about created snippet. def create_user_snippet(options = {}) - post("/snippets", body: options) + post('/snippets', body: options) end # Update an existing snippet. @@ -67,7 +67,7 @@ def create_user_snippet(options = {}) # @option options [String] :file_name (optional) Name of a snippet file. # @option options [String] :content (optional) Content of a snippet. # @option options [String] :description (optional) Description of a snippet. - # @option options [String] :visibility (optional) Snippet’s visibility. + # @option options [String] :visibility (optional) visibility of a snippet. # @return [Gitlab::ObjectifiedHash] Information about updated snippet. def edit_user_snippet(id, options = {}) put("/snippets/#{id}", body: options) @@ -96,7 +96,7 @@ def delete_user_snippet(id) # # @return [Array] List of all public snippets def public_snippets(options = {}) - get("/snippets/public", query: options) + get('/snippets/public', query: options) end # Get user agent details for a snippet. diff --git a/spec/gitlab/client/merge_request_approvals_spec.rb b/spec/gitlab/client/merge_request_approvals_spec.rb index 290ddf5a8..e7ec50fad 100644 --- a/spec/gitlab/client/merge_request_approvals_spec.rb +++ b/spec/gitlab/client/merge_request_approvals_spec.rb @@ -57,7 +57,7 @@ describe '.create_project_merge_request_approval_rule' do before do stub_post('/projects/1/approval_rules', 'project_merge_request_approval_rules') - @project_mr_approval_rules = Gitlab.create_project_merge_request_approval_rule(1, { name: 'security', approvals_required: 1 }) + @project_mr_approval_rules = Gitlab.create_project_merge_request_approval_rule(1, name: 'security', approvals_required: 1) end it 'creates the correct resource' do @@ -75,7 +75,7 @@ describe '.update_project_merge_request_approval_rule' do before do stub_put('/projects/1/approval_rules/1', 'project_merge_request_approval_rules') - @project_mr_approval_rules = Gitlab.update_project_merge_request_approval_rule(1, 1, { name: 'security', approvals_required: 1 }) + @project_mr_approval_rules = Gitlab.update_project_merge_request_approval_rule(1, 1, name: 'security', approvals_required: 1) end it 'updates the correct resource' do diff --git a/spec/gitlab/client/user_snippets_spec.rb b/spec/gitlab/client/user_snippets_spec.rb index f164f04ea..4a0854a06 100644 --- a/spec/gitlab/client/user_snippets_spec.rb +++ b/spec/gitlab/client/user_snippets_spec.rb @@ -106,7 +106,7 @@ .with(query: { per_page: 2, page: 1 })).to have_been_made end - it "returns a paginated response of public snippets" do + it 'returns a paginated response of public snippets' do expect(@snippets).to be_a Gitlab::PaginatedResponse expect(@snippets.last.visibility).to eq('public') end @@ -123,7 +123,7 @@ end it 'returns user agent detail information about the snippet' do - expect(@detail.to_h.keys.sort).to eq(['akismet_submitted', 'ip_address', 'user_agent']) + expect(@detail.to_h.keys.sort).to eq(%w[akismet_submitted ip_address user_agent]) end end end