diff --git a/cmd/app/cmd.go b/cmd/app/cmd.go index 6fdc06c1..25547db8 100644 --- a/cmd/app/cmd.go +++ b/cmd/app/cmd.go @@ -67,8 +67,8 @@ func NewCommand(ctx context.Context, cancel context.CancelFunc) *cobra.Command { return err } api.SetFlagsVariables(flags.variables) - api.SetMainBranches(flags.mainBranch) - api.SetVersions(flags.lastNVersions) + api.SetDefaultBranches(flags.mainBranch, options.DefaultBranches) + api.SetNVersions(flags.lastNVersions, options.LastNVersions) if doc, err = manifest(ctx, flags.documentationManifestPath, rhs); err != nil { return err } @@ -201,6 +201,8 @@ func NewOptions(f *cmdFlags, c configuration.Loader) *Options { UseGit: f.useGit, HomeDir: cacheHomeDir(f, config), LocalMappings: config.ResourceMappings, + DefaultBranches: config.DefaultBranches, + LastNVersions: config.LastNVersions, } } diff --git a/cmd/app/factory.go b/cmd/app/factory.go index 4ac63bdc..fef46bcd 100644 --- a/cmd/app/factory.go +++ b/cmd/app/factory.go @@ -61,6 +61,8 @@ type Options struct { UseGit bool HomeDir string LocalMappings map[string]string + DefaultBranches map[string]string + LastNVersions map[string]int } // Credentials holds repositories access credentials diff --git a/cmd/configuration/types_configuration.go b/cmd/configuration/types_configuration.go index 9e2c122e..5be61e46 100644 --- a/cmd/configuration/types_configuration.go +++ b/cmd/configuration/types_configuration.go @@ -12,6 +12,8 @@ type Config struct { // ResourceMappings defines URL -> location mapping for existing git repositories ResourceMappings map[string]string `yaml:"resourceMappings,omitempty"` Hugo *Hugo `yaml:"hugo,omitempty"` + DefaultBranches map[string]string `yaml:"defaultBranches,omitempty"` + LastNVersions map[string]int `yaml:"lastNVersions,omitempty"` } // Source holds repositories access credentials diff --git a/pkg/api/parser.go b/pkg/api/parser.go index fd774b3c..b22c086e 100755 --- a/pkg/api/parser.go +++ b/pkg/api/parser.go @@ -13,14 +13,15 @@ import ( "github.com/Masterminds/semver" "gopkg.in/yaml.v3" - "k8s.io/klog/v2" ) // flagsVars variables for template resolving var ( - flagsVars map[string]string - versions map[string]int - mainBranches map[string]string + flagsVars map[string]string + flagVersionsMap map[string]int + configVersionsMap map[string]int + flagBranchesMap map[string]string + configBranchesMap map[string]string ) // SetFlagsVariables initialize flags variables @@ -29,47 +30,64 @@ func SetFlagsVariables(vars map[string]string) { } // SetVersions sets the mapping of repo uri to last n versions to be iterated over -func SetVersions(vers map[string]int) { - versions = vers +func SetNVersions(flagNVersions map[string]int, configNVersions map[string]int) { + flagVersionsMap = flagNVersions + configVersionsMap = configNVersions } -// SetMainBranches sets the mappinf of repo uri to name of the default branch -func SetMainBranches(mb map[string]string) { - mainBranches = mb +// SetDefaultBranches sets the mappinf of repo uri to name of the default branch +func SetDefaultBranches(flagBranches map[string]string, configBranches map[string]string) { + flagBranchesMap = flagBranches + configBranchesMap = configBranches } -// ParseWithMetadata parse with tags and bytes as read -// fsHandled used to display warning -// uri used to get the proper main branch and versions -func ParseWithMetadata(tags []string, b []byte, fsHandled bool, uri string, defaultBranch string) (*Documentation, error) { +// ChooseTargetBranch chooses the default branch of the uri based on command variable, config file and repo default branch setup +func ChooseTargetBranch(uri string, repoCurrentBranch string) string { var ( - nTags int - err error - mainBranch string - ok bool + targetBranch string + ok bool ) - //setting main branch - if mainBranch, ok = mainBranches[uri]; !ok { - if mainBranch, ok = mainBranches["default"]; !ok { - mainBranch = defaultBranch + //choosing default branch + if targetBranch, ok = flagBranchesMap[uri]; !ok { + if targetBranch, ok = configBranchesMap[uri]; !ok { + if targetBranch, ok = flagBranchesMap["default"]; !ok { + targetBranch = repoCurrentBranch + } } } + return targetBranch +} + +// ChooseNVersions chooses how many versions to be iterated over given a repo uri +func ChooseNVersions(uri string) int { + var ( + nTags int + ok bool + ) //setting nTags - if nTags, ok = versions[uri]; !ok { - if nTags, ok = versions["default"]; !ok { - nTags = 0 + if nTags, ok = flagVersionsMap[uri]; !ok { + if nTags, ok = configVersionsMap[uri]; !ok { + if nTags, ok = flagVersionsMap["default"]; !ok { + nTags = 0 + } } } - if nTags > 0 && fsHandled { - klog.Warningf("There is a yaml file from file system not connected with a repository. Therefore LastNSupportedVersions is set to 0 %s", uri) - nTags = 0 - } - if tags, err = getLastNVersions(tags, nTags); err != nil { + return nTags +} + +// ParseWithMetadata parses a document's byte content given some other metainformation +func ParseWithMetadata(b []byte, allTags []string, nTags int, targetBranch string) (*Documentation, error) { + var ( + err error + tags []string + ) + if tags, err = getLastNVersions(allTags, nTags); err != nil { return nil, err } versionList := make([]string, 0) - versionList = append(versionList, mainBranch) + versionList = append(versionList, targetBranch) versionList = append(versionList, tags...) + versions := strings.Join(versionList, ",") flagsVars["versions"] = versions return Parse(b) diff --git a/pkg/api/parser_test.go b/pkg/api/parser_test.go index af54fe36..bc637f73 100755 --- a/pkg/api/parser_test.go +++ b/pkg/api/parser_test.go @@ -296,14 +296,16 @@ func TestGetLastNVersions(t *testing.T) { func TestParseWithMetadata(t *testing.T) { cases := []struct { - tags []string - b []byte - uri string - want *Documentation - err error + tags []string + nVersions int + b []byte + uri string + want *Documentation + err error }{ { []string{"v4.9", "v5.7", "v6.1", "v7.7"}, + 4, []byte(`structure: - name: community source: https://github.com/gardener/docforge/edit/master/integration-test/tested-doc/merge-test/testFile.md @@ -355,6 +357,7 @@ func TestParseWithMetadata(t *testing.T) { }, { []string{"v4.9", "v5.7"}, + 2, []byte(`structure: - name: community source: https://github.com/gardener/docforge/edit/master/integration-test/tested-doc/merge-test/testFile.md @@ -396,6 +399,7 @@ func TestParseWithMetadata(t *testing.T) { }, { []string{}, + 0, []byte(`structure: - name: community source: https://github.com/gardener/docforge/edit/master/integration-test/tested-doc/merge-test/testFile.md @@ -432,8 +436,8 @@ func TestParseWithMetadata(t *testing.T) { SetFlagsVariables(vars) for _, c := range cases { v["https://github.com/Kostov6/documentation/blob/master/.docforge/test.yamls"] = len(c.tags) - SetVersions(v) - got, gotErr := ParseWithMetadata(c.tags, c.b, false, c.uri, "master") + SetNVersions(v, v) + got, gotErr := ParseWithMetadata(c.b, c.tags, c.nVersions, "master") assert.Equal(t, c.err, gotErr) assert.Equal(t, c.want, got) } diff --git a/pkg/resourcehandlers/fs/fs.go b/pkg/resourcehandlers/fs/fs.go index 3bc761d2..96990c0f 100644 --- a/pkg/resourcehandlers/fs/fs.go +++ b/pkg/resourcehandlers/fs/fs.go @@ -11,6 +11,7 @@ import ( "github.com/gardener/docforge/pkg/markdown" "github.com/gardener/docforge/pkg/resourcehandlers" "github.com/gardener/docforge/pkg/resourcehandlers/github" + "k8s.io/klog/v2" "github.com/gardener/docforge/pkg/util/httpclient" ghclient "github.com/google/go-github/v32/github" "io/ioutil" @@ -125,8 +126,13 @@ func (fs *fsHandler) ResolveDocumentation(ctx context.Context, uri string) (*api if err != nil { return nil, err } - - return api.ParseWithMetadata([]string{}, blob, true, uri, "master") + targetBranch := api.ChooseTargetBranch(uri, "master") + //getting nVersions based on configuration + nVersions := api.ChooseNVersions(uri) + if nVersions > 0 { + klog.Warningf("There is a yaml file from file system not connected with a repository. Therefore LastNSupportedVersions is set to 0 for file %s", uri) + } + return api.ParseWithMetadata(blob, []string{}, 0, targetBranch) } // ReadGitInfo implements resourcehandlers.ResourceHandler#ReadGitInfo diff --git a/pkg/resourcehandlers/git/git_resource_handler.go b/pkg/resourcehandlers/git/git_resource_handler.go index 83dff69a..0d68fb40 100644 --- a/pkg/resourcehandlers/git/git_resource_handler.go +++ b/pkg/resourcehandlers/git/git_resource_handler.go @@ -414,6 +414,11 @@ func (g *Git) ResolveDocumentation(ctx context.Context, uri string) (*api.Docume return nil, err } } + //here rl.SHAAlias on the right side is the repo current branch + rl.SHAAlias = api.ChooseTargetBranch(uri, rl.SHAAlias) + //getting nVersions based on configuration + nVersions := api.ChooseNVersions(uri) + repositoryPath := g.repositoryPathFromResourceLocator(rl) if err := g.prepareGitRepository(ctx, repositoryPath, rl); err != nil { return nil, err @@ -433,7 +438,7 @@ func (g *Git) ResolveDocumentation(ctx context.Context, uri string) (*api.Docume if blob == nil { return nil, nil } - return api.ParseWithMetadata(tags, blob, false, uri, rl.SHAAlias) + return api.ParseWithMetadata(blob, tags, nVersions, rl.SHAAlias) } //internally used diff --git a/pkg/resourcehandlers/git/git_resource_handler_test.go b/pkg/resourcehandlers/git/git_resource_handler_test.go index 4f2c568d..4d72ce4d 100644 --- a/pkg/resourcehandlers/git/git_resource_handler_test.go +++ b/pkg/resourcehandlers/git/git_resource_handler_test.go @@ -1344,7 +1344,7 @@ func TestResolveDocumentation(t *testing.T) { } var s map[string]int = make(map[string]int) s[c.uri] = len(c.tags) - api.SetVersions(s) + api.SetNVersions(s, s) api.SetFlagsVariables(make(map[string]string)) //clear default branch cache ghub.ClearDefaultBranchesCache() diff --git a/pkg/resourcehandlers/github/github_resource_handler.go b/pkg/resourcehandlers/github/github_resource_handler.go index d6ddbbfc..0e1b48dd 100644 --- a/pkg/resourcehandlers/github/github_resource_handler.go +++ b/pkg/resourcehandlers/github/github_resource_handler.go @@ -315,6 +315,10 @@ func (gh *GitHub) ResolveDocumentation(ctx context.Context, path string) (*api.D if !(rl.Type == Blob || rl.Type == Raw) || urls.Ext(rl.String()) == ".md" { return nil, nil } + //here rl.SHAAlias on the right side is the repo current branch + rl.SHAAlias = api.ChooseTargetBranch(path, rl.SHAAlias) + //getting nVersions based on configuration + nVersions := api.ChooseNVersions(path) tags, err := gh.getAllTags(ctx, rl) if err != nil { return nil, err @@ -323,7 +327,7 @@ func (gh *GitHub) ResolveDocumentation(ctx context.Context, path string) (*api.D if err != nil { return nil, err } - return api.ParseWithMetadata(tags, blob, false, path, rl.SHAAlias) + return api.ParseWithMetadata(blob, tags, nVersions, rl.SHAAlias) } func (gh *GitHub) getAllTags(ctx context.Context, rl *ResourceLocator) ([]string, error) { diff --git a/pkg/resourcehandlers/github/github_resource_handler_test.go b/pkg/resourcehandlers/github/github_resource_handler_test.go index 0f72b45d..669cac85 100644 --- a/pkg/resourcehandlers/github/github_resource_handler_test.go +++ b/pkg/resourcehandlers/github/github_resource_handler_test.go @@ -892,7 +892,7 @@ func TestResolveDocumentation(t *testing.T) { gh.Client = client var s map[string]int = make(map[string]int) s["default"] = 4 - api.SetVersions(s) + api.SetNVersions(s, s) api.SetFlagsVariables(make(map[string]string)) got, gotErr := gh.ResolveDocumentation(ctx, c.uri) fmt.Println(gotErr)