From 168a419bbe596b1c49afcedccdba71f27a5ed4546bfaee185172624bab91f613 Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Wed, 3 Sep 2025 14:35:15 +0200 Subject: [PATCH] status: allow for package search endpoint OBS has issues searching for packages in scmsynced projects. Since we have a list of all the repositories, we can allow for a search endpoint here. /search?q=term1&q=term2... results is JSON [ project1/pkgA, project2/pkgB ] --- obs-status-service/main.go | 27 +++++++++++++++++++++++++++ obs-status-service/redis.go | 27 +++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/obs-status-service/main.go b/obs-status-service/main.go index 1407fd2..71c6c17 100644 --- a/obs-status-service/main.go +++ b/obs-status-service/main.go @@ -20,6 +20,7 @@ package main import ( "bytes" + "encoding/json" "flag" "fmt" "io" @@ -268,6 +269,32 @@ func main() { res.Write(BuildStatusSvg(nil, &common.PackageBuildStatus{Package: pkg, Code: "unknown"})) }) + http.HandleFunc("GET /search", func(res http.ResponseWriter, req *http.Request) { + common.LogInfo("GET /serach?" + req.URL.RawQuery) + queries := req.URL.Query() + if !queries.Has("q") { + res.WriteHeader(400) + return + } + + names := queries["q"] + if len(names) < 1 || len(names) > 10 { + res.WriteHeader(400) + return + } + + packages := FindPackages(names) + data, err := json.MarshalIndent(packages, "", " ") + if err != nil { + res.WriteHeader(500) + common.LogError("Error in marshalling data.", err) + return + } + + res.Write(data) + res.WriteHeader(200) + }) + http.HandleFunc("GET /buildlog/{Project}/{Package}/{Repository}/{Arch}", func(res http.ResponseWriter, req *http.Request) { prj := req.PathValue("Project") pkg := req.PathValue("Package") diff --git a/obs-status-service/redis.go b/obs-status-service/redis.go index f43c069..2b869a5 100644 --- a/obs-status-service/redis.go +++ b/obs-status-service/redis.go @@ -110,6 +110,33 @@ func FindRepoResults(project, repo string) []*common.BuildResult { return ret } +func FindPackages(search_terms []string) []string { + RepoStatusLock.RLock() + defer RepoStatusLock.RUnlock() + + data := make([]string, 0, 100) + for _, repo := range RepoStatus { + for _, status := range repo.Status { + pkg := status.Package + match := true + for _, term := range search_terms { + match = match && strings.Contains(pkg, term) + } + + if match { + entry := repo.Project + "/" + repo.Status[0].Package + if idx, found := slices.BinarySearch(data, entry); !found { + data = slices.Insert(data, idx, entry) + if len(data) >= 100 { + return data + } + } + } + } + } + return data +} + func FindAndUpdateProjectResults(project string) []*common.BuildResult { res := FindProjectResults(project) wg := &sync.WaitGroup{}