From a10d04e438987b26113fa2795943d016e9e9d195 Mon Sep 17 00:00:00 2001 From: Roman Dmytrenko Date: Wed, 20 Nov 2024 19:35:34 +0200 Subject: [PATCH 1/3] fix(config): add interval option for git configuration --- config.go | 7 +-- pkg/config/config_test.go | 103 ++++++++++++++++++++++++++++++++++++++ pkg/config/git.go | 15 ++++-- 3 files changed, 118 insertions(+), 7 deletions(-) diff --git a/config.go b/config.go index 9723537..45e9b3a 100644 --- a/config.go +++ b/config.go @@ -6,7 +6,6 @@ import ( "fmt" "log/slog" "strings" - "time" "github.com/get-glu/glu/internal/git" "github.com/get-glu/glu/internal/oci" @@ -71,7 +70,6 @@ func (c *Config) GitRepository(ctx context.Context, name string) (_ *git.Reposit method transport.AuthMethod srcOpts = []containers.Option[git.Repository]{ git.WithDefaultBranch(conf.DefaultBranch), - git.WithInterval(10 * time.Second), } ) @@ -82,7 +80,10 @@ func (c *Config) GitRepository(ctx context.Context, name string) (_ *git.Reposit if conf.Remote != nil { slog.Debug("configuring remote", "remote", conf.Remote.Name) - srcOpts = append(srcOpts, git.WithRemote(conf.Remote.Name, conf.Remote.URL)) + srcOpts = append(srcOpts, + git.WithRemote(conf.Remote.Name, conf.Remote.URL), + git.WithInterval(conf.Remote.Interval), + ) if conf.Remote.Credential != "" { creds, err := c.creds.Get(conf.Remote.Credential) diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index c4c400f..70b3c5d 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -2,8 +2,11 @@ package config import ( "errors" + "os" + "path/filepath" "reflect" "testing" + "time" ) type testDefaulter struct { @@ -155,3 +158,103 @@ func TestProcessValue(t *testing.T) { }) } } + +type testGitExpected struct { + remoteName string + url string + credential string + interval time.Duration +} + +func TestConfigGit(t *testing.T) { + configDir := t.TempDir() + + tests := []struct { + name string + input string + expected GitRepository + }{ + { + name: "default", + input: `sources: + git: + default: + remote: + name: upstream + url: https://corp-repos/default.git + `, + expected: GitRepository{ + Remote: &Remote{ + Name: "upstream", + URL: "https://corp-repos/default.git", + Interval: 10 * time.Second, + }, + DefaultBranch: "main", + }, + }, + { + name: "custom", + input: `sources: + git: + custom: + remote: + name: origin + url: https://corp-repos/custom + credential: vault + interval: 1m + path: v1 + default_branch: release-v1 + `, + expected: GitRepository{ + Remote: &Remote{ + Name: "origin", + URL: "https://corp-repos/custom", + Credential: "vault", + Interval: time.Minute, + }, + Path: "v1", + DefaultBranch: "release-v1", + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + configPath := filepath.Join(configDir, tt.name+"-glu.yaml") + err := os.WriteFile(configPath, []byte(tt.input), 0600) + if err != nil { + t.Fatalf("failed to write configuration file: %v", err) + } + + c, err := ReadFromPath(configPath) + if err != nil { + t.Errorf("expected no error, got %v", err) + } + repos := c.Sources.Git + if len(repos) == 0 { + t.Fatalf("expected at least one repo, got zero") + } + repo, ok := repos[tt.name] + if !ok { + t.Fatalf("expected repo %s to exist", tt.name) + } + + if !reflect.DeepEqual(tt.expected.Remote, repo.Remote) { + t.Errorf("expected remote %v, got %v", tt.expected.Remote, repo.Remote) + } + if tt.expected.Path != repo.Path { + t.Errorf("expected path %v, got %v", tt.expected.Path, repo.Path) + } + if tt.expected.DefaultBranch != repo.DefaultBranch { + t.Errorf("expected default branch %v, got %v", tt.expected.DefaultBranch, repo.DefaultBranch) + } + }) + } +} + +func TestReadFromFileWhenNoConfig(t *testing.T) { + _, err := ReadFromPath(filepath.Join(t.TempDir(), "non-existent-file.yaml")) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } +} diff --git a/pkg/config/git.go b/pkg/config/git.go index f268e15..4f01e00 100644 --- a/pkg/config/git.go +++ b/pkg/config/git.go @@ -1,6 +1,9 @@ package config -import "log/slog" +import ( + "log/slog" + "time" +) var ( _ validater = (*GitRepositories)(nil) @@ -53,15 +56,19 @@ func (r *GitRepository) setDefaults() error { remote.Name = "origin" } + if remote.Interval == 0 { + remote.Interval = 10 * time.Second + } } return nil } type Remote struct { - Name string `glu:"name"` - URL string `glu:"url"` - Credential string `glu:"credential"` + Name string `glu:"name"` + URL string `glu:"url"` + Credential string `glu:"credential"` + Interval time.Duration `glu:"interval"` } type Proposals struct { From 2e38c136f241c9696127fadc754923b195a8f879 Mon Sep 17 00:00:00 2001 From: Roman Dmytrenko Date: Wed, 20 Nov 2024 22:23:19 +0200 Subject: [PATCH 2/3] add documentation --- docs/configuration.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/configuration.md b/docs/configuration.md index 4d0a649..e9da946 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -179,6 +179,10 @@ The URL of the remote. The name of the credential to use for the remote. +#### `sources..git..remote.interval` + +The period between automatic fetches from the remote. + #### `sources..git..proposals` The configuration for the proposals for the git repository. From 506d67b3bd5380df9c10c3dfc49b65628946c428 Mon Sep 17 00:00:00 2001 From: Roman Dmytrenko Date: Wed, 20 Nov 2024 22:30:17 +0200 Subject: [PATCH 3/3] Update pkg/config/git.go Co-authored-by: Mark Phelps <209477+markphelps@users.noreply.github.com> --- pkg/config/git.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/config/git.go b/pkg/config/git.go index 4f01e00..8f4a439 100644 --- a/pkg/config/git.go +++ b/pkg/config/git.go @@ -56,7 +56,7 @@ func (r *GitRepository) setDefaults() error { remote.Name = "origin" } - if remote.Interval == 0 { + if remote.Interval < 1 { remote.Interval = 10 * time.Second } }