From 6f8dc529142f829924168dfbef8e2b405b839425 Mon Sep 17 00:00:00 2001 From: Ronnie Date: Sat, 21 Oct 2023 20:59:08 -0700 Subject: [PATCH] merge all projects if projects are being used (#557) --- .../unreleased/Changed-20231021-140739.yaml | 5 ++ cmd/merge.go | 60 +++++++++---------- cmd/merge_test.go | 2 - 3 files changed, 32 insertions(+), 35 deletions(-) create mode 100644 .changes/unreleased/Changed-20231021-140739.yaml diff --git a/.changes/unreleased/Changed-20231021-140739.yaml b/.changes/unreleased/Changed-20231021-140739.yaml new file mode 100644 index 00000000..85fd5812 --- /dev/null +++ b/.changes/unreleased/Changed-20231021-140739.yaml @@ -0,0 +1,5 @@ +kind: Changed +body: Merge will merge all projects versions if projects are enabled. +time: 2023-10-21T14:07:39.098558299-07:00 +custom: + Issue: "552" diff --git a/cmd/merge.go b/cmd/merge.go index 02796150..95907d09 100644 --- a/cmd/merge.go +++ b/cmd/merge.go @@ -16,7 +16,6 @@ type Merge struct { // cli args DryRun bool UnreleasedHeader string - Project string // dependencies ReadFile shared.ReadFiler @@ -66,39 +65,34 @@ Note that a newline is added between each version file.`, "", "Include unreleased changes with this value as the header", ) - cmd.Flags().StringVarP( - &m.Project, - "project", "j", - "", - "(Preview) Specify which project we are merging", - ) m.Command = cmd return m } -//nolint:gocyclo func (m *Merge) Run(cmd *cobra.Command, args []string) error { - config, err := core.LoadConfig(m.ReadFile) + cfg, err := core.LoadConfig(m.ReadFile) if err != nil { return err } - changelogPath := config.ChangelogPath - - if len(config.Projects) > 0 { - var pc *core.ProjectConfig - - pc, err = config.Project(m.Project) - if err != nil { - return err + // If we have projects, merge all of them. + if len(cfg.Projects) > 0 { + for _, pc := range cfg.Projects { + err = m.mergeProject(cfg, pc.Key, pc.ChangelogPath) + if err != nil { + return err + } } - m.Project = pc.Key - changelogPath = pc.ChangelogPath + return nil } + return m.mergeProject(cfg, "", cfg.ChangelogPath) +} + +func (m *Merge) mergeProject(cfg *core.Config, project, changelogPath string) error { var writer io.Writer if m.DryRun { writer = m.Command.OutOrStdout() @@ -111,25 +105,25 @@ func (m *Merge) Run(cmd *cobra.Command, args []string) error { writer = changeFile } - allVersions, err := core.GetAllVersions(m.ReadDir, config, false, m.Project) + allVersions, err := core.GetAllVersions(m.ReadDir, cfg, false, project) if err != nil { return err } - if config.HeaderPath != "" { - err = core.AppendFile(m.OpenFile, writer, filepath.Join(config.ChangesDir, config.HeaderPath)) + if cfg.HeaderPath != "" { + err = core.AppendFile(m.OpenFile, writer, filepath.Join(cfg.ChangesDir, cfg.HeaderPath)) if err != nil { return err } - _ = core.WriteNewlines(writer, config.Newlines.AfterChangelogHeader) + _ = core.WriteNewlines(writer, cfg.Newlines.AfterChangelogHeader) } if m.UnreleasedHeader != "" { var unrelErr error allChanges, unrelErr := core.GetChanges( - config, + cfg, nil, m.ReadDir, m.ReadFile, @@ -141,14 +135,14 @@ func (m *Merge) Run(cmd *cobra.Command, args []string) error { // Make sure we have any changes before writing the unreleased content. if len(allChanges) > 0 { - _ = core.WriteNewlines(writer, config.Newlines.BeforeChangelogVersion) - _ = core.WriteNewlines(writer, config.Newlines.BeforeVersion) + _ = core.WriteNewlines(writer, cfg.Newlines.BeforeChangelogVersion) + _ = core.WriteNewlines(writer, cfg.Newlines.BeforeVersion) _, _ = writer.Write([]byte(m.UnreleasedHeader)) - _ = core.WriteNewlines(writer, config.Newlines.AfterVersion) + _ = core.WriteNewlines(writer, cfg.Newlines.AfterVersion) // create a fake batch to write the changes b := &Batch{ - config: config, + config: cfg, writer: writer, TemplateCache: m.TemplateCache, } @@ -159,20 +153,20 @@ func (m *Merge) Run(cmd *cobra.Command, args []string) error { } _ = core.WriteNewlines(b.writer, b.config.Newlines.EndOfVersion) - _ = core.WriteNewlines(writer, config.Newlines.AfterChangelogVersion) + _ = core.WriteNewlines(writer, cfg.Newlines.AfterChangelogVersion) } } for _, version := range allVersions { - _ = core.WriteNewlines(writer, config.Newlines.BeforeChangelogVersion) - versionPath := filepath.Join(config.ChangesDir, m.Project, version.Original()+"."+config.VersionExt) + _ = core.WriteNewlines(writer, cfg.Newlines.BeforeChangelogVersion) + versionPath := filepath.Join(cfg.ChangesDir, project, version.Original()+"."+cfg.VersionExt) err = core.AppendFile(m.OpenFile, writer, versionPath) if err != nil { return err } - _ = core.WriteNewlines(writer, config.Newlines.AfterChangelogVersion) + _ = core.WriteNewlines(writer, cfg.Newlines.AfterChangelogVersion) } if len(allVersions) == 0 { @@ -194,7 +188,7 @@ func (m *Merge) Run(cmd *cobra.Command, args []string) error { Metadata: version.Metadata(), } - for _, rep := range config.Replacements { + for _, rep := range cfg.Replacements { err = rep.Execute(m.ReadFile, m.WriteFile, replaceData) if err != nil { return err diff --git a/cmd/merge_test.go b/cmd/merge_test.go index 421cec0a..daddc90c 100644 --- a/cmd/merge_test.go +++ b/cmd/merge_test.go @@ -94,7 +94,6 @@ func TestMergeVersionsSuccessfullyWithProject(t *testing.T) { os.Create, core.NewTemplateCache(), ) - cmd.Project = "a" err := cmd.Run(cmd.Command, nil) then.Nil(t, err) @@ -126,7 +125,6 @@ func TestMergeVersionsErrorInvalidProject(t *testing.T) { os.Create, core.NewTemplateCache(), ) - cmd.Project = "not_found" err := cmd.Run(cmd.Command, nil) then.NotNil(t, err)