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
1 change: 1 addition & 0 deletions lib/gitlab/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Client < API
include Templates
include Todos
include Users
include UserSnippets
include Versions
include Wikis

Expand Down
114 changes: 114 additions & 0 deletions lib/gitlab/client/user_snippets.rb
Original file line number Diff line number Diff line change
@@ -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 snippets of the current user.
#
# @example
# Gitlab.user_snippets
#
# @return [Array<Gitlab::ObjectifiedHash>] 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 raw contents of a single snippet.
#
# @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) visibility of a snippet.
# @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) visibility of a snippet.
# @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<Gitlab::ObjectifiedHash>] 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<Gitlab::ObjectifiedHash>] Details of the user agent
def snippet_user_agent_details(id)
get("/snippets/#{id}/user_agent_detail")
end
end
end
21 changes: 21 additions & 0 deletions spec/fixtures/created_user_snippet.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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"
}
39 changes: 39 additions & 0 deletions spec/fixtures/public_snippets.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
5 changes: 5 additions & 0 deletions spec/fixtures/snippet_user_agent_details.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"user_agent": "AppleWebKit/537.36",
"ip_address": "127.0.0.1",
"akismet_submitted": false
}
21 changes: 21 additions & 0 deletions spec/fixtures/updated_user_snippet.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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"
}
21 changes: 21 additions & 0 deletions spec/fixtures/user_snippet.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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"
}
42 changes: 42 additions & 0 deletions spec/fixtures/user_snippets.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
4 changes: 2 additions & 2 deletions spec/gitlab/client/merge_request_approvals_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading