Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added wrappers for group badges API #585

Merged
merged 2 commits into from
Oct 12, 2020
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
2 changes: 0 additions & 2 deletions lib/gitlab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ def self.method_missing(method, *args, &block)
end

# Delegate to Gitlab::Client
# rubocop:disable Style/OptionalBooleanParameter
def self.respond_to_missing?(method_name, include_private = false)
client.respond_to?(method_name) || super
end
# rubocop:enable Style/OptionalBooleanParameter

# Delegate to HTTParty.http_proxy
def self.http_proxy(address = nil, port = nil, username = nil, password = nil)
Expand Down
8 changes: 4 additions & 4 deletions lib/gitlab/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class Gitlab::CLI
# @param [Array] args The command and it's optional arguments.
def self.start(args)
command = begin
args.shift.strip
rescue StandardError
'help'
end
args.shift.strip
rescue StandardError
'help'
end
run(command, args)
end

Expand Down
1 change: 1 addition & 0 deletions lib/gitlab/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Client < API
include Epics
include Events
include Features
include GroupBadges
include GroupBoards
include GroupLabels
include GroupMilestones
Expand Down
88 changes: 88 additions & 0 deletions lib/gitlab/client/group_badges.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# frozen_string_literal: true

class Gitlab::Client
# Defines methods related to group badges.
# @see https://docs.gitlab.com/ee/api/group_badges.html
module GroupBadges
# Gets a list of a groups badges.
#
# @example
# Gitlab.group_badges(5)
# Gitlab.group_badges(5, 'Coverage')
#
# @param [Integer, String] group(required) The ID or URL-encoded path of the group owned by the authenticated user.
# @param [String] name(optional) Name of the badges to return (case-sensitive).
# @return [Array<Gitlab::ObjectifiedHash>] List of all badges of a group
def group_badges(group, name = nil)
query = { name: name } if name
get("/groups/#{url_encode group}/badges", query: query)
end

# Gets a badge of a group.
#
# @example
# Gitlab.group_badge(5, 42)
#
# @param [Integer, String] group(required) The ID or URL-encoded path of the group owned by the authenticated user.
# @param [Integer] badge_id(required) The badge ID.
# @return [Gitlab::ObjectifiedHash] Information about the requested badge
def group_badge(group, badge_id)
get("/groups/#{url_encode group}/badges/#{badge_id}")
end

# Adds a badge to a group.
#
# @example
# Gitlab.add_group_badge(5, { link_url: 'https://abc.com/gitlab/gitlab-ce/commits/master', image_url: 'https://shields.io/my/badge1' })
#
# @param [Integer, String] group(required) The ID or URL-encoded path of the group owned by the authenticated user.
# @param [Hash] options A customizable set of options.
# @option options [String] :link_url(required) URL of the badge link
# @option options [String] :image_url(required) URL of the badge image
# @return [Gitlab::ObjectifiedHash] Information about the added group badge.
def add_group_badge(group, options = {})
post("/groups/#{url_encode group}/badges", body: options)
end

# Updates a badge of a group.
#
# @example
# Gitlab.edit_group_badge(5, 1, { link_url: 'https://abc.com/gitlab/gitlab-ce/commits/master', image_url: 'https://shields.io/my/badge1' })
#
# @param [Integer, String] group(required) The ID or URL-encoded path of the group owned by the authenticated user.
# @param [Integer] badge_id(required) The badge ID.
# @param [Hash] options A customizable set of options.
# @option options [String] :link_url(optional) URL of the badge link
# @option options [String] :image_url(optional) URL of the badge image
# @return [Gitlab::ObjectifiedHash] Information about the updated group badge.
def edit_group_badge(group, badge_id, options = {})
put("/groups/#{url_encode group}/badges/#{badge_id}", body: options)
end

# Removes a badge from a group.
#
# @example
# Gitlab.remove_group_badge(5, 42)
#
# @param [Integer, String] group(required) The ID or URL-encoded path of the group owned by the authenticated user.
# @param [Integer] badge_id(required) The badge ID.
# @return [nil] This API call returns an empty response body.
def remove_group_badge(group, badge_id)
delete("/groups/#{url_encode group}/badges/#{badge_id}")
end

# Preview a badge from a group.
#
# @example
# Gitlab.preview_group_badge(3, 'https://abc.com/gitlab/gitlab-ce/commits/master', 'https://shields.io/my/badge1')
#
# @param [Integer, String] group(required) The ID or URL-encoded path of the group owned by the authenticated user.
# @param [String] :link_url(required) URL of the badge link
# @param [String] :image_url(required) URL of the badge image
# @return [Gitlab::ObjectifiedHash] Returns how the link_url and image_url final URLs would be after resolving the placeholder interpolation.
def preview_group_badge(group, link_url, image_url)
query = { link_url: link_url, image_url: image_url }
get("/groups/#{url_encode group}/badges/render", query: query)
end
end
end
2 changes: 0 additions & 2 deletions lib/gitlab/file_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ def method_missing(name, *args, &block)
end
end

# rubocop:disable Style/OptionalBooleanParameter
def respond_to_missing?(method_name, include_private = false)
super || @file.respond_to?(method_name, include_private)
end
# rubocop:enable Style/OptionalBooleanParameter

# Parse filename from the 'Content Disposition' header.
def parse_headers!(headers)
Expand Down
2 changes: 0 additions & 2 deletions lib/gitlab/objectified_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ def method_missing(method_name, *args, &block)
end
end

