routes/api/v1: codemod

pull/5786/head
unknwon 2019-08-10 13:40:48 -07:00
parent c7ba519af2
commit f1e0ebfe93
No known key found for this signature in database
GPG Key ID: 25B575AE3213B2B3
15 changed files with 175 additions and 244 deletions

View File

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

View File

@ -12,7 +12,6 @@ import (
"github.com/gogs/gogs/routes/api/v1/user" "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) { func CreateOrg(c *context.APIContext, form api.CreateOrgOption) {
org.CreateOrgForUser(c, form, user.GetUserByParams(c)) org.CreateOrgForUser(c, form, user.GetUserByParams(c))
} }

View File

@ -13,11 +13,7 @@ import (
func GetRepositoryByParams(c *context.APIContext) *models.Repository { func GetRepositoryByParams(c *context.APIContext) *models.Repository {
repo, err := models.GetRepositoryByName(c.Org.Team.OrgID, c.Params(":reponame")) repo, err := models.GetRepositoryByName(c.Org.Team.OrgID, c.Params(":reponame"))
if err != nil { if err != nil {
if errors.IsRepoNotExist(err) { c.NotFoundOrServerError("GetRepositoryByName", errors.IsRepoNotExist, err)
c.Status(404)
} else {
c.Error(500, "GetRepositoryByName", err)
}
return nil return nil
} }
return repo return repo
@ -29,11 +25,11 @@ func AddTeamRepository(c *context.APIContext) {
return return
} }
if err := c.Org.Team.AddRepository(repo); err != nil { if err := c.Org.Team.AddRepository(repo); err != nil {
c.Error(500, "AddRepository", err) c.ServerError("AddRepository", err)
return return
} }
c.Status(204) c.NoContent()
} }
func RemoveTeamRepository(c *context.APIContext) { func RemoveTeamRepository(c *context.APIContext) {
@ -42,9 +38,9 @@ func RemoveTeamRepository(c *context.APIContext) {
return return
} }
if err := c.Org.Team.RemoveRepository(repo.ID); err != nil { if err := c.Org.Team.RemoveRepository(repo.ID); err != nil {
c.Error(500, "RemoveRepository", err) c.ServerError("RemoveRepository", err)
return return
} }
c.Status(204) c.NoContent()
} }

View File

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

View File

@ -12,7 +12,6 @@ import (
"github.com/gogs/gogs/routes/api/v1/user" "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) { func CreateRepo(c *context.APIContext, form api.CreateRepoOption) {
owner := user.GetUserByParams(c) owner := user.GetUserByParams(c)
if c.Written() { if c.Written() {

View File

@ -5,6 +5,8 @@
package admin package admin
import ( import (
"net/http"
log "gopkg.in/clog.v1" log "gopkg.in/clog.v1"
api "github.com/gogs/go-gogs-client" 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) source, err := models.GetLoginSourceByID(sourceID)
if err != nil { if err != nil {
if errors.IsLoginSourceNotExist(err) { if errors.IsLoginSourceNotExist(err) {
c.Error(422, "", err) c.Error(http.StatusUnprocessableEntity, "", err)
} else { } else {
c.Error(500, "GetLoginSourceByID", err) c.ServerError("GetLoginSourceByID", err)
} }
return return
} }
@ -37,7 +39,6 @@ func parseLoginSource(c *context.APIContext, u *models.User, sourceID int64, log
u.LoginName = loginName 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) { func CreateUser(c *context.APIContext, form api.CreateUserOption) {
u := &models.User{ u := &models.User{
Name: form.Username, Name: form.Username,
@ -58,23 +59,22 @@ func CreateUser(c *context.APIContext, form api.CreateUserOption) {
models.IsErrEmailAlreadyUsed(err) || models.IsErrEmailAlreadyUsed(err) ||
models.IsErrNameReserved(err) || models.IsErrNameReserved(err) ||
models.IsErrNamePatternNotAllowed(err) { models.IsErrNamePatternNotAllowed(err) {
c.Error(422, "", err) c.Error(http.StatusUnprocessableEntity, "", err)
} else { } else {
c.Error(500, "CreateUser", err) c.ServerError("CreateUser", err)
} }
return 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. // Send email notification.
if form.SendNotify && setting.MailService != nil { if form.SendNotify && setting.MailService != nil {
mailer.SendRegisterNotifyMail(c.Context.Context, models.NewMailerUser(u)) 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) { func EditUser(c *context.APIContext, form api.EditUserOption) {
u := user.GetUserByParams(c) u := user.GetUserByParams(c)
if c.Written() { if c.Written() {
@ -90,7 +90,7 @@ func EditUser(c *context.APIContext, form api.EditUserOption) {
u.Passwd = form.Password u.Passwd = form.Password
var err error var err error
if u.Salt, err = models.GetUserSalt(); err != nil { if u.Salt, err = models.GetUserSalt(); err != nil {
c.Error(500, "UpdateUser", err) c.ServerError("GetUserSalt", err)
return return
} }
u.EncodePasswd() u.EncodePasswd()
@ -119,18 +119,17 @@ func EditUser(c *context.APIContext, form api.EditUserOption) {
if err := models.UpdateUser(u); err != nil { if err := models.UpdateUser(u); err != nil {
if models.IsErrEmailAlreadyUsed(err) { if models.IsErrEmailAlreadyUsed(err) {
c.Error(422, "", err) c.Error(http.StatusUnprocessableEntity, "", err)
} else { } else {
c.Error(500, "UpdateUser", err) c.ServerError("UpdateUser", err)
} }
return 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) { func DeleteUser(c *context.APIContext) {
u := user.GetUserByParams(c) u := user.GetUserByParams(c)
if c.Written() { if c.Written() {
@ -140,18 +139,17 @@ func DeleteUser(c *context.APIContext) {
if err := models.DeleteUser(u); err != nil { if err := models.DeleteUser(u); err != nil {
if models.IsErrUserOwnRepos(err) || if models.IsErrUserOwnRepos(err) ||
models.IsErrUserHasOrgs(err) { models.IsErrUserHasOrgs(err) {
c.Error(422, "", err) c.Error(http.StatusUnprocessableEntity, "", err)
} else { } else {
c.Error(500, "DeleteUser", err) c.ServerError("DeleteUser", err)
} }
return return
} }
log.Trace("Account deleted by admin(%s): %s", c.User.Name, u.Name) 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) { func CreatePublicKey(c *context.APIContext, form api.CreateKeyOption) {
u := user.GetUserByParams(c) u := user.GetUserByParams(c)
if c.Written() { if c.Written() {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1 +1 @@
0.11.91.0808 0.11.91.0810