Fix branch loading (#1249)

Fixes a panic I got while viewing the branches in the UI.

```
runtime error: index out of range [0] with length 0
/home/gitpod/go/src/runtime/panic.go:113 (0x44053e)
        goPanicIndex: panic(boundsError{x: int64(x), signed: true, y: y, code: boundsIndex})
/workspace/woodpecker/server/remote/gitea/gitea.go:444 (0xd36764)
        (*Gitea).Branches.func1: result[i] = branches[i].Name
/workspace/woodpecker/server/remote/common/utils.go:34 (0xd3942a)
        Paginate[...]: batch, err := get(page)
/workspace/woodpecker/server/remote/gitea/gitea.go:439 (0xd36665)
        (*Gitea).Branches: branches, err := common.Paginate(func(page int) ([]string, error) {
/workspace/woodpecker/server/api/repo.go:202 (0xc823a1)
        GetRepoBranches: branches, err := r.Branches(c, user, repo)
/workspace/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/context.go:173 (0xc71d44)
        (*Context).Next: c.handlers[c.index](c)
/workspace/woodpecker/server/router/middleware/session/repo.go:148 (0xc71bba)
        MustPull: c.Next()
/workspace/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/context.go:173 (0xc71aee)
        (*Context).Next: c.handlers[c.index](c)
/workspace/woodpecker/server/router/middleware/session/repo.go:139 (0xc71aaa)
        SetPerm.func1: c.Next()
/workspace/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/context.go:173 (0xc71251)
        (*Context).Next: c.handlers[c.index](c)
/workspace/woodpecker/server/router/middleware/session/repo.go:53 (0xc710ec)
        SetRepo.func1: c.Next()
/workspace/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/context.go:173 (0xccb50f)
        (*Context).Next: c.handlers[c.index](c)
/workspace/woodpecker/server/router/middleware/token/token.go:50 (0xccb248)
        Refresh: c.Next()
/workspace/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/context.go:173 (0xc721fc)
        (*Context).Next: c.handlers[c.index](c)
/workspace/woodpecker/server/router/middleware/session/user.go:72 (0xc721e3)
        SetUser.func1: c.Next()
/workspace/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/context.go:173 (0xcd069e)
        (*Context).Next: c.handlers[c.index](c)
/workspace/woodpecker/server/router/middleware/store.go:29 (0xcd0685)
        Store.func1: c.Next()
/workspace/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/context.go:173 (0xccff47)
        (*Context).Next: c.handlers[c.index](c)
/workspace/woodpecker/server/router/middleware/logger.go:23 (0xccff2a)
        Logger.func1: c.Next()
/workspace/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/context.go:173 (0xccaf99)
        (*Context).Next: c.handlers[c.index](c)
/workspace/woodpecker/server/router/middleware/header/header.go:38 (0xccaec4)
        Options: c.Next()
/workspace/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/context.go:173 (0xccae21)
        (*Context).Next: c.handlers[c.index](c)
/workspace/woodpecker/server/router/middleware/header/header.go:30 (0xccae06)
        NoCache: c.Next()
/workspace/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/context.go:173 (0xccf32a)
        (*Context).Next: c.handlers[c.index](c)
/workspace/woodpecker/server/router/router.go:39 (0xccf310)
        Load.func1: c.Next()
/workspace/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/context.go:173 (0xa2e0c1)
        (*Context).Next: c.handlers[c.index](c)
/workspace/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/recovery.go:101 (0xa2e0ac)
        CustomRecoveryWithWriter.func1: c.Next()
/workspace/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/context.go:173 (0xa2cf30)
        (*Context).Next: c.handlers[c.index](c)
/workspace/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/gin.go:616 (0xa2cb98)
        (*Engine).handleHTTPRequest: c.Next()
/workspace/go/pkg/mod/github.com/gin-gonic/gin@v1.8.1/gin.go:572 (0xa2c6dc)
        (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/home/gitpod/go/src/net/http/server.go:2947 (0x79cc4b)
        serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/home/gitpod/go/src/net/http/server.go:1991 (0x797e66)
        (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/home/gitpod/go/src/runtime/asm_amd64.s:1594 (0x476d80)
        goexit: BYTE    $0x90   // NOP
```

Co-authored-by: Anbraten <anton@ju60.de>
This commit is contained in:
Lukas 2022-10-09 21:10:36 +02:00 committed by GitHub
parent b3c816abbd
commit 12ff74c4cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -439,7 +439,7 @@ func (c *Gitea) Branches(ctx context.Context, u *model.User, r *model.Repo) ([]s
branches, err := common.Paginate(func(page int) ([]string, error) {
branches, _, err := client.ListRepoBranches(r.Owner, r.Name,
gitea.ListRepoBranchesOptions{ListOptions: gitea.ListOptions{Page: page}})
result := make([]string, 0, len(branches))
result := make([]string, len(branches))
for i := range branches {
result[i] = branches[i].Name
}