api: fix compatibility with newer gitea that paginates repo lists

This commit is contained in:
mappu 2020-11-08 13:25:36 +13:00
parent e34ab1fd16
commit 51cfe695bf
1 changed files with 30 additions and 3 deletions

33
api.go
View File

@ -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) {