diff --git a/.github/check_license_headers.rb b/.github/check_license_headers.rb index 0b7689a32..e32ce944a 100644 --- a/.github/check_license_headers.rb +++ b/.github/check_license_headers.rb @@ -25,7 +25,7 @@ # under the License. LICENSE = File.read('./.github/license-header.txt') -files = `git ls-files | grep -E '\.rb|Rakefile|\.rake|\.erb|Gemfile|gemspec'`.split("\n") +files = `git ls-files | grep -E '\.rb|^Rakefile$|\.rake|\.erb|^Gemfile$|^gemspec$'`.split("\n") errors = [] files.each do |file| diff --git a/.github/workflows/compatibility.yml b/.github/workflows/compatibility.yml index ba804e74b..b92d92ad2 100644 --- a/.github/workflows/compatibility.yml +++ b/.github/workflows/compatibility.yml @@ -5,11 +5,15 @@ on: - "*" paths-ignore: - '*.md' + - '.github/workflows/generate_api.yml' + - 'api_generator/**' pull_request: branches: - "*" paths-ignore: - '*.md' + - '.github/workflows/generate_api.yml' + - 'api_generator/**' jobs: test-opensearch: diff --git a/.github/workflows/generate_api.yml b/.github/workflows/generate_api.yml index edcd6f48d..22332e620 100644 --- a/.github/workflows/generate_api.yml +++ b/.github/workflows/generate_api.yml @@ -1,5 +1,9 @@ name: Generate API from Spec on: + pull_request: + paths: + - '.github/workflows/generate_api.yml' + - 'api_generator/**' workflow_dispatch: schedule: - cron: "0 0 * * 0" # Every Sunday at midnight GMT @@ -37,18 +41,30 @@ jobs: bundle exec rake download_spec bundle exec rake generate_api + # This should only trigger on pull requests that modifies the api_generator directory + # and NOT on the scheduled workflow since the scheduled workflow will trigger the lint workflow instead. + - name: Lint generated files + if: ${{ github.event_name == 'pull_request' }} + working-directory: ./ + run: |- + bundle install + bundle exec rubocop + - name: Get current date id: date + if: ${{ github.event_name != 'pull_request' }} run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_ENV - name: GitHub App token id: github_app_token + if: ${{ github.event_name != 'pull_request' }} uses: tibdex/github-app-token@v2.1.0 with: app_id: ${{ secrets.APP_ID }} private_key: ${{ secrets.APP_PRIVATE_KEY }} - name: Create pull request + if: ${{ github.event_name != 'pull_request' }} uses: peter-evans/create-pull-request@v7 with: token: ${{ steps.github_app_token.outputs.token }} diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 364afafcb..59b373107 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -5,11 +5,15 @@ on: - "*" paths-ignore: - '*.md' + - '.github/workflows/generate_api.yml' + - 'api_generator/**' pull_request: branches: - "*" paths-ignore: - '*.md' + - '.github/workflows/generate_api.yml' + - 'api_generator/**' jobs: test-opensearch: env: diff --git a/.github/workflows/test-unreleased.yml b/.github/workflows/test-unreleased.yml index 63a826fad..80acc475c 100644 --- a/.github/workflows/test-unreleased.yml +++ b/.github/workflows/test-unreleased.yml @@ -6,11 +6,15 @@ on: - 'main' paths-ignore: - '*.md' + - '.github/workflows/generate_api.yml' + - 'api_generator/**' pull_request: branches: - 'main' paths-ignore: - '*.md' + - '.github/workflows/generate_api.yml' + - 'api_generator/**' jobs: test: diff --git a/api_generator/gemfile b/api_generator/Gemfile similarity index 100% rename from api_generator/gemfile rename to api_generator/Gemfile diff --git a/api_generator/gemfile.lock b/api_generator/Gemfile.lock similarity index 100% rename from api_generator/gemfile.lock rename to api_generator/Gemfile.lock diff --git a/api_generator/lib/generators/action_generator.rb b/api_generator/lib/generators/action_generator.rb index ba424ed68..715c11d5c 100644 --- a/api_generator/lib/generators/action_generator.rb +++ b/api_generator/lib/generators/action_generator.rb @@ -27,7 +27,7 @@ def initialize(gem_folder, namespace, action) @module_name = namespace.root ? 'Root' : namespace.name.camelize @method_name = action.name.underscore @valid_params_constant_name = "#{action.name.upcase}_QUERY_PARAMS" - @method_description = action.description.squeeze("\n").gsub("\n", "\n # ") + @method_description = action.description.split("\n").filter(&:present?).map(&:strip).join("\n # ") end def argument_descriptions diff --git a/api_generator/lib/spec_hash.rb b/api_generator/lib/spec_hash.rb index b7205f6ab..f9925ada7 100644 --- a/api_generator/lib/spec_hash.rb +++ b/api_generator/lib/spec_hash.rb @@ -33,14 +33,25 @@ class << self; attr_reader :parsed; end # @param [Hash] hash def initialize(hash = {}, parsed: true) + raise ArgumentError, "#{self.class} must be initialized with a Hash" unless hash.is_a?(Hash) @hash = parsed ? hash : parse(hash) + @parsed_keys = Set.new + end + + def to_s + "" + end + + def is_a?(klass) + klass == SpecHash || super end def [](key) - parse(@hash[key]) + return @hash[key] if @parsed_keys.include?(key) + @hash[key] = parse(@hash[key]) end - def respond_to_missing?(name) + def respond_to_missing?(name, ...) @hash.key?(name.to_s) || {}.respond_to?(name) || super end @@ -49,7 +60,7 @@ def method_missing(name, ...) warn "Accessing Hash attribute `#{name}` which is also a key of the SpecHash instance." if @hash.key?(name.to_s) return @hash.send(name, ...) end - parse(@hash[name.to_s]) + self[name.to_s] end private @@ -59,8 +70,8 @@ def parse(value) return value unless value.is_a?(Hash) ref = value.delete('$ref') value.transform_values! { |v| parse(v) } - return SpecHash.new(value) unless ref - SpecHash.new(parse(resolve(ref)).merge(value)) + value.merge!(resolve(ref)) if ref + SpecHash.new(value) end def resolve(ref)