Skip to content
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

feat: support chart release-notes or changelog #137

Merged
merged 3 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
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.