create_pull_request.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # frozen_string_literal: true
  2. # Copyright 2022 Google LLC
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. require 'octokit'
  16. require 'optparse'
  17. @options = {
  18. repo_root: "./",
  19. pr_title: "Autogenerated PR",
  20. pr_body: "",
  21. base_branch: "generated-branch-for-pr-"+Time.new.strftime("%Y%m%d%H%M%S"),
  22. commit_comment: "Auto generated commit."
  23. }
  24. begin
  25. OptionParser.new do |opts|
  26. opts.banner = "Usage: create_pull_request.rb [options]"
  27. opts.on('-r', '--repo-root REPO_ROOT', 'Root path of the repo dir.') { |v| @options[:repo_root] = v }
  28. opts.on('-t', '--repo-token REPO_TOKEN', 'Token with write access') { |v| @options[:repo_token] = v }
  29. opts.on('-n', '--target-path PATH', 'Path of targeted file or dir') { |v| @options[:target_path] = v }
  30. opts.on('--pr-title PR_TITLE', 'Title of a PR') { |v| @options[:pr_title] = v }
  31. opts.on('--pr-body PR_BODY', 'Body of a PR') { |v| @options[:pr_body] = v }
  32. 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 }
  33. opts.on('--commit-comment COMMIT_COMMENT', 'Commit comment') { |v| @options[:commit_comment] = v }
  34. end.parse!
  35. raise OptionParser::MissingArgument if @options[:repo_token].nil? || @options[:target_path].nil?
  36. rescue OptionParser::MissingArgument
  37. puts "target path, `--target-path`, should be specified. " if @options[:target_path].nil?
  38. puts "A token ,`--repo-token`, should be provided for creating a pull request." if @options[:repo_token].nil?
  39. raise
  40. end
  41. REPO_ROOT=@options[:repo_root]
  42. ACCESS_TOKEN=@options[:repo_token]
  43. TARGET_PATH=@options[:target_path]
  44. PR_TITLE=@options[:pr_title]
  45. PR_BODY=@options[:pr_body]
  46. BASE_BRANCH=@options[:base_branch]
  47. COMMIT_COMMENT=@options[:commit_comment]
  48. def generate_pr_for_target_changes(repo_root:, target_path:)
  49. if `git diff #{TARGET_PATH}`==""
  50. puts "The file, #{TARGET_PATH}, has no changes."
  51. return
  52. end
  53. system("cd #{REPO_ROOT}\ngit checkout -b #{BASE_BRANCH}\ngit add #{TARGET_PATH}\ngit commit -m \"#{COMMIT_COMMENT}\"\ngit push -u origin #{BASE_BRANCH}")
  54. client = Octokit::Client.new(access_token: ACCESS_TOKEN)
  55. client.create_pull_request("firebase/firebase-ios-sdk", "main", BASE_BRANCH, PR_TITLE, PR_BODY)
  56. end
  57. def main()
  58. generate_pr_for_target_changes(repo_root: REPO_ROOT, target_path: TARGET_PATH)
  59. end
  60. main()