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 @@ -32,6 +32,7 @@ class Client < API
include PipelineTriggers
include Pipelines
include Projects
include ProtectedTags
include Repositories
include RepositoryFiles
include Runners
Expand Down
59 changes: 59 additions & 0 deletions lib/gitlab/client/protected_tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

class Gitlab::Client
# Defines methods related to Protected Tags.
# @see https://docs.gitlab.com/ce/api/protected_tags.html
module ProtectedTags
# Gets a list of protected tags from a project
#
# @example
# Gitlab.protected_tags(1)
#
# @param [Integer, String] project(required) The ID or name of a project.
# @option options [Integer] :page The page number.
# @option options [Integer] :per_page The number of results per page.
# @return [Array<Gitlab::ObjectifiedHash>] List of all protected tags requested
def protected_tags(project, options = {})
get("/projects/#{url_encode project}/protected_tags", query: options)
end

# Gets a single protected tag or wildcard protected tag.
#
# @example
# Gitlab.protected_tag(1, 'release-1-0')
#
# @param [Integer, String] project(required) The ID or name of a project.
# @param [String] name(required) The name of the tag or wildcard
# @return <Gitlab::ObjectifiedHash] Information about the requested protected tag
def protected_tag(project, name)
get("/projects/#{url_encode project}/protected_tags/#{name}")
end

# Protects a single repository tag or several project repository tags using a wildcard protected tag.
#
# @example
# Gitlab.protect_repository_tag(1, 'release-1-0')
# Gitlab.protect_repository_tag(1, 'release-1-0', create_access_level: 30)
#
# @param [Integer, String] project(required) The ID or name of a project.
# @param [String] name(required) The name of the tag or wildcard
# @option options [Integer] :create_access_level Access levels allowed to create (defaults: 40, maintainer access level)
# @return <Gitlab::ObjectifiedHash] Information about the protected repository tag
def protect_repository_tag(project, name, options = {})
body = { name: name }.merge(options)
post("/projects/#{url_encode project}/protected_tags", body: body)
end

# Unprotects the given protected tag or wildcard protected tag.
#
# @example
# Gitlab.unprotect_repository_tag(1, 'release-1-0')
#
# @param [Integer, String] project(required) The ID or name of a project.
# @param [String] name(required) The name of the tag or wildcard
# @return [nil] This API call returns an empty response body.
def unprotect_repository_tag(project, name)
delete("/projects/#{url_encode project}/protected_tags/#{name}")
end
end
end
9 changes: 9 additions & 0 deletions spec/fixtures/protected_tag.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "release-1-0",
"create_access_levels": [
{
"access_level": 40,
"access_level_description": "Maintainers"
}
]
}
11 changes: 11 additions & 0 deletions spec/fixtures/protected_tags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"name": "release-1-0",
"create_access_levels": [
{
"access_level": 40,
"access_level_description": "Maintainers"
}
]
}
]
62 changes: 62 additions & 0 deletions spec/gitlab/client/protected_tags_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Client do
describe '.protected_tags' do
before do
stub_get('/projects/1/protected_tags', 'protected_tags')
@protected_tags = Gitlab.protected_tags(1)
end

it 'gets the correct resource' do
expect(a_get('/projects/1/protected_tags')).to have_been_made
end

it "returns a response of a project's protected_tags" do
expect(@protected_tags).to be_a Gitlab::PaginatedResponse
end
end

describe '.protected_tag' do
before do
stub_get('/projects/1/protected_tags/release-1-0', 'protected_tag')
@protected_tag = Gitlab.protected_tag(1, 'release-1-0')
end

it 'gets the correct resource' do
expect(a_get('/projects/1/protected_tags/release-1-0')).to have_been_made
end

it 'returns correct information about the protected_tag' do
expect(@protected_tag.name).to eq 'release-1-0'
end
end

describe '.protect_repository_tag' do
before do
stub_post('/projects/1/protected_tags', 'protected_tag')
@protected_tag = Gitlab.protect_repository_tag(1, 'release-1-0')
end

it 'gets the correct resource' do
expect(a_post('/projects/1/protected_tags')
.with(body: { name: 'release-1-0' })).to have_been_made
end

it 'returns correct information about the protected repository tag' do
expect(@protected_tag.name).to eq 'release-1-0'
end
end

describe '.unprotect_repository_tag' do
before do
stub_delete('/projects/1/protected_tags/release-1-0', 'empty')
Gitlab.unprotect_repository_tag(1, 'release-1-0')
end

it 'gets the correct resource' do
expect(a_delete('/projects/1/protected_tags/release-1-0')).to have_been_made
end
end
end