Skip to content

Get previous template version #5230

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

Merged
merged 17 commits into from
Dec 6, 2022
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
Prev Previous commit
Next Next commit
Scope previous version to the template id
  • Loading branch information
BrunoQuaresma committed Dec 1, 2022
commit 764a596f002c142441b9b100452020537d634c03
28 changes: 18 additions & 10 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -1717,31 +1717,39 @@ func (q *fakeQuerier) GetTemplateVersionByJobID(_ context.Context, jobID uuid.UU
return database.TemplateVersion{}, sql.ErrNoRows
}

func (q *fakeQuerier) GetPreviousTemplateVersionByOrganizationAndName(_ context.Context, arg database.GetPreviousTemplateVersionByOrganizationAndNameParams) (database.TemplateVersion, error) {
func (q *fakeQuerier) GetPreviousTemplateVersion(_ context.Context, arg database.GetPreviousTemplateVersionParams) (database.TemplateVersion, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

templateVersions := slices.Clone(q.templateVersions)
slices.SortFunc(templateVersions, func(i, j database.TemplateVersion) bool {
sortedTemplateVersions := slices.Clone(q.templateVersions)
slices.SortFunc(sortedTemplateVersions, func(i, j database.TemplateVersion) bool {
return i.CreatedAt.After(j.CreatedAt)
})

var previousIndex = -1
for index, templateVersion := range templateVersions {
templateVersions := make([]database.TemplateVersion, 0)
for _, templateVersion := range sortedTemplateVersions {
if templateVersion.OrganizationID != arg.OrganizationID {
continue
}
if !strings.EqualFold(templateVersion.Name, arg.Name) {
if templateVersion.TemplateID != arg.TemplateID {
continue
}
previousIndex = index - 1
templateVersions = append(templateVersions, templateVersion)
}

if previousIndex < 0 {
return database.TemplateVersion{}, sql.ErrNoRows
templateVersionIndex := -1
for versionIndex, templateVersion := range templateVersions {
if strings.EqualFold(templateVersion.Name, arg.Name) {
templateVersionIndex = versionIndex
break
}
}

return templateVersions[previousIndex], nil
if templateVersionIndex > 0 {
return templateVersions[templateVersionIndex], nil
}

return database.TemplateVersion{}, sql.ErrNoRows
}

func (q *fakeQuerier) GetParameterSchemasByJobID(_ context.Context, jobID uuid.UUID) ([]database.ParameterSchema, error) {
Expand Down
2 changes: 1 addition & 1 deletion coderd/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions coderd/database/queries/templateversions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ SET
WHERE
job_id = $1;

-- name: GetPreviousTemplateVersionByOrganizationAndName :one
-- name: GetPreviousTemplateVersion :one
SELECT
*
FROM
Expand All @@ -120,7 +120,7 @@ WHERE
created_at < (
SELECT created_at
FROM template_versions as tv
WHERE tv.organization_id = $1 AND tv.name = $2
WHERE tv.organization_id = $1 AND tv.name = $2 AND tv.template_id = $3
)
ORDER BY created_at DESC
LIMIT 1;
28 changes: 24 additions & 4 deletions coderd/templateversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,13 +644,32 @@ func (api *API) previousTemplateVersionByOrganizationAndName(rw http.ResponseWri
ctx := r.Context()
organization := httpmw.OrganizationParam(r)
templateVersionName := chi.URLParam(r, "templateversionname")
templateVersion, err := api.Database.GetPreviousTemplateVersionByOrganizationAndName(ctx, database.GetPreviousTemplateVersionByOrganizationAndNameParams{
templateVersion, err := api.Database.GetTemplateVersionByOrganizationAndName(ctx, database.GetTemplateVersionByOrganizationAndNameParams{
OrganizationID: organization.ID,
Name: templateVersionName,
})
if errors.Is(err, sql.ErrNoRows) {
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
Message: fmt.Sprintf("No previous template version found for name %q.", templateVersionName),
Message: fmt.Sprintf("No template version found by name %q.", templateVersionName),
})
return
}
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching template version.",
Detail: err.Error(),
})
return
}

previousTemplateVersion, err := api.Database.GetPreviousTemplateVersion(ctx, database.GetPreviousTemplateVersionParams{
OrganizationID: organization.ID,
Name: templateVersionName,
TemplateID: templateVersion.TemplateID,
})
if errors.Is(err, sql.ErrNoRows) {
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
Message: fmt.Sprintf("No previous template version found for %q.", templateVersionName),
})
return
}
Expand All @@ -661,7 +680,8 @@ func (api *API) previousTemplateVersionByOrganizationAndName(rw http.ResponseWri
})
return
}
job, err := api.Database.GetProvisionerJobByID(ctx, templateVersion.JobID)

job, err := api.Database.GetProvisionerJobByID(ctx, previousTemplateVersion.JobID)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching provisioner job.",
Expand All @@ -679,7 +699,7 @@ func (api *API) previousTemplateVersionByOrganizationAndName(rw http.ResponseWri
return
}

httpapi.Write(ctx, rw, http.StatusOK, convertTemplateVersion(templateVersion, convertProvisionerJob(job), user))
httpapi.Write(ctx, rw, http.StatusOK, convertTemplateVersion(previousTemplateVersion, convertProvisionerJob(job), user))
}

func (api *API) patchActiveTemplateVersion(rw http.ResponseWriter, r *http.Request) {
Expand Down