Skip to content

Commit

Permalink
chore: Use discovery-artifact-manager to get discovery documents
Browse files Browse the repository at this point in the history
  • Loading branch information
dazuma committed Jul 18, 2024
1 parent ec72cbe commit d94613c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dailly-generate-updates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion .github/workflows/generate-updates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
11 changes: 10 additions & 1 deletion .toys/generate-updates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions lib/google_apis.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 4 additions & 0 deletions lib/google_apis/api_config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d94613c

Please sign in to comment.