Add url filter to repository search API endpoint (#2707)

Closes #1813

Signed-off-by: Sergio Castaño Arteaga <tegioz@icloud.com>
Signed-off-by: Cintia Sanchez Garcia <cynthiasg@icloud.com>
Co-authored-by: Sergio Castaño Arteaga <tegioz@icloud.com>
Co-authored-by: Cintia Sanchez Garcia <cynthiasg@icloud.com>
This commit is contained in:
Sergio Castaño Arteaga 2023-01-25 14:42:37 +01:00 committed by GitHub
parent 9adddfb43e
commit 3037ddcc6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 1 deletions

View File

@ -4,6 +4,7 @@ create or replace function search_repositories(p_input jsonb)
returns table(data json, total_count bigint) as $$
declare
v_name text := (p_input->>'name');
v_url text := nullif(p_input->>'url', '');
v_kinds int[];
v_users text[];
v_orgs text[];
@ -46,6 +47,8 @@ begin
left join organization o using (organization_id)
where
case when v_name is not null then r.name ~* v_name else true end
and
case when v_url is not null then trim(trailing '/' from r.url) = trim(trailing '/' from v_url) else true end
and
case when cardinality(v_kinds) > 0
then r.repository_kind_id = any(v_kinds) else true end

View File

@ -1,6 +1,6 @@
-- Start transaction and plan tests
begin;
select plan(9);
select plan(10);
-- Declare some variables
\set user1ID '00000000-0000-0000-0000-000000000001'
@ -362,6 +362,33 @@ select results_eq(
$$,
'Filtering by repo1 name including credentials, repository 1 returned'
);
select results_eq(
$$
select data::jsonb, total_count::integer from search_repositories('{
"url": "https://repo2.com"
}')
$$,
$$
values (
'[
{
"repository_id": "00000000-0000-0000-0000-000000000002",
"name": "repo2",
"display_name": "Repo 2",
"url": "https://repo2.com",
"kind": 0,
"verified_publisher": false,
"official": false,
"disabled": false,
"scanner_disabled": false,
"organization_name": "org1",
"organization_display_name": "Organization 1"
}
]'::jsonb,
1)
$$,
'Filtering by repo2 url, repository 2 returned'
);
-- Finish tests and rollback transaction
select * from finish();

View File

@ -614,6 +614,7 @@ paths:
- $ref: "#/components/parameters/UsersListParam"
- $ref: "#/components/parameters/OrgsListParam"
- $ref: "#/components/parameters/RepoNameQueryParam"
- $ref: "#/components/parameters/UrlQueryParam"
responses:
"200":
description: ""
@ -4846,6 +4847,14 @@ components:
by the PostgreSQL websearch_to_tsquery function. See
https://www.postgresql.org/docs/current/textsearch-controls.html
(12.3.2. Parsing Queries) for more details.
UrlQueryParam:
in: query
name: url
schema:
type: string
example: https://repo2.com
required: false
description: Repository url
UsersListParam:
in: query
name: user

View File

@ -219,6 +219,7 @@ func buildSearchInput(qs url.Values) (*hub.SearchRepositoryInput, error) {
return &hub.SearchRepositoryInput{
Name: qs.Get("name"),
URL: qs.Get("url"),
Kinds: kinds,
Orgs: qs["org"],
Users: qs["user"],

View File

@ -274,6 +274,7 @@ type RepositoryIgnoreEntry struct {
// SearchRepositoryInput represents the query input when searching for repositories.
type SearchRepositoryInput struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Kinds []RepositoryKind `json:"kinds,omitempty"`
Orgs []string `json:"orgs,omitempty"`
Users []string `json:"users,omitempty"`