Skip to content

Commit

Permalink
Fix rubocop offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
NARKOZ committed Aug 7, 2018
1 parent b141ed1 commit 33654bf
Show file tree
Hide file tree
Showing 108 changed files with 2,754 additions and 2,538 deletions.
32 changes: 12 additions & 20 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
Layout/SpaceAroundEqualsInParameterDefault:
Enabled: false

Style/Alias:
Enabled: false

Style/PercentLiteralDelimiters:
Enabled: false

Layout/DotPosition:
Enabled: false
---
AllCops:
TargetRubyVersion: 2.3

Metrics/LineLength:
Max: 100
Exclude:
- 'lib/gitlab/client/*'
- 'spec/**/*'

Style/ClassAndModuleChildren:
Metrics/BlockLength:
Exclude:
- 'lib/gitlab/client/*'
- 'spec/**/*'

Layout/SpaceInsideHashLiteralBraces:
Exclude:
- 'lib/gitlab/client/*'
Style/Documentation:
Enabled: false

Style/StringLiterals:
Style/ClassAndModuleChildren:
Exclude:
- 'spec/**/*'
- 'lib/gitlab/client/*'

Metrics/BlockLength:
Style/FrozenStringLiteralComment:
Exclude:
- 'spec/**/*'
- 'spec/gitlab/file_response_spec.rb'
- 'spec/gitlab/help_spec.rb'
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
language: ruby
rvm:
- 2.3
- 2.4
- 2.5
before_install: gem update bundler

matrix:
include:
- rvm: 2.4
- rvm: 2.3
env: "TRAVIS_CI_RUBOCOP=true"
allow_failures:
- env: "TRAVIS_CI_RUBOCOP=true"
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in gitlab.gemspec
Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'

require 'rspec/core/rake_task'
Expand Down
1 change: 1 addition & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

# ENV['GITLAB_API_ENDPOINT'] = ''
# ENV['GITLAB_API_PRIVATE_TOKEN'] = ''
Expand Down
3 changes: 2 additions & 1 deletion exe/gitlab
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)

require 'gitlab/cli'

Expand Down
15 changes: 9 additions & 6 deletions gitlab.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
lib = File.expand_path('../lib', __FILE__)
# frozen_string_literal: true

lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'gitlab/version'

Expand All @@ -11,23 +13,24 @@ Gem::Specification.new do |gem|
gem.summary = 'A Ruby wrapper and CLI for the GitLab API'
gem.homepage = 'https://github.com/narkoz/gitlab'

