-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1274 from michelle192837/query
Add ability to query a single job from Prow in ResultStore.
- Loading branch information
Showing
7 changed files
with
680 additions
and
357 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
Copyright 2024 The TestGrid Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package resultstore | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
func translateAtom(simpleAtom string) (string, error) { | ||
if simpleAtom == "" { | ||
return "", nil | ||
} | ||
// For now, we expect an atom with the exact form `target:"<target>"` | ||
// Split the `key:value` atom. | ||
parts := strings.SplitN(simpleAtom, ":", 2) | ||
if len(parts) != 2 { | ||
return "", fmt.Errorf("unrecognized atom %q", simpleAtom) | ||
} | ||
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 | ||
default: | ||
return "", fmt.Errorf("unrecognized atom key %q", key) | ||
} | ||
} | ||
|
||
var ( | ||
queryRe = regexp.MustCompile(`^target:".*"$`) | ||
) | ||
|
||
func translateQuery(simpleQuery string) (string, error) { | ||
if simpleQuery == "" { | ||
return "", nil | ||
} | ||
// For now, we expect a query with a single atom, with the exact form `target:"<target>"` | ||
if !queryRe.MatchString(simpleQuery) { | ||
return "", fmt.Errorf("invalid query %q: must match %q", simpleQuery, queryRe.String()) | ||
} | ||
query, err := translateAtom(simpleQuery) | ||
if err != nil { | ||
return "", fmt.Errorf("invalid query %q: %v", simpleQuery, err) | ||
} | ||
return query, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
Copyright 2024 The TestGrid Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package resultstore | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestTranslateAtom(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
atom string | ||
want string | ||
wantError bool | ||
}{ | ||
{ | ||
name: "empty", | ||
atom: "", | ||
want: "", | ||
}, | ||
{ | ||
name: "basic", | ||
atom: `target:"//my-target"`, | ||
want: `id.target_id="//my-target"`, | ||
}, | ||
{ | ||
name: "case-sensitive key", | ||
atom: `TARGET:"//MY-TARGET"`, | ||
wantError: true, | ||
}, | ||
{ | ||
name: "multiple colons", | ||
atom: `target:"//path/to:my-target"`, | ||
want: `id.target_id="//path/to:my-target"`, | ||
}, | ||
{ | ||
name: "unquoted", | ||
atom: `target://my-target`, | ||
want: `id.target_id="//my-target"`, | ||
}, | ||
{ | ||
name: "partial quotes", | ||
atom: `target://my-target"`, | ||
want: `id.target_id="//my-target"`, | ||
}, | ||
{ | ||
name: "not enough parts", | ||
atom: "target", | ||
wantError: true, | ||
}, | ||
{ | ||
name: "unknown atom", | ||
atom: "label:foo", | ||
wantError: true, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
got, err := translateAtom(tc.atom) | ||
if tc.want != got { | ||
t.Errorf("translateAtom(%q) differed; got %q, want %q", tc.atom, got, tc.want) | ||
} | ||
if err == nil && tc.wantError { | ||
t.Errorf("translateAtom(%q) did not error as expected", tc.atom) | ||
} else if err != nil && !tc.wantError { | ||
t.Errorf("translateAtom(%q) errored unexpectedly: %v", tc.atom, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestTranslateQuery(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
query string | ||
want string | ||
wantError bool | ||
}{ | ||
{ | ||
name: "empty", | ||
query: "", | ||
want: "", | ||
}, | ||
{ | ||
name: "basic", | ||
query: `target:"//my-target"`, | ||
want: `id.target_id="//my-target"`, | ||
}, | ||
{ | ||
name: "case-sensitive key", | ||
query: `TARGET:"//MY-TARGET"`, | ||
wantError: true, | ||
}, | ||
{ | ||
name: "multiple colons", | ||
query: `target:"//path/to:my-target"`, | ||
want: `id.target_id="//path/to:my-target"`, | ||
}, | ||
{ | ||
name: "unquoted", | ||
query: `target://my-target`, | ||
wantError: true, | ||
}, | ||
{ | ||
name: "partial quotes", | ||
query: `target://my-target"`, | ||
wantError: true, | ||
}, | ||
{ | ||
name: "invalid query", | ||
query: `label:foo`, | ||
wantError: true, | ||
}, | ||
{ | ||
name: "partial match", | ||
query: `some_target:foo`, | ||
wantError: true, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
got, err := translateQuery(tc.query) | ||
if tc.want != got { | ||
t.Errorf("translateQuery(%q) differed; got %q, want %q", tc.query, got, tc.want) | ||
} | ||
if tc.wantError && err == nil { | ||
t.Errorf("translateQuery(%q) did not error as expected", tc.query) | ||
} else if !tc.wantError && err != nil { | ||
t.Errorf("translateQuery(%q) errored unexpectedly: %v", tc.query, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.