Skip to content

Commit 8ba1d98

Browse files
authored
Refactor: extract translatePathsDashboards into a separate mutator (#3031)
## Why <!-- Why are these changes needed? Provide the context that the reviewer might be missing. For example, were there any decisions behind the change that are not reflected in the code itself? --> This change was suggested in #3006 [comment](#3006 (comment)) This simplifies the translatePaths mutator and adds explicit naming for the translatePathsDashboards, which is visible in debug logs. ## Tests <!-- How have you tested the changes? --> Existing acceptance tests for dashboards deployment <!-- If your PR needs to be included in the release notes for next release, add a separate entry in NEXT_CHANGELOG.md as part of your PR. -->
1 parent 48a01ca commit 8ba1d98

2 files changed

Lines changed: 35 additions & 26 deletions

File tree

acceptance/bundle/debug/out.stderr.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
10:07:59 Debug: Apply pid=12345 mutator=ProcessStaticResources
4545
10:07:59 Debug: Apply pid=12345 mutator=ProcessStaticResources mutator=ResolveVariableReferences(resources)
4646
10:07:59 Debug: Apply pid=12345 mutator=ProcessStaticResources mutator=NormalizePaths
47-
10:07:59 Debug: Apply pid=12345 mutator=ProcessStaticResources mutator=TranslatePaths
47+
10:07:59 Debug: Apply pid=12345 mutator=ProcessStaticResources mutator=TranslatePathsDashboards
4848
10:07:59 Debug: Apply pid=12345 mutator=PythonMutator(load)
4949
10:07:59 Debug: Apply pid=12345 mutator=PythonMutator(init)
5050
10:07:59 Debug: Apply pid=12345 mutator=PythonMutator(load_resources)

bundle/config/mutator/translate_paths.go

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ func (err ErrIsNotNotebook) Error() string {
4747
return fmt.Sprintf("file at %s is not a notebook", err.path)
4848
}
4949

50-
type translatePaths struct {
51-
dashboardsOnly bool
52-
}
50+
type translatePaths struct{}
51+
52+
type translatePathsDashboards struct{}
5353

5454
// TranslatePaths converts paths to local notebook files into paths in the workspace file system.
5555
func TranslatePaths() bundle.Mutator {
@@ -58,15 +58,17 @@ func TranslatePaths() bundle.Mutator {
5858

5959
// TranslatePathsDashboards converts paths to local dashboard files into paths in the workspace file system.
6060
func TranslatePathsDashboards() bundle.Mutator {
61-
return &translatePaths{
62-
dashboardsOnly: true,
63-
}
61+
return &translatePathsDashboards{}
6462
}
6563

6664
func (m *translatePaths) Name() string {
6765
return "TranslatePaths"
6866
}
6967

68+
func (m *translatePathsDashboards) Name() string {
69+
return "TranslatePathsDashboards"
70+
}
71+
7072
// translateContext is a context for rewriting paths in a config.
7173
// It is freshly instantiated on every mutator apply call.
7274
// It provides access to the underlying bundle object such that
@@ -290,12 +292,7 @@ func (t *translateContext) rewriteValue(ctx context.Context, p dyn.Path, v dyn.V
290292
return dyn.NewValue(out, v.Locations()), nil
291293
}
292294

293-
func (m *translatePaths) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
294-
t := &translateContext{
295-
b: b,
296-
seen: make(map[string]string),
297-
}
298-
295+
func applyTranslations(ctx context.Context, b *bundle.Bundle, t *translateContext, translations []func(context.Context, dyn.Value) (dyn.Value, error)) diag.Diagnostics {
299296
// Set the remote root to the sync root if source-linked deployment is enabled.
300297
// Otherwise, set it to the workspace file path.
301298
if config.IsExplicitlyEnabled(t.b.Config.Presets.SourceLinkedDeployment) {
@@ -307,19 +304,6 @@ func (m *translatePaths) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagn
307304
err := b.Config.Mutate(func(v dyn.Value) (dyn.Value, error) {
308305
var err error
309306

310-
translations := []func(context.Context, dyn.Value) (dyn.Value, error){
311-
t.applyJobTranslations,
312-
t.applyPipelineTranslations,
313-
t.applyArtifactTranslations,
314-
t.applyAppsTranslations,
315-
}
316-
317-
if m.dashboardsOnly {
318-
translations = []func(context.Context, dyn.Value) (dyn.Value, error){
319-
t.applyDashboardTranslations,
320-
}
321-
}
322-
323307
for _, fn := range translations {
324308
v, err = fn(ctx, v)
325309
if err != nil {
@@ -332,6 +316,31 @@ func (m *translatePaths) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagn
332316
return diag.FromErr(err)
333317
}
334318

319+
func (m *translatePaths) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
320+
t := &translateContext{
321+
b: b,
322+
seen: make(map[string]string),
323+
}
324+
325+
return applyTranslations(ctx, b, t, []func(context.Context, dyn.Value) (dyn.Value, error){
326+
t.applyJobTranslations,
327+
t.applyPipelineTranslations,
328+
t.applyArtifactTranslations,
329+
t.applyAppsTranslations,
330+
})
331+
}
332+
333+
func (m *translatePathsDashboards) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
334+
t := &translateContext{
335+
b: b,
336+
seen: make(map[string]string),
337+
}
338+
339+
return applyTranslations(ctx, b, t, []func(context.Context, dyn.Value) (dyn.Value, error){
340+
t.applyDashboardTranslations,
341+
})
342+
}
343+
335344
// gatherFallbackPaths collects the fallback paths for relative paths in the configuration.
336345
// Read more about the motivation for this functionality in the "fallback" path translation tests.
337346
func gatherFallbackPaths(v dyn.Value, typ string) (map[string]string, error) {

0 commit comments

Comments
 (0)