diff options
author | unknwon <u@gogs.io> | 2019-08-10 13:40:48 -0700 |
---|---|---|
committer | unknwon <u@gogs.io> | 2019-08-10 13:40:48 -0700 |
commit | f1e0ebfe937213be2af4a3180fae9f43949de00e (patch) | |
tree | 3b462bd02b3c5d28099ca5e47e21370d8c02688a /routes/api/v1/repo | |
parent | c7ba519af2f2d8e79077d2776cb34f8b61556471 (diff) |
routes/api/v1: codemod
Diffstat (limited to 'routes/api/v1/repo')
-rw-r--r-- | routes/api/v1/repo/file.go | 26 | ||||
-rw-r--r-- | routes/api/v1/repo/issue_comment.go | 49 | ||||
-rw-r--r-- | routes/api/v1/repo/issue_label.go | 82 | ||||
-rw-r--r-- | routes/api/v1/repo/label.go | 47 | ||||
-rw-r--r-- | routes/api/v1/repo/milestone.go | 33 |
5 files changed, 77 insertions, 160 deletions
diff --git a/routes/api/v1/repo/file.go b/routes/api/v1/repo/file.go index 5afb1113..744db738 100644 --- a/routes/api/v1/repo/file.go +++ b/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) } diff --git a/routes/api/v1/repo/issue_comment.go b/routes/api/v1/repo/issue_comment.go index 6b2612ae..83080492 100644 --- a/routes/api/v1/repo/issue_comment.go +++ b/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() } diff --git a/routes/api/v1/repo/issue_label.go b/routes/api/v1/repo/issue_label.go index c210825b..60b49e5d 100644 --- a/routes/api/v1/repo/issue_label.go +++ b/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() } diff --git a/routes/api/v1/repo/label.go b/routes/api/v1/repo/label.go index cd661aa2..833c0822 100644 --- a/routes/api/v1/repo/label.go +++ b/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() } diff --git a/routes/api/v1/repo/milestone.go b/routes/api/v1/repo/milestone.go index 4c73d8a3..d91e800c 100644 --- a/routes/api/v1/repo/milestone.go +++ b/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() } |