Skip to content
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
2 changes: 1 addition & 1 deletion internal/generator/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (generator *GitGenerator) Versions() ([]string, error) {
return true
}
if a != b {
return a < b
return a > b
}
aa := normalizeVersion(generator.filter.FindAllStringSubmatch(filtered[i], -1))
bb := normalizeVersion(generator.filter.FindAllStringSubmatch(filtered[j], -1))
Expand Down
28 changes: 28 additions & 0 deletions internal/generator/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,31 @@ func TestGitGeneratorVersionsCustomSort(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, []string{"2.10.0", "2.2.0", "2.1.0", "1.01.01", "1.0.0"}, versions)
}

func TestGitGeneratorVersionsSortByTimestamp(t *testing.T) {
baseTime := int64(1704000000)
newerTime := baseTime + 86400

bundles := []gitBundle{
{tag: "v1.0.0", timestamp: &baseTime, paths: []gitPath{{path: "crds/v1.yaml", file: "testdata/test-crd.yaml"}}},
{tag: "v2.0.0", timestamp: &baseTime, paths: []gitPath{{path: "crds/v2.yaml", file: "testdata/test-crd.yaml"}}},
{tag: "v3.0.0", timestamp: &newerTime, paths: []gitPath{{path: "crds/v3.yaml", file: "testdata/test-crd.yaml"}}},
}

p, err := setupGit(t, bundles)
assert.Nil(t, err)
assert.NotNil(t, p)

config := configuration.Configuration{
Kind: configuration.Git,
Repository: fmt.Sprintf("file://%s", *p),
}

filter := regexp.MustCompile(`^v([0-9]+\.[0-9]+\.[0-9]+)$`)
generator := NewGitGenerator(config, nil, filter)
defer generator.Close()

versions, err := generator.Versions()
assert.Nil(t, err)
assert.Equal(t, []string{"v3.0.0", "v2.0.0", "v1.0.0"}, versions)
}
15 changes: 11 additions & 4 deletions internal/generator/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"path"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -193,9 +194,10 @@ func setupOciServer(t *testing.T, chart ociChart) (mockServer, func()) {
}

type gitBundle struct {
tag string
branch string
paths []gitPath
tag string
branch string
paths []gitPath
timestamp *int64
}

type gitPath struct {
Expand Down Expand Up @@ -272,7 +274,12 @@ func setupGit(t *testing.T, bundles []gitBundle) (*string, error) {
}

if len(bundle.paths) > 0 {
err = exec.Command("git", "--git-dir", gitDir, "--work-tree", tmpDir, "commit", "--message", "Add bundle").Run()
cmd := exec.Command("git", "--git-dir", gitDir, "--work-tree", tmpDir, "commit", "--message", "Add bundle")
if bundle.timestamp != nil {
date := time.Unix(*bundle.timestamp, 0).Format("2006-01-02T15:04:05Z")
cmd.Env = append(os.Environ(), "GIT_AUTHOR_DATE="+date, "GIT_COMMITTER_DATE="+date)
}
err = cmd.Run()
if err != nil {
return nil, fmt.Errorf("unable to commit: %w", err)
}
Expand Down
Loading