|
| 1 | +# Copyright 2024 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 | +require "uri" |
| 16 | + |
| 17 | +desc "Freeze or unfreeze releases." |
| 18 | + |
| 19 | +flag :git_remote, "--remote=NAME" do |
| 20 | + desc "The name of the git remote to use as the pull request head. If omitted, does not open a pull request." |
| 21 | +end |
| 22 | +flag :enable_fork, "--fork" do |
| 23 | + desc "The github user for whom to create/use a fork" |
| 24 | +end |
| 25 | + |
| 26 | +exactly_one desc: "Operations" do |
| 27 | + flag :on, desc: "Turn on the release freeze" |
| 28 | + flag :off, desc: "Turn off the release freeze" |
| 29 | +end |
| 30 | + |
| 31 | +include :exec, e: true |
| 32 | +include :terminal |
| 33 | + |
| 34 | +include "yoshi-pr-generator" |
| 35 | + |
| 36 | +def run |
| 37 | + setup |
| 38 | + result = pr_update_freeze |
| 39 | + check_result result |
| 40 | +end |
| 41 | + |
| 42 | +def setup |
| 43 | + yoshi_utils.git_ensure_identity |
| 44 | + if enable_fork |
| 45 | + set :git_remote, "pull-request-fork" unless git_remote |
| 46 | + yoshi_utils.gh_ensure_fork remote: git_remote |
| 47 | + end |
| 48 | +end |
| 49 | + |
| 50 | +def pr_update_freeze |
| 51 | + timestamp = Time.now.utc.strftime("%Y%m%d-%H%M%S") |
| 52 | + file_path = "#{context_directory}/.kokoro/release.sh" |
| 53 | + branch_name = "pr/freeze-update-#{timestamp}" |
| 54 | + commit_message = "chore: #{on ? 'Activate' : 'Deactivate'} release freeze" |
| 55 | + yoshi_pr_generator.capture enabled: !git_remote.nil?, |
| 56 | + remote: git_remote, |
| 57 | + branch_name: branch_name, |
| 58 | + commit_message: commit_message do |
| 59 | + content = File.read file_path |
| 60 | + new_line = on ? "FREEZE_RELEASES=true" : "FREEZE_RELEASES=" |
| 61 | + new_content = content.gsub(/FREEZE_RELEASES=.*$/, new_line) |
| 62 | + if content == new_content |
| 63 | + puts "No changes for release freeze", :yellow, :bold |
| 64 | + exit 1 |
| 65 | + end |
| 66 | + File.write file_path, new_content |
| 67 | + end |
| 68 | +end |
| 69 | + |
| 70 | +def check_result result |
| 71 | + case result |
| 72 | + when Integer |
| 73 | + puts "Opened pull request #{result}", :green, :bold |
| 74 | + when :unchanged |
| 75 | + puts "No changes for release freeze", :yellow, :bold |
| 76 | + exit 1 |
| 77 | + when :aborted |
| 78 | + puts "Error when modifying scripts for release freeze", :red, :bold |
| 79 | + exit 1 |
| 80 | + else |
| 81 | + puts "Modified scripts for release freeze", :cyan |
| 82 | + end |
| 83 | +end |
0 commit comments