Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chore: include organization name when fetching templates#13751

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Emyrk merged 6 commits intomainfromstevenmasley/templates_include_org_name
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes fromall 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
2 changes: 1 addition & 1 deletioncli/templatelist.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,7 +13,7 @@ import (
func (r *RootCmd) templateList() *serpent.Command {
orgContext := NewOrganizationContext()
formatter := cliui.NewOutputFormatter(
cliui.TableFormat([]templateTableRow{}, []string{"name", "last updated", "used by"}),
cliui.TableFormat([]templateTableRow{}, []string{"name", "organization name", "last updated", "used by"}),
cliui.JSONFormat(),
)

Expand Down
36 changes: 19 additions & 17 deletionscli/templates.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -83,14 +83,15 @@ type templateTableRow struct {
Template codersdk.Template

// Used by table format:
Name string `json:"-" table:"name,default_sort"`
CreatedAt string `json:"-" table:"created at"`
LastUpdated string `json:"-" table:"last updated"`
OrganizationID uuid.UUID `json:"-" table:"organization id"`
Provisioner codersdk.ProvisionerType `json:"-" table:"provisioner"`
ActiveVersionID uuid.UUID `json:"-" table:"active version id"`
UsedBy string `json:"-" table:"used by"`
DefaultTTL time.Duration `json:"-" table:"default ttl"`
Name string `json:"-" table:"name,default_sort"`
CreatedAt string `json:"-" table:"created at"`
LastUpdated string `json:"-" table:"last updated"`
OrganizationID uuid.UUID `json:"-" table:"organization id"`
OrganizationName string `json:"-" table:"organization name"`
Provisioner codersdk.ProvisionerType `json:"-" table:"provisioner"`
ActiveVersionID uuid.UUID `json:"-" table:"active version id"`
UsedBy string `json:"-" table:"used by"`
DefaultTTL time.Duration `json:"-" table:"default ttl"`
}

// templateToRows converts a list of templates to a list of templateTableRow for
Expand All@@ -99,15 +100,16 @@ func templatesToRows(templates ...codersdk.Template) []templateTableRow {
rows := make([]templateTableRow, len(templates))
for i, template := range templates {
rows[i] = templateTableRow{
Template: template,
Name: template.Name,
CreatedAt: template.CreatedAt.Format("January 2, 2006"),
LastUpdated: template.UpdatedAt.Format("January 2, 2006"),
OrganizationID: template.OrganizationID,
Provisioner: template.Provisioner,
ActiveVersionID: template.ActiveVersionID,
UsedBy: pretty.Sprint(cliui.DefaultStyles.Fuchsia, formatActiveDevelopers(template.ActiveUserCount)),
DefaultTTL: (time.Duration(template.DefaultTTLMillis) * time.Millisecond),
Template: template,
Name: template.Name,
CreatedAt: template.CreatedAt.Format("January 2, 2006"),
LastUpdated: template.UpdatedAt.Format("January 2, 2006"),
OrganizationID: template.OrganizationID,
OrganizationName: template.OrganizationName,
Provisioner: template.Provisioner,
ActiveVersionID: template.ActiveVersionID,
UsedBy: pretty.Sprint(cliui.DefaultStyles.Fuchsia, formatActiveDevelopers(template.ActiveUserCount)),
DefaultTTL: (time.Duration(template.DefaultTTLMillis) * time.Millisecond),
}
}

Expand Down
6 changes: 3 additions & 3 deletionscli/testdata/coder_templates_list_--help.golden
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,10 +11,10 @@ OPTIONS:
-O, --org string, $CODER_ORGANIZATION
Select which organization (uuid or name) to use.

-c, --column string-array (default: name,last updated,used by)
-c, --column string-array (default: name,organization name,last updated,used by)
Columns to display in table output. Available columns: name, created
at, last updated, organization id,provisioner, active version id,
used by, default ttl.
at, last updated, organization id,organization name, provisioner,
active version id,used by, default ttl.

-o, --output string (default: table)
Output format. Available formats: table, json.
Expand Down
4 changes: 4 additions & 0 deletionscoderd/apidoc/docs.go
View file
Open in desktop

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

4 changes: 4 additions & 0 deletionscoderd/apidoc/swagger.json
View file
Open in desktop

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

30 changes: 20 additions & 10 deletionscoderd/database/dbmem/dbmem.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -515,7 +515,7 @@ func (q *FakeQuerier) getLatestWorkspaceBuildByWorkspaceIDNoLock(_ context.Conte
func (q *FakeQuerier) getTemplateByIDNoLock(_ context.Context, id uuid.UUID) (database.Template, error) {
for _, template := range q.templates {
if template.ID == id {
return q.templateWithUserNoLock(template), nil
return q.templateWithNameNoLock(template), nil
}
}
return database.Template{}, sql.ErrNoRows
Expand All@@ -524,26 +524,36 @@ func (q *FakeQuerier) getTemplateByIDNoLock(_ context.Context, id uuid.UUID) (da
func (q *FakeQuerier) templatesWithUserNoLock(tpl []database.TemplateTable) []database.Template {
cpy := make([]database.Template, 0, len(tpl))
for _, t := range tpl {
cpy = append(cpy, q.templateWithUserNoLock(t))
cpy = append(cpy, q.templateWithNameNoLock(t))
}
return cpy
}

func (q *FakeQuerier)templateWithUserNoLock(tpl database.TemplateTable) database.Template {
func (q *FakeQuerier)templateWithNameNoLock(tpl database.TemplateTable) database.Template {
var user database.User
for _, _user := range q.users {
if _user.ID == tpl.CreatedBy {
user = _user
break
}
}
var withUser database.Template

var org database.Organization
for _, _org := range q.organizations {
if _org.ID == tpl.OrganizationID {
org = _org
break
}
}

var withNames database.Template
// This is a cheeky way to copy the fields over without explicitly listing them all.
d, _ := json.Marshal(tpl)
_ = json.Unmarshal(d, &withUser)
withUser.CreatedByUsername = user.Username
withUser.CreatedByAvatarURL = user.AvatarURL
return withUser
_ = json.Unmarshal(d, &withNames)
withNames.CreatedByUsername = user.Username
withNames.CreatedByAvatarURL = user.AvatarURL
withNames.OrganizationName = org.Name
return withNames
}

func (q *FakeQuerier) templateVersionWithUserNoLock(tpl database.TemplateVersionTable) database.TemplateVersion {
Expand DownExpand Up@@ -3675,7 +3685,7 @@ func (q *FakeQuerier) GetTemplateByOrganizationAndName(_ context.Context, arg da
if template.Deleted != arg.Deleted {
continue
}
return q.templateWithUserNoLock(template), nil
return q.templateWithNameNoLock(template), nil
}
return database.Template{}, sql.ErrNoRows
}
Expand DownExpand Up@@ -9323,7 +9333,7 @@ func (q *FakeQuerier) GetAuthorizedTemplates(ctx context.Context, arg database.G

var templates []database.Template
for _, templateTable := range q.templates {
template := q.templateWithUserNoLock(templateTable)
template := q.templateWithNameNoLock(templateTable)
if prepared != nil && prepared.Authorize(ctx, template.RBACObject()) != nil {
continue
}
Expand Down
12 changes: 7 additions & 5 deletionscoderd/database/dump.sql
View file
Open in desktop

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

2 changes: 1 addition & 1 deletioncoderd/database/gentest/models_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -32,7 +32,7 @@ func TestViewSubsetTemplate(t *testing.T) {
tableFields := allFields(table)
joinedFields := allFields(joined)
if !assert.Subset(t, fieldNames(joinedFields), fieldNames(tableFields), "table is not subset") {
t.Log("Some fields were added to the Template Table without updating the 'template_with_users' view.")
t.Log("Some fields were added to the Template Table without updating the 'template_with_names' view.")
t.Log("See migration 000138_join_users.up.sql to create the view.")
}
}
Expand Down
View file
Open in desktop
Copy link
Member

Choose a reason for hiding this comment

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

reminder: double-check migration number before merge!

Emyrk reacted with thumbs up emoji
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
DROP VIEW template_with_names;

CREATE VIEW
template_with_users
AS
SELECT
templates.*,
coalesce(visible_users.avatar_url, '') AS created_by_avatar_url,
coalesce(visible_users.username, '') AS created_by_username
FROM
templates
LEFT JOIN
visible_users
ON
templates.created_by = visible_users.id;
COMMENT ON VIEW template_with_users IS 'Joins in the username + avatar url of the created by user.';
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
-- Update the template_with_users view by recreating it.
DROP VIEW template_with_users;

-- Renaming template_with_users -> template_with_names
CREATE VIEW
template_with_names
AS
SELECT
templates.*,
coalesce(visible_users.avatar_url, '') AS created_by_avatar_url,
coalesce(visible_users.username, '') AS created_by_username,
coalesce(organizations.name, '') AS organization_name
FROM
templates
LEFT JOIN
visible_users
ON
templates.created_by = visible_users.id
LEFT JOIN
organizations
ON templates.organization_id = organizations.id
;

COMMENT ON VIEW template_with_names IS 'Joins in the display name information such as username, avatar, and organization name.';
1 change: 1 addition & 0 deletionscoderd/database/modelqueries.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -116,6 +116,7 @@ func (q *sqlQuerier) GetAuthorizedTemplates(ctx context.Context, arg GetTemplate
&i.MaxPortSharingLevel,
&i.CreatedByAvatarURL,
&i.CreatedByUsername,
&i.OrganizationName,
); err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletioncoderd/database/models.go
View file
Open in desktop

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

18 changes: 11 additions & 7 deletionscoderd/database/queries.sql.go
View file
Open in desktop

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

8 changes: 4 additions & 4 deletionscoderd/database/queries/templates.sql
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,7 +2,7 @@
SELECT
*
FROM
template_with_users
template_with_names
WHERE
id = $1
LIMIT
Expand All@@ -12,7 +12,7 @@ LIMIT
SELECT
*
FROM
template_with_users AS templates
template_with_names AS templates
WHERE
-- Optionally include deleted templates
templates.deleted = @deleted
Expand DownExpand Up@@ -54,7 +54,7 @@ ORDER BY (name, id) ASC
SELECT
*
FROM
template_with_users AS templates
template_with_names AS templates
WHERE
organization_id = @organization_id
AND deleted = @deleted
Expand All@@ -63,7 +63,7 @@ LIMIT
1;

-- name: GetTemplates :many
SELECT * FROMtemplate_with_users AS templates
SELECT * FROMtemplate_with_names AS templates
ORDER BY (name, id) ASC
;

Expand Down
Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp