mirror of
https://github.com/gogs/gogs.git
synced 2025-09-04 19:37:58 +00:00
api: add list user and organization's repositories (#3984)
This commit is contained in:
parent
50a7c3c20d
commit
cd9b29ff3f
2
gogs.go
2
gogs.go
@ -16,7 +16,7 @@ import (
|
||||
"github.com/gogits/gogs/modules/setting"
|
||||
)
|
||||
|
||||
const APP_VER = "0.9.159.0218"
|
||||
const APP_VER = "0.9.160.0218"
|
||||
|
||||
func init() {
|
||||
setting.AppVer = APP_VER
|
||||
|
File diff suppressed because one or more lines are too long
@ -222,10 +222,12 @@ func RegisterRoutes(m *macaron.Macaron) {
|
||||
Delete(user.DeletePublicKey)
|
||||
})
|
||||
|
||||
m.Combo("/issues", reqToken()).Get(repo.ListUserIssues)
|
||||
m.Combo("/issues").Get(repo.ListUserIssues)
|
||||
}, reqToken())
|
||||
|
||||
// Repositories
|
||||
m.Get("/users/:username/repos", reqToken(), repo.ListUserRepositories)
|
||||
m.Get("/orgs/:org/repos", reqToken(), repo.ListOrgRepositories)
|
||||
m.Combo("/user/repos", reqToken()).Get(repo.ListMyRepos).
|
||||
Post(bind(api.CreateRepoOption{}), repo.Create)
|
||||
m.Post("/org/:org/repos", reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
|
||||
|
@ -77,32 +77,53 @@ func Search(ctx *context.APIContext) {
|
||||
})
|
||||
}
|
||||
|
||||
// https://github.com/gogits/go-gogs-client/wiki/Repositories#list-your-repositories
|
||||
func ListMyRepos(ctx *context.APIContext) {
|
||||
ownRepos, err := models.GetUserRepositories(&models.UserRepoOptions{
|
||||
UserID: ctx.User.ID,
|
||||
Private: true,
|
||||
Page: 1,
|
||||
PageSize: ctx.User.NumRepos,
|
||||
})
|
||||
func listUserRepositories(ctx *context.APIContext, username string) {
|
||||
user, err := models.GetUserByName(username)
|
||||
if err != nil {
|
||||
ctx.Error(500, "GetRepositories", err)
|
||||
ctx.NotFoundOrServerError("GetUserByName", models.IsErrUserNotExist, err)
|
||||
return
|
||||
}
|
||||
numOwnRepos := len(ownRepos)
|
||||
|
||||
accessibleRepos, err := ctx.User.GetRepositoryAccesses()
|
||||
// Only list public repositories if user requests someone else's repository list,
|
||||
// or an organization isn't a member of.
|
||||
var ownRepos []*models.Repository
|
||||
if user.IsOrganization() {
|
||||
ownRepos, _, err = user.GetUserRepositories(ctx.User.ID, 1, user.NumRepos)
|
||||
} else {
|
||||
ownRepos, err = models.GetUserRepositories(&models.UserRepoOptions{
|
||||
UserID: user.ID,
|
||||
Private: ctx.User.ID == user.ID,
|
||||
Page: 1,
|
||||
PageSize: user.NumRepos,
|
||||
})
|
||||
}
|
||||
if err != nil {
|
||||
ctx.Error(500, "GetUserRepositories", err)
|
||||
return
|
||||
}
|
||||
|
||||
if ctx.User.ID != user.ID {
|
||||
repos := make([]*api.Repository, len(ownRepos))
|
||||
for i := range ownRepos {
|
||||
repos[i] = ownRepos[i].APIFormat(&api.Permission{true, true, true})
|
||||
}
|
||||
ctx.JSON(200, &repos)
|
||||
return
|
||||
}
|
||||
|
||||
accessibleRepos, err := user.GetRepositoryAccesses()
|
||||
if err != nil {
|
||||
ctx.Error(500, "GetRepositoryAccesses", err)
|
||||
return
|
||||
}
|
||||
|
||||
numOwnRepos := len(ownRepos)
|
||||
repos := make([]*api.Repository, numOwnRepos+len(accessibleRepos))
|
||||
for i := range ownRepos {
|
||||
repos[i] = ownRepos[i].APIFormat(&api.Permission{true, true, true})
|
||||
}
|
||||
i := numOwnRepos
|
||||
|
||||
i := numOwnRepos
|
||||
for repo, access := range accessibleRepos {
|
||||
repos[i] = repo.APIFormat(&api.Permission{
|
||||
Admin: access >= models.ACCESS_MODE_ADMIN,
|
||||
@ -115,6 +136,18 @@ func ListMyRepos(ctx *context.APIContext) {
|
||||
ctx.JSON(200, &repos)
|
||||
}
|
||||
|
||||
func ListMyRepos(ctx *context.APIContext) {
|
||||
listUserRepositories(ctx, ctx.User.Name)
|
||||
}
|
||||
|
||||
func ListUserRepositories(ctx *context.APIContext) {
|
||||
listUserRepositories(ctx, ctx.Params(":username"))
|
||||
}
|
||||
|
||||
func ListOrgRepositories(ctx *context.APIContext) {
|
||||
listUserRepositories(ctx, ctx.Params(":org"))
|
||||
}
|
||||
|
||||
func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateRepoOption) {
|
||||
repo, err := models.CreateRepository(owner, models.CreateRepoOptions{
|
||||
Name: opt.Name,
|
||||
|
@ -1 +1 @@
|
||||
0.9.159.0218
|
||||
0.9.160.0218
|
Loading…
x
Reference in New Issue
Block a user