Skip to content

feat: add examples to api #5331

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 9 commits into from
Dec 9, 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
add icon and tag parsing
  • Loading branch information
f0ssel committed Dec 9, 2022
commit 85266d017679b5fcdfd336e8d836dc75cdc0ebf3
12 changes: 7 additions & 5 deletions codersdk/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ type UpdateTemplateMeta struct {
}

type TemplateExample struct {
ID string `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Description string `json:"description"`
Markdown string `json:"markdown"`
ID string `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Description string `json:"description"`
Icon string `json:"icon"`
Tags []string `json:"tags"`
Markdown string `json:"markdown"`
}

// Template returns a single template.
Expand Down
30 changes: 30 additions & 0 deletions examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,41 @@ func List() ([]codersdk.TemplateExample, error) {
return
}

tags := []string{}
tagsRaw, exists := frontMatter.FrontMatter["tags"]
if exists {
tagsI, valid := tagsRaw.([]interface{})
if !valid {
returnError = xerrors.Errorf("example %q tags isn't a slice: type %T", exampleID, tagsRaw)
return
}
for _, tagI := range tagsI {
tag, valid := tagI.(string)
if !valid {
returnError = xerrors.Errorf("example %q tag isn't a string: type %T", exampleID, tagI)
return
}
tags = append(tags, tag)
}
}

var icon string
iconRaw, exists := frontMatter.FrontMatter["icon"]
if exists {
icon, valid = iconRaw.(string)
if !valid {
returnError = xerrors.Errorf("example %q icon isn't a string", exampleID)
return
}
}

examples = append(examples, codersdk.TemplateExample{
ID: exampleID,
URL: exampleURL,
Name: name,
Description: description,
Icon: icon,
Tags: tags,
Markdown: string(frontMatter.Content),
})
}
Expand Down
1 change: 1 addition & 0 deletions examples/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestTemplate(t *testing.T) {
assert.NotEmpty(t, eg.Name, "example name should not be empty")
assert.NotEmpty(t, eg.Description, "example description should not be empty")
assert.NotEmpty(t, eg.Markdown, "example markdown should not be empty")
assert.NotNil(t, eg.Tags, "example tags should not be nil, should be empty array if no tags")
_, err := examples.Archive(eg.ID)
assert.NoError(t, err, "error archiving example")
})
Expand Down
2 changes: 2 additions & 0 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,8 @@ export interface TemplateExample {
readonly url: string
readonly name: string
readonly description: string
readonly icon: string
readonly tags: string[]
readonly markdown: string
}

Expand Down