Fix GET /users/:username/repos endpoint (#2125)
This commit is contained in:
parent
c016d48735
commit
8f1d62ad3b
2 changed files with 63 additions and 34 deletions
|
@ -6,19 +6,48 @@ package integrations
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/models"
|
||||||
|
api "code.gitea.io/sdk/gitea"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAPIUserReposNotLogin(t *testing.T) {
|
func TestAPIUserReposNotLogin(t *testing.T) {
|
||||||
prepareTestEnv(t)
|
prepareTestEnv(t)
|
||||||
|
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User)
|
||||||
|
|
||||||
req := NewRequest(t, "GET", "/api/v1/users/user2/repos")
|
req := NewRequestf(t, "GET", "/api/v1/users/%s/repos", user.Name)
|
||||||
MakeRequest(t, req, http.StatusOK)
|
resp := MakeRequest(t, req, http.StatusOK)
|
||||||
|
|
||||||
|
var apiRepos []api.Repository
|
||||||
|
DecodeJSON(t, resp, &apiRepos)
|
||||||
|
expectedLen := models.GetCount(t, models.Repository{OwnerID: user.ID},
|
||||||
|
models.Cond("is_private = ?", false))
|
||||||
|
assert.Len(t, apiRepos, expectedLen)
|
||||||
|
for _, repo := range apiRepos {
|
||||||
|
assert.EqualValues(t, user.ID, repo.Owner.ID)
|
||||||
|
assert.False(t, repo.Private)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type searchResponseBody struct {
|
||||||
|
ok bool
|
||||||
|
data []api.Repository
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAPISearchRepoNotLogin(t *testing.T) {
|
func TestAPISearchRepoNotLogin(t *testing.T) {
|
||||||
prepareTestEnv(t)
|
prepareTestEnv(t)
|
||||||
|
const keyword = "test"
|
||||||
|
|
||||||
req := NewRequest(t, "GET", "/api/v1/repos/search?q=Test")
|
req := NewRequestf(t, "GET", "/api/v1/repos/search?q=%s", keyword)
|
||||||
MakeRequest(t, req, http.StatusOK)
|
resp := MakeRequest(t, req, http.StatusOK)
|
||||||
|
|
||||||
|
var body searchResponseBody
|
||||||
|
DecodeJSON(t, resp, &body)
|
||||||
|
for _, repo := range body.data {
|
||||||
|
assert.True(t, strings.Contains(repo.Name, keyword))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,33 +8,29 @@ import (
|
||||||
|
|
||||||
// listUserRepos - List the repositories owned by the given user.
|
// listUserRepos - List the repositories owned by the given user.
|
||||||
func listUserRepos(ctx *context.APIContext, u *models.User) {
|
func listUserRepos(ctx *context.APIContext, u *models.User) {
|
||||||
userID := u.ID
|
showPrivateRepos := ctx.IsSigned && (ctx.User.ID == u.ID || ctx.User.IsAdmin)
|
||||||
showPrivateRepos := ctx.IsSigned && (ctx.User.ID == userID || ctx.User.IsAdmin)
|
repos, err := models.GetUserRepositories(u.ID, showPrivateRepos, 1, u.NumRepos, "")
|
||||||
ownRepos, err := models.GetUserRepositories(userID, showPrivateRepos, 1, u.NumRepos, "")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Error(500, "GetUserRepositories", err)
|
ctx.Error(500, "GetUserRepositories", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var accessibleRepos []*api.Repository
|
apiRepos := make([]*api.Repository, len(repos))
|
||||||
|
var ctxUserID int64
|
||||||
if ctx.User != nil {
|
if ctx.User != nil {
|
||||||
accessibleRepos, err = getAccessibleRepos(ctx)
|
ctxUserID = ctx.User.ID
|
||||||
|
}
|
||||||
|
for i := range repos {
|
||||||
|
access, err := models.AccessLevel(ctxUserID, repos[i])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Error(500, "GetAccessibleRepos", err)
|
ctx.Error(500, "AccessLevel", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
apiRepos[i] = repos[i].APIFormat(access)
|
||||||
apiRepos := make([]*api.Repository, len(ownRepos)+len(accessibleRepos))
|
|
||||||
// Set owned repositories.
|
|
||||||
for i := range ownRepos {
|
|
||||||
apiRepos[i] = ownRepos[i].APIFormat(models.AccessModeOwner)
|
|
||||||
}
|
|
||||||
// Set repositories user has access to.
|
|
||||||
for i := 0; i < len(accessibleRepos); i++ {
|
|
||||||
apiRepos[i+len(ownRepos)] = accessibleRepos[i]
|
|
||||||
}
|
}
|
||||||
ctx.JSON(200, &apiRepos)
|
ctx.JSON(200, &apiRepos)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListUserRepos - list the repos owned and accessible by the given user.
|
// ListUserRepos - list the repos owned by the given user.
|
||||||
func ListUserRepos(ctx *context.APIContext) {
|
func ListUserRepos(ctx *context.APIContext) {
|
||||||
// swagger:route GET /users/{username}/repos userListRepos
|
// swagger:route GET /users/{username}/repos userListRepos
|
||||||
//
|
//
|
||||||
|
@ -52,7 +48,7 @@ func ListUserRepos(ctx *context.APIContext) {
|
||||||
listUserRepos(ctx, user)
|
listUserRepos(ctx, user)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListMyRepos - list the repositories owned by you.
|
// ListMyRepos - list the repositories you own or have access to.
|
||||||
func ListMyRepos(ctx *context.APIContext) {
|
func ListMyRepos(ctx *context.APIContext) {
|
||||||
// swagger:route GET /user/repos userCurrentListRepos
|
// swagger:route GET /user/repos userCurrentListRepos
|
||||||
//
|
//
|
||||||
|
@ -62,21 +58,25 @@ func ListMyRepos(ctx *context.APIContext) {
|
||||||
// Responses:
|
// Responses:
|
||||||
// 200: RepositoryList
|
// 200: RepositoryList
|
||||||
// 500: error
|
// 500: error
|
||||||
|
ownRepos, err := models.GetUserRepositories(ctx.User.ID, true, 1, ctx.User.NumRepos, "")
|
||||||
listUserRepos(ctx, ctx.User)
|
|
||||||
}
|
|
||||||
|
|
||||||
// getAccessibleRepos - Get the repositories a user has access to.
|
|
||||||
func getAccessibleRepos(ctx *context.APIContext) ([]*api.Repository, error) {
|
|
||||||
accessibleRepos, err := ctx.User.GetRepositoryAccesses()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
ctx.Error(500, "GetUserRepositories", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
i := 0
|
accessibleReposMap, err := ctx.User.GetRepositoryAccesses()
|
||||||
repos := make([]*api.Repository, len(accessibleRepos))
|
if err != nil {
|
||||||
for repo, access := range accessibleRepos {
|
ctx.Error(500, "GetRepositoryAccesses", err)
|
||||||
repos[i] = repo.APIFormat(access)
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
apiRepos := make([]*api.Repository, len(ownRepos)+len(accessibleReposMap))
|
||||||
|
for i := range ownRepos {
|
||||||
|
apiRepos[i] = ownRepos[i].APIFormat(models.AccessModeOwner)
|
||||||
|
}
|
||||||
|
i := len(ownRepos)
|
||||||
|
for repo, access := range accessibleReposMap {
|
||||||
|
apiRepos[i] = repo.APIFormat(access)
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
return repos, nil
|
ctx.JSON(200, &apiRepos)
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue