Skip to content

Commit

Permalink
feat: support chart release-notes or changelog (#137)
Browse files Browse the repository at this point in the history
* feat: support chart release-notes or changelog

Signed-off-by: Ahmed AbouZaid <ahmed@aabouzaid.com>

* add docs and unit tests for chart release-notes option

Signed-off-by: Ahmed AbouZaid <ahmed@aabouzaid.com>
  • Loading branch information
aabouzaid authored Oct 28, 2021
1 parent c59a66c commit 4eb598f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Flags:
-o, --owner string GitHub username or organization
-p, --package-path string Path to directory with chart packages (default ".cr-release-packages")
--release-name-template string Go template for computing release names, using chart metadata (default "{{ .Name }}-{{ .Version }}")
--release-notes-file string Markdown file with chart release notes. If it is set to empty string, or the file is not found, the chart description will be used instead
--skip-existing Skip upload if release exists
-t, --token string GitHub Auth Token

Expand Down
2 changes: 2 additions & 0 deletions cr/cmd/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ func init() {
uploadCmd.Flags().StringP("commit", "c", "", "Target commit for release")
uploadCmd.Flags().Bool("skip-existing", false, "Skip upload if release exists")
uploadCmd.Flags().String("release-name-template", "{{ .Name }}-{{ .Version }}", "Go template for computing release names, using chart metadata")
uploadCmd.Flags().String("release-notes-file", "", "Markdown file with chart release notes. "+
"If it is set to empty string, or the file is not found, the chart description will be used instead")
}
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Options struct {
Remote string `mapstructure:"remote"`
ReleaseNameTemplate string `mapstructure:"release-name-template"`
SkipExisting bool `mapstructure:"skip-existing"`
ReleaseNotesFile string `mapstructure:"release-notes-file"`
}

func LoadConfiguration(cfgFile string, cmd *cobra.Command, requiredFlags []string) (*Options, error) {
Expand Down
14 changes: 13 additions & 1 deletion pkg/releaser/releaser.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,17 @@ func (r *Releaser) computeReleaseName(chart *chart.Chart) (string, error) {
return releaseName, nil
}

func (r *Releaser) getReleaseNotes(chart *chart.Chart) string {
if r.config.ReleaseNotesFile != "" {
for _, f := range chart.Files {
if f.Name == r.config.ReleaseNotesFile {
return string(f.Data)
}
}
}
return chart.Metadata.Description
}

func (r *Releaser) splitPackageNameAndVersion(pkg string) []string {
delimIndex := strings.LastIndex(pkg, "-")
return []string{pkg[0:delimIndex], pkg[delimIndex+1:]}
Expand Down Expand Up @@ -317,9 +328,10 @@ func (r *Releaser) CreateReleases() error {
if err != nil {
return err
}

release := &github.Release{
Name: releaseName,
Description: ch.Metadata.Description,
Description: r.getReleaseNotes(ch),
Assets: []*github.Asset{
{Path: p},
},
Expand Down
52 changes: 52 additions & 0 deletions pkg/releaser/releaser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,55 @@ func TestReleaser_CreateReleases(t *testing.T) {
})
}
}

func TestReleaser_ReleaseNotes(t *testing.T) {
tests := []struct {
name string
packagePath string
chart string
version string
releaseNotesFile string
expectedReleaseNotes string
}{
{
"chart-package-with-release-notes-file",
"testdata/release-packages",
"test-chart",
"0.1.0",
"release-notes.md",
"The release notes file content is used as release notes",
},
{
"chart-package-with-non-exists-release-notes-file",
"testdata/release-packages",
"test-chart",
"0.1.0",
"non-exists-release-notes.md",
"A Helm chart for Kubernetes",
},
{
"chart-package-with-empty-release-notes-file-config-value",
"testdata/release-packages",
"test-chart",
"0.1.0",
"",
"A Helm chart for Kubernetes",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fakeGitHub := new(FakeGitHub)
r := &Releaser{
config: &config.Options{
PackagePath: "testdata/release-packages",
ReleaseNotesFile: tt.releaseNotesFile,
},
github: fakeGitHub,
}
fakeGitHub.On("CreateRelease", mock.Anything, mock.Anything).Return(nil)
err := r.CreateReleases()
assert.NoError(t, err)
assert.Equal(t, tt.expectedReleaseNotes, fakeGitHub.release.Description)
})
}
}
Binary file modified pkg/releaser/testdata/release-packages/test-chart-0.1.0.tgz
Binary file not shown.

0 comments on commit 4eb598f

Please sign in to comment.