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

fix: Skip reference parameter creation if parameter is unused in template #1673

Merged
merged 1 commit into from
Jan 17, 2025

Conversation

arthurpitman
Copy link
Contributor

This PR ensures that no reference parameter is added to a config if the parameter is unused in the template.

Due to the way the code currently works, the fix is made in both dependency resolvers. This could maybe be improved in a follow up PR, however changes to this code have performance implications and thus should be measured.

A test is also added demonstrating the correct behavior.

In practice the simplest way to demonstrate it is to create a classic config with a name that occurs often in configs out of context. A good example is a network zone with the name _.

This string occurs often in templates however as a false positive, which is detected via a regex:

func replaceAll(content string, key string, s string) string {
// The prefix and suffix we search for are alphanumerical, as well as the "-", and "_".
// From investigating, this character set seems to be the most basic regex that still avoids false positive substring matches.
str := fmt.Sprintf("([^a-zA-Z0-9_-])(%s)([^a-zA-Z0-9_-])", key)
// replace only strings that are not part of another larger string. See testcases for exact in/out values.
re, err := regexp.Compile(str)
if err != nil {
log.Debug("Failed to compile string %q to regex. Falling back to use simple string replace.", str)
return strings.ReplaceAll(content, key, s)
}
return re.ReplaceAllString(content, fmt.Sprintf("$1%s$3", s))
}

The simple fix here just checks if the replaceAll function actually changed the content of the template and if not, skips parameter creation.

Copy link

Unit Test Results

1 966 tests  +2   1 965 ✅ +2   54s ⏱️ -1s
  135 suites ±0       1 💤 ±0 
    1 files   ±0       0 ❌ ±0 

Results for commit e2c707b. ± Comparison against base commit 3290212.

@arthurpitman arthurpitman added the run-e2e-test Manually trigger the E2E tests for reviewed PRs label Jan 17, 2025
@arthurpitman arthurpitman marked this pull request as ready for review January 17, 2025 09:51
@arthurpitman arthurpitman requested a review from a team as a code owner January 17, 2025 09:51
Copy link

E2E Test Results

    4 files   -   1    272 suites   - 135   24m 58s ⏱️ - 49m 58s
2 085 tests ±  0  2 083 ✅ ±  0  2 💤 ±0  0 ❌ ±0 
2 237 runs   - 152  2 235 ✅  - 152  2 💤 ±0  0 ❌ ±0 

Results for commit e2c707b. ± Comparison against base commit 3290212.

This pull request removes 2 and adds 2 tests. Note that renamed tests count towards both.
github.com/dynatrace/dynatrace-configuration-as-code/v2/cmd/monaco/integrationtest/v2 ‑ TestPaginationClassic
github.com/dynatrace/dynatrace-configuration-as-code/v2/cmd/monaco/integrationtest/v2 ‑ TestPaginationPlatform
github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/download/dependency_resolution ‑ TestDependencyResolution/no_parameter_created_for_false_positive_BasicResolver
github.com/dynatrace/dynatrace-configuration-as-code/v2/pkg/download/dependency_resolution ‑ TestDependencyResolution/no_parameter_created_for_false_positive_FastResolver

@arthurpitman arthurpitman self-assigned this Jan 17, 2025
Copy link
Contributor

@jskelin jskelin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@arthurpitman arthurpitman merged commit 43bd64a into main Jan 17, 2025
22 checks passed
@arthurpitman arthurpitman deleted the fix/no-parameters-created-for-false-positives branch January 17, 2025 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
run-e2e-test Manually trigger the E2E tests for reviewed PRs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants