Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpl doctor command, including validation for duplicate app matching when prefix #150

Closed
wants to merge 2 commits into from

Conversation

rafaelgomesxyz
Copy link
Collaborator

@rafaelgomesxyz rafaelgomesxyz commented Apr 2, 2024

Add controlplane.yml verification, first check is invalid prefixes that collide with other apps or prefixes

There's currently a bug where if there's an app defined after another app (both with match_if_app_name_starts_with set to true), and the name of the second app is a prefix in the name of the first app, and we try to run commands for apps with a name matching the first app, the second app will be used instead.

For example, let's say that we have this in .controlplane/controlplane.yml:

app-test-other:
  <<: *common
  match_if_app_name_starts_with: true

app-test:
  <<: *common
  match_if_app_name_starts_with: true

If we call a command like cpl deploy-image -a app-test-other-123, it will use the config from app-test instead of the one from app-test-other.

This PR fixes that by immediately returning an exit code of 1 from any cpl command.

Maybe add a cpl doctor command, like brew doctor, giving helpful tips and verifying configuration.

@rafaelgomesxyz rafaelgomesxyz self-assigned this Apr 2, 2024
@rafaelgomesxyz rafaelgomesxyz added the bug Something isn't working label Apr 2, 2024
@justin808
Copy link
Member

Change to simply show error in this case where any prefix is included in any other app name (including prefixes).

Thus, we need some controlplane.yml verifier, and here's our first verification check:

def app_name_contained_in_other_app_name(app_name_list, app_prefix_list)
  sorted_list = app_name_list.sort
  matches = []
  app_prefix_list.each do |prefix|
    index = sorted_list.bsearch_index { |app_name| prefix <= app_name }
    next unless index
    next_app_name = sorted_list[index]
    if next_app_name.start_with?(prefix) && next_app_name != prefix
      matches << "'#{prefix}' is a prefix of '#{next_app_name}'"
    end
  end
  matches.empty? ? nil : matches.join(", ")
end

Tests:

require 'rspec'

describe '#app_name_contained_in_other_app_name' do
  it 'returns an informative message when a match is found' do
    result = app_name_contained_in_other_app_name(['app-test-other', 'app-test-again'], ['app-test'])
    expect(result).to eq("'app-test' is a prefix of 'app-test-other', 'app-test' is a prefix of 'app-test-again'")
  end

  it 'returns nil when no match is found' do
    result = app_name_contained_in_other_app_name(['app-review'], ['app-storybook'])
    expect(result).to be_nil
  end

  it 'returns nil when the prefix is the same as the app name' do
    result = app_name_contained_in_other_app_name(['short-name', 'short-name-etc'], ['short-name'])
    expect(result).to be_nil
  end

  it 'returns an informative message when the prefix is a prefix of the app name' do
    result = app_name_contained_in_other_app_name(['short-name-etc', 'short-name-etc-long'], ['short-name-etc'])
    expect(result).to eq("'short-name-etc' is a prefix of 'short-name-etc-long'")
  end

  it 'returns all matches when multiple prefixes match' do
    result = app_name_contained_in_other_app_name(['app-test-other', 'app-test-again', 'short-name-etc-long'], ['app-test', 'short-name-etc'])
    expect(result).to eq("'app-test' is a prefix of 'app-test-other', 'app-test' is a prefix of 'app-test-again', 'short-name-etc' is a prefix of 'short-name-etc-long'")
  end
end

@justin808 justin808 changed the title Fix app matching when name starts with cpl doctor command, including validation for duplicate app matching when prefix Apr 5, 2024
@rafaelgomesxyz
Copy link
Collaborator Author

Will open a separate PR for cpl doctor to keep things clean.

@rafaelgomesxyz rafaelgomesxyz deleted the app-matching-fix branch May 15, 2024 22:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants