aboutsummaryrefslogtreecommitdiff
path: root/routes/api/v1/repo
diff options
context:
space:
mode:
Diffstat (limited to 'routes/api/v1/repo')
-rw-r--r--routes/api/v1/repo/issue.go53
-rw-r--r--routes/api/v1/repo/repo.go93
2 files changed, 64 insertions, 82 deletions
diff --git a/routes/api/v1/repo/issue.go b/routes/api/v1/repo/issue.go
index 47268175..a3cc02ee 100644
--- a/routes/api/v1/repo/issue.go
+++ b/routes/api/v1/repo/issue.go
@@ -6,6 +6,7 @@ package repo
import (
"fmt"
+ "net/http"
"strings"
api "github.com/gogs/go-gogs-client"
@@ -19,13 +20,13 @@ import (
func listIssues(c *context.APIContext, opts *models.IssuesOptions) {
issues, err := models.Issues(opts)
if err != nil {
- c.Error(500, "Issues", err)
+ c.ServerError("Issues", err)
return
}
count, err := models.IssuesCount(opts)
if err != nil {
- c.Error(500, "IssuesCount", err)
+ c.ServerError("IssuesCount", err)
return
}
@@ -33,14 +34,14 @@ func listIssues(c *context.APIContext, opts *models.IssuesOptions) {
apiIssues := make([]*api.Issue, len(issues))
for i := range issues {
if err = issues[i].LoadAttributes(); err != nil {
- c.Error(500, "LoadAttributes", err)
+ c.ServerError("LoadAttributes", err)
return
}
apiIssues[i] = issues[i].APIFormat()
}
c.SetLinkHeader(int(count), setting.UI.IssuePagingNum)
- c.JSON(200, &apiIssues)
+ c.JSONSuccess(&apiIssues)
}
func ListUserIssues(c *context.APIContext) {
@@ -66,14 +67,10 @@ func ListIssues(c *context.APIContext) {
func GetIssue(c *context.APIContext) {
issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
if err != nil {
- if errors.IsIssueNotExist(err) {
- c.Status(404)
- } else {
- c.Error(500, "GetIssueByIndex", err)
- }
+ c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
return
}
- c.JSON(200, issue.APIFormat())
+ c.JSONSuccess(issue.APIFormat())
}
func CreateIssue(c *context.APIContext, form api.CreateIssueOption) {
@@ -90,9 +87,9 @@ func CreateIssue(c *context.APIContext, form api.CreateIssueOption) {
assignee, err := models.GetUserByName(form.Assignee)
if err != nil {
if errors.IsUserNotExist(err) {
- c.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", form.Assignee))
+ c.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("assignee does not exist: [name: %s]", form.Assignee))
} else {
- c.Error(500, "GetUserByName", err)
+ c.ServerError("GetUserByName", err)
}
return
}
@@ -104,13 +101,13 @@ func CreateIssue(c *context.APIContext, form api.CreateIssueOption) {
}
if err := models.NewIssue(c.Repo.Repository, issue, form.Labels, nil); err != nil {
- c.Error(500, "NewIssue", err)
+ c.ServerError("NewIssue", err)
return
}
if form.Closed {
if err := issue.ChangeStatus(c.User, c.Repo.Repository, true); err != nil {
- c.Error(500, "ChangeStatus", err)
+ c.ServerError("ChangeStatus", err)
return
}
}
@@ -119,25 +116,21 @@ func CreateIssue(c *context.APIContext, form api.CreateIssueOption) {
var err error
issue, err = models.GetIssueByID(issue.ID)
if err != nil {
- c.Error(500, "GetIssueByID", err)
+ c.ServerError("GetIssueByID", err)
return
}
- c.JSON(201, issue.APIFormat())
+ c.JSON(http.StatusCreated, issue.APIFormat())
}
func EditIssue(c *context.APIContext, form api.EditIssueOption) {
issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
if err != nil {
- if errors.IsIssueNotExist(err) {
- c.Status(404)
- } else {
- c.Error(500, "GetIssueByIndex", err)
- }
+ c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
return
}
if !issue.IsPoster(c.User.ID) && !c.Repo.IsWriter() {
- c.Status(403)
+ c.Status(http.StatusForbidden)
return
}
@@ -156,9 +149,9 @@ func EditIssue(c *context.APIContext, form api.EditIssueOption) {
assignee, err := models.GetUserByName(*form.Assignee)
if err != nil {
if errors.IsUserNotExist(err) {
- c.Error(422, "", fmt.Sprintf("assignee does not exist: [name: %s]", *form.Assignee))
+ c.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("assignee does not exist: [name: %s]", *form.Assignee))
} else {
- c.Error(500, "GetUserByName", err)
+ c.ServerError("GetUserByName", err)
}
return
}
@@ -166,7 +159,7 @@ func EditIssue(c *context.APIContext, form api.EditIssueOption) {
}
if err = models.UpdateIssueUserByAssignee(issue); err != nil {
- c.Error(500, "UpdateIssueUserByAssignee", err)
+ c.ServerError("UpdateIssueUserByAssignee", err)
return
}
}
@@ -175,18 +168,18 @@ func EditIssue(c *context.APIContext, form api.EditIssueOption) {
oldMilestoneID := issue.MilestoneID
issue.MilestoneID = *form.Milestone
if err = models.ChangeMilestoneAssign(c.User, issue, oldMilestoneID); err != nil {
- c.Error(500, "ChangeMilestoneAssign", err)
+ c.ServerError("ChangeMilestoneAssign", err)
return
}
}
if err = models.UpdateIssue(issue); err != nil {
- c.Error(500, "UpdateIssue", err)
+ c.ServerError("UpdateIssue", err)
return
}
if form.State != nil {
if err = issue.ChangeStatus(c.User, c.Repo.Repository, api.STATE_CLOSED == api.StateType(*form.State)); err != nil {
- c.Error(500, "ChangeStatus", err)
+ c.ServerError("ChangeStatus", err)
return
}
}
@@ -194,8 +187,8 @@ func EditIssue(c *context.APIContext, form api.EditIssueOption) {
// Refetch from database to assign some automatic values
issue, err = models.GetIssueByID(issue.ID)
if err != nil {
- c.Error(500, "GetIssueByID", err)
+ c.ServerError("GetIssueByID", err)
return
}
- c.JSON(201, issue.APIFormat())
+ c.JSON(http.StatusCreated, issue.APIFormat())
}
diff --git a/routes/api/v1/repo/repo.go b/routes/api/v1/repo/repo.go
index 9c1ab476..9be8ad02 100644
--- a/routes/api/v1/repo/repo.go
+++ b/routes/api/v1/repo/repo.go
@@ -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)
}