Skip to content

Commit 61b08b5

Browse files
authored
bug fixed for API to get user's repos (#1622)
* bug fixed for API to get user's repos * add tests and fix another place * test user2 since user1 has no repos
1 parent cebe3a6 commit 61b08b5

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

integrations/api_repo_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"net/http"
9+
"testing"
10+
11+
"code.gitea.io/gitea/models"
12+
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestAPIUserReposNotLogin(t *testing.T) {
17+
assert.NoError(t, models.LoadFixtures())
18+
19+
req, err := http.NewRequest("GET", "/api/v1/users/user2/repos", nil)
20+
assert.NoError(t, err)
21+
resp := MakeRequest(req)
22+
assert.EqualValues(t, http.StatusOK, resp.HeaderCode)
23+
}

routers/api/v1/user/repo.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@ import (
99
// listUserRepos - List the repositories owned by the given user.
1010
func listUserRepos(ctx *context.APIContext, u *models.User) {
1111
userID := u.ID
12-
showPrivateRepos := (ctx.User.ID == userID || ctx.User.IsAdmin) && ctx.IsSigned
12+
showPrivateRepos := ctx.IsSigned && (ctx.User.ID == userID || ctx.User.IsAdmin)
1313
ownRepos, err := models.GetUserRepositories(userID, showPrivateRepos, 1, u.NumRepos, "")
1414
if err != nil {
1515
ctx.Error(500, "GetUserRepositories", err)
1616
return
1717
}
18-
accessibleRepos, err := getAccessibleRepos(ctx)
19-
if err != nil {
20-
ctx.Error(500, "GetAccessibleRepos", err)
18+
var accessibleRepos []*api.Repository
19+
if ctx.User != nil {
20+
accessibleRepos, err = getAccessibleRepos(ctx)
21+
if err != nil {
22+
ctx.Error(500, "GetAccessibleRepos", err)
23+
}
2124
}
2225
apiRepos := make([]*api.Repository, len(ownRepos)+len(accessibleRepos))
2326
// Set owned repositories.

0 commit comments

Comments
 (0)