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

feat: Preview endpoint #2162

Merged
merged 16 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/pkg/service/templates/api/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (s *service) Preview(ctx context.Context, d dependencies.ProjectRequestScop
}

return task.
OkResult(fmt.Sprintf(`template preview created`))
OkResult(`template preview created`)
},
})
if err != nil {
Expand Down
22 changes: 14 additions & 8 deletions internal/pkg/template/mappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ import (

// useContext is common interface for *use.Context and *upgrade.Context.
type useContext interface {
TemplateRef() model.TemplateRef
previewContext
InstanceID() string
ObjectIds() metadata.ObjectIdsMap
InputsUsage() *metadata.InputsUsage
}

// useContext is common interface for *use.Context and *upgrade.Context.
// previewContext is common interface for *preview.Context.
type previewContext interface {
TemplateRef() model.TemplateRef
ObjectIds() metadata.ObjectIdsMap
Expand Down Expand Up @@ -74,14 +72,22 @@ func MappersFor(s *state.State, d dependencies, ctx Context) (mapper.Mappers, er
replacevalues.NewMapper(s, replacements),
}

// Add metadata on "template preview" operation
if c, ok := ctx.(previewContext); ok {
if _, isUseContext := ctx.(useContext); !isUseContext {
Matovidlo marked this conversation as resolved.
Show resolved Hide resolved
mappers = append(mappers, metadata.NewMapperWithoutInstanceID(
s,
c.TemplateRef(),
c.ObjectIds(),
c.InputsUsage(),
))
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The logic for adding metadata mappers in the template preview operation was previously duplicated. This update refactors the code to ensure that the metadata. NewMapperWithoutInstanceID mapper is added only once for the template preview operation.


// Add metadata on "template use" operation
if c, ok := ctx.(useContext); ok {
mappers = append(mappers, metadata.NewMapper(s, c.TemplateRef(), c.InstanceID(), c.ObjectIds(), c.InputsUsage()))
}
// Add metadata on "template preview" operation
if c, ok := ctx.(previewContext); ok {
mappers = append(mappers, metadata.NewMapperWithoutInstanceID(s, c.TemplateRef(), c.ObjectIds(), c.InputsUsage()))
}

return mappers, nil
}