From a3dab990c56ceaa541948c197b6b80d3571d3c78 Mon Sep 17 00:00:00 2001 From: Koen Van der Auwera Date: Fri, 7 May 2021 15:08:05 +0000 Subject: [PATCH 01/10] Remove warning: instance variable @json_output not initialized --- lib/gitlab/cli.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gitlab/cli.rb b/lib/gitlab/cli.rb index a2cfcd743..597da4db5 100644 --- a/lib/gitlab/cli.rb +++ b/lib/gitlab/cli.rb @@ -80,7 +80,7 @@ def self.run(cmd, args = []) # Helper method that checks whether we want to get the output as json # @return [nil] def self.render_output(cmd, args, data) - if @json_output + if defined?(@json_output) && @json_output output_json(cmd, args, data) else output_table(cmd, args, data) From ed5baa3d873da9ab1b829f4f157c61e68a880fe2 Mon Sep 17 00:00:00 2001 From: Koen Van der Auwera Date: Fri, 7 May 2021 15:12:25 +0000 Subject: [PATCH 02/10] Ruby 3 support. More specific for method_missing: Keyword arguments are separated from other argument https://www.ruby-lang.org/en/news/2020/12/25/ruby-3-0-0-released/ --- lib/gitlab.rb | 20 ++++++++++++++++++++ lib/gitlab/help.rb | 10 +++++----- spec/gitlab/client/build_variables_spec.rb | 2 +- spec/gitlab/client/users_spec.rb | 4 ++-- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/lib/gitlab.rb b/lib/gitlab.rb index 0a875bb0d..92259f7d6 100644 --- a/lib/gitlab.rb +++ b/lib/gitlab.rb @@ -20,6 +20,26 @@ module Gitlab def self.client(options = {}) Gitlab::Client.new(options) end + + method_missing_ruby_2 = %( + def self.method_missing(method, *args, &block) + return super unless client.respond_to?(method) + client.send(method, *args, &block) + end + ) + + method_missing_ruby_3 = %( + def self.method_missing(method, ...) + return super unless client.respond_to?(method) + client.send(method, ...) + end + ) + + if RUBY_VERSION >= "3.0.0" + eval(method_missing_ruby_3) + else + eval(method_missing_ruby_2) + end # Delegate to Gitlab::Client def self.method_missing(method, *args, &block) diff --git a/lib/gitlab/help.rb b/lib/gitlab/help.rb index 8802fff09..64e9064ff 100644 --- a/lib/gitlab/help.rb +++ b/lib/gitlab/help.rb @@ -80,15 +80,15 @@ def namespace(cmd) # Massage output from 'ri'. def change_help_output!(cmd, output_str) output_str = +output_str - output_str.gsub!(/#{cmd}\((.*?)\)/m, "#{cmd} \1") - output_str.gsub!(/,\s*/, ' ') + output_str.gsub!(/#{cmd}(\(.*?\))/m, "#{cmd}\\1") + output_str.gsub!(/,\s*/, ', ') # Ensure @option descriptions are on a single line output_str.gsub!(/\n\[/, " \[") output_str.gsub!(/\s(@)/, "\n@") - output_str.gsub!(/(\])\n(:)/, '\1 \2') - output_str.gsub!(/(:.*)(\n)(.*\.)/, '\1 \3') - output_str.gsub!(/\{(.+)\}/, '"{\1}"') + output_str.gsub!(/(\])\n(:)/, '\\1 \\2') + output_str.gsub!(/(:.*)(\n)(.*\.)/, '\\1 \\3') + output_str.gsub!(/\{(.+)\}/, '"{\\1}"') end end end diff --git a/spec/gitlab/client/build_variables_spec.rb b/spec/gitlab/client/build_variables_spec.rb index 1703f7090..4f3c2fed8 100644 --- a/spec/gitlab/client/build_variables_spec.rb +++ b/spec/gitlab/client/build_variables_spec.rb @@ -38,7 +38,7 @@ describe '.create_variable' do before do - stub_post('/projects/3/variables', 'variable') + stub_post('/projects/3/variables', 'variable', { masked: true }) @variable = Gitlab.create_variable(3, 'NEW_VARIABLE', 'new value') end diff --git a/spec/gitlab/client/users_spec.rb b/spec/gitlab/client/users_spec.rb index 4abdbe7e9..9f2b8834c 100644 --- a/spec/gitlab/client/users_spec.rb +++ b/spec/gitlab/client/users_spec.rb @@ -588,7 +588,7 @@ describe 'create' do before do stub_post('/users/2/impersonation_tokens', 'impersonation_create') - @token = Gitlab.create_user_impersonation_token(2, 'mytoken', scopes) + @token = Gitlab.create_user_impersonation_token(2, 'mytoken', ['api']) end it 'gets the correct resource' do @@ -614,7 +614,7 @@ it 'removes a token' do expect(a_delete('/users/2/impersonation_tokens/2')).to have_been_made - expect(@token).to be_empty + expect(@token.to_hash()).to be_empty end end end From 37fa4b73cacd99cff46e88c795ad4a6669b08762 Mon Sep 17 00:00:00 2001 From: Koen Van der Auwera Date: Wed, 12 May 2021 16:05:33 +0000 Subject: [PATCH 03/10] Need to remove the original one, duh. --- lib/gitlab.rb | 7 ------- 1 file changed, 7 deletions(-) diff --git a/lib/gitlab.rb b/lib/gitlab.rb index 92259f7d6..414e86afc 100644 --- a/lib/gitlab.rb +++ b/lib/gitlab.rb @@ -41,13 +41,6 @@ def self.method_missing(method, ...) eval(method_missing_ruby_2) end - # Delegate to Gitlab::Client - def self.method_missing(method, *args, &block) - return super unless client.respond_to?(method) - - client.send(method, *args, &block) - end - # Delegate to Gitlab::Client def self.respond_to_missing?(method_name, include_private = false) client.respond_to?(method_name) || super From 07ee05cdc2e2da9f275b2e21374c97d6423f1071 Mon Sep 17 00:00:00 2001 From: Koen Van der Auwera Date: Fri, 14 May 2021 12:25:20 +0000 Subject: [PATCH 04/10] Remove trailing whitespace --- lib/gitlab.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/gitlab.rb b/lib/gitlab.rb index 414e86afc..f45e8ace5 100644 --- a/lib/gitlab.rb +++ b/lib/gitlab.rb @@ -20,21 +20,21 @@ module Gitlab def self.client(options = {}) Gitlab::Client.new(options) end - + method_missing_ruby_2 = %( def self.method_missing(method, *args, &block) return super unless client.respond_to?(method) client.send(method, *args, &block) end ) - + method_missing_ruby_3 = %( def self.method_missing(method, ...) return super unless client.respond_to?(method) client.send(method, ...) end ) - + if RUBY_VERSION >= "3.0.0" eval(method_missing_ruby_3) else From bdb78ca95b808b37a9f73bb8f65afdbd49557a85 Mon Sep 17 00:00:00 2001 From: Koen Van der Auwera Date: Fri, 14 May 2021 12:28:07 +0000 Subject: [PATCH 05/10] Use normalcase for variable numbers. --- lib/gitlab.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/gitlab.rb b/lib/gitlab.rb index f45e8ace5..79a8d6ba3 100644 --- a/lib/gitlab.rb +++ b/lib/gitlab.rb @@ -21,14 +21,14 @@ def self.client(options = {}) Gitlab::Client.new(options) end - method_missing_ruby_2 = %( + method_missing_ruby2 = %( def self.method_missing(method, *args, &block) return super unless client.respond_to?(method) client.send(method, *args, &block) end ) - method_missing_ruby_3 = %( + method_missing_ruby3 = %( def self.method_missing(method, ...) return super unless client.respond_to?(method) client.send(method, ...) @@ -36,9 +36,9 @@ def self.method_missing(method, ...) ) if RUBY_VERSION >= "3.0.0" - eval(method_missing_ruby_3) + eval(method_missing_ruby3) else - eval(method_missing_ruby_2) + eval(method_missing_ruby2) end # Delegate to Gitlab::Client From 8041f8b6690b3c7db010d12228e3ce9329eeeedd Mon Sep 17 00:00:00 2001 From: Koen Van der Auwera Date: Fri, 14 May 2021 12:29:19 +0000 Subject: [PATCH 06/10] Prefer single-quoted strings when you don't need string interpolation or special symbols. --- lib/gitlab.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gitlab.rb b/lib/gitlab.rb index 79a8d6ba3..79edea5b3 100644 --- a/lib/gitlab.rb +++ b/lib/gitlab.rb @@ -35,7 +35,7 @@ def self.method_missing(method, ...) end ) - if RUBY_VERSION >= "3.0.0" + if RUBY_VERSION >= '3.0.0' eval(method_missing_ruby3) else eval(method_missing_ruby2) From 5a85c97dfc4f76c9f1c76fcfd490207a169053a8 Mon Sep 17 00:00:00 2001 From: Koen Van der Auwera Date: Fri, 14 May 2021 12:35:40 +0000 Subject: [PATCH 07/10] Do not use parentheses for method calls with no arguments --- spec/gitlab/client/users_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/gitlab/client/users_spec.rb b/spec/gitlab/client/users_spec.rb index 9f2b8834c..3b3ffc203 100644 --- a/spec/gitlab/client/users_spec.rb +++ b/spec/gitlab/client/users_spec.rb @@ -614,7 +614,7 @@ it 'removes a token' do expect(a_delete('/users/2/impersonation_tokens/2')).to have_been_made - expect(@token.to_hash()).to be_empty + expect(@token.to_hash).to be_empty end end end From 1d92c3bf9c984e111f26df20159d03b808f4f96a Mon Sep 17 00:00:00 2001 From: Koen Van der Auwera Date: Fri, 14 May 2021 12:36:36 +0000 Subject: [PATCH 08/10] Refactored out use of `eval`. --- lib/gitlab.rb | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/lib/gitlab.rb b/lib/gitlab.rb index 79edea5b3..a1f279ef0 100644 --- a/lib/gitlab.rb +++ b/lib/gitlab.rb @@ -21,24 +21,16 @@ def self.client(options = {}) Gitlab::Client.new(options) end - method_missing_ruby2 = %( - def self.method_missing(method, *args, &block) + if RUBY_VERSION >= '3.0.0' + def self.method_missing(method, *args, **keywargs, &block) return super unless client.respond_to?(method) - client.send(method, *args, &block) + client.send(method, *args, **keywargs, &block) end - ) - - method_missing_ruby3 = %( - def self.method_missing(method, ...) + else + def self.method_missing(method, *args, &block) return super unless client.respond_to?(method) - client.send(method, ...) + client.send(method, *args, &block) end - ) - - if RUBY_VERSION >= '3.0.0' - eval(method_missing_ruby3) - else - eval(method_missing_ruby2) end # Delegate to Gitlab::Client From c4a6e57d798f27a97cbb74f2117d0ac8f822a9fe Mon Sep 17 00:00:00 2001 From: Koen Van der Auwera Date: Sun, 16 May 2021 10:34:19 +0000 Subject: [PATCH 09/10] Add empty line after guard clause --- lib/gitlab.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/gitlab.rb b/lib/gitlab.rb index a1f279ef0..daf7733bd 100644 --- a/lib/gitlab.rb +++ b/lib/gitlab.rb @@ -24,11 +24,13 @@ def self.client(options = {}) if RUBY_VERSION >= '3.0.0' def self.method_missing(method, *args, **keywargs, &block) return super unless client.respond_to?(method) + client.send(method, *args, **keywargs, &block) end else def self.method_missing(method, *args, &block) return super unless client.respond_to?(method) + client.send(method, *args, &block) end end From b75b04e42d8326894b61aa3f129ecc5d0d6e4373 Mon Sep 17 00:00:00 2001 From: Koen Van der Auwera Date: Mon, 17 May 2021 09:28:15 +0000 Subject: [PATCH 10/10] As suggested, use Gem::Version to compare Ruby version numbers. --- lib/gitlab.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/gitlab.rb b/lib/gitlab.rb index daf7733bd..b87ba9538 100644 --- a/lib/gitlab.rb +++ b/lib/gitlab.rb @@ -21,7 +21,7 @@ def self.client(options = {}) Gitlab::Client.new(options) end - if RUBY_VERSION >= '3.0.0' + if Gem::Version.new(RUBY_VERSION).release >= Gem::Version.new('3.0.0') def self.method_missing(method, *args, **keywargs, &block) return super unless client.respond_to?(method) @@ -30,7 +30,7 @@ def self.method_missing(method, *args, **keywargs, &block) else def self.method_missing(method, *args, &block) return super unless client.respond_to?(method) - + client.send(method, *args, &block) end end