8356
Wx
www-xonecas-io / files
www-xonecas-io / internal / store / meta.go
2.0 KB
1package store 2 3import ( 4 "encoding/json" 5 "fmt" 6 "net/http" 7 "strings" 8) 9 10// Meta is what a host says about a remote, fetched at import time. 11type Meta struct { 12 Fork bool `json:"fork"` 13 Description string `json:"description"` 14} 15 16// FetchMeta lists the public repos of every GitHub and GitLab owner in urls, keyed by clone URL. Other sources have none. 17// That is one request per owner, well inside GitHub's 60 unauthenticated requests an hour. 18func FetchMeta(urls []string) (map[string]Meta, error) { 19 owners := map[string]bool{} 20 for _, u := range urls { 21 host, path, _ := strings.Cut(strings.TrimPrefix(u, "https://"), "/") 22 owner, _, _ := strings.Cut(path, "/") 23 owners[host+"/"+owner] = true 24 } 25 metas := map[string]Meta{} 26 for o := range owners { 27 host, owner, _ := strings.Cut(o, "/") 28 api := ownerAPI(host, owner) 29 if api == "" { 30 continue // a local repo or another host: the README describes it 31 } 32 var rows []struct { 33 CloneURL string `json:"clone_url"` 34 HTTPURL string `json:"http_url_to_repo"` 35 Fork bool `json:"fork"` 36 ForkedFrom any `json:"forked_from_project"` 37 Description string `json:"description"` 38 } 39 resp, err := http.Get(api) 40 if err != nil { 41 return nil, err 42 } 43 err = json.NewDecoder(resp.Body).Decode(&rows) 44 resp.Body.Close() 45 if err != nil || resp.StatusCode != 200 { 46 return nil, fmt.Errorf("%s: %s %v", api, resp.Status, err) 47 } 48 if len(rows) == 100 { 49 return nil, fmt.Errorf("%s: 100 repos, the list needs paging", api) 50 } 51 for _, r := range rows { 52 metas[r.CloneURL+r.HTTPURL] = Meta{r.Fork || r.ForkedFrom != nil, strings.TrimSpace(r.Description)} 53 } 54 } 55 return metas, nil 56} 57 58// ownerAPI is the host's URL for owner's repo list, or "" for a host without one. 59func ownerAPI(host, owner string) string { 60 return map[string]string{ 61 "github.com": "https://api.github.com/users/" + owner + "/repos?per_page=100&type=owner", 62 "gitlab.com": "https://gitlab.com/api/v4/users/" + owner + "/projects?per_page=100", 63 }[host] 64}