Skip to content

Commit

Permalink
chore: Rewrite update-api-list wrapper using Toys (#11012)
Browse files Browse the repository at this point in the history
  • Loading branch information
dazuma authored Mar 13, 2024
1 parent dc534c8 commit 35dbc59
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 52 deletions.
63 changes: 11 additions & 52 deletions .github/workflows/update-api-list.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 "[email protected]"
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
82 changes: 82 additions & 0 deletions .toys/update-api-list.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 35dbc59

Please sign in to comment.