2018-04-17 12:50:16 -04:00
|
|
|
#!/usr/bin/env ruby
|
2019-01-03 07:24:01 -05:00
|
|
|
|
2018-12-18 04:21:29 -05:00
|
|
|
# frozen_string_literal: false
|
|
|
|
|
|
|
|
require 'active_support/core_ext/object/to_query'
|
2018-04-17 12:50:16 -04:00
|
|
|
require 'optparse'
|
|
|
|
require 'open3'
|
|
|
|
require 'rainbow/refinement'
|
|
|
|
using Rainbow
|
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
module Secpick
|
|
|
|
BRANCH_PREFIX = 'security'.freeze
|
|
|
|
DEFAULT_REMOTE = 'dev'.freeze
|
|
|
|
NEW_MR_URL = 'https://dev.gitlab.org/gitlab/gitlabhq/merge_requests/new'.freeze
|
2018-04-17 12:50:16 -04:00
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
class SecurityFix
|
|
|
|
def initialize
|
|
|
|
@options = self.class.options
|
|
|
|
end
|
2018-04-17 12:50:16 -04:00
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
def ee?
|
|
|
|
File.exist?('./CHANGELOG-EE.md')
|
|
|
|
end
|
2019-01-03 06:58:41 -05:00
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
def dry_run?
|
|
|
|
@options[:try] == true
|
|
|
|
end
|
2019-01-03 06:58:41 -05:00
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
def original_branch
|
|
|
|
@options[:branch].strip
|
|
|
|
end
|
2018-04-17 12:50:16 -04:00
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
def source_branch
|
|
|
|
branch = "#{original_branch}-#{@options[:version]}"
|
|
|
|
branch.prepend("#{BRANCH_PREFIX}-") unless branch.start_with?("#{BRANCH_PREFIX}-")
|
|
|
|
branch.freeze
|
|
|
|
end
|
2018-04-17 12:50:16 -04:00
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
def security_branch
|
|
|
|
"#{BRANCH_PREFIX}-#{@options[:version]}".tap do |name|
|
|
|
|
name << "-ee" if ee?
|
|
|
|
end.freeze
|
|
|
|
end
|
2018-04-17 12:50:16 -04:00
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
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
|
2018-10-24 13:20:03 -04:00
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
def gitlab_params
|
|
|
|
{
|
|
|
|
merge_request: {
|
|
|
|
source_branch: source_branch,
|
|
|
|
target_branch: security_branch,
|
|
|
|
title: "WIP: [#{@options[:version].tr('-', '.')}] ",
|
|
|
|
description: '/label ~security'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2018-04-17 12:50:16 -04:00
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
def new_mr_url
|
|
|
|
if ee?
|
|
|
|
NEW_MR_URL.sub('gitlabhq', 'gitlab-ee')
|
|
|
|
else
|
|
|
|
NEW_MR_URL
|
|
|
|
end
|
|
|
|
end
|
2019-01-03 06:58:41 -05:00
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
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
|
2019-01-03 06:58:41 -05:00
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
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
|
2019-01-03 06:58:41 -05:00
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
opts.on('-b', '--branch security-fix-branch', 'Original branch name (optional, defaults to current)') do |branch|
|
|
|
|
options[:branch] = branch
|
|
|
|
end
|
2019-01-03 06:58:41 -05:00
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
opts.on('-s', '--sha abcd', 'SHA to cherry pick') do |sha|
|
|
|
|
options[:sha] = sha
|
|
|
|
end
|
2019-01-03 06:58:41 -05:00
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
opts.on('-r', '--remote abcd', 'Git remote name of dev.gitlab.org (optional, defaults to `dev`)') do |remote|
|
|
|
|
options[:remote] = remote
|
|
|
|
end
|
2019-01-03 06:58:41 -05:00
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
opts.on('-d', '--dry-run', 'Only show Git commands, without calling them') do |remote|
|
|
|
|
options[:try] = true
|
|
|
|
end
|
2019-01-03 06:58:41 -05:00
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
opts.on('-h', '--help', 'Displays Help') do
|
|
|
|
puts opts
|
2018-12-18 04:21:29 -05:00
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
2019-01-03 07:14:09 -05:00
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
parser.parse!
|
2019-01-03 06:58:41 -05:00
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
options[:branch] ||= `git rev-parse --abbrev-ref HEAD`
|
|
|
|
options[:remote] ||= DEFAULT_REMOTE
|
2019-01-03 06:58:41 -05:00
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
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/
|
2019-01-03 06:58:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-12-18 04:21:29 -05:00
|
|
|
end
|
|
|
|
|
2019-01-03 07:24:01 -05:00
|
|
|
Secpick::SecurityFix.new.create!
|