Skip to content

Commit 44ede0b

Browse files
authored
Add fragment summary autocompletion from title (#94)
1 parent bf2ba51 commit 44ede0b

File tree

5 files changed

+63
-11
lines changed

5 files changed

+63
-11
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Kind can be one of:
2+
# - breaking-change: a change to previously-documented behavior
3+
# - deprecation: functionality that is being removed in a later release
4+
# - bug-fix: fixes a problem in a previous version
5+
# - enhancement: extends functionality but does not break or fix existing behavior
6+
# - feature: new functionality
7+
# - known-issue: problems that we are aware of in a given version
8+
# - security: impacts on the security of a product or a user’s deployment.
9+
# - upgrade: important information for someone upgrading from a prior version
10+
# - other: does not fit into any of the other categories
11+
kind: feature
12+
13+
# Change summary; a 80ish characters long description of the change.
14+
summary: Add fragment summary autocompletion from title
15+
16+
# Long description; in case the summary is not enough to describe the change
17+
# this field accommodate a description without length limits.
18+
#description:
19+
20+
# Affected component; a word indicating the component this changeset affects.
21+
component:
22+
23+
# PR number; optional; the PR number that added the changeset.
24+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
25+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
26+
# Please provide it if you are adding a fragment for a different PR.
27+
#pr: 1234
28+
29+
# Issue number; optional; the GitHub issue related to this changeset (either closes or is part of).
30+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
31+
#issue: 1234
32+
33+
# Repository URL; optional; the repository URL related to this changeset and pr and issue numbers.
34+
# If not present is automatically filled by the tooling based on the repository this file has been committed in.
35+
#repository: https://github.com/elastic/elastic-agent-changelog-tool

internal/changelog/fragment/creator.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,17 @@ var fragmentPerm = os.FileMode(0660)
5858

5959
// Create marshal changelog fragment and persist it to file.
6060
func (c FragmentCreator) Create(slug string) error {
61-
data, err := Template()
62-
if err != nil {
63-
return err
64-
}
65-
6661
if err := c.fs.MkdirAll(c.location, fragmentLocPerm); err != nil {
6762
return fmt.Errorf("cannot create fragment location folder: %v", err)
6863
}
6964

65+
template, err := Template(slug)
66+
if err != nil {
67+
return err
68+
}
69+
7070
filePath := path.Join(c.location, c.filename(slug))
71-
if err := afero.WriteFile(c.fs, filePath, data, fragmentPerm); err != nil {
71+
if err := afero.WriteFile(c.fs, filePath, template, fragmentPerm); err != nil {
7272
return err
7373
}
7474

internal/changelog/fragment/creator_internal_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func TestCreate(t *testing.T) {
8585
kind: feature
8686
8787
# Change summary; a 80ish characters long description of the change.
88-
summary:
88+
summary: foobar
8989
9090
# Long description; in case the summary is not enough to describe the change
9191
# this field accommodate a description without length limits.

internal/changelog/fragment/template.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,35 @@
55
package fragment
66

77
import (
8+
"bytes"
89
"embed"
910
"fmt"
11+
txttempl "text/template"
1012
)
1113

1214
//go:embed template.yaml
1315
var template embed.FS
1416

15-
func Template() ([]byte, error) {
17+
func Template(slug string) ([]byte, error) {
1618
data, err := template.ReadFile("template.yaml")
1719
if err != nil {
18-
return []byte{}, fmt.Errorf("cannot read embedded template: %w", err)
20+
return nil, fmt.Errorf("cannot read embedded template: %w", err)
1921
}
2022

21-
return data, nil
23+
tmpl, err := txttempl.New("template").Parse(string(data))
24+
if err != nil {
25+
return nil, fmt.Errorf("cannot parse template: %w", err)
26+
}
27+
28+
vars := make(map[string]interface{})
29+
vars["Summary"] = slug
30+
31+
buf := bytes.NewBuffer(nil)
32+
33+
err = tmpl.Execute(buf, vars)
34+
if err != nil {
35+
return nil, fmt.Errorf("cannot execute template: %w", err)
36+
}
37+
38+
return buf.Bytes(), nil
2239
}

internal/changelog/fragment/template.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
kind: feature
1212

1313
# Change summary; a 80ish characters long description of the change.
14-
summary:
14+
summary: {{.Summary}}
1515

1616
# Long description; in case the summary is not enough to describe the change
1717
# this field accommodate a description without length limits.

0 commit comments

Comments
 (0)