diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 3c2a877930..e794e05f1f 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -13,6 +13,7 @@ import ( "os" "path" "path/filepath" + "regexp" "strings" "time" @@ -501,6 +502,13 @@ func getBucketSearchNextPageParam(jsonParsed *gabs.Container) string { return "&pageToken=" + nextPageToken } +// IsCommit returns true if the string matches commit format +func IsCommit(s string) bool { + re := regexp.MustCompile(`\b[0-9a-f]{5,40}\b`) + + return re.MatchString(s) +} + func processBucketSearchPage(jsonParsed *gabs.Container, currentPage int, bucket string, prefix string, object string) (string, error) { items := jsonParsed.Path("items").Children() diff --git a/internal/utils/utils_test.go b/internal/utils/utils_test.go index 9516ef75d5..e897ebbba4 100644 --- a/internal/utils/utils_test.go +++ b/internal/utils/utils_test.go @@ -313,6 +313,22 @@ func TestGetGCPBucketCoordinates_Snapshots(t *testing.T) { }) } +func TestIsCommit(t *testing.T) { + t.Run("Returns true with commits", func(t *testing.T) { + assert.True(t, IsCommit("abcdef1234")) + assert.True(t, IsCommit("a12345")) + assert.True(t, IsCommit("abcdef1")) + }) + + t.Run("Returns false with non-commits", func(t *testing.T) { + assert.False(t, IsCommit("master")) + assert.False(t, IsCommit("7.x")) + assert.False(t, IsCommit("7.12.x")) + assert.False(t, IsCommit("7.11.x")) + assert.False(t, IsCommit("pr12345")) + }) +} + func TestProcessBucketSearchPage_CommitFound(t *testing.T) { // retrieving last element in commits.json object := "024b732844d40bdb2bf806480af2b03fcb8fbdbe/elastic-agent/elastic-agent-8.0.0-SNAPSHOT-darwin-x86_64.tar.gz"