From af03a26412f0d87612a99c7714c6408be35c645f Mon Sep 17 00:00:00 2001 From: Pablo Baeyens Date: Tue, 9 Jul 2024 17:55:22 +0200 Subject: [PATCH] [multimod] Resolve version for commit per module instead of per module set (#582) * [multimod] Resolve version for commit per module instead of per module set * Add issue number to changelog * Update comment --- .chloggen/mx-psi_multimod-modref.yaml | 16 ++++++++++++++++ multimod/internal/common/tools.go | 15 +++++++-------- multimod/internal/common/tools_test.go | 10 +++++----- multimod/internal/common/versions.go | 6 ++++++ multimod/internal/prerelease/prerelease.go | 7 ++++++- multimod/internal/sync/sync.go | 21 ++++++++++++--------- 6 files changed, 52 insertions(+), 23 deletions(-) create mode 100755 .chloggen/mx-psi_multimod-modref.yaml diff --git a/.chloggen/mx-psi_multimod-modref.yaml b/.chloggen/mx-psi_multimod-modref.yaml new file mode 100755 index 00000000..876c3171 --- /dev/null +++ b/.chloggen/mx-psi_multimod-modref.yaml @@ -0,0 +1,16 @@ +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. crosslink) +component: multimod + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Get pseudoversion for each module in a module set separately to support moving modules between module sets. + +# One or more tracking issues related to the change +issues: [582] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: diff --git a/multimod/internal/common/tools.go b/multimod/internal/common/tools.go index 14c78efc..a27e6253 100644 --- a/multimod/internal/common/tools.go +++ b/multimod/internal/common/tools.go @@ -60,8 +60,8 @@ func GetModuleSet(modSetName, versioningFilename string) (ModuleSet, error) { } // updateGoModVersions updates one go.mod file, given by modFilePath, by updating all modules listed in -// newModPaths to use the newVersion given. -func updateGoModVersions(modFilePath ModuleFilePath, newModPaths []ModulePath, newVersion string) error { +// newModRefs to use the new versions given. +func updateGoModVersions(modFilePath ModuleFilePath, newModRefs []ModuleRef) error { if !strings.HasSuffix(string(modFilePath), "go.mod") { return errors.New("cannot update file passed that does not end with go.mod") } @@ -71,8 +71,8 @@ func updateGoModVersions(modFilePath ModuleFilePath, newModPaths []ModulePath, n panic(err) } - for _, modPath := range newModPaths { - newGoModFile, err = replaceModVersion(modPath, newVersion, newGoModFile) + for _, modRef := range newModRefs { + newGoModFile, err = replaceModVersion(modRef.Path, modRef.Version, newGoModFile) if err != nil { return err } @@ -101,14 +101,13 @@ func replaceModVersion(modPath ModulePath, version string, newGoModFile []byte) } // UpdateGoModFiles updates the go.mod files in modFilePaths by updating all modules listed in -// newModPaths to use the newVersion given. -func UpdateGoModFiles(modFilePaths []ModuleFilePath, newModPaths []ModulePath, newVersion string) error { +// moduleRefs to use the versions given. +func UpdateGoModFiles(modFilePaths []ModuleFilePath, newModuleRefs []ModuleRef) error { log.Println("Updating all module versions in go.mod files...") for _, modFilePath := range modFilePaths { if err := updateGoModVersions( modFilePath, - newModPaths, - newVersion, + newModuleRefs, ); err != nil { return fmt.Errorf("could not update module versions in file %v: %w", modFilePath, err) } diff --git a/multimod/internal/common/tools_test.go b/multimod/internal/common/tools_test.go index caa02b64..48f892a8 100644 --- a/multimod/internal/common/tools_test.go +++ b/multimod/internal/common/tools_test.go @@ -124,13 +124,13 @@ func TestUpdateGoModVersions(t *testing.T) { ")"), } - newModPaths := []ModulePath{ - "go.opentelemetry.io/build-tools/multimod/internal/prerelease/test/test1", - "go.opentelemetry.io/build-tools/multimod/internal/prerelease/test/test2", - } newVersion := "v1.2.3-RC1+meta" + newModRefs := []ModuleRef{ + {Path: "go.opentelemetry.io/build-tools/multimod/internal/prerelease/test/test1", Version: newVersion}, + {Path: "go.opentelemetry.io/build-tools/multimod/internal/prerelease/test/test2", Version: newVersion}, + } - require.NoError(t, UpdateGoModFiles(modFilePaths, newModPaths, newVersion)) + require.NoError(t, UpdateGoModFiles(modFilePaths, newModRefs)) for modFilePath, expectedByteOutput := range expectedModFiles { actual, err := os.ReadFile(filepath.Clean(modFilePath)) require.NoError(t, err) diff --git a/multimod/internal/common/versions.go b/multimod/internal/common/versions.go index f7b0816a..04b2cd91 100644 --- a/multimod/internal/common/versions.go +++ b/multimod/internal/common/versions.go @@ -52,6 +52,12 @@ type ModuleSet struct { // ModulePath holds the module import path, such as "go.opentelemetry.io/otel". type ModulePath string +// ModuleRef holds a module import path and a version for that module. +type ModuleRef struct { + Path ModulePath + Version string +} + // ModuleInfoMap is a mapping from a module's import path to its ModuleInfo struct. type ModuleInfoMap map[ModulePath]ModuleInfo diff --git a/multimod/internal/prerelease/prerelease.go b/multimod/internal/prerelease/prerelease.go index c0bb3212..cbf75f64 100644 --- a/multimod/internal/prerelease/prerelease.go +++ b/multimod/internal/prerelease/prerelease.go @@ -194,7 +194,12 @@ func (p prerelease) updateAllGoModFiles() error { modFilePaths = append(modFilePaths, filePath) } - if err := common.UpdateGoModFiles(modFilePaths, p.ModuleSetRelease.ModSetPaths(), p.ModuleSetRelease.ModSetVersion()); err != nil { + var newModRefs []common.ModuleRef + ver := p.ModuleSetRelease.ModSetVersion() + for _, mod := range p.ModuleSetRelease.ModSetPaths() { + newModRefs = append(newModRefs, common.ModuleRef{Path: mod, Version: ver}) + } + if err := common.UpdateGoModFiles(modFilePaths, newModRefs); err != nil { return fmt.Errorf("could not update all go mod files: %w", err) } diff --git a/multimod/internal/sync/sync.go b/multimod/internal/sync/sync.go index 0cc16823..290b3640 100644 --- a/multimod/internal/sync/sync.go +++ b/multimod/internal/sync/sync.go @@ -146,20 +146,23 @@ func (s sync) updateAllGoModFiles() error { modFilePaths = append(modFilePaths, filePath) } - ver := s.OtherModuleSet.Version - if s.OtherModuleVersionCommit != "" { - version, err := s.parseVersionInfo(string(s.OtherModuleSet.Modules[0]), s.OtherModuleVersionCommit) - if err != nil { - return err + var newModRefs []common.ModuleRef + for _, mod := range s.OtherModuleSet.Modules { + ver := s.OtherModuleSet.Version + if s.OtherModuleVersionCommit != "" { + version, err := s.parseVersionInfo(string(mod), s.OtherModuleVersionCommit) + if err != nil { + return err + } + ver = version } - ver = version + log.Printf("Version for module %q: %s\n", mod, ver) + newModRefs = append(newModRefs, common.ModuleRef{Path: mod, Version: ver}) } - log.Printf("Version: %s\n", ver) if err := common.UpdateGoModFiles( modFilePaths, - s.OtherModuleSet.Modules, - ver, + newModRefs, ); err != nil { return fmt.Errorf("could not update all go mod files: %w", err) }