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

fix(config): add interval option for git configuration #50

Merged
merged 3 commits into from
Nov 20, 2024
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
7 changes: 4 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"log/slog"
"strings"
"time"

"github.com/get-glu/glu/internal/git"
"github.com/get-glu/glu/internal/oci"
Expand Down Expand Up @@ -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),
}
)

Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ The URL of the remote.

The name of the credential to use for the remote.

#### `sources.<name>.git.<repository>.remote.interval`

The period between automatic fetches from the remote.

#### `sources.<name>.git.<repository>.proposals`

The configuration for the proposals for the git repository.
Expand Down
103 changes: 103 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package config

import (
"errors"
"os"
"path/filepath"
"reflect"
"testing"
"time"
)

type testDefaulter struct {
Expand Down Expand Up @@ -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)
}
}
15 changes: 11 additions & 4 deletions pkg/config/git.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package config

import "log/slog"
import (
"log/slog"
"time"
)

var (
_ validater = (*GitRepositories)(nil)
Expand Down Expand Up @@ -53,15 +56,19 @@ func (r *GitRepository) setDefaults() error {

remote.Name = "origin"
}
if remote.Interval < 1 {
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 {
Expand Down