Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
4 changes: 4 additions & 0 deletions coderd/audit/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,14 @@ func TestDiff(t *testing.T) {
UpdatedAt: time.Now(),
OrganizationID: uuid.UUID{3},
Name: "rust",
CreatedBy: uuid.UUID{4},
},
exp: audit.Map{
"id": uuid.UUID{1}.String(),
"template_id": uuid.UUID{2}.String(),
"organization_id": uuid.UUID{3}.String(),
"name": "rust",
"created_by": uuid.UUID{4}.String(),
},
},
{
Expand All @@ -132,11 +134,13 @@ func TestDiff(t *testing.T) {
UpdatedAt: time.Now(),
OrganizationID: uuid.UUID{3},
Name: "rust",
CreatedBy: uuid.UUID{4},
},
exp: audit.Map{
"id": uuid.UUID{1}.String(),
"organization_id": uuid.UUID{3}.String(),
"name": "rust",
"created_by": uuid.UUID{4}.String(),
},
},
})
Expand Down
1 change: 1 addition & 0 deletions coderd/audit/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ var AuditableResources = auditMap(map[any]map[string]Action{
"name": ActionTrack,
"readme": ActionTrack,
"job_id": ActionIgnore, // Not helpful in a diff because jobs aren't tracked in audit logs.
"created_by": ActionTrack,
},
&database.User{}: {
"id": ActionTrack,
Expand Down
1 change: 1 addition & 0 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -1511,6 +1511,7 @@ func (q *fakeQuerier) InsertTemplateVersion(_ context.Context, arg database.Inse
Name: arg.Name,
Readme: arg.Readme,
JobID: arg.JobID,
CreatedBy: arg.CreatedBy,
}
q.templateVersions = append(q.templateVersions, version)
return version, nil
Expand Down
6 changes: 5 additions & 1 deletion coderd/database/dump.sql

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

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE ONLY template_versions DROP COLUMN IF EXISTS created_by;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ALTER TABLE ONLY template_versions ADD COLUMN IF NOT EXISTS created_by uuid REFERENCES users (id) ON DELETE RESTRICT;

UPDATE
template_versions
SET
created_by = (
SELECT created_by FROM templates
WHERE template_versions.template_id = templates.id
LIMIT 1
)
WHERE
created_by IS NULL;

ALTER TABLE ONLY template_versions ALTER COLUMN created_by SET NOT NULL;
1 change: 1 addition & 0 deletions coderd/database/models.go

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

23 changes: 16 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.

5 changes: 3 additions & 2 deletions coderd/database/queries/templateversions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ INSERT INTO
updated_at,
"name",
readme,
job_id
job_id,
created_by
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *;
($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING *;

-- name: UpdateTemplateVersionByID :exec
UPDATE
Expand Down
57 changes: 52 additions & 5 deletions coderd/templateversions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package coderd

import (
"context"
"database/sql"
"encoding/json"
"errors"
Expand Down Expand Up @@ -36,7 +37,16 @@ func (api *API) templateVersion(rw http.ResponseWriter, r *http.Request) {
return
}

httpapi.Write(rw, http.StatusOK, convertTemplateVersion(templateVersion, convertProvisionerJob(job)))
createdByName, err := getUsernameByUserID(r.Context(), api.Database, templateVersion.CreatedBy)
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching creator name.",
Detail: err.Error(),
})
return
}

httpapi.Write(rw, http.StatusOK, convertTemplateVersion(templateVersion, convertProvisionerJob(job), createdByName))
}

func (api *API) patchCancelTemplateVersion(rw http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -476,7 +486,15 @@ func (api *API) templateVersionsByTemplate(rw http.ResponseWriter, r *http.Reque
})
return err
}
apiVersions = append(apiVersions, convertTemplateVersion(version, convertProvisionerJob(job)))
createdByName, err := getUsernameByUserID(r.Context(), api.Database, version.CreatedBy)
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching creator name.",
Detail: err.Error(),
})
return err
}
apiVersions = append(apiVersions, convertTemplateVersion(version, convertProvisionerJob(job), createdByName))
}