gem.files = `git ls-files`.split($/).
reject { |f| f[/^spec/] } -
%w[Dockerfile docker-compose.yml docker.env .travis.yml .rubocop.yml .dockerignore]
gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
.grep_v(/^spec/) -
%w[Dockerfile docker-compose.yml docker.env .travis.yml
.rubocop.yml .dockerignore]
gem.bindir = 'exe'
gem.executables = gem.files.grep(%r{^exe/}) { |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ['lib']
gem.license = 'BSD'

gem.required_ruby_version = '>= 2.0.0'
gem.required_ruby_version = '>= 2.3.0'

gem.add_runtime_dependency 'httparty', '>= 0.14.0'
gem.add_runtime_dependency 'terminal-table', '>= 1.5.1'

gem.add_development_dependency 'pry'
gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec'
gem.add_development_dependency 'webmock'
gem.add_development_dependency 'rubocop'
gem.add_development_dependency 'webmock'
end
8 changes: 5 additions & 3 deletions lib/gitlab.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'gitlab/version'
require 'gitlab/objectified_hash'
require 'gitlab/configuration'
Expand All @@ -15,7 +17,7 @@ module Gitlab
# Alias for Gitlab::Client.new
#
# @return [Gitlab::Client]
def self.client(options={})
def self.client(options = {})
Gitlab::Client.new(options)
end

Expand All @@ -26,12 +28,12 @@ def self.method_missing(method, *args, &block)
end

# Delegate to Gitlab::Client
def self.respond_to_missing?(method_name, include_private=false)
def self.respond_to_missing?(method_name, include_private = false)
client.respond_to?(method_name) || super
end

# Delegate to HTTParty.http_proxy
def self.http_proxy(address=nil, port=nil, username=nil, password=nil)
def self.http_proxy(address = nil, port = nil, username = nil, password = nil)
Gitlab::Request.http_proxy(address, port, username, password)
end

Expand Down
6 changes: 4 additions & 2 deletions lib/gitlab/api.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# frozen_string_literal: true

module Gitlab
# @private
class API < Request
# @private
attr_accessor(*Configuration::VALID_OPTIONS_KEYS)
# @private
alias_method :auth_token=, :private_token=
alias auth_token= private_token=

# Creates a new API.
# @raise [Error:MissingCredentials]
def initialize(options={})
def initialize(options = {})
options = Gitlab.options.merge(options)
(Configuration::VALID_OPTIONS_KEYS + [:auth_token]).each do |key|
send("#{key}=", options[key]) if options[key]
Expand Down
16 changes: 11 additions & 5 deletions lib/gitlab/cli.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'gitlab'
require 'terminal-table/import'
require_relative 'cli_helpers'
Expand All @@ -17,7 +19,11 @@ class Gitlab::CLI
#
# @param [Array] args The command and it's optional arguments.
def self.start(args)
command = args.shift.strip rescue 'help'
command = begin
args.shift.strip
rescue StandardError
'help'
end
run(command, args)
end

Expand All @@ -30,13 +36,13 @@ def self.start(args)
# @param [String] cmd The name of a command.
# @param [Array] args The optional arguments for a command.
# @return [nil]
def self.run(cmd, args=[])
def self.run(cmd, args = [])
case cmd
when 'help'
puts help(args.shift) { |out| out.gsub!(/Gitlab\./, 'gitlab ') }
when 'info'
endpoint = Gitlab.endpoint ? Gitlab.endpoint : 'not set'
private_token = Gitlab.private_token ? Gitlab.private_token : 'not set'
endpoint = Gitlab.endpoint || 'not set'
private_token = Gitlab.private_token || 'not set'
puts "Gitlab endpoint is #{endpoint}"
puts "Gitlab private token is #{private_token}"
puts "Ruby Version is #{RUBY_VERSION}"
Expand All @@ -60,7 +66,7 @@ def self.run(cmd, args=[])

begin
command_args.map! { |arg| symbolize_keys(yaml_load(arg)) }
rescue => e
rescue StandardError => e
puts e.message
exit 1
end
Expand Down
36 changes: 19 additions & 17 deletions lib/gitlab/cli_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# frozen_string_literal: true

require 'yaml'
require 'json'
require 'base64'

class Gitlab::CLI
# Defines methods related to CLI output and formatting.
module Helpers
extend self
module_function

# Returns actions available to CLI & Shell
#
Expand Down Expand Up @@ -71,7 +73,7 @@ def confirm_command(cmd)

puts 'Are you sure? (y/n)'

if %w(y yes).include?($stdin.gets.to_s.strip.downcase)
if %w[y yes].include?($stdin.gets.to_s.strip.downcase)
puts 'Proceeding..'
else
puts 'Command aborted.'
Expand All @@ -82,7 +84,7 @@ def confirm_command(cmd)
# Gets defined help for a specific command/action.
#
# @return [String]
def help(cmd=nil, &block)
def help(cmd = nil, &block)
if cmd.nil? || Gitlab::Help.help_map.key?(cmd)
Gitlab::Help.actions_table(cmd)
else
Expand Down Expand Up @@ -159,7 +161,7 @@ def record_table(data, cmd, args)
# @param [Array] args Options passed to the API call
# @param [bool] single_value If set to true, a single result should be returned
# @return [Hash] Result hash
def record_hash(data, cmd, args, single_value=false)
def record_hash(data, cmd, args, single_value = false)
if data.empty?
result = nil
else
Expand All @@ -169,21 +171,21 @@ def record_hash(data, cmd, args, single_value=false)
row = {}

keys.each do |key|
case hash[key]
when Hash
row[key] = 'Hash'
when StringIO
row[key] = Base64.encode64(hash[key].read)
when nil
row[key] = nil
else
row[key] = hash[key]
end
row[key] = case hash[key]
when Hash
'Hash'
when StringIO
Base64.encode64(hash[key].read)
when nil
nil
else
hash[key]
end
end

result.push row
end
result = result[0] if single_value && result.count > 0
result = result[0] if single_value && result.count.positive?
end

{
Expand All @@ -202,10 +204,10 @@ def get_keys(args, data)
end

# Helper function to call Gitlab commands with args.
def gitlab_helper(cmd, args=[])
def gitlab_helper(cmd, args = [])
begin
data = args.any? ? Gitlab.send(cmd, *args) : Gitlab.send(cmd)
rescue => e
rescue StandardError => e
puts e.message
yield if block_given?
end
Expand Down
8 changes: 5 additions & 3 deletions lib/gitlab/client.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

module Gitlab
# Wrapper for the Gitlab REST API.
class Client < API
Dir[File.expand_path('../client/*.rb', __FILE__)].each { |f| require f }
Dir[File.expand_path('client/*.rb', __dir__)].each { |f| require f }

# Please keep in alphabetical order
include AccessRequests
Expand Down Expand Up @@ -53,8 +55,8 @@ def inspect
inspected
end

def url_encode(s)
URI.encode(s.to_s, /\W/)
def url_encode(url)
URI.encode(url.to_s, /\W/)
end

private
Expand Down
11 changes: 10 additions & 1 deletion lib/gitlab/client/access_requests.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Gitlab::Client
# Defines methods related to Award Emojis.
# @see https://docs.gitlab.com/ce/api/access_requests.html
Expand All @@ -12,6 +14,7 @@ module AccessRequests
def project_access_requests(project)
get("/projects/#{url_encode project}/access_requests")
end

# Gets a list of access requests for a group viewable by the authenticated user.
#
# @example
Expand All @@ -22,6 +25,7 @@ def project_access_requests(project)
def group_access_requests(group)
get("/groups/#{url_encode group}/access_requests")
end

# Requests access for the authenticated user to a project.
#
# @example
Expand All @@ -32,6 +36,7 @@ def group_access_requests(group)
def request_project_access(project)
post("/projects/#{url_encode project}/access_requests")
end

# Requests access for the authenticated user to a group.
#
# @example
Expand All @@ -42,6 +47,7 @@ def request_project_access(project)
def request_group_access(group)
post("/groups/#{url_encode group}/access_requests")
end

# Approves a project access request for the given user.
#
# @example
Expand All @@ -55,6 +61,7 @@ def request_group_access(group)
def approve_project_access_request(project, user_id, options = {})
put("/projects/#{url_encode project}/access_requests/#{user_id}/approve", body: options)
end

# Approves a group access request for the given user.
#
# @example
Expand All @@ -68,6 +75,7 @@ def approve_project_access_request(project, user_id, options = {})
def approve_group_access_request(group, user_id, options = {})
put("/groups/#{url_encode group}/access_requests/#{user_id}/approve", body: options)
end

# Denies a project access request for the given user.
#
# @example
Expand All @@ -79,6 +87,7 @@ def approve_group_access_request(group, user_id, options = {})
def deny_project_access_request(project, user_id)
delete("/projects/#{url_encode project}/access_requests/#{user_id}")
end

# Denies a group access request for the given user.
#
# @example
Expand All @@ -91,4 +100,4 @@ def deny_group_access_request(group, user_id)
delete("/groups/#{url_encode group}/access_requests/#{user_id}")
end
end
end
end
Loading

0 comments on commit 33654bf

Please sign in to comment.