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

config feature #237

Merged
merged 2 commits into from
Nov 3, 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
6 changes: 4 additions & 2 deletions cmd/app/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
}
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/app/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions cmd/configuration/types_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 48 additions & 30 deletions pkg/api/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
18 changes: 11 additions & 7 deletions pkg/api/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/resourcehandlers/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion pkg/resourcehandlers/git/git_resource_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/resourcehandlers/git/git_resource_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 5 additions & 1 deletion pkg/resourcehandlers/github/github_resource_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down