From 51cfe695bf86f8f2ef803d4ddc28e86612adf84a Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 8 Nov 2020 13:25:36 +1300 Subject: [PATCH] api: fix compatibility with newer gitea that paginates repo lists --- api.go | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/api.go b/api.go index 0bbdb7f..63b03db 100644 --- a/api.go +++ b/api.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "log" "net/http" "net/url" "strings" @@ -45,15 +46,15 @@ type MarkdownRequest struct { Wiki bool } -// repos gets a list of Git repositories in this organisation. -func (this *Application) repos(ctx context.Context) ([]Repo, error) { +// reposPage gets a single page of the list of Git repositories in this organisation. +func (this *Application) reposPage(ctx context.Context, page, limit int) ([]Repo, error) { err := this.apiSem.Acquire(ctx, 1) if err != nil { return nil, err // e.g. ctx closed } defer this.apiSem.Release(1) - req, err := http.NewRequestWithContext(ctx, http.MethodGet, this.cfg.Gitea.URL+`api/v1/orgs/`+url.PathEscape(this.cfg.Gitea.Org)+`/repos`, nil) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, this.cfg.Gitea.URL+`api/v1/orgs/`+url.PathEscape(this.cfg.Gitea.Org)+fmt.Sprintf(`/repos?page=%d&limit=%d`, page, limit), nil) if err != nil { return nil, err } @@ -77,6 +78,32 @@ func (this *Application) repos(ctx context.Context) ([]Repo, error) { return repos, nil } +// repos gets a list of Git repositories in this organisation. It may have to +// make multiple network requests. +func (this *Application) repos(ctx context.Context) ([]Repo, error) { + // Seems like gitea-1.13.0-rc1 returns 30 results by default, and supports up to a limit of 100 + // Make a much larger request + + ret := make([]Repo, 0) + + nextPage := 1 // Counting starts at 1 + for { + page, err := this.reposPage(ctx, nextPage, 300) + if err != nil { + return nil, err + } + + log.Printf("Page %d with %d results", nextPage, len(page)) + + if len(page) == 0 && len(ret) > 0 { + return ret, nil // Found enough already + } + + ret = append(ret, page...) + nextPage += 1 + } +} + // repoFile gets a single file from the default branch of the git repository // Usually the default branch is `master`. func (this *Application) repoFile(ctx context.Context, repo, filename string) ([]byte, error) {