From b3f7ab657e03b3a7454b6d4481521c52e201b2c9 Mon Sep 17 00:00:00 2001 From: Michelle Shepardson Date: Fri, 8 Nov 2024 03:13:25 +0000 Subject: [PATCH] Allow label/target search for ResultStore sources. Instead of requiring `label:"prow"` in queries, allow any number of label or target atoms in a query. (This still limits the queries we accept, but it should work for most simple uses of ResultStore sources). Test: Unit tests, and verified with a local instance of TestGrid that a ResultStore source config will pick up results from ResultStore correctly. --- pkg/updater/resultstore/query/query.go | 71 +++++--- pkg/updater/resultstore/query/query_test.go | 176 ++++++++++++-------- pkg/updater/resultstore/resultstore.go | 29 +--- pkg/updater/resultstore/resultstore_test.go | 82 ++------- 4 files changed, 185 insertions(+), 173 deletions(-) diff --git a/pkg/updater/resultstore/query/query.go b/pkg/updater/resultstore/query/query.go index 3bb941bb..db524177 100644 --- a/pkg/updater/resultstore/query/query.go +++ b/pkg/updater/resultstore/query/query.go @@ -17,47 +17,76 @@ limitations under the License. package query import ( + "errors" "fmt" "regexp" "strings" ) -func translateAtom(simpleAtom string) (string, error) { - if simpleAtom == "" { - return "", nil +type keyValue struct { + key string + value string +} + +func translateAtom(simpleAtom keyValue, queryTarget bool) (string, error) { + if simpleAtom.key == "" { + return "", errors.New("missing key") } - // For now, we expect an atom with the exact form `target:""` - // Split the `key:value` atom. - parts := strings.SplitN(simpleAtom, ":", 2) - if len(parts) != 2 { - return "", fmt.Errorf("unrecognized atom %q", simpleAtom) + if simpleAtom.value == "" { + return "", errors.New("missing value") } - key := strings.TrimSpace(parts[0]) - val := strings.Trim(strings.TrimSpace(parts[1]), `"`) - switch { - case key == "target": - return fmt.Sprintf(`id.target_id="%s"`, val), nil + case simpleAtom.key == "label" && queryTarget: + return fmt.Sprintf(`invocation.invocation_attributes.labels:"%s"`, simpleAtom.value), nil + case simpleAtom.key == "label": + return fmt.Sprintf(`invocation_attributes.labels:"%s"`, simpleAtom.value), nil + case simpleAtom.key == "target": + return fmt.Sprintf(`id.target_id="%s"`, simpleAtom.value), nil default: - return "", fmt.Errorf("unrecognized atom key %q", key) + return "", fmt.Errorf("unknown type of atom %q", simpleAtom.key) } } var ( - queryRe = regexp.MustCompile(`^target:".*"$`) + // Captures any atoms of the form `label:"