Browse Source

api: add list user and organization's repositories (#3984)

pull/4155/head
Unknwon 8 years ago
parent
commit
cd9b29ff3f
No known key found for this signature in database
GPG Key ID: 25B575AE3213B2B3
  1. 2
      gogs.go
  2. 4
      modules/bindata/bindata.go
  3. 4
      routers/api/v1/api.go
  4. 53
      routers/api/v1/repo/repo.go
  5. 2
      templates/.VERSION

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

4
modules/bindata/bindata.go

File diff suppressed because one or more lines are too long

4
routers/api/v1/api.go

@ -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)

53
routers/api/v1/repo/repo.go

@ -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,
func listUserRepositories(ctx *context.APIContext, username string) {
user, err := models.GetUserByName(username)
if err != nil {
ctx.NotFoundOrServerError("GetUserByName", models.IsErrUserNotExist, err)
return
}
// 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: ctx.User.NumRepos,
PageSize: user.NumRepos,
})
}
if err != nil {
ctx.Error(500, "GetRepositories", err)
ctx.Error(500, "GetUserRepositories", err)
return
}
numOwnRepos := len(ownRepos)
accessibleRepos, err := ctx.User.GetRepositoryAccesses()
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,

2
templates/.VERSION

@ -1 +1 @@
0.9.159.0218
0.9.160.0218
Loading…
Cancel
Save