Skip to content

Commit

Permalink
New AQL query for build-info json
Browse files Browse the repository at this point in the history
  • Loading branch information
yahavi committed Nov 8, 2023
1 parent cdf63f6 commit 20c118e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
47 changes: 46 additions & 1 deletion artifactory/services/utils/aqlquerybuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@ package utils

import (
"fmt"
"golang.org/x/exp/slices"
"strconv"
"strings"

"golang.org/x/exp/slices"

"github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
)

var specialAqlCharacters = map[rune]string{
'/': "%2F",
'\\': "%5C",
'|': "%7C",
'*': "%2A",
'?': "%3F",
'\'': "%22",
':': "%3A",
';': "%3B",
'%': "%25",
}

// Returns an AQL body string to search file in Artifactory by pattern, according the specified arguments requirements.
func CreateAqlBodyForSpecWithPattern(params *CommonParams) (string, error) {
searchPattern := prepareSourceSearchPattern(params.Pattern, params.Target)
Expand Down Expand Up @@ -149,6 +162,38 @@ func CreateAqlQueryForPypi(repo, file string) string {
return fmt.Sprintf(itemsPart, repo, file, buildIncludeQueryPart([]string{"name", "repo", "path", "actual_md5", "actual_sha1", "sha256"}))
}

// noinspection GoUnusedExportedFunction
func CreateAqlQueryForBuildInfoJson(project, buildName, buildNumber, timestamp string) string {
if project == "" {
project = "artifactory"
} else {
project = encodeForBuildInfoRepository(project)
}
itemsPart :=
`items.find({
"repo": "%s",
"path": {
"$match": "%s"
},
"name": {
"$match": "%s-%s.json"
}
})%s`
return fmt.Sprintf(itemsPart, project+"-build-info", encodeForBuildInfoRepository(buildName), encodeForBuildInfoRepository(buildNumber), timestamp, buildIncludeQueryPart([]string{"name", "repo", "path", "actual_sha1", "actual_md5"}))
}

func encodeForBuildInfoRepository(value string) string {
results := ""
for _, char := range value {
if encoding, exist := specialAqlCharacters[char]; exist {
results += encoding
} else {
results += string(char)
}
}
return results
}

func CreateAqlQueryForLatestCreated(repo, path string) string {
itemsPart :=
`items.find({` +
Expand Down
28 changes: 28 additions & 0 deletions artifactory/services/utils/aqlquerybuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,31 @@ func TestBuildKeyValQueryPart(t *testing.T) {
})
}
}

var encodeForBuildInfoRepositoryProvider = []struct {
value string
expectedEncoding string
}{
// Shouldn't encode
{"", ""},
{"a", "a"},
{"a b", "a b"},
{"a.b", "a.b"},
{"a&b", "a&b"},

// Should encode
{"a/b", "a%2Fb"},
{"a\\b", "a%5Cb"},
{"a:b", "a%3Ab"},
{"a|b", "a%7Cb"},
{"a*b", "a%2Ab"},
{"a?b", "a%3Fb"},
}

func TestEncodeForBuildInfoRepository(t *testing.T) {
for _, testCase := range encodeForBuildInfoRepositoryProvider {
t.Run(testCase.value, func(t *testing.T) {
assert.Equal(t, testCase.expectedEncoding, encodeForBuildInfoRepository(testCase.value))
})
}
}

0 comments on commit 20c118e

Please sign in to comment.