From 1a2b9e31908876871b3a9c96b862f86cef53229d Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Thu, 3 Jan 2019 13:24:01 +0100 Subject: [PATCH] Fix Rubocop offenses in bin/secpick script --- bin/secpick | 221 +++++++++++++++++++++++++++------------------------- 1 file changed, 113 insertions(+), 108 deletions(-) diff --git a/bin/secpick b/bin/secpick index e015cc562b7..3d032f696a2 100755 --- a/bin/secpick +++ b/bin/secpick @@ -1,4 +1,5 @@ #!/usr/bin/env ruby + # frozen_string_literal: false require 'active_support/core_ext/object/to_query' @@ -7,128 +8,132 @@ require 'open3' require 'rainbow/refinement' using Rainbow -BRANCH_PREFIX = 'security'.freeze -DEFAULT_REMOTE = 'dev'.freeze -NEW_MR_URL = 'https://dev.gitlab.org/gitlab/gitlabhq/merge_requests/new'.freeze +module Secpick + BRANCH_PREFIX = 'security'.freeze + DEFAULT_REMOTE = 'dev'.freeze + NEW_MR_URL = 'https://dev.gitlab.org/gitlab/gitlabhq/merge_requests/new'.freeze -options = { version: nil, branch: nil, sha: nil } + class SecurityFix + def initialize + @options = self.class.options + end -parser = OptionParser.new do |opts| - opts.banner = "Usage: #{$0} [options]" - opts.on('-v', '--version 10.0', 'Version') do |version| - options[:version] = version&.tr('.', '-') - end + def ee? + File.exist?('./CHANGELOG-EE.md') + end - opts.on('-b', '--branch security-fix-branch', 'Original branch name (optional, defaults to current)') do |branch| - options[:branch] = branch - end + def dry_run? + @options[:try] == true + end - opts.on('-s', '--sha abcd', 'SHA to cherry pick') do |sha| - options[:sha] = sha - end + def original_branch + @options[:branch].strip + end - opts.on('-r', '--remote abcd', 'Git remote name of dev.gitlab.org (optional, defaults to `dev`)') do |remote| - options[:remote] = remote - end + def source_branch + branch = "#{original_branch}-#{@options[:version]}" + branch.prepend("#{BRANCH_PREFIX}-") unless branch.start_with?("#{BRANCH_PREFIX}-") + branch.freeze + end - opts.on('-d', '--dry-run', 'Only show Git commands, without calling them') do |remote| - options[:try] = true - end + def security_branch + "#{BRANCH_PREFIX}-#{@options[:version]}".tap do |name| + name << "-ee" if ee? + end.freeze + end - opts.on('-h', '--help', 'Displays Help') do - puts opts + def git_commands + ["git fetch #{@options[:remote]} #{security_branch}", + "git checkout #{security_branch}", + "git pull #{@options[:remote]} #{security_branch}", + "git checkout -B #{source_branch}", + "git cherry-pick #{@options[:sha]}", + "git push #{@options[:remote]} #{source_branch}", + "git checkout #{original_branch}"] + end - exit - end -end - -parser.parse! - -options[:branch] ||= `git rev-parse --abbrev-ref HEAD` -options[:remote] ||= DEFAULT_REMOTE - -abort("Missing options. Use #{$0} --help to see the list of options available".red) if options.values.include?(nil) -abort("Wrong version format #{options[:version].bold}".red) unless options[:version] =~ /\A\d*\-\d*\Z/ - -class SecurityFix - def initialize(options) - @options = options - end - - def ee? - File.exist?('./CHANGELOG-EE.md') - end - - def dry_run? - @options[:try] == true - end - - def original_branch - @options[:branch].strip - end - - def source_branch - branch = "#{original_branch}-#{@options[:version]}" - branch.prepend("#{BRANCH_PREFIX}-") unless branch.start_with?("#{BRANCH_PREFIX}-") - branch = branch.freeze - end - - def security_branch - "#{BRANCH_PREFIX}-#{@options[:version]}".tap do |name| - name << "-ee" if ee? - end.freeze - end - - def git_commands - ["git fetch #{@options[:remote]} #{security_branch}", - "git checkout #{security_branch}", - "git pull #{@options[:remote]} #{security_branch}", - "git checkout -B #{source_branch}", - "git cherry-pick #{@options[:sha]}", - "git push #{@options[:remote]} #{source_branch}", - "git checkout #{original_branch}"] - end - - def gitlab_params - { - merge_request: { - source_branch: source_branch, - target_branch: security_branch, - title: "WIP: [#{@options[:version].tr('-', '.')}] ", - description: '/label ~security' + def gitlab_params + { + merge_request: { + source_branch: source_branch, + target_branch: security_branch, + title: "WIP: [#{@options[:version].tr('-', '.')}] ", + description: '/label ~security' + } } - } - end - - def new_mr_url - if ee? - NEW_MR_URL.sub('gitlabhq', 'gitlab-ee') - else - NEW_MR_URL end - end - def create! - if dry_run? - puts git_commands.join("\n").green - puts "\nMerge request params: ".blue - pp gitlab_params - else - cmd = git_commands.join(' && ') - stdin, stdout, stderr, wait_thr = Open3.popen3(cmd) - - puts stdout.read&.green - puts stderr.read&.red - - if wait_thr.value.success? - puts "#{new_mr_url}?#{gitlab_params.to_query}".blue + def new_mr_url + if ee? + NEW_MR_URL.sub('gitlabhq', 'gitlab-ee') + else + NEW_MR_URL end + end - stdin.close - stdout.close - stderr.close + def create! + if dry_run? + puts git_commands.join("\n").green + puts "\nMerge request params: ".blue + pp gitlab_params + else + cmd = git_commands.join(' && ') + stdin, stdout, stderr, wait_thr = Open3.popen3(cmd) + + puts stdout.read&.green + puts stderr.read&.red + + if wait_thr.value.success? + puts "#{new_mr_url}?#{gitlab_params.to_query}".blue + end + + stdin.close + stdout.close + stderr.close + end + end + + def self.options + { version: nil, branch: nil, sha: nil }.tap do |options| + parser = OptionParser.new do |opts| + opts.banner = "Usage: #{$0} [options]" + opts.on('-v', '--version 10.0', 'Version') do |version| + options[:version] = version&.tr('.', '-') + end + + opts.on('-b', '--branch security-fix-branch', 'Original branch name (optional, defaults to current)') do |branch| + options[:branch] = branch + end + + opts.on('-s', '--sha abcd', 'SHA to cherry pick') do |sha| + options[:sha] = sha + end + + opts.on('-r', '--remote abcd', 'Git remote name of dev.gitlab.org (optional, defaults to `dev`)') do |remote| + options[:remote] = remote + end + + opts.on('-d', '--dry-run', 'Only show Git commands, without calling them') do |remote| + options[:try] = true + end + + opts.on('-h', '--help', 'Displays Help') do + puts opts + + exit + end + end + + parser.parse! + + options[:branch] ||= `git rev-parse --abbrev-ref HEAD` + options[:remote] ||= DEFAULT_REMOTE + + abort("Missing options. Use #{$0} --help to see the list of options available".red) if options.values.include?(nil) + abort("Wrong version format #{options[:version].bold}".red) unless options[:version] =~ /\A\d*\-\d*\Z/ + end end end end -SecurityFix.new(options).create! +Secpick::SecurityFix.new.create!