Commit 1a2b9e31 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Fix Rubocop offenses in bin/secpick script

parent ab714baf
#!/usr/bin/env ruby #!/usr/bin/env ruby
# frozen_string_literal: false # frozen_string_literal: false
require 'active_support/core_ext/object/to_query' require 'active_support/core_ext/object/to_query'
...@@ -7,128 +8,132 @@ require 'open3' ...@@ -7,128 +8,132 @@ require 'open3'
require 'rainbow/refinement' require 'rainbow/refinement'
using Rainbow using Rainbow
BRANCH_PREFIX = 'security'.freeze module Secpick
DEFAULT_REMOTE = 'dev'.freeze BRANCH_PREFIX = 'security'.freeze
NEW_MR_URL = 'https://dev.gitlab.org/gitlab/gitlabhq/merge_requests/new'.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 }
parser = OptionParser.new do |opts| class SecurityFix
opts.banner = "Usage: #{$0} [options]" def initialize
opts.on('-v', '--version 10.0', 'Version') do |version| @options = self.class.options
options[:version] = version&.tr('.', '-') end
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| def ee?
options[:remote] = remote File.exist?('./CHANGELOG-EE.md')
end end
opts.on('-d', '--dry-run', 'Only show Git commands, without calling them') do |remote| def dry_run?
options[:try] = true @options[:try] == true
end end
opts.on('-h', '--help', 'Displays Help') do def original_branch
puts opts @options[:branch].strip
end
exit def source_branch
end branch = "#{original_branch}-#{@options[:version]}"
end branch.prepend("#{BRANCH_PREFIX}-") unless branch.start_with?("#{BRANCH_PREFIX}-")
branch.freeze
end
parser.parse! def security_branch
"#{BRANCH_PREFIX}-#{@options[:version]}".tap do |name|
name << "-ee" if ee?
end.freeze
end
options[:branch] ||= `git rev-parse --abbrev-ref HEAD` def git_commands
options[:remote] ||= DEFAULT_REMOTE ["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
abort("Missing options. Use #{$0} --help to see the list of options available".red) if options.values.include?(nil) def gitlab_params
abort("Wrong version format #{options[:version].bold}".red) unless options[:version] =~ /\A\d*\-\d*\Z/ {
merge_request: {
source_branch: source_branch,
target_branch: security_branch,
title: "WIP: [#{@options[:version].tr('-', '.')}] ",
description: '/label ~security'
}
}
end
class SecurityFix def new_mr_url
def initialize(options) if ee?
@options = options NEW_MR_URL.sub('gitlabhq', 'gitlab-ee')
end else
NEW_MR_URL
end
end
def ee? def create!
File.exist?('./CHANGELOG-EE.md') if dry_run?
end 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 dry_run? def self.options
@options[:try] == true { version: nil, branch: nil, sha: nil }.tap do |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 original_branch opts.on('-b', '--branch security-fix-branch', 'Original branch name (optional, defaults to current)') do |branch|
@options[:branch].strip options[:branch] = branch
end end
def source_branch opts.on('-s', '--sha abcd', 'SHA to cherry pick') do |sha|
branch = "#{original_branch}-#{@options[:version]}" options[:sha] = sha
branch.prepend("#{BRANCH_PREFIX}-") unless branch.start_with?("#{BRANCH_PREFIX}-") end
branch = branch.freeze
end
def security_branch opts.on('-r', '--remote abcd', 'Git remote name of dev.gitlab.org (optional, defaults to `dev`)') do |remote|
"#{BRANCH_PREFIX}-#{@options[:version]}".tap do |name| options[:remote] = remote
name << "-ee" if ee? end
end.freeze
end
def git_commands opts.on('-d', '--dry-run', 'Only show Git commands, without calling them') do |remote|
["git fetch #{@options[:remote]} #{security_branch}", options[:try] = true
"git checkout #{security_branch}", end
"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 opts.on('-h', '--help', 'Displays Help') do
{ puts opts
merge_request: {
source_branch: source_branch,
target_branch: security_branch,
title: "WIP: [#{@options[:version].tr('-', '.')}] ",
description: '/label ~security'
}
}
end
def new_mr_url exit
if ee? end
NEW_MR_URL.sub('gitlabhq', 'gitlab-ee') end
else
NEW_MR_URL
end
end
def create! parser.parse!
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 options[:branch] ||= `git rev-parse --abbrev-ref HEAD`
puts stderr.read&.red options[:remote] ||= DEFAULT_REMOTE
if wait_thr.value.success? abort("Missing options. Use #{$0} --help to see the list of options available".red) if options.values.include?(nil)
puts "#{new_mr_url}?#{gitlab_params.to_query}".blue abort("Wrong version format #{options[:version].bold}".red) unless options[:version] =~ /\A\d*\-\d*\Z/
end end
stdin.close
stdout.close
stderr.close
end end
end end
end end
SecurityFix.new(options).create! Secpick::SecurityFix.new.create!
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment