From 35dbc592c80ddd9dde4575813bf6fdf02b3d709c Mon Sep 17 00:00:00 2001 From: Daniel Azuma Date: Tue, 12 Mar 2024 18:09:43 -0700 Subject: [PATCH] chore: Rewrite update-api-list wrapper using Toys (#11012) --- .github/workflows/update-api-list.yml | 63 ++++---------------- .toys/update-api-list.rb | 82 +++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 52 deletions(-) create mode 100644 .toys/update-api-list.rb diff --git a/.github/workflows/update-api-list.yml b/.github/workflows/update-api-list.yml index e39923c726..806592b134 100644 --- a/.github/workflows/update-api-list.yml +++ b/.github/workflows/update-api-list.yml @@ -9,68 +9,27 @@ jobs: update-api-list: if: ${{ github.repository_owner == 'googleapis' }} runs-on: ubuntu-20.04 + env: + GITHUB_TOKEN: ${{ secrets.YOSHI_CODE_BOT_TOKEN }} steps: - name: Checkout repo uses: actions/checkout@v4 + - name: Install Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: "3.3" - name: Install Elixir uses: erlef/setup-beam@v1 with: otp-version: "23.3.4.19" elixir-version: "1.12.3-otp-23" version-type: strict + - name: Install tools + run: | + gem install --no-document toys - name: Compile run: | mix do deps.get, compile - - name: Find apis.json updates + - name: Discover updates run: | - mix google_apis.discover_update apis.json - if [ -n "$(git diff)" ]; then - echo "FOUND_CHANGES=true" >> "$GITHUB_ENV" - echo "Changes were made to apis.json" - else - echo "FOUND_CHANGES=false" >> "$GITHUB_ENV" - echo "No changes were made to apis.json" - fi - - name: Push changes to branch - if: ${{ env.FOUND_CHANGES == 'true' }} - run: | - git config --global user.email "yoshi-automation@google.com" - git config --global user.name "Yoshi Automation Bot" - git checkout -b action/auto-update-api-list - git commit -a -m "chore: Automatic update of apis.json" - git push -f origin action/auto-update-api-list - - name: Open PR - if: ${{ env.FOUND_CHANGES == 'true' }} - uses: actions/github-script@v7 - with: - github-token: ${{ secrets.YOSHI_CODE_BOT_TOKEN }} - script: | - const existing = await github.rest.pulls.list({ - owner: context.repo.owner, - repo: context.repo.repo, - state: "open", - head: context.repo.owner + ":action/auto-update-api-list" - }); - if (existing.data.length == 0) { - core.info("No existing PR found."); - const result = await github.rest.pulls.create({ - owner: context.repo.owner, - repo: context.repo.repo, - title: "chore: Automatic update of apis.json", - head: context.repo.owner + ":action/auto-update-api-list", - base: "main", - maintainer_can_modify: true, - body: "Created by the update-api-list workflow.\n\nBe sure to review and edit CHECKME entries before merging." - }); - core.info("Created PR number " + result.data.number); - } else { - const prNumber = existing.data[0].number; - core.info("Found existing PR number " + prNumber); - await github.rest.issues.createComment({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: prNumber, - body: "Updated by the update-api-list workflow" - }); - core.info("Added comment to existing PR.") - } + toys update-api-list -v --fork diff --git a/.toys/update-api-list.rb b/.toys/update-api-list.rb new file mode 100644 index 0000000000..7261f13e05 --- /dev/null +++ b/.toys/update-api-list.rb @@ -0,0 +1,82 @@ +# Copyright 2024 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. + +desc "Update the API list from discovery" + +flag :git_remote, "--remote=NAME" do + desc "The name of the git remote to use as the pull request head. If omitted, does not open a pull request." +end +flag :enable_fork, "--fork" do + desc "The github user for whom to create/use a fork" +end + +include :exec, e: true +include :git_cache +include :terminal + +include "yoshi-pr-generator" + +def run + require "json" + Dir.chdir context_directory + + yoshi_utils.git_ensure_identity + if enable_fork + set :git_remote, "pull-request-fork" unless git_remote + yoshi_utils.gh_ensure_fork remote: git_remote + end + + @timestamp = Time.now.utc.strftime("%Y%m%d-%H%M%S") + setup_builds + update_api_list +end + +def setup_builds + exec ["mix", "deps.get"] + exec ["mix", "compile"] +end + +def update_api_list + branch_name = "action/auto-update-api-list" + commit_message = "chore: Automatic update of apis.json" + if open_pr_exists? commit_message + puts "Pull request already exists", :yellow + return + end + + result = yoshi_pr_generator.capture enabled: !git_remote.nil?, + remote: git_remote, + branch_name: branch_name, + commit_message: commit_message do + run_update + end + + case result + when Integer + puts "Opened pull request #{result}", :green, :bold + when :unchanged + puts "No changes", :magenta + else + puts "Generated API list", :cyan + end +end + +def open_pr_exists? title + content = capture ["gh", "pr", "list", "--search", "\"#{title}\" in:title", "--state=open", "--json=number"] + !JSON.parse(content).empty? +end + +def run_update + exec ["mix", "google_apis.discover_update", "apis.json"] +end