2022-04-29 17:30:43 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
# Copyright 2022 Google LLC
|
|
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
|
|
require 'octokit'
|
|
|
|
|
require 'optparse'
|
|
|
|
|
|
|
|
|
|
@options = {
|
2022-05-02 17:35:12 -07:00
|
|
|
repo_root: "./",
|
|
|
|
|
pr_title: "Autogenerated PR",
|
|
|
|
|
pr_body: "",
|
|
|
|
|
base_branch: "generated-branch-for-pr-"+Time.new.strftime("%Y%m%d%H%M%S"),
|
|
|
|
|
commit_comment: "Auto generated commit."
|
2022-04-29 17:30:43 -07:00
|
|
|
}
|
|
|
|
|
begin
|
|
|
|
|
OptionParser.new do |opts|
|
|
|
|
|
opts.banner = "Usage: create_pull_request.rb [options]"
|
|
|
|
|
opts.on('-r', '--repo-root REPO_ROOT', 'Root path of the repo dir.') { |v| @options[:repo_root] = v }
|
|
|
|
|
opts.on('-t', '--repo-token REPO_TOKEN', 'Token with write access') { |v| @options[:repo_token] = v }
|
2022-05-02 17:35:12 -07:00
|
|
|
opts.on('-n', '--target-path PATH', 'Path of targeted file or dir') { |v| @options[:target_path] = v }
|
|
|
|
|
opts.on('--pr-title PR_TITLE', 'Title of a PR') { |v| @options[:pr_title] = v }
|
|
|
|
|
opts.on('--pr-body PR_BODY', 'Body of a PR') { |v| @options[:pr_body] = v }
|
2022-05-09 12:55:16 -07:00
|
|
|
opts.on('--base-branch BASE_BRANCH', 'A new branch will be generated if not specified or the branch does not exist.') { |v| @options[:base_branch] = v }
|
|
|
|
|
opts.on('--commit-comment COMMIT_COMMENT', 'Commit comment') { |v| @options[:commit_comment] = v }
|
2022-04-29 17:30:43 -07:00
|
|
|
end.parse!
|
|
|
|
|
|
2022-05-02 17:35:12 -07:00
|
|
|
raise OptionParser::MissingArgument if @options[:repo_token].nil? || @options[:target_path].nil?
|
2022-04-29 17:30:43 -07:00
|
|
|
rescue OptionParser::MissingArgument
|
2022-05-02 17:35:12 -07:00
|
|
|
puts "target path, `--target-path`, should be specified. " if @options[:target_path].nil?
|
2022-04-29 17:30:43 -07:00
|
|
|
puts "A token ,`--repo-token`, should be provided for creating a pull request." if @options[:repo_token].nil?
|
|
|
|
|
raise
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
REPO_ROOT=@options[:repo_root]
|
|
|
|
|
ACCESS_TOKEN=@options[:repo_token]
|
2022-05-02 17:35:12 -07:00
|
|
|
TARGET_PATH=@options[:target_path]
|
|
|
|
|
PR_TITLE=@options[:pr_title]
|
2022-05-09 12:55:16 -07:00
|
|
|
PR_BODY=@options[:pr_body]
|
2022-05-02 17:35:12 -07:00
|
|
|
BASE_BRANCH=@options[:base_branch]
|
|
|
|
|
COMMIT_COMMENT=@options[:commit_comment]
|
2022-04-29 17:30:43 -07:00
|
|
|
|
2022-05-02 17:35:12 -07:00
|
|
|
def generate_pr_for_target_changes(repo_root:, target_path:)
|
2026-08-19 17:35:12 -04:00
|
|
|
abs_target_path = File.expand_path(target_path)
|
|
|
|
|
Dir.chdir(repo_root) do
|
|
|
|
|
if system('git', 'diff', '--quiet', abs_target_path)
|
|
|
|
|
puts "The file, #{target_path}, has no changes."
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
system('git', 'checkout', '-B', BASE_BRANCH) || raise("git checkout failed")
|
|
|
|
|
system('git', 'add', abs_target_path) || raise("git add failed")
|
|
|
|
|
system('git', 'commit', '-m', COMMIT_COMMENT) || raise("git commit failed")
|
|
|
|
|
system('git', 'push', '-u', 'origin', BASE_BRANCH) || raise("git push failed")
|
2022-05-02 17:35:12 -07:00
|
|
|
end
|
2022-04-29 17:30:43 -07:00
|
|
|
client = Octokit::Client.new(access_token: ACCESS_TOKEN)
|
2023-12-15 15:29:22 -05:00
|
|
|
client.create_pull_request("firebase/firebase-ios-sdk", "main", BASE_BRANCH, PR_TITLE, PR_BODY)
|
2022-04-29 17:30:43 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main()
|
2022-05-02 17:35:12 -07:00
|
|
|
generate_pr_for_target_changes(repo_root: REPO_ROOT, target_path: TARGET_PATH)
|
2022-04-29 17:30:43 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
main()
|