Skip to content

Commit

Permalink
multimod: ignore excluded-modules when -a flag is enabled (#442)
Browse files Browse the repository at this point in the history
This allows users of the `sync` command to sync all modules in a monorepo, including those listed in the excluded-modules. This is useful for repositories where some modules may not yet be ready for releasing (therefore listed under excluded-modules) but their dependencies still need to be managed via multimod.

Signed-off-by: Alex Boten <[email protected]>
  • Loading branch information
Alex Boten authored Nov 24, 2023
1 parent 49952c1 commit 0ec5f4b
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 5 deletions.
20 changes: 20 additions & 0 deletions .chloggen/codeboten_add-ignore-excluded.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# 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: ignore excluded-modules when -a flag is enabled

# One or more tracking issues related to the change
issues: [442]

# (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: |
This allows users of the sync command to sync all modules in a monorepo, including
those listed in the excluded-modules. This is useful for repositories where some modules
may not yet be ready for releasing (therefore listed under excluded-modules) but their
dependencies still need to be managed via multimod.
12 changes: 10 additions & 2 deletions multimod/internal/common/module_versioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ type ModuleVersioning struct {
ModInfoMap ModuleInfoMap
}

// NewModuleVersioning returns a ModuleVersioning struct from a versioning file and repo root.
func NewModuleVersioning(versioningFilename string, repoRoot string) (ModuleVersioning, error) {
// NewModuleVersioningWithIgnoreExcluded returns a ModuleVersioning struct from a versioning file and repo root and supports
// ignoring the excluded-modules configuration.
func NewModuleVersioningWithIgnoreExcluded(versioningFilename string, repoRoot string, ignoreExcluded bool) (ModuleVersioning, error) {
repoRoot, err := filepath.Abs(repoRoot)
if err != nil {
return ModuleVersioning{}, fmt.Errorf("could not get absolute path of repo root: %w", err)
}

vCfg, err := readVersioningFile(versioningFilename)
vCfg.ignoreExcluded = ignoreExcluded

if err != nil {
return ModuleVersioning{}, fmt.Errorf("error reading versioning file %v: %w", versioningFilename, err)
}
Expand All @@ -56,3 +59,8 @@ func NewModuleVersioning(versioningFilename string, repoRoot string) (ModuleVers
ModInfoMap: modInfoMap,
}, nil
}

// NewModuleVersioning returns a ModuleVersioning struct from a versioning file and repo root.
func NewModuleVersioning(versioningFilename string, repoRoot string) (ModuleVersioning, error) {
return NewModuleVersioningWithIgnoreExcluded(versioningFilename, repoRoot, false)
}
8 changes: 8 additions & 0 deletions multimod/internal/common/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
type versionConfig struct {
ModuleSets ModuleSetMap `mapstructure:"module-sets"`
ExcludedModules []ModulePath `mapstructure:"excluded-modules"`
ignoreExcluded bool
}

// excludedModules functions as a set containing all module paths that are excluded
Expand Down Expand Up @@ -129,6 +130,10 @@ func (versionCfg versionConfig) buildModuleMap() (ModuleInfoMap, error) {

// getExcludedModules returns if a given module path is listed in the excluded modules section of a versioning file.
func (versionCfg versionConfig) shouldExcludeModule(modPath ModulePath) bool {
if versionCfg.ignoreExcluded {
return false
}

excludedModules := versionCfg.getExcludedModules()
_, exists := excludedModules[modPath]

Expand All @@ -138,6 +143,9 @@ func (versionCfg versionConfig) shouldExcludeModule(modPath ModulePath) bool {
// getExcludedModules returns a map structure containing all excluded module paths as keys and empty values.
func (versionCfg versionConfig) getExcludedModules() excludedModulesSet {
excludedModules := make(excludedModulesSet)
if versionCfg.ignoreExcluded {
return excludedModules
}
// add all excluded modules to the excludedModulesSet
for _, mod := range versionCfg.ExcludedModules {
excludedModules[mod] = struct{}{}
Expand Down
6 changes: 3 additions & 3 deletions multimod/internal/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func Run(myVersioningFile string, otherVersioningFile string, otherRepoRoot stri
}

for _, moduleSetName := range otherModuleSetNames {
s, err := newSync(myVersioningFile, otherVersioningFile, moduleSetName, myRepoRoot, otherVersionCommit)
s, err := newSync(myVersioningFile, otherVersioningFile, moduleSetName, myRepoRoot, otherVersionCommit, allModuleSets)
if err != nil {
log.Fatalf("error creating new sync struct: %v", err)
}
Expand Down Expand Up @@ -98,13 +98,13 @@ type sync struct {
client *http.Client
}

func newSync(myVersioningFilename, otherVersioningFilename, modSetToUpdate, myRepoRoot string, otherVersionCommit string) (sync, error) {
func newSync(myVersioningFilename, otherVersioningFilename, modSetToUpdate, myRepoRoot string, otherVersionCommit string, ignoreExcluded bool) (sync, error) {
otherModuleSet, err := common.GetModuleSet(modSetToUpdate, otherVersioningFilename)
if err != nil {
return sync{}, fmt.Errorf("error creating new sync struct: %w", err)
}

myModVersioning, err := common.NewModuleVersioning(myVersioningFilename, myRepoRoot)
myModVersioning, err := common.NewModuleVersioningWithIgnoreExcluded(myVersioningFilename, myRepoRoot, ignoreExcluded)
if err != nil {
return sync{}, fmt.Errorf("could not get my ModuleVersioning: %w", err)
}
Expand Down
73 changes: 73 additions & 0 deletions multimod/internal/sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func TestNewSync(t *testing.T) {
tc.modSetName,
tmpRootDir,
"",
false,
)
require.NoError(t, err)

Expand Down Expand Up @@ -308,6 +309,7 @@ func TestUpdateAllGoModFilesWithCommitHash(t *testing.T) {
tc.modSetName,
tmpRootDir,
tc.commit,
false,
)
s.client = tc.client
require.NoError(t, err)
Expand Down Expand Up @@ -340,6 +342,7 @@ func TestUpdateAllGoModFiles(t *testing.T) {
testCases := []struct {
modSetName string
expectedOutputModFiles map[string][]byte
allModules bool
}{
{
modSetName: "other-mod-set-1",
Expand Down Expand Up @@ -372,6 +375,13 @@ func TestUpdateAllGoModFiles(t *testing.T) {
"go.opentelemetry.io/build-tools/multimod/internal/sync/test/test2 v1.2.3-RC1+meta\n\t" +
"go.opentelemetry.io/other/test/test1 v1.2.3-RC1+meta\n" +
")"),
filepath.Join("my", "test", "testexcluded", "go.mod"): []byte("module go.opentelemetry.io/my/test/testexcluded\n\n" +
"go 1.16\n\n" +
"require (\n\t" +
"go.opentelemetry.io/build-tools/multimod/internal/sync/test/test1 v1.0.0-old\n\t" +
"go.opentelemetry.io/other/testroot/v2 v1.0.0\n\t" +
"go.opentelemetry.io/other/test/test1 v1.0.0-old\n" +
")"),
},
},
{
Expand Down Expand Up @@ -405,6 +415,13 @@ func TestUpdateAllGoModFiles(t *testing.T) {
"go.opentelemetry.io/build-tools/multimod/internal/sync/test/test2 v1.2.3-RC1+meta\n\t" +
"go.opentelemetry.io/other/test/test1 v1.0.0-old\n" +
")"),
filepath.Join("my", "test", "testexcluded", "go.mod"): []byte("module go.opentelemetry.io/my/test/testexcluded\n\n" +
"go 1.16\n\n" +
"require (\n\t" +
"go.opentelemetry.io/build-tools/multimod/internal/sync/test/test1 v1.0.0-old\n\t" +
"go.opentelemetry.io/other/testroot/v2 v1.0.0\n\t" +
"go.opentelemetry.io/other/test/test1 v1.0.0-old\n" +
")"),
},
},
{
Expand Down Expand Up @@ -438,8 +455,56 @@ func TestUpdateAllGoModFiles(t *testing.T) {
"go.opentelemetry.io/build-tools/multimod/internal/sync/test/test2 v1.2.3-RC1+meta\n\t" +
"go.opentelemetry.io/other/test/test1 v1.0.0-old\n" +
")"),
filepath.Join("my", "test", "testexcluded", "go.mod"): []byte("module go.opentelemetry.io/my/test/testexcluded\n\n" +
"go 1.16\n\n" +
"require (\n\t" +
"go.opentelemetry.io/build-tools/multimod/internal/sync/test/test1 v1.0.0-old\n\t" +
"go.opentelemetry.io/other/testroot/v2 v1.0.0\n\t" +
"go.opentelemetry.io/other/test/test1 v1.0.0-old\n" +
")"),
},
},
{
modSetName: "other-mod-set-3",
expectedOutputModFiles: map[string][]byte{
filepath.Join("my", "test", "test1", "go.mod"): []byte("module go.opentelemetry.io/build-tools/multimod/internal/sync/test/test1\n\n" +
"go 1.16\n\n" +
"require (\n\t" +
"go.opentelemetry.io/build-tools/multimod/internal/sync/test/test2 v1.2.3-RC1+meta\n\t" +
"go.opentelemetry.io/other/test/test1 v1.0.0-old\n\t" +
"go.opentelemetry.io/other/testroot/v2 v2.2.2\n" +
")"),
filepath.Join("my", "test", "test2", "go.mod"): []byte("module go.opentelemetry.io/build-tools/multimod/internal/sync/test/test2\n\n" +
"go 1.16\n\n" +
"require (\n\t" +
"go.opentelemetry.io/build-tools/multimod/internal/sync/test/test1 v1.2.3-RC1+meta\n\t" +
"go.opentelemetry.io/other/test/test1 v1.0.0-old\n\t" +
"go.opentelemetry.io/other/testroot/v2 v2.2.2\n" +
")"),
filepath.Join("my", "test", "go.mod"): []byte("module go.opentelemetry.io/build-tools/multimod/internal/sync/test3\n\n" +
"go 1.16\n\n" +
"require (\n\t" +
"go.opentelemetry.io/build-tools/multimod/internal/sync/test/test1 v1.2.3-RC1+meta\n\t" +
"go.opentelemetry.io/build-tools/multimod/internal/sync/test/test2 v1.2.3-RC1+meta\n\t" +
"go.opentelemetry.io/other/test2 v0.1.0-old\n" +
")"),
filepath.Join("my", "go.mod"): []byte("module go.opentelemetry.io/build-tools/multimod/internal/sync/testroot/v2\n\n" +
"go 1.16\n\n" +
"require (\n\t" +
"go.opentelemetry.io/build-tools/multimod/internal/sync/test/test1 v1.2.3-RC1+meta\n\t" +
"go.opentelemetry.io/build-tools/multimod/internal/sync/test/test2 v1.2.3-RC1+meta\n\t" +
"go.opentelemetry.io/other/test/test1 v1.0.0-old\n" +
")"),
filepath.Join("my", "test", "testexcluded", "go.mod"): []byte("module go.opentelemetry.io/my/test/testexcluded\n\n" +
"go 1.16\n\n" +
"require (\n\t" +
"go.opentelemetry.io/build-tools/multimod/internal/sync/test/test1 v1.0.0-old\n\t" +
"go.opentelemetry.io/other/testroot/v2 v2.2.2\n\t" +
"go.opentelemetry.io/other/test/test1 v1.0.0-old\n" +
")"),
},
allModules: true,
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -479,6 +544,13 @@ func TestUpdateAllGoModFiles(t *testing.T) {
"go.opentelemetry.io/build-tools/multimod/internal/sync/test/test2 v1.2.3-RC1+meta\n\t" +
"go.opentelemetry.io/other/test/test1 v1.0.0-old\n" +
")"),
filepath.Join(tmpRootDir, "my", "test", "testexcluded", "go.mod"): []byte("module go.opentelemetry.io/my/test/testexcluded\n\n" +
"go 1.16\n\n" +
"require (\n\t" +
"go.opentelemetry.io/build-tools/multimod/internal/sync/test/test1 v1.0.0-old\n\t" +
"go.opentelemetry.io/other/testroot/v2 v1.0.0\n\t" +
"go.opentelemetry.io/other/test/test1 v1.0.0-old\n" +
")"),
}

t.Run(tc.modSetName, func(t *testing.T) {
Expand All @@ -490,6 +562,7 @@ func TestUpdateAllGoModFiles(t *testing.T) {
tc.modSetName,
tmpRootDir,
"",
tc.allModules,
)
require.NoError(t, err)

Expand Down

0 comments on commit 0ec5f4b

Please sign in to comment.