Browse Source

routes/api/v1: codemod

unknwon 4 years ago
parent
commit
f1e0ebfe93

+ 1 - 1
gogs.go

@@ -16,7 +16,7 @@ import (
 	"github.com/gogs/gogs/pkg/setting"
 )
 
-const Version = "0.11.91.0808"
+const Version = "0.11.91.0810"
 
 func init() {
 	setting.AppVer = Version

+ 0 - 1
routes/api/v1/admin/org.go

@@ -12,7 +12,6 @@ import (
 	"github.com/gogs/gogs/routes/api/v1/user"
 )
 
-// https://github.com/gogs/go-gogs-client/wiki/Administration-Organizations#create-a-new-organization
 func CreateOrg(c *context.APIContext, form api.CreateOrgOption) {
 	org.CreateOrgForUser(c, form, user.GetUserByParams(c))
 }

+ 5 - 9
routes/api/v1/admin/org_repo.go

@@ -13,11 +13,7 @@ import (
 func GetRepositoryByParams(c *context.APIContext) *models.Repository {
 	repo, err := models.GetRepositoryByName(c.Org.Team.OrgID, 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
 	}
 	return repo
@@ -29,11 +25,11 @@ func AddTeamRepository(c *context.APIContext) {
 		return
 	}
 	if err := c.Org.Team.AddRepository(repo); err != nil {
-		c.Error(500, "AddRepository", err)
+		c.ServerError("AddRepository", err)
 		return
 	}
 
-	c.Status(204)
+	c.NoContent()
 }
 
 func RemoveTeamRepository(c *context.APIContext) {
@@ -42,9 +38,9 @@ func RemoveTeamRepository(c *context.APIContext) {
 		return
 	}
 	if err := c.Org.Team.RemoveRepository(repo.ID); err != nil {
-		c.Error(500, "RemoveRepository", err)
+		c.ServerError("RemoveRepository", err)
 		return
 	}
 
-	c.Status(204)
+	c.NoContent()
 }

+ 9 - 7
routes/api/v1/admin/org_team.go

@@ -5,6 +5,8 @@
 package admin
 
 import (
+	"net/http"
+
 	api "github.com/gogs/go-gogs-client"
 
 	"github.com/gogs/gogs/models"
@@ -22,14 +24,14 @@ func CreateTeam(c *context.APIContext, form api.CreateTeamOption) {
 	}
 	if err := models.NewTeam(team); err != nil {
 		if models.IsErrTeamAlreadyExist(err) {
-			c.Error(422, "", err)
+			c.Error(http.StatusUnprocessableEntity, "", err)
 		} else {
-			c.Error(500, "NewTeam", err)
+			c.ServerError("NewTeam", err)
 		}
 		return
 	}
 
-	c.JSON(201, convert.ToTeam(team))
+	c.JSON(http.StatusCreated, convert.ToTeam(team))
 }
 
 func AddTeamMember(c *context.APIContext) {
@@ -38,11 +40,11 @@ func AddTeamMember(c *context.APIContext) {
 		return
 	}
 	if err := c.Org.Team.AddMember(u.ID); err != nil {
-		c.Error(500, "AddMember", err)
+		c.ServerError("AddMember", err)
 		return
 	}
 
-	c.Status(204)
+	c.NoContent()
 }
 
 func RemoveTeamMember(c *context.APIContext) {
@@ -52,9 +54,9 @@ func RemoveTeamMember(c *context.APIContext) {
 	}
 
 	if err := c.Org.Team.RemoveMember(u.ID); err != nil {
-		c.Error(500, "RemoveMember", err)
+		c.ServerError("RemoveMember", err)
 		return
 	}
 
-	c.Status(204)
+	c.NoContent()
 }

+ 0 - 1
routes/api/v1/admin/repo.go

@@ -12,7 +12,6 @@ import (
 	"github.com/gogs/gogs/routes/api/v1/user"
 )
 
-// https://github.com/gogs/go-gogs-client/wiki/Administration-Repositories#create-a-new-repository
 func CreateRepo(c *context.APIContext, form api.CreateRepoOption) {
 	owner := user.GetUserByParams(c)
 	if c.Written() {

+ 16 - 18
routes/api/v1/admin/user.go

@@ -5,6 +5,8 @@
 package admin
 
 import (
+	"net/http"
+
 	log "gopkg.in/clog.v1"
 
 	api "github.com/gogs/go-gogs-client"
@@ -25,9 +27,9 @@ func parseLoginSource(c *context.APIContext, u *models.User, sourceID int64, log
 	source, err := models.GetLoginSourceByID(sourceID)
 	if err != nil {
 		if errors.IsLoginSourceNotExist(err) {
-			c.Error(422, "", err)
+			c.Error(http.StatusUnprocessableEntity, "", err)
 		} else {
-			c.Error(500, "GetLoginSourceByID", err)
+			c.ServerError("GetLoginSourceByID", err)
 		}
 		return
 	}
@@ -37,7 +39,6 @@ func parseLoginSource(c *context.APIContext, u *models.User, sourceID int64, log
 	u.LoginName = loginName
 }
 
-// https://github.com/gogs/go-gogs-client/wiki/Administration-Users#create-a-new-user
 func CreateUser(c *context.APIContext, form api.CreateUserOption) {
 	u := &models.User{
 		Name:      form.Username,
@@ -58,23 +59,22 @@ func CreateUser(c *context.APIContext, form api.CreateUserOption) {
 			models.IsErrEmailAlreadyUsed(err) ||
 			models.IsErrNameReserved(err) ||
 			models.IsErrNamePatternNotAllowed(err) {
-			c.Error(422, "", err)
+			c.Error(http.StatusUnprocessableEntity, "", err)
 		} else {
-			c.Error(500, "CreateUser", err)
+			c.ServerError("CreateUser", err)
 		}
 		return
 	}
-	log.Trace("Account created by admin (%s): %s", c.User.Name, u.Name)
+	log.Trace("Account created by admin %q: %s", c.User.Name, u.Name)
 
 	// Send email notification.
 	if form.SendNotify && setting.MailService != nil {
 		mailer.SendRegisterNotifyMail(c.Context.Context, models.NewMailerUser(u))
 	}
 
-	c.JSON(201, u.APIFormat())
+	c.JSON(http.StatusCreated, u.APIFormat())
 }
 
-// https://github.com/gogs/go-gogs-client/wiki/Administration-Users#edit-an-existing-user
 func EditUser(c *context.APIContext, form api.EditUserOption) {
 	u := user.GetUserByParams(c)
 	if c.Written() {
@@ -90,7 +90,7 @@ func EditUser(c *context.APIContext, form api.EditUserOption) {
 		u.Passwd = form.Password
 		var err error
 		if u.Salt, err = models.GetUserSalt(); err != nil {
-			c.Error(500, "UpdateUser", err)
+			c.ServerError("GetUserSalt", err)
 			return
 		}
 		u.EncodePasswd()
@@ -119,18 +119,17 @@ func EditUser(c *context.APIContext, form api.EditUserOption) {
 
 	if err := models.UpdateUser(u); err != nil {
 		if models.IsErrEmailAlreadyUsed(err) {
-			c.Error(422, "", err)
+			c.Error(http.StatusUnprocessableEntity, "", err)
 		} else {
-			c.Error(500, "UpdateUser", err)
+			c.ServerError("UpdateUser", err)
 		}
 		return
 	}
-	log.Trace("Account profile updated by admin (%s): %s", c.User.Name, u.Name)
+	log.Trace("Account profile updated by admin %q: %s", c.User.Name, u.Name)
 
-	c.JSON(200, u.APIFormat())
+	c.JSONSuccess(u.APIFormat())
 }
 
-// https://github.com/gogs/go-gogs-client/wiki/Administration-Users#delete-a-user
 func DeleteUser(c *context.APIContext) {
 	u := user.GetUserByParams(c)
 	if c.Written() {
@@ -140,18 +139,17 @@ func DeleteUser(c *context.APIContext) {
 	if err := models.DeleteUser(u); err != nil {
 		if models.IsErrUserOwnRepos(err) ||
 			models.IsErrUserHasOrgs(err) {
-			c.Error(422, "", err)
+			c.Error(http.StatusUnprocessableEntity, "", err)
 		} else {
-			c.Error(500, "DeleteUser", err)
+			c.ServerError("DeleteUser", err)
 		}
 		return
 	}
 	log.Trace("Account deleted by admin(%s): %s", c.User.Name, u.Name)
 
-	c.Status(204)
+	c.NoContent()
 }
 
-// https://github.com/gogs/go-gogs-client/wiki/Administration-Users#create-a-public-key-for-user
 func CreatePublicKey(c *context.APIContext, form api.CreateKeyOption) {
 	u := user.GetUserByParams(c)
 	if c.Written() {

+ 55 - 28
routes/api/v1/api.go

@@ -216,7 +216,10 @@ func RegisterRoutes(m *macaron.Macaron) {
 			m.Get("/followers", user.ListMyFollowers)
 			m.Group("/following", func() {
 				m.Get("", user.ListMyFollowing)
-				m.Combo("/:username").Get(user.CheckMyFollowing).Put(user.Follow).Delete(user.Unfollow)
+				m.Combo("/:username").
+					Get(user.CheckMyFollowing).
+					Put(user.Follow).
+					Delete(user.Unfollow)
 			})
 
 			m.Group("/keys", func() {
@@ -228,7 +231,7 @@ func RegisterRoutes(m *macaron.Macaron) {
 					Delete(user.DeletePublicKey)
 			})
 
-			m.Combo("/issues").Get(repo.ListUserIssues)
+			m.Get("/issues", repo.ListUserIssues)
 		}, reqToken())
 
 		// Repositories
@@ -294,40 +297,54 @@ func RegisterRoutes(m *macaron.Macaron) {
 						Post(bind(api.CreateIssueOption{}), repo.CreateIssue)
 					m.Group("/comments", func() {
 						m.Get("", repo.ListRepoIssueComments)
-						m.Combo("/:id").Patch(bind(api.EditIssueCommentOption{}), repo.EditIssueComment)
+						m.Patch("/:id", bind(api.EditIssueCommentOption{}), repo.EditIssueComment)
 					})
 					m.Group("/:index", func() {
-						m.Combo("").Get(repo.GetIssue).Patch(bind(api.EditIssueOption{}), repo.EditIssue)
+						m.Combo("").
+							Get(repo.GetIssue).
+							Patch(bind(api.EditIssueOption{}), repo.EditIssue)
 
 						m.Group("/comments", func() {
-							m.Combo("").Get(repo.ListIssueComments).Post(bind(api.CreateIssueCommentOption{}), repo.CreateIssueComment)
-							m.Combo("/:id").Patch(bind(api.EditIssueCommentOption{}), repo.EditIssueComment).
+							m.Combo("").
+								Get(repo.ListIssueComments).
+								Post(bind(api.CreateIssueCommentOption{}), repo.CreateIssueComment)
+							m.Combo("/:id").
+								Patch(bind(api.EditIssueCommentOption{}), repo.EditIssueComment).
 								Delete(repo.DeleteIssueComment)
 						})
 
+						m.Get("/labels", repo.ListIssueLabels)
 						m.Group("/labels", func() {
-							m.Combo("").Get(repo.ListIssueLabels).
+							m.Combo("").
 								Post(bind(api.IssueLabelsOption{}), repo.AddIssueLabels).
 								Put(bind(api.IssueLabelsOption{}), repo.ReplaceIssueLabels).
 								Delete(repo.ClearIssueLabels)
 							m.Delete("/:id", repo.DeleteIssueLabel)
-						})
-
+						}, reqRepoWriter())
 					})
 				}, mustEnableIssues)
+
 				m.Group("/labels", func() {
-					m.Combo("").Get(repo.ListLabels).
-						Post(bind(api.CreateLabelOption{}), repo.CreateLabel)
-					m.Combo("/:id").Get(repo.GetLabel).Patch(bind(api.EditLabelOption{}), repo.EditLabel).
-						Delete(repo.DeleteLabel)
+					m.Get("", repo.ListLabels)
+					m.Get("/:id", repo.GetLabel)
 				})
+				m.Group("/labels", func() {
+					m.Post("", bind(api.CreateLabelOption{}), repo.CreateLabel)
+					m.Combo("/:id").
+						Patch(bind(api.EditLabelOption{}), repo.EditLabel).
+						Delete(repo.DeleteLabel)
+				}, reqRepoWriter())
+
 				m.Group("/milestones", func() {
-					m.Combo("").Get(repo.ListMilestones).
-						Post(reqRepoWriter(), bind(api.CreateMilestoneOption{}), repo.CreateMilestone)
-					m.Combo("/:id").Get(repo.GetMilestone).
-						Patch(reqRepoWriter(), bind(api.EditMilestoneOption{}), repo.EditMilestone).
-						Delete(reqRepoWriter(), repo.DeleteMilestone)
+					m.Get("", repo.ListMilestones)
+					m.Get("/:id", repo.GetMilestone)
 				})
+				m.Group("/milestones", func() {
+					m.Post("", bind(api.CreateMilestoneOption{}), repo.CreateMilestone)
+					m.Combo("/:id").
+						Patch(bind(api.EditMilestoneOption{}), repo.EditMilestone).
+						Delete(repo.DeleteMilestone)
+				}, reqRepoWriter())
 
 				m.Patch("/issue-tracker", reqRepoWriter(), bind(api.EditIssueTrackerOption{}), repo.IssueTracker)
 				m.Post("/mirror-sync", reqRepoWriter(), repo.MirrorSync)
@@ -338,24 +355,25 @@ func RegisterRoutes(m *macaron.Macaron) {
 		m.Get("/issues", reqToken(), repo.ListUserIssues)
 
 		// Organizations
-		m.Combo("/user/orgs", reqToken()).Get(org.ListMyOrgs).Post(bind(api.CreateOrgOption{}), org.CreateMyOrg)
+		m.Combo("/user/orgs", reqToken()).
+			Get(org.ListMyOrgs).
+			Post(bind(api.CreateOrgOption{}), org.CreateMyOrg)
 
 		m.Get("/users/:username/orgs", org.ListUserOrgs)
 		m.Group("/orgs/:orgname", func() {
-			m.Combo("").Get(org.Get).Patch(bind(api.EditOrgOption{}), org.Edit)
-			m.Combo("/teams").Get(org.ListTeams)
+			m.Combo("").
+				Get(org.Get).
+				Patch(bind(api.EditOrgOption{}), org.Edit)
+			m.Get("/teams", org.ListTeams)
 		}, orgAssignment(true))
 
-		m.Any("/*", func(c *context.Context) {
-			c.NotFound()
-		})
-
 		m.Group("/admin", func() {
 			m.Group("/users", func() {
 				m.Post("", bind(api.CreateUserOption{}), admin.CreateUser)
 
 				m.Group("/:username", func() {
-					m.Combo("").Patch(bind(api.EditUserOption{}), admin.EditUser).
+					m.Combo("").
+						Patch(bind(api.EditUserOption{}), admin.EditUser).
 						Delete(admin.DeleteUser)
 					m.Post("/keys", bind(api.CreateKeyOption{}), admin.CreatePublicKey)
 					m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg)
@@ -368,12 +386,21 @@ func RegisterRoutes(m *macaron.Macaron) {
 					m.Post("", orgAssignment(true), bind(api.CreateTeamOption{}), admin.CreateTeam)
 				})
 			})
+
 			m.Group("/teams", func() {
 				m.Group("/:teamid", func() {
-					m.Combo("/members/:username").Put(admin.AddTeamMember).Delete(admin.RemoveTeamMember)
-					m.Combo("/repos/:reponame").Put(admin.AddTeamRepository).Delete(admin.RemoveTeamRepository)
+					m.Combo("/members/:username").
+						Put(admin.AddTeamMember).
+						Delete(admin.RemoveTeamMember)
+					m.Combo("/repos/:reponame").
+						Put(admin.AddTeamRepository).
+						Delete(admin.RemoveTeamRepository)
 				}, orgAssignment(false, true))
 			})
 		}, reqAdmin())
+
+		m.Any("/*", func(c *context.Context) {
+			c.NotFound()
+		})
 	}, context.APIContexter())
 }

+ 10 - 13
routes/api/v1/org/org.go

@@ -5,6 +5,8 @@
 package org
 
 import (
+	"net/http"
+
 	api "github.com/gogs/go-gogs-client"
 
 	"github.com/gogs/gogs/models"
@@ -31,9 +33,9 @@ func CreateOrgForUser(c *context.APIContext, apiForm api.CreateOrgOption, user *
 		if models.IsErrUserAlreadyExist(err) ||
 			models.IsErrNameReserved(err) ||
 			models.IsErrNamePatternNotAllowed(err) {
-			c.Error(422, "", err)
+			c.Error(http.StatusUnprocessableEntity, "", err)
 		} else {
-			c.Error(500, "CreateOrganization", err)
+			c.ServerError("CreateOrganization", err)
 		}
 		return
 	}
@@ -43,7 +45,7 @@ func CreateOrgForUser(c *context.APIContext, apiForm api.CreateOrgOption, user *
 
 func listUserOrgs(c *context.APIContext, u *models.User, all bool) {
 	if err := u.GetOrganizations(all); err != nil {
-		c.Error(500, "GetOrganizations", err)
+		c.ServerError("GetOrganizations", err)
 		return
 	}
 
@@ -51,20 +53,17 @@ func listUserOrgs(c *context.APIContext, u *models.User, all bool) {
 	for i := range u.Orgs {
 		apiOrgs[i] = convert.ToOrganization(u.Orgs[i])
 	}
-	c.JSON(200, &apiOrgs)
+	c.JSONSuccess(&apiOrgs)
 }
 
-// https://github.com/gogs/go-gogs-client/wiki/Organizations#list-your-organizations
 func ListMyOrgs(c *context.APIContext) {
 	listUserOrgs(c, c.User, true)
 }
 
-// https://github.com/gogs/go-gogs-client/wiki/Organizations#create-your-organization
 func CreateMyOrg(c *context.APIContext, apiForm api.CreateOrgOption) {
 	CreateOrgForUser(c, apiForm, c.User)
 }
 
-// https://github.com/gogs/go-gogs-client/wiki/Organizations#list-user-organizations
 func ListUserOrgs(c *context.APIContext) {
 	u := user.GetUserByParams(c)
 	if c.Written() {
@@ -73,16 +72,14 @@ func ListUserOrgs(c *context.APIContext) {
 	listUserOrgs(c, u, false)
 }
 
-// https://github.com/gogs/go-gogs-client/wiki/Organizations#get-an-organization
 func Get(c *context.APIContext) {
-	c.JSON(200, convert.ToOrganization(c.Org.Organization))
+	c.JSONSuccess(convert.ToOrganization(c.Org.Organization))
 }
 
-// https://github.com/gogs/go-gogs-client/wiki/Organizations#edit-an-organization
 func Edit(c *context.APIContext, form api.EditOrgOption) {
 	org := c.Org.Organization
 	if !org.IsOwnedBy(c.User.ID) {
-		c.Status(403)
+		c.Status(http.StatusForbidden)
 		return
 	}
 
@@ -91,9 +88,9 @@ func Edit(c *context.APIContext, form api.EditOrgOption) {
 	org.Website = form.Website
 	org.Location = form.Location
 	if err := models.UpdateUser(org); err != nil {
-		c.Error(500, "UpdateUser", err)
+		c.ServerError("UpdateUser", err)
 		return
 	}
 
-	c.JSON(200, convert.ToOrganization(org))
+	c.JSONSuccess(convert.ToOrganization(org))
 }

+ 8 - 18
routes/api/v1/repo/file.go

@@ -12,38 +12,32 @@ import (
 	"github.com/gogs/gogs/routes/repo"
 )
 
-// https://github.com/gogs/go-gogs-client/wiki/Repositories-Contents#download-raw-content
 func GetRawFile(c *context.APIContext) {
 	if !c.Repo.HasAccess() {
-		c.Status(404)
+		c.NotFound()
 		return
 	}
 
 	if c.Repo.Repository.IsBare {
-		c.Status(404)
+		c.NotFound()
 		return
 	}
 
 	blob, err := c.Repo.Commit.GetBlobByPath(c.Repo.TreePath)
 	if err != nil {
-		if git.IsErrNotExist(err) {
-			c.Status(404)
-		} else {
-			c.Error(500, "GetBlobByPath", err)
-		}
+		c.NotFoundOrServerError("GetBlobByPath", git.IsErrNotExist, err)
 		return
 	}
 	if err = repo.ServeBlob(c.Context, blob); err != nil {
-		c.Error(500, "ServeBlob", err)
+		c.ServerError("ServeBlob", err)
 	}
 }
 
-// https://github.com/gogs/go-gogs-client/wiki/Repositories-Contents#download-archive
 func GetArchive(c *context.APIContext) {
 	repoPath := models.RepoPath(c.Params(":username"), c.Params(":reponame"))
 	gitRepo, err := git.OpenRepository(repoPath)
 	if err != nil {
-		c.Error(500, "OpenRepository", err)
+		c.ServerError("OpenRepository", err)
 		return
 	}
 	c.Repo.GitRepo = gitRepo
@@ -54,19 +48,15 @@ func GetArchive(c *context.APIContext) {
 func GetEditorconfig(c *context.APIContext) {
 	ec, err := c.Repo.GetEditorconfig()
 	if err != nil {
-		if git.IsErrNotExist(err) {
-			c.Error(404, "GetEditorconfig", err)
-		} else {
-			c.Error(500, "GetEditorconfig", err)
-		}
+		c.NotFoundOrServerError("GetEditorconfig", git.IsErrNotExist, err)
 		return
 	}
 
 	fileName := c.Params("filename")
 	def := ec.GetDefinitionForFilename(fileName)
 	if def == nil {
-		c.Error(404, "GetDefinitionForFilename", err)
+		c.NotFound()
 		return
 	}
-	c.JSON(200, def)
+	c.JSONSuccess(def)
 }

+ 21 - 28
routes/api/v1/repo/issue_comment.go

@@ -4,6 +4,7 @@
 package repo
 
 import (
+	"net/http"
 	"time"
 
 	api "github.com/gogs/go-gogs-client"
@@ -18,7 +19,7 @@ func ListIssueComments(c *context.APIContext) {
 		var err error
 		since, err = time.Parse(time.RFC3339, c.Query("since"))
 		if err != nil {
-			c.Error(422, "", err)
+			c.Error(http.StatusUnprocessableEntity, "", err)
 			return
 		}
 	}
@@ -26,13 +27,13 @@ func ListIssueComments(c *context.APIContext) {
 	// comments,err:=models.GetCommentsByIssueIDSince(, since)
 	issue, err := models.GetRawIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
 	if err != nil {
-		c.Error(500, "GetRawIssueByIndex", err)
+		c.ServerError("GetRawIssueByIndex", err)
 		return
 	}
 
 	comments, err := models.GetCommentsByIssueIDSince(issue.ID, since.Unix())
 	if err != nil {
-		c.Error(500, "GetCommentsByIssueIDSince", err)
+		c.ServerError("GetCommentsByIssueIDSince", err)
 		return
 	}
 
@@ -40,7 +41,7 @@ func ListIssueComments(c *context.APIContext) {
 	for i := range comments {
 		apiComments[i] = comments[i].APIFormat()
 	}
-	c.JSON(200, &apiComments)
+	c.JSONSuccess(&apiComments)
 }
 
 func ListRepoIssueComments(c *context.APIContext) {
@@ -49,14 +50,14 @@ func ListRepoIssueComments(c *context.APIContext) {
 		var err error
 		since, err = time.Parse(time.RFC3339, c.Query("since"))
 		if err != nil {
-			c.Error(422, "", err)
+			c.Error(http.StatusUnprocessableEntity, "", err)
 			return
 		}
 	}
 
 	comments, err := models.GetCommentsByRepoIDSince(c.Repo.Repository.ID, since.Unix())
 	if err != nil {
-		c.Error(500, "GetCommentsByRepoIDSince", err)
+		c.ServerError("GetCommentsByRepoIDSince", err)
 		return
 	}
 
@@ -64,75 +65,67 @@ func ListRepoIssueComments(c *context.APIContext) {
 	for i := range comments {
 		apiComments[i] = comments[i].APIFormat()
 	}
-	c.JSON(200, &apiComments)
+	c.JSONSuccess(&apiComments)
 }
 
 func CreateIssueComment(c *context.APIContext, form api.CreateIssueCommentOption) {
 	issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
 	if err != nil {
-		c.Error(500, "GetIssueByIndex", err)
+		c.ServerError("GetIssueByIndex", err)
 		return
 	}
 
 	comment, err := models.CreateIssueComment(c.User, c.Repo.Repository, issue, form.Body, nil)
 	if err != nil {
-		c.Error(500, "CreateIssueComment", err)
+		c.ServerError("CreateIssueComment", err)
 		return
 	}
 
-	c.JSON(201, comment.APIFormat())
+	c.JSON(http.StatusCreated, comment.APIFormat())
 }
 
 func EditIssueComment(c *context.APIContext, form api.EditIssueCommentOption) {
 	comment, err := models.GetCommentByID(c.ParamsInt64(":id"))
 	if err != nil {
-		if models.IsErrCommentNotExist(err) {
-			c.Error(404, "GetCommentByID", err)
-		} else {
-			c.Error(500, "GetCommentByID", err)
-		}
+		c.NotFoundOrServerError("GetCommentByID", models.IsErrCommentNotExist, err)
 		return
 	}
 
 	if c.User.ID != comment.PosterID && !c.Repo.IsAdmin() {
-		c.Status(403)
+		c.Status(http.StatusForbidden)
 		return
 	} else if comment.Type != models.COMMENT_TYPE_COMMENT {
-		c.Status(204)
+		c.NoContent()
 		return
 	}
 
 	oldContent := comment.Content
 	comment.Content = form.Body
 	if err := models.UpdateComment(c.User, comment, oldContent); err != nil {
-		c.Error(500, "UpdateComment", err)
+		c.ServerError("UpdateComment", err)
 		return
 	}
-	c.JSON(200, comment.APIFormat())
+	c.JSONSuccess(comment.APIFormat())
 }
 
 func DeleteIssueComment(c *context.APIContext) {
 	comment, err := models.GetCommentByID(c.ParamsInt64(":id"))
 	if err != nil {
-		if models.IsErrCommentNotExist(err) {
-			c.Error(404, "GetCommentByID", err)
-		} else {
-			c.Error(500, "GetCommentByID", err)
-		}
+		c.NotFoundOrServerError("GetCommentByID", models.IsErrCommentNotExist, err)
 		return
 	}
 
 	if c.User.ID != comment.PosterID && !c.Repo.IsAdmin() {
-		c.Status(403)
+		c.Status(http.StatusForbidden)
 		return
 	} else if comment.Type != models.COMMENT_TYPE_COMMENT {
-		c.Status(204)
+		c.NoContent()
 		return
 	}
 
 	if err = models.DeleteCommentByID(c.User, comment.ID); err != nil {
-		c.Error(500, "DeleteCommentByID", err)
+		c.ServerError("DeleteCommentByID", err)
 		return
 	}
-	c.Status(204)
+	c.NoContent()
 }

+ 22 - 60
routes/api/v1/repo/issue_label.go

@@ -5,6 +5,8 @@
 package repo
 
 import (
+	"net/http"
+
 	api "github.com/gogs/go-gogs-client"
 
 	"github.com/gogs/gogs/models"
@@ -15,11 +17,7 @@ import (
 func ListIssueLabels(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
 	}
 
@@ -27,39 +25,30 @@ func ListIssueLabels(c *context.APIContext) {
 	for i := range issue.Labels {
 		apiLabels[i] = issue.Labels[i].APIFormat()
 	}
-	c.JSON(200, &apiLabels)
+	c.JSONSuccess(&apiLabels)
 }
 
 func AddIssueLabels(c *context.APIContext, form api.IssueLabelsOption) {
-	if !c.Repo.IsWriter() {
-		c.Status(403)
-		return
-	}
-
 	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
 	}
 
 	labels, err := models.GetLabelsInRepoByIDs(c.Repo.Repository.ID, form.Labels)
 	if err != nil {
-		c.Error(500, "GetLabelsInRepoByIDs", err)
+		c.ServerError("GetLabelsInRepoByIDs", err)
 		return
 	}
 
 	if err = issue.AddLabels(c.User, labels); err != nil {
-		c.Error(500, "AddLabels", err)
+		c.ServerError("AddLabels", err)
 		return
 	}
 
 	labels, err = models.GetLabelsByIssueID(issue.ID)
 	if err != nil {
-		c.Error(500, "GetLabelsByIssueID", err)
+		c.ServerError("GetLabelsByIssueID", err)
 		return
 	}
 
@@ -67,73 +56,55 @@ func AddIssueLabels(c *context.APIContext, form api.IssueLabelsOption) {
 	for i := range labels {
 		apiLabels[i] = issue.Labels[i].APIFormat()
 	}
-	c.JSON(200, &apiLabels)
+	c.JSONSuccess(&apiLabels)
 }
 
 func DeleteIssueLabel(c *context.APIContext) {
-	if !c.Repo.IsWriter() {
-		c.Status(403)
-		return
-	}
-
 	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
 	}
 
 	label, err := models.GetLabelOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
 	if err != nil {
 		if models.IsErrLabelNotExist(err) {
-			c.Error(422, "", err)
+			c.Error(http.StatusUnprocessableEntity, "", err)
 		} else {
-			c.Error(500, "GetLabelInRepoByID", err)
+			c.ServerError("GetLabelInRepoByID", err)
 		}
 		return
 	}
 
 	if err := models.DeleteIssueLabel(issue, label); err != nil {
-		c.Error(500, "DeleteIssueLabel", err)
+		c.ServerError("DeleteIssueLabel", err)
 		return
 	}
 
-	c.Status(204)
+	c.NoContent()
 }
 
 func ReplaceIssueLabels(c *context.APIContext, form api.IssueLabelsOption) {
-	if !c.Repo.IsWriter() {
-		c.Status(403)
-		return
-	}
-
 	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
 	}
 
 	labels, err := models.GetLabelsInRepoByIDs(c.Repo.Repository.ID, form.Labels)
 	if err != nil {
-		c.Error(500, "GetLabelsInRepoByIDs", err)
+		c.ServerError("GetLabelsInRepoByIDs", err)
 		return
 	}
 
 	if err := issue.ReplaceLabels(labels); err != nil {
-		c.Error(500, "ReplaceLabels", err)
+		c.ServerError("ReplaceLabels", err)
 		return
 	}
 
 	labels, err = models.GetLabelsByIssueID(issue.ID)
 	if err != nil {
-		c.Error(500, "GetLabelsByIssueID", err)
+		c.ServerError("GetLabelsByIssueID", err)
 		return
 	}
 
@@ -141,29 +112,20 @@ func ReplaceIssueLabels(c *context.APIContext, form api.IssueLabelsOption) {
 	for i := range labels {
 		apiLabels[i] = issue.Labels[i].APIFormat()
 	}
-	c.JSON(200, &apiLabels)
+	c.JSONSuccess(&apiLabels)
 }
 
 func ClearIssueLabels(c *context.APIContext) {
-	if !c.Repo.IsWriter() {
-		c.Status(403)
-		return
-	}
-
 	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 err := issue.ClearLabels(c.User); err != nil {
-		c.Error(500, "ClearLabels", err)
+		c.ServerError("ClearLabels", err)
 		return
 	}
 
-	c.Status(204)
+	c.NoContent()
 }

+ 13 - 34
routes/api/v1/repo/label.go

@@ -5,6 +5,8 @@
 package repo
 
 import (
+	"net/http"
+
 	"github.com/Unknwon/com"
 
 	api "github.com/gogs/go-gogs-client"
@@ -16,7 +18,7 @@ import (
 func ListLabels(c *context.APIContext) {
 	labels, err := models.GetLabelsByRepoID(c.Repo.Repository.ID)
 	if err != nil {
-		c.Error(500, "GetLabelsByRepoID", err)
+		c.ServerError("GetLabelsByRepoID", err)
 		return
 	}
 
@@ -24,7 +26,7 @@ func ListLabels(c *context.APIContext) {
 	for i := range labels {
 		apiLabels[i] = labels[i].APIFormat()
 	}
-	c.JSON(200, &apiLabels)
+	c.JSONSuccess(&apiLabels)
 }
 
 func GetLabel(c *context.APIContext) {
@@ -37,48 +39,30 @@ func GetLabel(c *context.APIContext) {
 		label, err = models.GetLabelOfRepoByName(c.Repo.Repository.ID, idStr)
 	}
 	if err != nil {
-		if models.IsErrLabelNotExist(err) {
-			c.Status(404)
-		} else {
-			c.Error(500, "GetLabelByRepoID", err)
-		}
+		c.NotFoundOrServerError("GetLabel", models.IsErrLabelNotExist, err)
 		return
 	}
 
-	c.JSON(200, label.APIFormat())
+	c.JSONSuccess(label.APIFormat())
 }
 
 func CreateLabel(c *context.APIContext, form api.CreateLabelOption) {
-	if !c.Repo.IsWriter() {
-		c.Status(403)
-		return
-	}
-
 	label := &models.Label{
 		Name:   form.Name,
 		Color:  form.Color,
 		RepoID: c.Repo.Repository.ID,
 	}
 	if err := models.NewLabels(label); err != nil {
-		c.Error(500, "NewLabel", err)
+		c.ServerError("NewLabel", err)
 		return
 	}
-	c.JSON(201, label.APIFormat())
+	c.JSON(http.StatusCreated, label.APIFormat())
 }
 
 func EditLabel(c *context.APIContext, form api.EditLabelOption) {
-	if !c.Repo.IsWriter() {
-		c.Status(403)
-		return
-	}
-
 	label, err := models.GetLabelOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
 	if err != nil {
-		if models.IsErrLabelNotExist(err) {
-			c.Status(404)
-		} else {
-			c.Error(500, "GetLabelByRepoID", err)
-		}
+		c.NotFoundOrServerError("GetLabelOfRepoByID", models.IsErrLabelNotExist, err)
 		return
 	}
 
@@ -89,22 +73,17 @@ func EditLabel(c *context.APIContext, form api.EditLabelOption) {
 		label.Color = *form.Color
 	}
 	if err := models.UpdateLabel(label); err != nil {
-		c.Handle(500, "UpdateLabel", err)
+		c.ServerError("UpdateLabel", err)
 		return
 	}
-	c.JSON(200, label.APIFormat())
+	c.JSONSuccess(label.APIFormat())
 }
 
 func DeleteLabel(c *context.APIContext) {
-	if !c.Repo.IsWriter() {
-		c.Status(403)
-		return
-	}
-
 	if err := models.DeleteLabel(c.Repo.Repository.ID, c.ParamsInt64(":id")); err != nil {
-		c.Error(500, "DeleteLabel", err)
+		c.ServerError("DeleteLabel", err)
 		return
 	}
 
-	c.Status(204)
+	c.NoContent()
 }

+ 13 - 20
routes/api/v1/repo/milestone.go

@@ -5,6 +5,7 @@
 package repo
 
 import (
+	"net/http"
 	"time"
 
 	api "github.com/gogs/go-gogs-client"
@@ -16,7 +17,7 @@ import (
 func ListMilestones(c *context.APIContext) {
 	milestones, err := models.GetMilestonesByRepoID(c.Repo.Repository.ID)
 	if err != nil {
-		c.Error(500, "GetMilestonesByRepoID", err)
+		c.ServerError("GetMilestonesByRepoID", err)
 		return
 	}
 
@@ -24,20 +25,16 @@ func ListMilestones(c *context.APIContext) {
 	for i := range milestones {
 		apiMilestones[i] = milestones[i].APIFormat()
 	}
-	c.JSON(200, &apiMilestones)
+	c.JSONSuccess(&apiMilestones)
 }
 
 func GetMilestone(c *context.APIContext) {
 	milestone, err := models.GetMilestoneByRepoID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
 	if err != nil {
-		if models.IsErrMilestoneNotExist(err) {
-			c.Status(404)
-		} else {
-			c.Error(500, "GetMilestoneByRepoID", err)
-		}
+		c.NotFoundOrServerError("GetMilestoneByRepoID", models.IsErrMilestoneNotExist, err)
 		return
 	}
-	c.JSON(200, milestone.APIFormat())
+	c.JSONSuccess(milestone.APIFormat())
 }
 
 func CreateMilestone(c *context.APIContext, form api.CreateMilestoneOption) {
@@ -54,20 +51,16 @@ func CreateMilestone(c *context.APIContext, form api.CreateMilestoneOption) {
 	}
 
 	if err := models.NewMilestone(milestone); err != nil {
-		c.Error(500, "NewMilestone", err)
+		c.ServerError("NewMilestone", err)
 		return
 	}
-	c.JSON(201, milestone.APIFormat())
+	c.JSON(http.StatusCreated, milestone.APIFormat())
 }
 
 func EditMilestone(c *context.APIContext, form api.EditMilestoneOption) {
 	milestone, err := models.GetMilestoneByRepoID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
 	if err != nil {
-		if models.IsErrMilestoneNotExist(err) {
-			c.Status(404)
-		} else {
-			c.Error(500, "GetMilestoneByRepoID", err)
-		}
+		c.NotFoundOrServerError("GetMilestoneByRepoID", models.IsErrMilestoneNotExist, err)
 		return
 	}
 
@@ -83,21 +76,21 @@ func EditMilestone(c *context.APIContext, form api.EditMilestoneOption) {
 
 	if form.State != nil {
 		if err = milestone.ChangeStatus(api.STATE_CLOSED == api.StateType(*form.State)); err != nil {
-			c.Error(500, "ChangeStatus", err)
+			c.ServerError("ChangeStatus", err)
 			return
 		}
 	} else if err = models.UpdateMilestone(milestone); err != nil {
-		c.Handle(500, "UpdateMilestone", err)
+		c.ServerError("UpdateMilestone", err)
 		return
 	}
 
-	c.JSON(200, milestone.APIFormat())
+	c.JSONSuccess(milestone.APIFormat())
 }
 
 func DeleteMilestone(c *context.APIContext) {
 	if err := models.DeleteMilestoneOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id")); err != nil {
-		c.Error(500, "DeleteMilestoneByRepoID", err)
+		c.ServerError("DeleteMilestoneByRepoID", err)
 		return
 	}
-	c.Status(204)
+	c.NoContent()
 }

+ 1 - 5
routes/api/v1/user/user.go

@@ -58,11 +58,7 @@ func Search(c *context.APIContext) {
 func GetInfo(c *context.APIContext) {
 	u, err := models.GetUserByName(c.Params(":username"))
 	if err != nil {
-		if errors.IsUserNotExist(err) {
-			c.NotFound()
-		} else {
-			c.ServerError("GetUserByName", err)
-		}
+		c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
 		return
 	}
 

+ 1 - 1
templates/.VERSION

@@ -1 +1 @@
-0.11.91.0808
+0.11.91.0810