From 3037ddcc6f0e2403dc93ee6ab43c8b26a19b51e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Casta=C3=B1o=20Arteaga?= Date: Wed, 25 Jan 2023 14:42:37 +0100 Subject: [PATCH] Add url filter to repository search API endpoint (#2707) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #1813 Signed-off-by: Sergio Castaño Arteaga Signed-off-by: Cintia Sanchez Garcia Co-authored-by: Sergio Castaño Arteaga Co-authored-by: Cintia Sanchez Garcia --- .../repositories/search_repositories.sql | 3 ++ .../repositories/search_repositories.sql | 29 ++++++++++++++++++- docs/api/openapi.yaml | 9 ++++++ internal/handlers/repo/handlers.go | 1 + internal/hub/repo.go | 1 + 5 files changed, 42 insertions(+), 1 deletion(-) diff --git a/database/migrations/functions/repositories/search_repositories.sql b/database/migrations/functions/repositories/search_repositories.sql index 91d73cb0..71cb83e2 100644 --- a/database/migrations/functions/repositories/search_repositories.sql +++ b/database/migrations/functions/repositories/search_repositories.sql @@ -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 diff --git a/database/tests/functions/repositories/search_repositories.sql b/database/tests/functions/repositories/search_repositories.sql index fffb2590..a04f764f 100644 --- a/database/tests/functions/repositories/search_repositories.sql +++ b/database/tests/functions/repositories/search_repositories.sql @@ -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(); diff --git a/docs/api/openapi.yaml b/docs/api/openapi.yaml index 9d46fe9e..88d109f7 100644 --- a/docs/api/openapi.yaml +++ b/docs/api/openapi.yaml @@ -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 diff --git a/internal/handlers/repo/handlers.go b/internal/handlers/repo/handlers.go index de877950..87a69190 100644 --- a/internal/handlers/repo/handlers.go +++ b/internal/handlers/repo/handlers.go @@ -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"], diff --git a/internal/hub/repo.go b/internal/hub/repo.go index 5d1c98ed..6113880c 100644 --- a/internal/hub/repo.go +++ b/internal/hub/repo.go @@ -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"`