Skip to content

feat: add README parsing to template versions #1500

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 4 commits into from
May 17, 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 .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@
{
"match": "database/queries/*.sql",
"cmd": "make gen"
},
{
"match": "provisionerd/proto/provisionerd.proto",
"cmd": "make provisionerd/proto/provisionerd.pb.go",
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion coderd/audit/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ var AuditableResources = auditMap(map[any]map[string]Action{
"created_at": ActionIgnore, // Never changes, but is implicit and not helpful in a diff.
"updated_at": ActionIgnore, // Changes, but is implicit and not helpful in a diff.
"name": ActionTrack,
"description": ActionTrack,
"readme": ActionTrack,
"job_id": ActionIgnore, // Not helpful in a diff because jobs aren't tracked in audit logs.
},
&database.User{}: {
Expand Down
3 changes: 1 addition & 2 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ func New(options *Options) (http.Handler, func()) {
r.Use(
apiKeyMiddleware,
httpmw.ExtractTemplateParam(options.Database),
httpmw.ExtractOrganizationParam(options.Database),
)

r.Get("/", api.template)
r.Delete("/", api.deleteTemplate)
r.Route("/versions", func(r chi.Router) {
Expand All @@ -189,7 +189,6 @@ func New(options *Options) (http.Handler, func()) {
r.Use(
apiKeyMiddleware,
httpmw.ExtractTemplateVersionParam(options.Database),
httpmw.ExtractOrganizationParam(options.Database),
)

r.Get("/", api.templateVersion)
Expand Down
43 changes: 30 additions & 13 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sort"
"strings"
"sync"
"time"

"github.com/google/uuid"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -1189,7 +1190,7 @@ func (q *fakeQuerier) InsertTemplateVersion(_ context.Context, arg database.Inse
CreatedAt: arg.CreatedAt,
UpdatedAt: arg.UpdatedAt,
Name: arg.Name,
Description: arg.Description,
Readme: arg.Readme,
JobID: arg.JobID,
}
q.templateVersions = append(q.templateVersions, version)
Expand Down Expand Up @@ -1478,7 +1479,7 @@ func (q *fakeQuerier) UpdateTemplateActiveVersionByID(_ context.Context, arg dat
defer q.mutex.Unlock()

for index, template := range q.templates {
if template.ID.String() != arg.ID.String() {
if template.ID != arg.ID {
continue
}
template.ActiveVersionID = arg.ActiveVersionID
Expand All @@ -1493,7 +1494,7 @@ func (q *fakeQuerier) UpdateTemplateDeletedByID(_ context.Context, arg database.
defer q.mutex.Unlock()

for index, template := range q.templates {
if template.ID.String() != arg.ID.String() {
if template.ID != arg.ID {
continue
}
template.Deleted = arg.Deleted
Expand All @@ -1508,7 +1509,7 @@ func (q *fakeQuerier) UpdateTemplateVersionByID(_ context.Context, arg database.
defer q.mutex.Unlock()

for index, templateVersion := range q.templateVersions {
if templateVersion.ID.String() != arg.ID.String() {
if templateVersion.ID != arg.ID {
continue
}
templateVersion.TemplateID = arg.TemplateID
Expand All @@ -1519,12 +1520,28 @@ func (q *fakeQuerier) UpdateTemplateVersionByID(_ context.Context, arg database.
return sql.ErrNoRows
}

func (q *fakeQuerier) UpdateTemplateVersionDescriptionByJobID(_ context.Context, arg database.UpdateTemplateVersionDescriptionByJobIDParams) error {
q.mutex.Lock()
defer q.mutex.Unlock()

for index, templateVersion := range q.templateVersions {
if templateVersion.JobID != arg.JobID {
continue
}
templateVersion.Readme = arg.Readme
templateVersion.UpdatedAt = time.Now()
q.templateVersions[index] = templateVersion
return nil
}
return sql.ErrNoRows
}

func (q *fakeQuerier) UpdateProvisionerDaemonByID(_ context.Context, arg database.UpdateProvisionerDaemonByIDParams) error {
q.mutex.Lock()
defer q.mutex.Unlock()

for index, daemon := range q.provisionerDaemons {
if arg.ID.String() != daemon.ID.String() {
if arg.ID != daemon.ID {
continue
}
daemon.UpdatedAt = arg.UpdatedAt
Expand All @@ -1540,7 +1557,7 @@ func (q *fakeQuerier) UpdateWorkspaceAgentConnectionByID(_ context.Context, arg
defer q.mutex.Unlock()

for index, agent := range q.provisionerJobAgents {
if agent.ID.String() != arg.ID.String() {
if agent.ID != arg.ID {
continue
}
agent.FirstConnectedAt = arg.FirstConnectedAt
Expand All @@ -1557,7 +1574,7 @@ func (q *fakeQuerier) UpdateProvisionerJobByID(_ context.Context, arg database.U
defer q.mutex.Unlock()

for index, job := range q.provisionerJobs {
if arg.ID.String() != job.ID.String() {
if arg.ID != job.ID {
continue
}
job.UpdatedAt = arg.UpdatedAt
Expand All @@ -1572,7 +1589,7 @@ func (q *fakeQuerier) UpdateProvisionerJobWithCancelByID(_ context.Context, arg
defer q.mutex.Unlock()

for index, job := range q.provisionerJobs {
if arg.ID.String() != job.ID.String() {
if arg.ID != job.ID {
continue
}
job.CanceledAt = arg.CanceledAt
Expand All @@ -1587,7 +1604,7 @@ func (q *fakeQuerier) UpdateProvisionerJobWithCompleteByID(_ context.Context, ar
defer q.mutex.Unlock()

for index, job := range q.provisionerJobs {
if arg.ID.String() != job.ID.String() {
if arg.ID != job.ID {
continue
}
job.UpdatedAt = arg.UpdatedAt
Expand All @@ -1604,7 +1621,7 @@ func (q *fakeQuerier) UpdateWorkspaceAutostart(_ context.Context, arg database.U
defer q.mutex.Unlock()

for index, workspace := range q.workspaces {
if workspace.ID.String() != arg.ID.String() {
if workspace.ID != arg.ID {
continue
}
workspace.AutostartSchedule = arg.AutostartSchedule
Expand All @@ -1620,7 +1637,7 @@ func (q *fakeQuerier) UpdateWorkspaceAutostop(_ context.Context, arg database.Up
defer q.mutex.Unlock()

for index, workspace := range q.workspaces {
if workspace.ID.String() != arg.ID.String() {
if workspace.ID != arg.ID {
continue
}
workspace.AutostopSchedule = arg.AutostopSchedule
Expand All @@ -1636,7 +1653,7 @@ func (q *fakeQuerier) UpdateWorkspaceBuildByID(_ context.Context, arg database.U
defer q.mutex.Unlock()

for index, workspaceBuild := range q.workspaceBuilds {
if workspaceBuild.ID.String() != arg.ID.String() {
if workspaceBuild.ID != arg.ID {
continue
}
workspaceBuild.UpdatedAt = arg.UpdatedAt
Expand All @@ -1653,7 +1670,7 @@ func (q *fakeQuerier) UpdateWorkspaceDeletedByID(_ context.Context, arg database
defer q.mutex.Unlock()

for index, workspace := range q.workspaces {
if workspace.ID.String() != arg.ID.String() {
if workspace.ID != arg.ID {
continue
}
workspace.Deleted = arg.Deleted
Expand Down
2 changes: 1 addition & 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 template_versions RENAME README TO description;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE template_versions RENAME description TO readme;
2 changes: 1 addition & 1 deletion coderd/database/models.go

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

1 change: 1 addition & 0 deletions coderd/database/querier.go

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

46 changes: 33 additions & 13 deletions coderd/database/queries.sql.go

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

11 changes: 10 additions & 1 deletion coderd/database/queries/templateversions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ INSERT INTO
created_at,
updated_at,
"name",
description,
readme,
job_id
)
VALUES
Expand All @@ -80,3 +80,12 @@ SET
updated_at = $3
WHERE
id = $1;

-- name: UpdateTemplateVersionDescriptionByJobID :exec
UPDATE
template_versions
SET
readme = $2,
updated_at = now()
WHERE
job_id = $1;
10 changes: 10 additions & 0 deletions coderd/provisionerdaemons.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,16 @@ func (server *provisionerdServer) UpdateJob(ctx context.Context, request *proto.
}
}

if len(request.Readme) > 0 {
err := server.Database.UpdateTemplateVersionDescriptionByJobID(ctx, database.UpdateTemplateVersionDescriptionByJobIDParams{
JobID: job.ID,
Readme: string(request.Readme),
})
if err != nil {
return nil, xerrors.Errorf("update template version description: %w", err)
}
}

if len(request.ParameterSchemas) > 0 {
for _, protoParameter := range request.ParameterSchemas {
validationTypeSystem, err := convertValidationTypeSystem(protoParameter.ValidationTypeSystem)
Expand Down
Loading