|
@@ -5,6 +5,8 @@
|
|
|
package repo
|
|
|
|
|
|
import (
|
|
|
+ "fmt"
|
|
|
+ "net/http"
|
|
|
"path"
|
|
|
|
|
|
log "gopkg.in/clog.v1"
|
|
@@ -19,7 +21,6 @@ import (
|
|
|
"github.com/gogs/gogs/routes/api/v1/convert"
|
|
|
)
|
|
|
|
|
|
-// https://github.com/gogs/go-gogs-client/wiki/Repositories#search-repositories
|
|
|
func Search(c *context.APIContext) {
|
|
|
opts := &models.SearchRepoOptions{
|
|
|
Keyword: path.Base(c.Query("q")),
|
|
@@ -35,7 +36,7 @@ func Search(c *context.APIContext) {
|
|
|
} else {
|
|
|
u, err := models.GetUserByID(opts.OwnerID)
|
|
|
if err != nil {
|
|
|
- c.JSON(500, map[string]interface{}{
|
|
|
+ c.JSON(http.StatusInternalServerError, map[string]interface{}{
|
|
|
"ok": false,
|
|
|
"error": err.Error(),
|
|
|
})
|
|
@@ -50,7 +51,7 @@ func Search(c *context.APIContext) {
|
|
|
|
|
|
repos, count, err := models.SearchRepositoryByName(opts)
|
|
|
if err != nil {
|
|
|
- c.JSON(500, map[string]interface{}{
|
|
|
+ c.JSON(http.StatusInternalServerError, map[string]interface{}{
|
|
|
"ok": false,
|
|
|
"error": err.Error(),
|
|
|
})
|
|
@@ -58,7 +59,7 @@ func Search(c *context.APIContext) {
|
|
|
}
|
|
|
|
|
|
if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
|
|
|
- c.JSON(500, map[string]interface{}{
|
|
|
+ c.JSON(http.StatusInternalServerError, map[string]interface{}{
|
|
|
"ok": false,
|
|
|
"error": err.Error(),
|
|
|
})
|
|
@@ -71,7 +72,7 @@ func Search(c *context.APIContext) {
|
|
|
}
|
|
|
|
|
|
c.SetLinkHeader(int(count), opts.PageSize)
|
|
|
- c.JSON(200, map[string]interface{}{
|
|
|
+ c.JSONSuccess(map[string]interface{}{
|
|
|
"ok": true,
|
|
|
"data": results,
|
|
|
})
|
|
@@ -98,12 +99,12 @@ func listUserRepositories(c *context.APIContext, username string) {
|
|
|
})
|
|
|
}
|
|
|
if err != nil {
|
|
|
- c.Error(500, "GetUserRepositories", err)
|
|
|
+ c.ServerError("GetUserRepositories", err)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
if err = models.RepositoryList(ownRepos).LoadAttributes(); err != nil {
|
|
|
- c.Error(500, "LoadAttributes(ownRepos)", err)
|
|
|
+ c.ServerError("LoadAttributes(ownRepos)", err)
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -113,13 +114,13 @@ func listUserRepositories(c *context.APIContext, username string) {
|
|
|
for i := range ownRepos {
|
|
|
repos[i] = ownRepos[i].APIFormat(&api.Permission{true, true, true})
|
|
|
}
|
|
|
- c.JSON(200, &repos)
|
|
|
+ c.JSONSuccess(&repos)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
accessibleRepos, err := user.GetRepositoryAccesses()
|
|
|
if err != nil {
|
|
|
- c.Error(500, "GetRepositoryAccesses", err)
|
|
|
+ c.ServerError("GetRepositoryAccesses", err)
|
|
|
return
|
|
|
}
|
|
|
|
|
@@ -139,7 +140,7 @@ func listUserRepositories(c *context.APIContext, username string) {
|
|
|
i++
|
|
|
}
|
|
|
|
|
|
- c.JSON(200, &repos)
|
|
|
+ c.JSONSuccess(&repos)
|
|
|
}
|
|
|
|
|
|
func ListMyRepos(c *context.APIContext) {
|
|
@@ -168,14 +169,14 @@ func CreateUserRepo(c *context.APIContext, owner *models.User, opt api.CreateRep
|
|
|
if models.IsErrRepoAlreadyExist(err) ||
|
|
|
models.IsErrNameReserved(err) ||
|
|
|
models.IsErrNamePatternNotAllowed(err) {
|
|
|
- c.Error(422, "", err)
|
|
|
+ c.Error(http.StatusUnprocessableEntity, "", err)
|
|
|
} else {
|
|
|
if repo != nil {
|
|
|
if err = models.DeleteRepository(c.User.ID, repo.ID); err != nil {
|
|
|
log.Error(2, "DeleteRepository: %v", err)
|
|
|
}
|
|
|
}
|
|
|
- c.Error(500, "CreateRepository", err)
|
|
|
+ c.ServerError("CreateRepository", err)
|
|
|
}
|
|
|
return
|
|
|
}
|
|
@@ -183,11 +184,10 @@ func CreateUserRepo(c *context.APIContext, owner *models.User, opt api.CreateRep
|
|
|
c.JSON(201, repo.APIFormat(&api.Permission{true, true, true}))
|
|
|
}
|
|
|
|
|
|
-// https://github.com/gogs/go-gogs-client/wiki/Repositories#create
|
|
|
func Create(c *context.APIContext, opt api.CreateRepoOption) {
|
|
|
// Shouldn't reach this condition, but just in case.
|
|
|
if c.User.IsOrganization() {
|
|
|
- c.Error(422, "", "not allowed creating repository for organization")
|
|
|
+ c.Error(http.StatusUnprocessableEntity, "", "not allowed creating repository for organization")
|
|
|
return
|
|
|
}
|
|
|
CreateUserRepo(c, c.User, opt)
|
|
@@ -196,22 +196,17 @@ func Create(c *context.APIContext, opt api.CreateRepoOption) {
|
|
|
func CreateOrgRepo(c *context.APIContext, opt api.CreateRepoOption) {
|
|
|
org, err := models.GetOrgByName(c.Params(":org"))
|
|
|
if err != nil {
|
|
|
- if errors.IsUserNotExist(err) {
|
|
|
- c.Error(422, "", err)
|
|
|
- } else {
|
|
|
- c.Error(500, "GetOrgByName", err)
|
|
|
- }
|
|
|
+ c.NotFoundOrServerError("GetOrgByName", errors.IsUserNotExist, err)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
if !org.IsOwnedBy(c.User.ID) {
|
|
|
- c.Error(403, "", "Given user is not owner of organization.")
|
|
|
+ c.Error(http.StatusForbidden, "", "given user is not owner of organization")
|
|
|
return
|
|
|
}
|
|
|
CreateUserRepo(c, org, opt)
|
|
|
}
|
|
|
|
|
|
-// https://github.com/gogs/go-gogs-client/wiki/Repositories#migrate
|
|
|
func Migrate(c *context.APIContext, f form.MigrateRepo) {
|
|
|
ctxUser := c.User
|
|
|
// Not equal means context user is an organization,
|
|
@@ -220,27 +215,27 @@ func Migrate(c *context.APIContext, f form.MigrateRepo) {
|
|
|
org, err := models.GetUserByID(f.Uid)
|
|
|
if err != nil {
|
|
|
if errors.IsUserNotExist(err) {
|
|
|
- c.Error(422, "", err)
|
|
|
+ c.Error(http.StatusUnprocessableEntity, "", err)
|
|
|
} else {
|
|
|
- c.Error(500, "GetUserByID", err)
|
|
|
+ c.Error(http.StatusInternalServerError, "GetUserByID", err)
|
|
|
}
|
|
|
return
|
|
|
} else if !org.IsOrganization() && !c.User.IsAdmin {
|
|
|
- c.Error(403, "", "Given user is not an organization")
|
|
|
+ c.Error(http.StatusForbidden, "", "given user is not an organization")
|
|
|
return
|
|
|
}
|
|
|
ctxUser = org
|
|
|
}
|
|
|
|
|
|
if c.HasError() {
|
|
|
- c.Error(422, "", c.GetErrMsg())
|
|
|
+ c.Error(http.StatusUnprocessableEntity, "", c.GetErrMsg())
|
|
|
return
|
|
|
}
|
|
|
|
|
|
if ctxUser.IsOrganization() && !c.User.IsAdmin {
|
|
|
// Check ownership of organization.
|
|
|
if !ctxUser.IsOwnedBy(c.User.ID) {
|
|
|
- c.Error(403, "", "Given user is not owner of organization")
|
|
|
+ c.Error(http.StatusForbidden, "", "Given user is not owner of organization")
|
|
|
return
|
|
|
}
|
|
|
}
|
|
@@ -251,16 +246,16 @@ func Migrate(c *context.APIContext, f form.MigrateRepo) {
|
|
|
addrErr := err.(models.ErrInvalidCloneAddr)
|
|
|
switch {
|
|
|
case addrErr.IsURLError:
|
|
|
- c.Error(422, "", err)
|
|
|
+ c.Error(http.StatusUnprocessableEntity, "", err)
|
|
|
case addrErr.IsPermissionDenied:
|
|
|
- c.Error(422, "", "You are not allowed to import local repositories")
|
|
|
+ c.Error(http.StatusUnprocessableEntity, "", "you are not allowed to import local repositories")
|
|
|
case addrErr.IsInvalidPath:
|
|
|
- c.Error(422, "", "Invalid local path, it does not exist or not a directory")
|
|
|
+ c.Error(http.StatusUnprocessableEntity, "", "invalid local path, it does not exist or not a directory")
|
|
|
default:
|
|
|
- c.Error(500, "ParseRemoteAddr", "Unknown error type (ErrInvalidCloneAddr): "+err.Error())
|
|
|
+ c.ServerError("ParseRemoteAddr", fmt.Errorf("unknown error type (ErrInvalidCloneAddr): %v", err))
|
|
|
}
|
|
|
} else {
|
|
|
- c.Error(500, "ParseRemoteAddr", err)
|
|
|
+ c.ServerError("ParseRemoteAddr", err)
|
|
|
}
|
|
|
return
|
|
|
}
|
|
@@ -280,9 +275,9 @@ func Migrate(c *context.APIContext, f form.MigrateRepo) {
|
|
|
}
|
|
|
|
|
|
if errors.IsReachLimitOfRepo(err) {
|
|
|
- c.Error(422, "", err)
|
|
|
+ c.Error(http.StatusUnprocessableEntity, "", err)
|
|
|
} else {
|
|
|
- c.Error(500, "MigrateRepository", models.HandleMirrorCredentials(err.Error(), true))
|
|
|
+ c.ServerError("MigrateRepository", errors.New(models.HandleMirrorCredentials(err.Error(), true)))
|
|
|
}
|
|
|
return
|
|
|
}
|
|
@@ -291,46 +286,40 @@ func Migrate(c *context.APIContext, f form.MigrateRepo) {
|
|
|
c.JSON(201, repo.APIFormat(&api.Permission{true, true, true}))
|
|
|
}
|
|
|
|
|
|
-// FIXME: Inject to *context.APIContext
|
|
|
+// FIXME: inject in the handler chain
|
|
|
func parseOwnerAndRepo(c *context.APIContext) (*models.User, *models.Repository) {
|
|
|
owner, err := models.GetUserByName(c.Params(":username"))
|
|
|
if err != nil {
|
|
|
if errors.IsUserNotExist(err) {
|
|
|
- c.Error(422, "", err)
|
|
|
+ c.Error(http.StatusUnprocessableEntity, "", err)
|
|
|
} else {
|
|
|
- c.Error(500, "GetUserByName", err)
|
|
|
+ c.ServerError("GetUserByName", err)
|
|
|
}
|
|
|
return nil, nil
|
|
|
}
|
|
|
|
|
|
repo, err := models.GetRepositoryByName(owner.ID, c.Params(":reponame"))
|
|
|
if err != nil {
|
|
|
- if errors.IsRepoNotExist(err) {
|
|
|
- c.Status(404)
|
|
|
- } else {
|
|
|
- c.Error(500, "GetRepositoryByName", err)
|
|
|
- }
|
|
|
+ c.NotFoundOrServerError("GetRepositoryByName", errors.IsRepoNotExist, err)
|
|
|
return nil, nil
|
|
|
}
|
|
|
|
|
|
return owner, repo
|
|
|
}
|
|
|
|
|
|
-// https://github.com/gogs/go-gogs-client/wiki/Repositories#get
|
|
|
func Get(c *context.APIContext) {
|
|
|
_, repo := parseOwnerAndRepo(c)
|
|
|
if c.Written() {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- c.JSON(200, repo.APIFormat(&api.Permission{
|
|
|
+ c.JSONSuccess(repo.APIFormat(&api.Permission{
|
|
|
Admin: c.Repo.IsAdmin(),
|
|
|
Push: c.Repo.IsWriter(),
|
|
|
Pull: true,
|
|
|
}))
|
|
|
}
|
|
|
|
|
|
-// https://github.com/gogs/go-gogs-client/wiki/Repositories#delete
|
|
|
func Delete(c *context.APIContext) {
|
|
|
owner, repo := parseOwnerAndRepo(c)
|
|
|
if c.Written() {
|
|
@@ -338,30 +327,30 @@ func Delete(c *context.APIContext) {
|
|
|
}
|
|
|
|
|
|
if owner.IsOrganization() && !owner.IsOwnedBy(c.User.ID) {
|
|
|
- c.Error(403, "", "Given user is not owner of organization.")
|
|
|
+ c.Error(http.StatusForbidden, "", "given user is not owner of organization")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
if err := models.DeleteRepository(owner.ID, repo.ID); err != nil {
|
|
|
- c.Error(500, "DeleteRepository", err)
|
|
|
+ c.ServerError("DeleteRepository", err)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
log.Trace("Repository deleted: %s/%s", owner.Name, repo.Name)
|
|
|
- c.Status(204)
|
|
|
+ c.NoContent()
|
|
|
}
|
|
|
|
|
|
func ListForks(c *context.APIContext) {
|
|
|
forks, err := c.Repo.Repository.GetForks()
|
|
|
if err != nil {
|
|
|
- c.Error(500, "GetForks", err)
|
|
|
+ c.ServerError("GetForks", err)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
apiForks := make([]*api.Repository, len(forks))
|
|
|
for i := range forks {
|
|
|
if err := forks[i].GetOwner(); err != nil {
|
|
|
- c.Error(500, "GetOwner", err)
|
|
|
+ c.ServerError("GetOwner", err)
|
|
|
return
|
|
|
}
|
|
|
apiForks[i] = forks[i].APIFormat(&api.Permission{
|
|
@@ -371,7 +360,7 @@ func ListForks(c *context.APIContext) {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- c.JSON(200, &apiForks)
|
|
|
+ c.JSONSuccess(&apiForks)
|
|
|
}
|
|
|
|
|
|
func IssueTracker(c *context.APIContext, form api.EditIssueTrackerOption) {
|
|
@@ -409,10 +398,10 @@ func MirrorSync(c *context.APIContext) {
|
|
|
if c.Written() {
|
|
|
return
|
|
|
} else if !repo.IsMirror {
|
|
|
- c.Status(404)
|
|
|
+ c.NotFound()
|
|
|
return
|
|
|
}
|
|
|
|
|
|
go models.MirrorQueue.Add(repo.ID)
|
|
|
- c.Status(202)
|
|
|
+ c.Status(http.StatusAccepted)
|
|
|
}
|