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

feat: allow retrieval of commits for tags #675

Merged
merged 1 commit into from
Oct 17, 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
13 changes: 11 additions & 2 deletions testhelper/gittest/gittest.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,17 @@ func (s *Server) GetServerCA() []byte {
return getServerCA(s.Server)
}

func (s *Server) GetLatestCommitHash(t testing.TB, branch string) string {
cmd := exec.Command("git", "-c", "http.sslVerify=false", "ls-remote", s.URL, fmt.Sprintf("refs/heads/%s", branch))
func (s *Server) GetLatestCommitHash(t testing.TB, referenceType, ref string) string {
var refType string
switch referenceType {
case "branch":
refType = "heads"
case "tag":
refType = "tags"
default:
require.Fail(t, "invalid refType")
}
cmd := exec.Command("git", "-c", "http.sslVerify=false", "ls-remote", s.URL, fmt.Sprintf("refs/%s/%s", refType, ref))
out, err := cmd.Output()
require.NoError(t, err, "should be able to run the ls-remote command")
commitHash := strings.Split(string(out), "\t")[0]
Expand Down
12 changes: 10 additions & 2 deletions testhelper/gittest/gittest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestGitServer(t *testing.T) {
require.NoError(t, err, "should be able to get the HEAD commit")
require.NotEmpty(t, out, "HEAD commit should not be empty")

latestCommit := s.GetLatestCommitHash(t, "develop")
latestCommit := s.GetLatestCommitHash(t, "branch", "develop")
require.Equal(t, strings.TrimSpace(out), latestCommit, "HEAD commit should match the latest commit")
})

Expand All @@ -79,7 +79,15 @@ func TestGitServer(t *testing.T) {
require.NoError(t, err, "should be able to get the HEAD commit")
require.NotEmpty(t, out, "HEAD commit should not be empty")

latestCommit := s.GetLatestCommitHash(t, "main")
out, err = execCmd("git", "-C", tempDir, "tag", "-a", "v1.0.0", "-m", "tag v1.0.0")
require.NoErrorf(t, err, "should be able to create a new tag: %s", out)
out, err = execCmd("git", "-c", "http.sslVerify=false", "-C", tempDir, "push", "origin", "v1.0.0")
require.NoErrorf(t, err, "should be able to push the new tag: %s", out)

out, err = execCmd("git", "-C", tempDir, "rev-parse", "v1.0.0")
require.NoError(t, err, "should be able to get the HEAD commit")
latestCommit := s.GetLatestCommitHash(t, "tag", "v1.0.0")

require.Equal(t, strings.TrimSpace(out), latestCommit, "HEAD commit should match the latest commit")
})
}
Expand Down
Loading