api: fix nil pointer dereference when listing user repos (#8069)

Co-authored-by: Joe Chen <jc@unknwon.io>
This commit is contained in:
Mukaiu
2026-01-19 10:01:03 +08:00
committed by GitHub
parent e44284fada
commit 3ef71a43d9
2 changed files with 13 additions and 3 deletions

View File

@@ -21,6 +21,7 @@ All notable changes to Gogs are documented in this file.
- Submodules using `ssh://` protocol and a port number are not rendered correctly. [#4941](https://github.com/gogs/gogs/issues/4941)
- Missing link to user profile on the first commit in commits history page. [#7404](https://github.com/gogs/gogs/issues/7404)
- Route `GET /api/v1/user/repos` responses 500 when accessible repositories contain forks. [#8069](https://github.com/gogs/gogs/pull/8069)
## 0.13.3

View File

@@ -112,19 +112,28 @@ func listUserRepositories(c *context.APIContext, username string) {
return
}
accessibleRepos, err := database.Handle.Repositories().GetByCollaboratorIDWithAccessMode(c.Req.Context(), user.ID)
accessibleReposWithAccessMode, err := database.Handle.Repositories().GetByCollaboratorIDWithAccessMode(c.Req.Context(), user.ID)
if err != nil {
c.Error(err, "get repositories accesses by collaborator")
return
}
accessibleRepos := make([]*database.Repository, 0, len(accessibleReposWithAccessMode))
for repo := range accessibleReposWithAccessMode {
accessibleRepos = append(accessibleRepos, repo)
}
if err = database.RepositoryList(accessibleRepos).LoadAttributes(); err != nil {
c.Error(err, "load attributes for accessible repositories")
return
}
numOwnRepos := len(ownRepos)
repos := make([]*api.Repository, 0, numOwnRepos+len(accessibleRepos))
repos := make([]*api.Repository, 0, numOwnRepos+len(accessibleReposWithAccessMode))
for _, r := range ownRepos {
repos = append(repos, r.APIFormatLegacy(&api.Permission{Admin: true, Push: true, Pull: true}))
}
for repo, access := range accessibleRepos {
for repo, access := range accessibleReposWithAccessMode {
repos = append(repos,
repo.APIFormatLegacy(&api.Permission{
Admin: access >= database.AccessModeAdmin,