return nil
Expand Down Expand Up @@ -525,7 +543,16 @@ func (api *API) templateVersionByName(rw http.ResponseWriter, r *http.Request) {
return
}

httpapi.Write(rw, http.StatusOK, convertTemplateVersion(templateVersion, convertProvisionerJob(job)))
createdByName, err := getUsernameByUserID(r.Context(), api.Database, templateVersion.CreatedBy)
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching creator name.",
Detail: err.Error(),
})
return
}

httpapi.Write(rw, http.StatusOK, convertTemplateVersion(templateVersion, convertProvisionerJob(job), createdByName))
}

func (api *API) patchActiveTemplateVersion(rw http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -735,6 +762,7 @@ func (api *API) postTemplateVersionsByOrganization(rw http.ResponseWriter, r *ht
Name: namesgenerator.GetRandomName(1),
Readme: "",
JobID: provisionerJob.ID,
CreatedBy: apiKey.UserID,
})
if err != nil {
return xerrors.Errorf("insert template version: %w", err)
Expand All @@ -748,7 +776,16 @@ func (api *API) postTemplateVersionsByOrganization(rw http.ResponseWriter, r *ht
return
}

httpapi.Write(rw, http.StatusCreated, convertTemplateVersion(templateVersion, convertProvisionerJob(provisionerJob)))
createdByName, err := getUsernameByUserID(r.Context(), api.Database, templateVersion.CreatedBy)
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
Message: "Internal error fetching creator name.",
Detail: err.Error(),
})
return
}

httpapi.Write(rw, http.StatusCreated, convertTemplateVersion(templateVersion, convertProvisionerJob(provisionerJob), createdByName))
}

// templateVersionResources returns the workspace agent resources associated
Expand Down Expand Up @@ -796,7 +833,15 @@ func (api *API) templateVersionLogs(rw http.ResponseWriter, r *http.Request) {
api.provisionerJobLogs(rw, r, job)
}

func convertTemplateVersion(version database.TemplateVersion, job codersdk.ProvisionerJob) codersdk.TemplateVersion {
func getUsernameByUserID(ctx context.Context, db database.Store, userID uuid.UUID) (string, error) {
user, err := db.GetUserByID(ctx, userID)
if err != nil {
return "", err
}
return user.Username, nil
}

func convertTemplateVersion(version database.TemplateVersion, job codersdk.ProvisionerJob, createdByName string) codersdk.TemplateVersion {
return codersdk.TemplateVersion{
ID: version.ID,
TemplateID: &version.TemplateID.UUID,
Expand All @@ -806,5 +851,7 @@ func convertTemplateVersion(version database.TemplateVersion, job codersdk.Provi
Name: version.Name,
Job: job,
Readme: version.Readme,
CreatedByID: version.CreatedBy,
CreatedByName: createdByName,
}
}
2 changes: 2 additions & 0 deletions codersdk/templateversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type TemplateVersion struct {
Name string `json:"name"`
Job ProvisionerJob `json:"job"`
Readme string `json:"readme"`
CreatedByID uuid.UUID `json:"created_by_id"`
CreatedByName string `json:"created_by_name"`
}

// TemplateVersion returns a template version by ID.
Expand Down
4 changes: 3 additions & 1 deletion site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export interface CreateTemplateRequest {
readonly min_autostart_interval_ms?: number
}

// From codersdk/templateversions.go:106:6
// From codersdk/templateversions.go:108:6
export interface CreateTemplateVersionDryRunRequest {
readonly WorkspaceName: string
readonly ParameterValues: CreateParameterRequest[]
Expand Down Expand Up @@ -290,6 +290,8 @@ export interface TemplateVersion {
readonly name: string
readonly job: ProvisionerJob
readonly readme: string
readonly created_by_id: string
readonly created_by_name: string
}

// From codersdk/templates.go:100:6
Expand Down
2 changes: 2 additions & 0 deletions site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ name:Template test
You can add instructions here

[Some link info](https://coder.com)`,
created_by_id: "test-creator-id",
created_by_name: "test_creator",
}

export const MockTemplate: TypesGen.Template = {
Expand Down