# rubocop:disable Style/OptionalBooleanParameter
def respond_to_missing?(method_name, include_private = false)
hash.keys.map(&:to_sym).include?(method_name.to_sym) || super
end
# rubocop:enable Style/OptionalBooleanParameter
end
end
2 changes: 0 additions & 2 deletions lib/gitlab/paginated_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ def method_missing(name, *args, &block)
end
end

# rubocop:disable Style/OptionalBooleanParameter
def respond_to_missing?(method_name, include_private = false)
super || @array.respond_to?(method_name, include_private)
end
# rubocop:enable Style/OptionalBooleanParameter

def parse_headers!(headers)
@links = PageLinks.new headers
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/group_badge.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":1,"link_url":"http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}","image_url":"https://shields.io/my/badge","rendered_link_url":"http://example.com/ci_status.svg?project=example-org/example-project&ref=master","rendered_image_url":"https://shields.io/my/badge","kind":"project"}
1 change: 1 addition & 0 deletions spec/fixtures/group_badges.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"id":1,"link_url":"http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}","image_url":"https://shields.io/my/badge","rendered_link_url":"http://example.com/ci_status.svg?project=example-org/example-project&ref=master","rendered_image_url":"https://shields.io/my/badge","kind":"project"},{"id":2,"link_url":"http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}","image_url":"https://shields.io/my/badge","rendered_link_url":"http://example.com/ci_status.svg?project=example-org/example-project&ref=master","rendered_image_url":"https://shields.io/my/badge","kind":"group"}]
1 change: 1 addition & 0 deletions spec/fixtures/preview_group_badge.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"link_url":"http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}","image_url":"https://shields.io/my/badge","rendered_link_url":"http://example.com/ci_status.svg?project=example-org/example-project&ref=master","rendered_image_url":"https://shields.io/my/badge"}
99 changes: 99 additions & 0 deletions spec/gitlab/client/group_badges_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# frozen_string_literal: true

# rubocop:disable Style/FormatStringToken

require 'spec_helper'

describe Gitlab::Client do
describe '.group_badges' do
before do
stub_get('/groups/3/badges', 'group_badges')
@group_badges = Gitlab.group_badges(3)
end

it 'gets the correct resource' do
expect(a_get('/groups/3/badges')).to have_been_made
end

it "returns a paginated response of group's badges" do
expect(@group_badges).to be_a Gitlab::PaginatedResponse
end
end

describe '.group_badge' do
before do
stub_get('/groups/3/badges/1', 'group_badge')
@group_badge = Gitlab.group_badge(3, 1)
end

it 'gets the correct resource' do
expect(a_get('/groups/3/badges/1')).to have_been_made
end

it 'returns information about a badge' do
expect(@group_badge.id).to eq(1)
end
end

describe '.add_group_badge' do
before do
stub_post('/groups/3/badges', 'group_badge')
@group_badge = Gitlab.add_group_badge(3, link_url: 'http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}', image_url: 'https://shields.io/my/badge')
end

it 'gets the correct resource' do
expect(a_post('/groups/3/badges')
.with(body: { link_url: 'http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}', image_url: 'https://shields.io/my/badge' })).to have_been_made
end

it 'returns information about an added group badge' do
expect(@group_badge.link_url).to eq('http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}')
expect(@group_badge.image_url).to eq('https://shields.io/my/badge')
end
end

describe '.edit_group_badge' do
before do
stub_put('/groups/3/badges/1', 'group_badge')
@group_badge = Gitlab.edit_group_badge(3, 1, link_url: 'http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}', image_url: 'https://shields.io/my/badge')
end

it 'gets the correct resource' do
expect(a_put('/groups/3/badges/1')
.with(body: { link_url: 'http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}', image_url: 'https://shields.io/my/badge' })).to have_been_made
end

it 'returns information about an edited group badge' do
expect(@group_badge.link_url).to eq('http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}')
expect(@group_badge.image_url).to eq('https://shields.io/my/badge')
end
end

describe '.remove_group_badge' do
before do
stub_delete('/groups/3/badges/3', 'empty')
@group_badge = Gitlab.remove_group_badge(3, 3)
end

it 'gets the correct resource' do
expect(a_delete('/groups/3/badges/3')).to have_been_made
end
end

describe '.preview_group_badge' do
before do
stub_get('/groups/3/badges/render?image_url=https://shields.io/my/badge&link_url=http://example.com/ci_status.svg?project=%25%7Bproject_path%7D%26ref=%25%7Bdefault_branch%7D', 'preview_group_badge')
@preview_group_badge = Gitlab.preview_group_badge(3, 'http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}', 'https://shields.io/my/badge')
end

it 'gets the correct resource' do
expect(a_get('/groups/3/badges/render?image_url=https://shields.io/my/badge&link_url=http://example.com/ci_status.svg?project=%25%7Bproject_path%7D%26ref=%25%7Bdefault_branch%7D')).to have_been_made
end

it 'returns information about the rendered values of a badge' do
expect(@preview_group_badge.link_url).to eq('http://example.com/ci_status.svg?project=%{project_path}&ref=%{default_branch}')
expect(@preview_group_badge.image_url).to eq('https://shields.io/my/badge')
end
end
end
# rubocop:enable Style/FormatStringToken