From f01a3c4b3f1bbc70337807f274cfde411099314a Mon Sep 17 00:00:00 2001 From: Philipp Heuer Date: Sat, 25 Mar 2023 00:35:26 +0100 Subject: [PATCH] fix: issue with tags in ParseGitRefLogLine --- pkg/vcsrepository/git/client_git.go | 29 ++++++++++++++---------- pkg/vcsrepository/git/client_git_test.go | 7 ++++++ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/pkg/vcsrepository/git/client_git.go b/pkg/vcsrepository/git/client_git.go index 5ce457e..74808cf 100644 --- a/pkg/vcsrepository/git/client_git.go +++ b/pkg/vcsrepository/git/client_git.go @@ -124,23 +124,28 @@ func (c GitClient) VCSHead() (vcsHead vcsapi.VCSRef, err error) { // detached HEAD, check git reflog for the true reference gitRefLogFile := filepath.Join(c.dir, ".git", "logs", "HEAD") lastLine := readLastLine(gitRefLogFile) - - pattern := regexp.MustCompile(`.*checkout: moving from (?P.*) to (?P.*)$`) - match := pattern.FindStringSubmatch(lastLine) - - if strings.HasPrefix(match[2], "refs/remotes/pull") { - // handle github merge request as virtual branch - return vcsapi.VCSRef{Type: "branch", Value: match[2][13:], Hash: ref.Hash().String()}, nil - } else if len(match[2]) == 40 { - return vcsapi.VCSRef{Type: "branch", Value: match[1], Hash: ref.Hash().String()}, nil - } else { - return vcsapi.VCSRef{Type: "tag", Value: match[2], Hash: ref.Hash().String()}, nil - } + return ParseGitRefLogLine(lastLine, ref.Hash().String()), nil } return vcsapi.VCSRef{}, errors.New("can't determinate repo head") } +func ParseGitRefLogLine(line string, hash string) vcsapi.VCSRef { + pattern := regexp.MustCompile(`.*checkout: moving from (?P.*) to (?P.*)$`) + match := pattern.FindStringSubmatch(line) + + if strings.HasPrefix(match[2], "refs/remotes/pull") { + // handle github merge request as virtual branch + return vcsapi.VCSRef{Type: "branch", Value: match[2][13:], Hash: hash} + } else if len(match[2]) == 40 { + return vcsapi.VCSRef{Type: "branch", Value: match[1], Hash: hash} + } else if strings.HasPrefix(match[2], "refs/tags/") { + return vcsapi.VCSRef{Type: "tag", Value: match[2][10:], Hash: hash} + } else { + return vcsapi.VCSRef{Type: "tag", Value: match[2], Hash: hash} + } +} + func (c GitClient) GetTags() []vcsapi.VCSRef { if c.tags == nil { var tags []vcsapi.VCSRef diff --git a/pkg/vcsrepository/git/client_git_test.go b/pkg/vcsrepository/git/client_git_test.go index 2d669f0..9d320ce 100644 --- a/pkg/vcsrepository/git/client_git_test.go +++ b/pkg/vcsrepository/git/client_git_test.go @@ -135,3 +135,10 @@ func TestFindLatestGitReleaseFromCommit(t *testing.T) { assert.Regexp(t, "v[0-9]+.[0-9]+.[0-9]+", release.Value) assert.Regexp(t, "[0-9]+.[0-9]+.[0-9]+", release.Version) } + +func TestParseGitRefLogLine_Tag(t *testing.T) { + vcsRef := ParseGitRefLogLine("0000000000000000000000000000000000000000 1cafbbdb80ce27304ac92a9e2fde6c3df8119a19 runner 1679700466 +0000\tcheckout: moving from master to refs/tags/v2.0.0-alpha.1", "1cafbbdb80ce27304ac92a9e2fde6c3df8119a19") + assert.Equal(t, "1cafbbdb80ce27304ac92a9e2fde6c3df8119a19", vcsRef.Hash) + assert.Equal(t, "tag", vcsRef.Type) + assert.Equal(t, "v2.0.0-alpha.1", vcsRef.Value) +}