secpick 1.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/usr/bin/env ruby
require 'optparse'
require 'open3'
require 'rainbow/refinement'
using Rainbow

BRANCH_PREFIX = 'security'.freeze
REMOTE = 'dev'.freeze

options = { version: nil, branch: nil, sha: nil }

parser = OptionParser.new do |opts|
  opts.banner = "Usage: #{$0} [options]"
  opts.on('-v', '--version 10.0', 'Version') do |version|
    options[:version] = version&.tr('.', '-')
  end

18
  opts.on('-b', '--branch security-fix-branch', 'Original branch name (optional, defaults to current)') do |branch|
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    options[:branch] = branch
  end

  opts.on('-s', '--sha abcd', 'SHA to cherry pick') do |sha|
    options[:sha] = sha
  end

  opts.on('-h', '--help', 'Displays Help') do
    puts opts

    exit
  end
end

parser.parse!

35 36
options[:branch] ||= `git rev-parse --abbrev-ref HEAD`

37 38 39
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/

40 41 42
ee = File.exist?('./CHANGELOG-EE.md')
original_branch = options[:branch].strip
branch = "#{original_branch}-#{options[:version]}"
43 44
branch.prepend("#{BRANCH_PREFIX}-") unless branch.start_with?("#{BRANCH_PREFIX}-")
branch = branch.freeze
45 46 47
stable_branch = "#{BRANCH_PREFIX}-#{options[:version]}".tap do |name|
  name << "-ee" if ee
end.freeze
48

49
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}"
50 51 52 53 54

_stdin, stdout, stderr = Open3.popen3(command)

puts stdout.read&.green
puts stderr.read&.red