api: begin conversion to gitea SDK

This commit is contained in:
mappu 2020-11-19 10:17:10 +13:00
parent dbb915a226
commit e4fa4386da
4 changed files with 32 additions and 50 deletions

54
api.go
View File

@ -9,15 +9,9 @@ import (
"net/http"
"net/url"
"strings"
"time"
)
type Repo struct {
Name string `json:"name"`
Description string `json:"description"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
"code.gitea.io/sdk/gitea"
)
type ContentsResponse struct {
Content []byte `json:"content"` // Assume base64 "encoding" parameter in Gitea response, and use Go's auto decode
@ -46,30 +40,14 @@ type MarkdownRequest struct {
}
// 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) {
func (this *Application) reposPage(ctx context.Context, page, limit int) ([]*gitea.Repository, 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)+fmt.Sprintf(`/repos?page=%d&limit=%d`, page, limit), nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("HTTP %d", resp.StatusCode)
}
var repos []Repo
err = json.NewDecoder(resp.Body).Decode(&repos)
repos, _, err := this.gc.ListOrgRepos(this.cfg.Gitea.Org, gitea.ListOrgReposOptions{ListOptions: gitea.ListOptions{Page: page, PageSize: limit}})
if err != nil {
return nil, err
}
@ -79,11 +57,11 @@ func (this *Application) reposPage(ctx context.Context, page, limit int) ([]Repo
// 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) {
func (this *Application) repos(ctx context.Context) ([]*gitea.Repository, 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)
ret := make([]*gitea.Repository, 0)
nextPage := 1 // Counting starts at 1
for {
@ -110,28 +88,12 @@ func (this *Application) repoFile(ctx context.Context, repo, filename string) ([
}
defer this.apiSem.Release(1)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, this.cfg.Gitea.URL+`api/v1/repos/`+url.PathEscape(this.cfg.Gitea.Org)+`/`+url.PathEscape(repo)+`/contents/`+url.PathEscape(filename), nil)
resp, _, err := this.gc.GetContents(this.cfg.Gitea.Org, repo, "", filename)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("HTTP %d", resp.StatusCode)
}
var cr ContentsResponse
err = json.NewDecoder(resp.Body).Decode(&cr)
if err != nil {
return nil, err
}
return cr.Content, nil
return []byte(*resp.Content), nil
}
func (this *Application) filesInDirectory(ctx context.Context, repo, dir string) ([]ReaddirEntry, error) {

1
go.mod
View File

@ -3,6 +3,7 @@ module teafolio
go 1.13
require (
code.gitea.io/sdk/gitea v0.13.1
github.com/BurntSushi/toml v0.3.1
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a
)

11
go.sum
View File

@ -1,4 +1,15 @@
code.gitea.io/sdk v0.11.0 h1:R3VdjBCxObyLKnv4Svd/TM6oGsXzN8JORbzgkEFb83w=
code.gitea.io/sdk/gitea v0.13.1 h1:Y7bpH2iO6Q0KhhMJfjP/LZ0AmiYITeRQlCD8b0oYqhk=
code.gitea.io/sdk/gitea v0.13.1/go.mod h1:z3uwDV/b9Ls47NGukYM9XhnHtqPh/J+t40lsUrR6JDY=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/hashicorp/go-version v1.2.0 h1:3vNe/fWF5CBgRIguda1meWhsZHy3m8gCJ5wx+dIzX/E=
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a h1:WXEvlFVvvGxCJLG6REjsT03iWnKLEWinaScsxF2Vm2o=
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

16
main.go
View File

@ -13,6 +13,7 @@ import (
"sort"
"strings"
"code.gitea.io/sdk/gitea"
"github.com/BurntSushi/toml"
"golang.org/x/sync/semaphore"
)
@ -36,6 +37,7 @@ type Application struct {
rxRepoPage, rxRepoImage *regexp.Regexp
apiSem *semaphore.Weighted
gc *gitea.Client
}
func (this *Application) Templatepage(w http.ResponseWriter, r *http.Request, pageDesc, extraHead string, cb func()) {
@ -106,7 +108,7 @@ func (this *Application) Homepage(w http.ResponseWriter, r *http.Request) {
// But then make sure the final sort is by most-recently-created
sort.Slice(repos, func(i, j int) bool {
return repos[i].CreatedAt.After(repos[j].CreatedAt)
return repos[i].Created.After(repos[j].Created)
})
// Ready for template
@ -151,9 +153,9 @@ func (this *Application) Homepage(w http.ResponseWriter, r *http.Request) {
<tr
class="`+html.EscapeString(rowClass)+`"
data-sort-al="`+fmt.Sprintf("-%d", alphabeticalOrderIndexes[repo.Name])+`"
data-sort-ls="`+fmt.Sprintf("%.0f", repo.UpdatedAt.Sub(repo.CreatedAt).Seconds())+`"
data-sort-ct="`+fmt.Sprintf("%d", repo.CreatedAt.Unix())+`"
data-sort-mt="`+fmt.Sprintf("%d", repo.UpdatedAt.Unix())+`"
data-sort-ls="`+fmt.Sprintf("%.0f", repo.Updated.Sub(repo.Created).Seconds())+`"
data-sort-ct="`+fmt.Sprintf("%d", repo.Created.Unix())+`"
data-sort-mt="`+fmt.Sprintf("%d", repo.Updated.Unix())+`"
>
<td>
<a href="`+pageHref+`"><img class="homeimage" loading="lazy" src="`+html.EscapeString(`/:banner/`+url.PathEscape(repo.Name))+`"></div></a>
@ -397,5 +399,11 @@ func main() {
app.apiSem = semaphore.NewWeighted(app.cfg.Gitea.MaxConnections)
}
gc, err := gitea.NewClient(app.cfg.Gitea.URL)
if err != nil {
log.Fatalf("gitea.NewClient: %s", err.Error())
}
app.gc = gc
log.Fatal(http.ListenAndServe(app.cfg.BindTo, &app))
}