Convert all API handers to use *context.APIContext

This commit is contained in:
Unknwon 2016-03-13 18:49:16 -04:00
parent db4da7beec
commit dd6faf7f9b
20 changed files with 204 additions and 196 deletions

View file

@ -20,7 +20,7 @@ import (
)
// https://github.com/gogits/go-gogs-client/wiki/Repositories#search-repositories
func Search(ctx *context.Context) {
func Search(ctx *context.APIContext) {
opts := &models.SearchRepoOptions{
Keyword: path.Base(ctx.Query("q")),
OwnerID: com.StrTo(ctx.Query("uid")).MustInt64(),
@ -81,17 +81,17 @@ func Search(ctx *context.Context) {
}
// https://github.com/gogits/go-gogs-client/wiki/Repositories#list-your-repositories
func ListMyRepos(ctx *context.Context) {
func ListMyRepos(ctx *context.APIContext) {
ownRepos, err := models.GetRepositories(ctx.User.Id, true)
if err != nil {
ctx.APIError(500, "GetRepositories", err)
ctx.Error(500, "GetRepositories", err)
return
}
numOwnRepos := len(ownRepos)
accessibleRepos, err := ctx.User.GetRepositoryAccesses()
if err != nil {
ctx.APIError(500, "GetRepositoryAccesses", err)
ctx.Error(500, "GetRepositoryAccesses", err)
return
}
@ -113,7 +113,7 @@ func ListMyRepos(ctx *context.Context) {
ctx.JSON(200, &repos)
}
func CreateUserRepo(ctx *context.Context, owner *models.User, opt api.CreateRepoOption) {
func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateRepoOption) {
repo, err := models.CreateRepository(owner, models.CreateRepoOptions{
Name: opt.Name,
Description: opt.Description,
@ -127,14 +127,14 @@ func CreateUserRepo(ctx *context.Context, owner *models.User, opt api.CreateRepo
if models.IsErrRepoAlreadyExist(err) ||
models.IsErrNameReserved(err) ||
models.IsErrNamePatternNotAllowed(err) {
ctx.APIError(422, "", err)
ctx.Error(422, "", err)
} else {
if repo != nil {
if err = models.DeleteRepository(ctx.User.Id, repo.ID); err != nil {
log.Error(4, "DeleteRepository: %v", err)
}
}
ctx.APIError(500, "CreateRepository", err)
ctx.Error(500, "CreateRepository", err)
}
return
}
@ -143,35 +143,35 @@ func CreateUserRepo(ctx *context.Context, owner *models.User, opt api.CreateRepo
}
// https://github.com/gogits/go-gogs-client/wiki/Repositories#create
func Create(ctx *context.Context, opt api.CreateRepoOption) {
func Create(ctx *context.APIContext, opt api.CreateRepoOption) {
// Shouldn't reach this condition, but just in case.
if ctx.User.IsOrganization() {
ctx.APIError(422, "", "not allowed creating repository for organization")
ctx.Error(422, "", "not allowed creating repository for organization")
return
}
CreateUserRepo(ctx, ctx.User, opt)
}
func CreateOrgRepo(ctx *context.Context, opt api.CreateRepoOption) {
func CreateOrgRepo(ctx *context.APIContext, opt api.CreateRepoOption) {
org, err := models.GetOrgByName(ctx.Params(":org"))
if err != nil {
if models.IsErrUserNotExist(err) {
ctx.APIError(422, "", err)
ctx.Error(422, "", err)
} else {
ctx.APIError(500, "GetOrgByName", err)
ctx.Error(500, "GetOrgByName", err)
}
return
}
if !org.IsOwnedBy(ctx.User.Id) {
ctx.APIError(403, "", "Given user is not owner of organization.")
ctx.Error(403, "", "Given user is not owner of organization.")
return
}
CreateUserRepo(ctx, org, opt)
}
// https://github.com/gogits/go-gogs-client/wiki/Repositories#migrate
func Migrate(ctx *context.Context, form auth.MigrateRepoForm) {
func Migrate(ctx *context.APIContext, form auth.MigrateRepoForm) {
ctxUser := ctx.User
// Not equal means context user is an organization,
// or is another user/organization if current user is admin.
@ -179,9 +179,9 @@ func Migrate(ctx *context.Context, form auth.MigrateRepoForm) {
org, err := models.GetUserByID(form.Uid)
if err != nil {
if models.IsErrUserNotExist(err) {
ctx.APIError(422, "", err)
ctx.Error(422, "", err)
} else {
ctx.APIError(500, "GetUserByID", err)
ctx.Error(500, "GetUserByID", err)
}
return
}
@ -189,14 +189,14 @@ func Migrate(ctx *context.Context, form auth.MigrateRepoForm) {
}
if ctx.HasError() {
ctx.APIError(422, "", ctx.GetErrMsg())
ctx.Error(422, "", ctx.GetErrMsg())
return
}
if ctxUser.IsOrganization() && !ctx.User.IsAdmin {
// Check ownership of organization.
if !ctxUser.IsOwnedBy(ctx.User.Id) {
ctx.APIError(403, "", "Given user is not owner of organization.")
ctx.Error(403, "", "Given user is not owner of organization.")
return
}
}
@ -207,16 +207,16 @@ func Migrate(ctx *context.Context, form auth.MigrateRepoForm) {
addrErr := err.(models.ErrInvalidCloneAddr)
switch {
case addrErr.IsURLError:
ctx.APIError(422, "", err)
ctx.Error(422, "", err)
case addrErr.IsPermissionDenied:
ctx.APIError(422, "", "You are not allowed to import local repositories.")
ctx.Error(422, "", "You are not allowed to import local repositories.")
case addrErr.IsInvalidPath:
ctx.APIError(422, "", "Invalid local path, it does not exist or not a directory.")
ctx.Error(422, "", "Invalid local path, it does not exist or not a directory.")
default:
ctx.APIError(500, "ParseRemoteAddr", "Unknown error type (ErrInvalidCloneAddr): "+err.Error())
ctx.Error(500, "ParseRemoteAddr", "Unknown error type (ErrInvalidCloneAddr): "+err.Error())
}
} else {
ctx.APIError(500, "ParseRemoteAddr", err)
ctx.Error(500, "ParseRemoteAddr", err)
}
return
}
@ -234,7 +234,7 @@ func Migrate(ctx *context.Context, form auth.MigrateRepoForm) {
log.Error(4, "DeleteRepository: %v", errDelete)
}
}
ctx.APIError(500, "MigrateRepository", models.HandleCloneUserCredentials(err.Error(), true))
ctx.Error(500, "MigrateRepository", models.HandleCloneUserCredentials(err.Error(), true))
return
}
@ -242,13 +242,13 @@ func Migrate(ctx *context.Context, form auth.MigrateRepoForm) {
ctx.JSON(201, convert.ToApiRepository(ctxUser, repo, api.Permission{true, true, true}))
}
func parseOwnerAndRepo(ctx *context.Context) (*models.User, *models.Repository) {
func parseOwnerAndRepo(ctx *context.APIContext) (*models.User, *models.Repository) {
owner, err := models.GetUserByName(ctx.Params(":username"))
if err != nil {
if models.IsErrUserNotExist(err) {
ctx.APIError(422, "", err)
ctx.Error(422, "", err)
} else {
ctx.APIError(500, "GetUserByName", err)
ctx.Error(500, "GetUserByName", err)
}
return nil, nil
}
@ -256,9 +256,9 @@ func parseOwnerAndRepo(ctx *context.Context) (*models.User, *models.Repository)
repo, err := models.GetRepositoryByName(owner.Id, ctx.Params(":reponame"))
if err != nil {
if models.IsErrRepoNotExist(err) {
ctx.Error(404)
ctx.Status(404)
} else {
ctx.APIError(500, "GetRepositoryByName", err)
ctx.Error(500, "GetRepositoryByName", err)
}
return nil, nil
}
@ -267,7 +267,7 @@ func parseOwnerAndRepo(ctx *context.Context) (*models.User, *models.Repository)
}
// https://github.com/gogits/go-gogs-client/wiki/Repositories#get
func Get(ctx *context.Context) {
func Get(ctx *context.APIContext) {
owner, repo := parseOwnerAndRepo(ctx)
if ctx.Written() {
return
@ -277,19 +277,19 @@ func Get(ctx *context.Context) {
}
// https://github.com/gogits/go-gogs-client/wiki/Repositories#delete
func Delete(ctx *context.Context) {
func Delete(ctx *context.APIContext) {
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.Error(403, "", "Given user is not owner of organization.")
return
}
if err := models.DeleteRepository(owner.Id, repo.ID); err != nil {
ctx.APIError(500, "DeleteRepository", err)
ctx.Error(500, "DeleteRepository", err)
return
}