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

33
api.go
View File

@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@ -45,15 +46,15 @@ type MarkdownRequest struct {
Wiki bool Wiki bool
} }
// repos gets a list of Git repositories in this organisation. // reposPage gets a single page of the list of Git repositories in this organisation.
func (this *Application) repos(ctx context.Context) ([]Repo, error) { func (this *Application) reposPage(ctx context.Context, page, limit int) ([]Repo, error) {
err := this.apiSem.Acquire(ctx, 1) err := this.apiSem.Acquire(ctx, 1)
if err != nil { if err != nil {
return nil, err // e.g. ctx closed return nil, err // e.g. ctx closed
} }
defer this.apiSem.Release(1) 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 { if err != nil {
return nil, err return nil, err
} }
@ -77,6 +78,32 @@ func (this *Application) repos(ctx context.Context) ([]Repo, error) {
return repos, nil 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 // repoFile gets a single file from the default branch of the git repository
// Usually the default branch is `master`. // Usually the default branch is `master`.
func (this *Application) repoFile(ctx context.Context, repo, filename string) ([]byte, error) { func (this *Application) repoFile(ctx context.Context, repo, filename string) ([]byte, error) {