From 24cb8092add993674ebf5b2bd6ff488e0813d165 Mon Sep 17 00:00:00 2001 From: dannylongeuay Date: Thu, 25 Apr 2024 12:04:36 -0700 Subject: [PATCH] fix(*): :bug: check commits log ordering --- pkg/provider/git.go | 3 ++- pkg/provider/git_test.go | 55 +++++++++++++++++++++++++++++++++++++++- scripts/start-gitea.sh | 18 +++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/pkg/provider/git.go b/pkg/provider/git.go index c816043..ec35119 100644 --- a/pkg/provider/git.go +++ b/pkg/provider/git.go @@ -108,7 +108,8 @@ func (repo *Repository) GetCommits(fromSha, toSha string) ([]*semrel.RawCommit, } commits, err := repo.repo.Log(&git.LogOptions{ - From: *toHash, + From: *toHash, + Order: git.LogOrderCommitterTime, }) if err != nil { return nil, err diff --git a/pkg/provider/git_test.go b/pkg/provider/git_test.go index 1a4a7ec..9eb1d21 100644 --- a/pkg/provider/git_test.go +++ b/pkg/provider/git_test.go @@ -26,6 +26,7 @@ func TestGit(t *testing.T) { t.Run("GetInfo", getInfo) t.Run("GetReleases", getReleases) t.Run("GetCommits", getCommits) + t.Run("GetCommitsNoFFMerge", getCommitsNoFFMerge) t.Run("CreateRelease", createRelease) } @@ -151,6 +152,31 @@ func createRepo() (*Repository, error) { return repo, nil } +func cloneRepo(path string, url string) (*Repository, error) { + _, err := git.PlainClone(path, false, &git.CloneOptions{ + Auth: &http.BasicAuth{ + Username: "test", + Password: "test", + }, + URL: url, + }) + if err != nil { + return nil, err + } + repo := &Repository{} + err = repo.Init(map[string]string{ + "git_path": path, + "auth": "basic", + "auth_username": "test", + "auth_password": "test", + }) + if err != nil { + return nil, err + } + + return repo, nil +} + func getInfo(t *testing.T) { require := require.New(t) repo, err := createRepo() @@ -179,6 +205,20 @@ func getCommits(t *testing.T) { } } +func getCommitsNoFFMerge(t *testing.T) { + require := require.New(t) + dir, err := os.MkdirTemp("", "provider-git") + require.NoError(err) + repo, err := cloneRepo(dir, "http://localhost:3000/test/no_ff_merge.git") + require.NoError(err) + releases, err := repo.GetReleases("") + require.Len(releases, 1) + initialCommitSha := releases[0].GetSHA() + commits, err := repo.GetCommits(initialCommitSha, "master") + require.NoError(err) + require.Len(commits, 2) +} + func createRelease(t *testing.T) { require := require.New(t) repo, err := createRepo() @@ -211,14 +251,27 @@ func createRelease(t *testing.T) { Changelog: testCase.changelog, }) require.NoError(err) + tagName := "v" + testCase.version - tagRef, err := gRepo.Tag("v" + testCase.version) + tagRef, err := gRepo.Tag(tagName) require.NoError(err) tagObj, err := gRepo.TagObject(tagRef.Hash()) require.NoError(err) require.Equal(testCase.changelog+"\n", tagObj.Message) + + // Clean up tags so future test runs succeed + tagRefName := ":refs/tags/" + tagName + err = gRepo.Push(&git.PushOptions{ + RemoteName: "origin", + RefSpecs: []config.RefSpec{config.RefSpec(tagRefName)}, + Auth: &http.BasicAuth{ + Username: "test", + Password: "test", + }, + }) + require.NoError(err) } } diff --git a/scripts/start-gitea.sh b/scripts/start-gitea.sh index b150ec1..2a95099 100755 --- a/scripts/start-gitea.sh +++ b/scripts/start-gitea.sh @@ -17,3 +17,21 @@ sleep 10 echo "creating test repo..." curl -u 'test:test' -XPOST -H 'Content-Type: application/json' -d '{"name":"test"}' http://localhost:3000/api/v1/user/repos + +echo "creating no_ff_merge repo..." +curl -u 'test:test' -XPOST -H 'Content-Type: application/json' -d '{"name":"no_ff_merge"}' http://localhost:3000/api/v1/user/repos + +echo "populating no_ff_merge repo..." +tmpdir=$(mktemp -d 2>/dev/null || mktemp -d -t 'tmpdir') +cd $tmpdir +git init --initial-branch=master +git commit -m "feat: initial commit" --allow-empty +git tag v1.0.0 +git switch -C feature +sleep 1 +git commit -m "feat: feature" --allow-empty +git switch master +git merge --no-ff feature --no-edit +git push http://test:test@localhost:3000/test/no_ff_merge.git master --tags +cd - +rm -rf $tmpdir