Skip to content

Commit 2db2fa4

Browse files
authored
Update PR when NOTICES changes. (#9735)
* Update PR when NOTICES changes.
1 parent 126965b commit 2db2fa4

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

.github/workflows/notice_generation.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,11 @@ jobs:
3939
- name: Create a pull request
4040
run: |
4141
gem install octokit
42-
ruby scripts/create_pull_request.rb --repo-root ${GITHUB_WORKSPACE} --repo-token ${{ secrets.GITHUB_TOKEN }} --notices-path ${{ env.NOTICES_PATH }}
42+
ruby scripts/create_pull_request.rb \
43+
--repo-root ${GITHUB_WORKSPACE} \
44+
--repo-token ${{ secrets.GITHUB_TOKEN }} \
45+
--target-path ${{ env.NOTICES_PATH }} \
46+
--pr-title "NOTICES Change" \
47+
--pr-body "NOTICES Change is detected." \
48+
--commit-comment "NOTICES change."
4349
shell: bash

scripts/create_pull_request.rb

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,53 @@
1818
require 'optparse'
1919

2020
@options = {
21-
repo_root: "./"
21+
repo_root: "./",
22+
pr_title: "Autogenerated PR",
23+
pr_body: "",
24+
base_branch: "generated-branch-for-pr-"+Time.new.strftime("%Y%m%d%H%M%S"),
25+
commit_comment: "Auto generated commit."
2226
}
2327
begin
2428
OptionParser.new do |opts|
2529
opts.banner = "Usage: create_pull_request.rb [options]"
2630
opts.on('-r', '--repo-root REPO_ROOT', 'Root path of the repo dir.') { |v| @options[:repo_root] = v }
2731
opts.on('-t', '--repo-token REPO_TOKEN', 'Token with write access') { |v| @options[:repo_token] = v }
28-
opts.on('-n', '--notices-path NOTICES_PATH', 'Path of NOTICES file') { |v| @options[:notices_path] = v }
32+
opts.on('-n', '--target-path PATH', 'Path of targeted file or dir') { |v| @options[:target_path] = v }
33+
opts.on('--pr-title PR_TITLE', 'Title of a PR') { |v| @options[:pr_title] = v }
34+
opts.on('--pr-body PR_BODY', 'Body of a PR') { |v| @options[:pr_body] = v }
35+
opts.on('--base-branch', 'A new branch will be generated if not specified or the branch does not exist.') { |v| @options[:base_branch] = v }
36+
opts.on('--commit-comment', 'Commit comment') { |v| @options[:commit_comment] = v }
2937
end.parse!
3038

31-
raise OptionParser::MissingArgument if @options[:repo_token].nil? || @options[:notices_path].nil?
39+
raise OptionParser::MissingArgument if @options[:repo_token].nil? || @options[:target_path].nil?
3240
rescue OptionParser::MissingArgument
33-
puts "Notices path, `--notices-path`, should be specified. " if @options[:notices_path].nil?
41+
puts "target path, `--target-path`, should be specified. " if @options[:target_path].nil?
3442
puts "A token ,`--repo-token`, should be provided for creating a pull request." if @options[:repo_token].nil?
3543
raise
3644
end
3745

3846
REPO_ROOT=@options[:repo_root]
3947
ACCESS_TOKEN=@options[:repo_token]
40-
NOTICES_PATH=@options[:notices_path]
48+
TARGET_PATH=@options[:target_path]
49+
PR_TITLE=@options[:pr_title]
50+
PR_BODY=@options[:pr_boday]
51+
BASE_BRANCH=@options[:base_branch]
52+
COMMIT_COMMENT=@options[:commit_comment]
4153

42-
def generate_pr_for_notices_changes(repo_root:, notices_path:)
43-
system("cd #{repo_root}")
44-
system("git checkout -b notices_diff_detected\n git add CoreOnly/NOTICES\n git commit -m \"NOTICES diff detected.\"\n git push -u origin notices_diff_detected")
54+
def generate_pr_for_target_changes(repo_root:, target_path:)
55+
system("cd #{REPO_ROOT}\ngit checkout -b #{BASE_BRANCH}\n")
56+
if `git diff #{TARGET_PATH}`==""
57+
puts "The file, #{TARGET_PATH}, has no changes."
58+
return
59+
end
60+
system("git add #{TARGET_PATH}\ngit commit -m \"#{COMMIT_COMMENT}\"\n git push -u origin #{BASE_BRANCH}")
4561
client = Octokit::Client.new(access_token: ACCESS_TOKEN)
46-
client.create_pull_request("firebase/firebase-ios-sdk", "master", "notices_diff_detected", "Pull Request title", "Pull Request body")
62+
client.create_pull_request("firebase/firebase-ios-sdk", "master", BASE_BRANCH, PR_TITLE, PR_BODY)
4763
end
4864

4965

5066
def main()
51-
generate_pr_for_notices_changes(repo_root: REPO_ROOT, notices_path: NOTICES_PATH)
67+
generate_pr_for_target_changes(repo_root: REPO_ROOT, target_path: TARGET_PATH)
5268
end
5369

5470
main()

0 commit comments

Comments
 (0)