From d94613c9ed2248ac5e8e163a467d8582e7251be3 Mon Sep 17 00:00:00 2001 From: Daniel Azuma Date: Thu, 18 Jul 2024 19:17:06 +0000 Subject: [PATCH] chore: Use discovery-artifact-manager to get discovery documents --- .github/workflows/dailly-generate-updates.yml | 2 +- .github/workflows/generate-updates.yml | 2 +- .toys/generate-updates.rb | 11 +++++++++- lib/google_apis.ex | 21 +++++++++++++++++++ lib/google_apis/api_config.ex | 4 ++++ 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/.github/workflows/dailly-generate-updates.yml b/.github/workflows/dailly-generate-updates.yml index 714e354e01..d52fbce857 100644 --- a/.github/workflows/dailly-generate-updates.yml +++ b/.github/workflows/dailly-generate-updates.yml @@ -27,4 +27,4 @@ jobs: run: "gem install --no-document toys" - name: execute run: | - toys generate-updates -v --fork --all + toys generate-updates -v --fork --all --use-dam diff --git a/.github/workflows/generate-updates.yml b/.github/workflows/generate-updates.yml index dd4dd59b25..5b00cbce37 100644 --- a/.github/workflows/generate-updates.yml +++ b/.github/workflows/generate-updates.yml @@ -30,4 +30,4 @@ jobs: run: "gem install --no-document toys" - name: execute run: | - toys generate-updates -v --fork ${{ github.event.inputs.args }} + toys generate-updates -v --fork --use-dam ${{ github.event.inputs.args }} diff --git a/.toys/generate-updates.rb b/.toys/generate-updates.rb index 9e4b7946dd..5375c13fe2 100644 --- a/.toys/generate-updates.rb +++ b/.toys/generate-updates.rb @@ -14,6 +14,9 @@ desc "Run standard Google client generation." +flag :use_dam do + desc "Get discovery documents from discovery-artifact-manager instead of the discovery service" +end 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 @@ -53,6 +56,7 @@ def run @timestamp = Time.now.utc.strftime("%Y%m%d-%H%M%S") setup_builds + setup_dam if use_dam api_names = list_apis api_names.each_with_index do |entry, index| handle_package entry, index + 1, api_names.size @@ -64,6 +68,11 @@ def setup_builds exec ["mix", "compile"] end +def setup_dam + ENV["DISCOVERIES_DIR"] = git_cache.get("https://github.com/googleapis/discovery-artifact-manager.git", + path: "discoveries", update: true) +end + def list_apis return requested unless all api_list = JSON.parse File.read "#{context_directory}/config/apis.json" @@ -73,7 +82,7 @@ def list_apis def handle_package api_name, index, total branch_name = "gen/#{api_name}-#{@timestamp}" commit_message = "feat: Automated regeneration of #{api_name} client" - if open_pr_exists? commit_message + if !git_remote.nil? && open_pr_exists?(commit_message) puts "(#{index}/#{total}) Pull request already exists for #{api_name}", :yellow return end diff --git a/lib/google_apis.ex b/lib/google_apis.ex index dadad83814..69a1fc8554 100644 --- a/lib/google_apis.ex +++ b/lib/google_apis.ex @@ -22,6 +22,27 @@ defmodule GoogleApis do alias GoogleApis.ApiConfig def fetch(api_config) do + case System.fetch_env("DISCOVERIES_DIR") do + {:ok, dir} -> fetch_dir(api_config, dir) + :error -> fetch_discovery(api_config) + end + end + + def fetch_dir(api_config, dir) do + dest_file = ApiConfig.google_spec_file(api_config) + dam_name = ApiConfig.dam_name(api_config) + src_file = Path.expand(dam_name, dir) + + with {:ok, body} <- File.read(src_file), + :ok <- File.mkdir_p(Path.dirname(dest_file)), + :ok <- File.write(dest_file, body) do + {:ok, dest_file} + else + error -> IO.inspect(error) + end + end + + def fetch_discovery(api_config) do file = ApiConfig.google_spec_file(api_config) with {:ok, {body, _format}} <- GoogleApis.Discovery.fetch(api_config.url), diff --git a/lib/google_apis/api_config.ex b/lib/google_apis/api_config.ex index 144791c98b..b1eaf4c791 100644 --- a/lib/google_apis/api_config.ex +++ b/lib/google_apis/api_config.ex @@ -41,6 +41,10 @@ defmodule GoogleApis.ApiConfig do Macro.underscore(name) end + def dam_name(%{name: name, version: version}) do + "#{String.downcase(name)}.#{version}.json" + end + def library_namespace(api_config) do "#{library_root_namespace(api_config)}.#{module_version(api_config)}" end