|
| 1 | +# Copyright 2022 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +desc "Run standard Google client generation." |
| 16 | + |
| 17 | +flag :git_remote, "--remote=NAME" do |
| 18 | + desc "The name of the git remote to use as the pull request head. If omitted, does not open a pull request." |
| 19 | +end |
| 20 | +flag :enable_fork, "--fork" do |
| 21 | + desc "The github user for whom to create/use a fork" |
| 22 | +end |
| 23 | +flag :approval_token, "--approval-token" do |
| 24 | + default ENV["APPROVAL_GITHUB_TOKEN"] |
| 25 | + desc "GitHub token for adding labels and approving pull requests" |
| 26 | +end |
| 27 | +flag :all do |
| 28 | + desc "Generate all APIs" |
| 29 | +end |
| 30 | +flag :clean do |
| 31 | + desc "Open a PR to clean out old APIs if needed" |
| 32 | +end |
| 33 | + |
| 34 | +remaining_args :requested do |
| 35 | + desc "Requested api names" |
| 36 | +end |
| 37 | + |
| 38 | +include :exec, e: true |
| 39 | +include :git_cache |
| 40 | +include :terminal |
| 41 | + |
| 42 | +include "yoshi-pr-generator" |
| 43 | + |
| 44 | +def run |
| 45 | + require "json" |
| 46 | + Dir.chdir context_directory |
| 47 | + |
| 48 | + yoshi_utils.git_ensure_identity |
| 49 | + if enable_fork |
| 50 | + set :git_remote, "pull-request-fork" unless git_remote |
| 51 | + yoshi_utils.gh_ensure_fork remote: git_remote |
| 52 | + end |
| 53 | + |
| 54 | + @timestamp = Time.now.utc.strftime("%Y%m%d-%H%M%S") |
| 55 | + setup_builds |
| 56 | + api_names = list_apis |
| 57 | + api_names.each_with_index do |entry, index| |
| 58 | + handle_package entry, index + 1, api_names.size |
| 59 | + end |
| 60 | +end |
| 61 | + |
| 62 | +def setup_builds |
| 63 | + exec ["mix", "deps.get"] |
| 64 | +end |
| 65 | + |
| 66 | +def list_apis |
| 67 | + return requested unless all |
| 68 | + api_list = JSON.parse File.read "#{context_directory}/config/apis.json" |
| 69 | + api_list.map { |entry| entry["name"] }.uniq.compact.shuffle |
| 70 | +end |
| 71 | + |
| 72 | +def handle_package api_name, index, total |
| 73 | + branch_name = "gen/#{api_name}-#{@timestamp}" |
| 74 | + commit_message = "feat: Automated regeneration of #{api_name} client" |
| 75 | + if open_pr_exists? commit_message |
| 76 | + puts "(#{index}/#{total}) Pull request already exists for #{api_name}", :yellow |
| 77 | + return |
| 78 | + end |
| 79 | + approval_message = "Rubber-stamped client auto-generation!" |
| 80 | + result = yoshi_pr_generator.capture enabled: !git_remote.nil?, |
| 81 | + remote: git_remote, |
| 82 | + branch_name: branch_name, |
| 83 | + commit_message: commit_message, |
| 84 | + labels: ["do not merge"], |
| 85 | + auto_approve: approval_message, |
| 86 | + approval_token: approval_token do |
| 87 | + generate_package api_name |
| 88 | + end |
| 89 | + case result |
| 90 | + when Integer |
| 91 | + puts "(#{index}/#{total}) Opened pull request #{result} for #{api_name}", :green, :bold |
| 92 | + when :unchanged |
| 93 | + puts "(#{index}/#{total}) No changes for #{api_name}", :magenta |
| 94 | + else |
| 95 | + puts "(#{index}/#{total}) Generated #{api_name}", :cyan |
| 96 | + end |
| 97 | +end |
| 98 | + |
| 99 | +def open_pr_exists? title |
| 100 | + content = capture ["gh", "pr", "list", "--search", "\"#{title}\" in:title", "--state=open", "--json=number"] |
| 101 | + !JSON.parse(content).empty? |
| 102 | +end |
| 103 | + |
| 104 | +def generate_package api_name |
| 105 | + lower_api_name = api_name.gsub(/([A-Z]+)([A-Z][a-z])/, "\\1_\\2").gsub(/([a-z0-9])([A-Z])/, "\\1_\\2").downcase |
| 106 | + exec ["mix", "google_apis.generate", api_name] |
| 107 | + unless capture(["git", "status", "--porcelain", "clients/#{lower_api_name}"]).empty? |
| 108 | + exec ["mix", "google_apis.bump_version", api_name] |
| 109 | + end |
| 110 | +end |
0 commit comments