Improve `bin/secpick` script and add more options
This adds additional options that make it easier to use this script: 1. It adds `--dry-run` option that only displays Git commands that are going to be executed. 2. It adds `--remote` option that makes it possible to override Git remote name.
This commit is contained in:
parent
32fbc12cc5
commit
fde4eb7331
101
bin/secpick
101
bin/secpick
|
|
@ -8,7 +8,7 @@ require 'rainbow/refinement'
|
||||||
using Rainbow
|
using Rainbow
|
||||||
|
|
||||||
BRANCH_PREFIX = 'security'.freeze
|
BRANCH_PREFIX = 'security'.freeze
|
||||||
REMOTE = 'dev'.freeze
|
DEFAULT_REMOTE = 'dev'.freeze
|
||||||
NEW_MR_URL = 'https://dev.gitlab.org/gitlab/gitlabhq/merge_requests/new'.freeze
|
NEW_MR_URL = 'https://dev.gitlab.org/gitlab/gitlabhq/merge_requests/new'.freeze
|
||||||
|
|
||||||
options = { version: nil, branch: nil, sha: nil }
|
options = { version: nil, branch: nil, sha: nil }
|
||||||
|
|
@ -27,6 +27,14 @@ parser = OptionParser.new do |opts|
|
||||||
options[:sha] = sha
|
options[:sha] = sha
|
||||||
end
|
end
|
||||||
|
|
||||||
|
opts.on('-r', '--remote abcd', 'Git remote name of dev.gitlab.org (optional, default to `dev`)') do |remote|
|
||||||
|
options[:remote] = remote
|
||||||
|
end
|
||||||
|
|
||||||
|
opts.on('-d', '--dry-run', 'Show resulting Git commands without calling them') do |remote|
|
||||||
|
options[:try] = true
|
||||||
|
end
|
||||||
|
|
||||||
opts.on('-h', '--help', 'Displays Help') do
|
opts.on('-h', '--help', 'Displays Help') do
|
||||||
puts opts
|
puts opts
|
||||||
|
|
||||||
|
|
@ -37,39 +45,82 @@ end
|
||||||
parser.parse!
|
parser.parse!
|
||||||
|
|
||||||
options[:branch] ||= `git rev-parse --abbrev-ref HEAD`
|
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("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/
|
abort("Wrong version format #{options[:version].bold}".red) unless options[:version] =~ /\A\d*\-\d*\Z/
|
||||||
|
|
||||||
ee = File.exist?('./CHANGELOG-EE.md')
|
class SecurityFix
|
||||||
original_branch = options[:branch].strip
|
def initialize(options)
|
||||||
branch = "#{original_branch}-#{options[:version]}"
|
@options = options
|
||||||
branch.prepend("#{BRANCH_PREFIX}-") unless branch.start_with?("#{BRANCH_PREFIX}-")
|
end
|
||||||
branch = branch.freeze
|
|
||||||
stable_branch = "#{BRANCH_PREFIX}-#{options[:version]}".tap do |name|
|
|
||||||
name << "-ee" if ee
|
|
||||||
end.freeze
|
|
||||||
|
|
||||||
command = "git fetch #{REMOTE} #{stable_branch} && git checkout #{stable_branch} && git pull #{REMOTE} #{stable_branch} && git checkout -B #{branch} && git cherry-pick #{options[:sha]} && git push #{REMOTE} #{branch} && git checkout #{original_branch}"
|
def ee?
|
||||||
|
File.exist?('./CHANGELOG-EE.md')
|
||||||
|
end
|
||||||
|
|
||||||
stdin, stdout, stderr, wait_thr = Open3.popen3(command)
|
def dry_run?
|
||||||
|
@options[:try] == true
|
||||||
|
end
|
||||||
|
|
||||||
puts stdout.read&.green
|
def original_branch
|
||||||
puts stderr.read&.red
|
@options[:branch].strip
|
||||||
|
end
|
||||||
|
|
||||||
if wait_thr.value.success?
|
def source_branch
|
||||||
params = {
|
branch = "#{original_branch}-#{@options[:version]}"
|
||||||
merge_request: {
|
branch.prepend("#{BRANCH_PREFIX}-") unless branch.start_with?("#{BRANCH_PREFIX}-")
|
||||||
source_branch: branch,
|
branch = branch.freeze
|
||||||
target_branch: stable_branch,
|
end
|
||||||
title: "WIP: [#{options[:version].tr('-', '.')}] ",
|
|
||||||
description: '/label ~security'
|
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'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
end
|
||||||
|
|
||||||
puts "#{NEW_MR_URL}?#{params.to_query}".blue
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
stdin.close
|
SecurityFix.new(options).create!
|
||||||
stdout.close
|
|
||||||
stderr.close
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue