Skip to content

feat: add template version creator #3001

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 8 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all 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.NullUUID{UUID: uuid.UUID{4}, Valid: true},
},
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.NullUUID{UUID: uuid.UUID{4}, Valid: true},
},
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,16 @@
BEGIN;

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;

COMMIT;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dwahler I am hoping wrapping it in BEGIN-COMMIT would help to fix the problem caused by the migration. Not really sure if this broke the server though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interestingly, make gen is failing with:

panic: reached retry deadline
goroutine 1 [running]:
main.main()
	/home/runner/work/coder/coder/coderd/database/dump/main.go:19 +0x80c
exit status 2
make: *** [Makefile:60: coderd/database/dump.sql] Error 1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed the bug, the scoping logic was not good.

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
1 change: 1 addition & 0 deletions coderd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
UUID: dbTemplate.ID,
Valid: true,
},
UpdatedAt: database.Now(),
})
if err != nil {
return xerrors.Errorf("insert template version: %s", err)
Expand Down
63 changes: 58 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(), store, 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,10 @@ func (api *API) postTemplateVersionsByOrganization(rw http.ResponseWriter, r *ht
Name: namesgenerator.GetRandomName(1),
Readme: "",
JobID: provisionerJob.ID,
CreatedBy: uuid.NullUUID{
UUID: apiKey.UserID,
Valid: true,
},
})
if err != nil {
return xerrors.Errorf("insert template version: %w", err)
Expand All @@ -748,7 +779,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 +836,18 @@ 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.NullUUID) (string, error) {
if !userID.Valid {
return "", nil
}
user, err := db.GetUserByID(ctx, userID.UUID)
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 +857,7 @@ func convertTemplateVersion(version database.TemplateVersion, job codersdk.Provi
Name: version.Name,
Job: job,
Readme: version.Readme,
CreatedByID: version.CreatedBy.UUID,
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