add get repo API

This commit is contained in:
Unknwon 2015-10-22 17:46:07 -04:00
parent 04af4b24fd
commit 35a65736fa
2 changed files with 32 additions and 13 deletions

View file

@ -224,11 +224,12 @@ func runWeb(ctx *cli.Context) {
m.Group("/repos", func() { m.Group("/repos", func() {
m.Get("/search", v1.SearchRepos) m.Get("/search", v1.SearchRepos)
})
m.Group("", func() { m.Group("/repos", func() {
m.Post("/migrate", bindIgnErr(auth.MigrateRepoForm{}), v1.MigrateRepo) m.Post("/migrate", bindIgnErr(auth.MigrateRepoForm{}), v1.MigrateRepo)
m.Delete("/:username/:reponame", v1.DeleteRepo) m.Combo("/:username/:reponame").Get(v1.GetRepo)
}, middleware.ApiReqToken()) m.Delete("/:username/:reponame", v1.DeleteRepo)
m.Group("/:username/:reponame", func() { m.Group("/:username/:reponame", func() {
m.Combo("/hooks").Get(v1.ListRepoHooks). m.Combo("/hooks").Get(v1.ListRepoHooks).
@ -236,8 +237,8 @@ func runWeb(ctx *cli.Context) {
m.Patch("/hooks/:id:int", bind(api.EditHookOption{}), v1.EditRepoHook) m.Patch("/hooks/:id:int", bind(api.EditHookOption{}), v1.EditRepoHook)
m.Get("/raw/*", middleware.RepoRef(), v1.GetRepoRawFile) m.Get("/raw/*", middleware.RepoRef(), v1.GetRepoRawFile)
m.Get("/archive/*", v1.GetRepoArchive) m.Get("/archive/*", v1.GetRepoArchive)
}, middleware.ApiRepoAssignment(), middleware.ApiReqToken()) }, middleware.ApiRepoAssignment())
}) }, middleware.ApiReqToken())
m.Any("/*", func(ctx *middleware.Context) { m.Any("/*", func(ctx *middleware.Context) {
ctx.Error(404) ctx.Error(404)

View file

@ -252,37 +252,55 @@ func MigrateRepo(ctx *middleware.Context, form auth.MigrateRepoForm) {
ctx.JSON(201, ToApiRepository(ctxUser, repo, api.Permission{true, true, true})) ctx.JSON(201, ToApiRepository(ctxUser, repo, api.Permission{true, true, true}))
} }
func DeleteRepo(ctx *middleware.Context) { func parseOwnerAndRepo(ctx *middleware.Context) (*models.User, *models.Repository) {
user, err := models.GetUserByName(ctx.Params(":username")) owner, err := models.GetUserByName(ctx.Params(":username"))
if err != nil { if err != nil {
if models.IsErrUserNotExist(err) { if models.IsErrUserNotExist(err) {
ctx.APIError(422, "", err) ctx.APIError(422, "", err)
} else { } else {
ctx.APIError(500, "GetUserByName", err) ctx.APIError(500, "GetUserByName", err)
} }
return return nil, nil
} }
repo, err := models.GetRepositoryByName(user.Id, ctx.Params(":reponame")) repo, err := models.GetRepositoryByName(owner.Id, ctx.Params(":reponame"))
if err != nil { if err != nil {
if models.IsErrRepoNotExist(err) { if models.IsErrRepoNotExist(err) {
ctx.Error(404) ctx.Error(404)
} else { } else {
ctx.APIError(500, "GetRepositoryByName", err) ctx.APIError(500, "GetRepositoryByName", err)
} }
return nil, nil
}
return owner, repo
}
func GetRepo(ctx *middleware.Context) {
owner, repo := parseOwnerAndRepo(ctx)
if ctx.Written() {
return return
} }
if user.IsOrganization() && !user.IsOwnedBy(ctx.User.Id) { ctx.JSON(200, ToApiRepository(owner, repo, api.Permission{true, true, true}))
}
func DeleteRepo(ctx *middleware.Context) {
owner, repo := parseOwnerAndRepo(ctx)
if ctx.Written() {
return
}
if owner.IsOrganization() && !owner.IsOwnedBy(ctx.User.Id) {
ctx.APIError(403, "", "Given user is not owner of organization.") ctx.APIError(403, "", "Given user is not owner of organization.")
return return
} }
if err := models.DeleteRepository(user.Id, repo.ID); err != nil { if err := models.DeleteRepository(owner.Id, repo.ID); err != nil {
ctx.APIError(500, "DeleteRepository", err) ctx.APIError(500, "DeleteRepository", err)
return return
} }
log.Trace("Repository deleted: %s/%s", user.Name, repo.Name) log.Trace("Repository deleted: %s/%s", owner.Name, repo.Name)
ctx.Status(204) ctx.Status(204)
} }