8000 feat: store and display template owner by AbhineetJain · Pull Request #2228 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: store and display template owner #2228

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 7 commits into from
Jun 10, 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
rename owner to created_by
  • Loading branch information
AbhineetJain committed Jun 10, 2022
commit 6760cf968761ba7dce47e13cb54846c061f11676
4 changes: 2 additions & 2 deletions coderd/audit/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestDiff(t *testing.T) {
ActiveVersionID: uuid.UUID{3},
MaxTtl: int64(time.Hour),
MinAutostartInterval: int64(time.Minute),
OwnerID: uuid.NullUUID{UUID: uuid.UUID{4}, Valid: true},
CreatedBy: uuid.NullUUID{UUID: uuid.UUID{4}, Valid: true},
},
exp: audit.Map{
"id": uuid.UUID{1}.String(),
Expand All @@ -98,7 +98,7 @@ func TestDiff(t *testing.T) {
"active_version_id": uuid.UUID{3}.String(),
"max_ttl": int64(3600000000000),
"min_autostart_interval": int64(60000000000),
"owner_id": uuid.UUID{4}.String(),
"created_by": uuid.UUID{4}.String(),
},
},
})
Expand Down
2 changes: 1 addition & 1 deletion coderd/audit/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var AuditableResources = auditMap(map[any]map[string]Action{
"description": ActionTrack,
"max_ttl": ActionTrack,
"min_autostart_interval": ActionTrack,
"owner_id": ActionTrack,
"created_by": ActionTrack,
},
&database.TemplateVersion{}: {
"id": ActionTrack,
Expand Down
2 changes: 1 addition & 1 deletion coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,7 @@ func (q *fakeQuerier) InsertTemplate(_ context.Context, arg database.InsertTempl
Description: arg.Description,
MaxTtl: arg.MaxTtl,
MinAutostartInterval: arg.MinAutostartInterval,
OwnerID: arg.OwnerID,
CreatedBy: arg.CreatedBy,
}
q.templates = append(q.templates, template)
return template, nil
Expand Down
6 changes: 3 additions & 3 deletions coderd/database/dump.sql

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

2 changes: 1 addition & 1 deletion coderd/database/migrations/000022_template_owner.down.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ALTER TABLE ONLY templates DROP COLUMN IF EXISTS owner_id;
ALTER TABLE ONLY templates DROP COLUMN IF EXISTS created_by;
2 changes: 1 addition & 1 deletion coderd/database/migrations/000022_template_owner.up.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ALTER TABLE ONLY templates ADD COLUMN IF NOT EXISTS owner_id uuid REFERENCES users (id) ON DELETE RESTRICT;
ALTER TABLE ONLY templates ADD COLUMN IF NOT EXISTS created_by uuid REFERENCES users (id) ON DELETE RESTRICT;
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.

28 changes: 14 additions & 14 deletions coderd/database/queries.sql.go

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

2 changes: 1 addition & 1 deletion coderd/database/queries/templates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ INSERT INTO
description,
max_ttl,
min_autostart_interval,
owner_id
created_by
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING *;
Expand Down
14 changes: 7 additions & 7 deletions coderd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
Description: createTemplate.Description,
MaxTtl: int64(maxTTL),
MinAutostartInterval: int64(minAutostartInterval),
OwnerID: uuid.NullUUID{
CreatedBy: uuid.NullUUID{
UUID: apiKey.UserID,
Valid: true,
},
Expand Down Expand Up @@ -429,11 +429,11 @@ func convertTemplates(ctx context.Context, db database.Store, templates []databa
}

func convertTemplate(ctx context.Context, db database.Store, template database.Template, workspaceOwnerCount uint32) codersdk.Template {
var ownerName string
if template.OwnerID.Valid {
owner, err := db.GetUserByID(ctx, template.OwnerID.UUID)
var createdByName string
if template.CreatedBy.Valid {
creator, err := db.GetUserByID(ctx, template.CreatedBy.UUID)
if err == nil {
ownerName = owner.Username
createdByName = creator.Username
}
}
return codersdk.Template{
Expand All @@ -448,7 +448,7 @@ func convertTemplate(ctx context.Context, db database.Store, template database.T
Description: template.Description,
MaxTTLMillis: time.Duration(template.MaxTtl).Milliseconds(),
MinAutostartIntervalMillis: time.Duration(template.MinAutostartInterval).Milliseconds(),
OwnerID: template.OwnerID,
OwnerName: ownerName,
CreatedByID: template.CreatedBy,
CreatedByName: createdByName,
}
}
4 changes: 2 additions & 2 deletions codersdk/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type Template struct {
Description string `json:"description"`
MaxTTLMillis int64 `json:"max_ttl_ms"`
MinAutostartIntervalMillis int64 `json:"min_autostart_interval_ms"`
OwnerID uuid.NullUUID `json:"owner_id"`
OwnerName string `json:"owner_name"`
CreatedByID uuid.NullUUID `json:"created_by_id"`
CreatedByName string `json:"created_by_name"`
}

type UpdateActiveTemplateVersion struct {
Expand Down
4 changes: 2 additions & 2 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ export interface Template {
readonly description: string
readonly max_ttl_ms: number
readonly min_autostart_interval_ms: number
readonly owner_id?: string
readonly owner_name: string
readonly created_by_id?: string
readonly created_by_name: string
}

// From codersdk/templateversions.go:14:6
Expand Down
6 changes: 3 additions & 3 deletions site/src/components/TemplateStats/TemplateStats.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ UsedByMany.args = {
activeVersion: Mocks.MockTemplateVersion,
}

export const UnknownOwner = Template.bind({})
UnknownOwner.args = {
export const UnknownCreator = Template.bind({})
UnknownCreator.args = {
template: {
...Mocks.MockTemplate,
owner_name: "",
created_by_name: "",
},
activeVersion: Mocks.MockTemplateVersion,
}
2 changes: 1 addition & 1 deletion site/src/components/TemplateStats/TemplateStats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const TemplateStats: FC<TemplateStatsProps> = ({ template, activeVersion
<div className={styles.statsDivider} />
<div className={styles.statItem}>
<span className={styles.statsLabel}>{Language.createdByLabel}</span>
<span className={styles.statsValue}>{template.owner_name || Language.defaultTemplateCreator}</span>
<span className={styles.statsValue}>{template.created_by_name || Language.defaultTemplateCreator}</span>
</div>
</div>
)
Expand Down
2 changes: 1 addition & 1 deletion site/src/pages/TemplatesPage/TemplatesPageView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export const TemplatesPageView: FC<TemplatesPageViewProps> = (props) => {
<TableCell>{Language.developerCount(template.workspace_owner_count)}</TableCell>

<TableCell data-chromatic="ignore">{dayjs().to(dayjs(template.updated_at))}</TableCell>
<TableCell>{template.owner_name || Language.defaultTemplateOwner}</TableCell>
<TableCell>{template.created_by_name || Language.defaultTemplateOwner}</TableCell>
<TableCell>
<div className={styles.arrowCell}>
<KeyboardArrowRight className={styles.arrowRight} />
Expand Down
4 changes: 2 additions & 2 deletions site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ export const MockTemplate: TypesGen.Template = {
description: "This is a test description.",
max_ttl_ms: 604800000,
min_autostart_interval_ms: 3600000,
owner_id: "test-owner-id",
owner_name: "test_owner",
created_by_id: "test-owner-id",
created_by_name: "test_owner",
}

export const MockWorkspaceAutostartDisabled: TypesGen.UpdateWorkspaceAutostartRequest = {
Expand Down
0