diff options
Diffstat (limited to 'routers/api/v1/repo')
-rw-r--r-- | routers/api/v1/repo/branch.go | 28 | ||||
-rw-r--r-- | routers/api/v1/repo/collaborators.go | 56 | ||||
-rw-r--r-- | routers/api/v1/repo/file.go | 44 | ||||
-rw-r--r-- | routers/api/v1/repo/hook.go | 56 | ||||
-rw-r--r-- | routers/api/v1/repo/issue.go | 100 | ||||
-rw-r--r-- | routers/api/v1/repo/issue_comment.go | 78 | ||||
-rw-r--r-- | routers/api/v1/repo/issue_label.go | 96 | ||||
-rw-r--r-- | routers/api/v1/repo/key.go | 62 | ||||
-rw-r--r-- | routers/api/v1/repo/label.go | 58 | ||||
-rw-r--r-- | routers/api/v1/repo/milestone.go | 48 | ||||
-rw-r--r-- | routers/api/v1/repo/repo.go | 192 |
11 files changed, 409 insertions, 409 deletions
diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index df950040..3d5e646c 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -13,43 +13,43 @@ import ( ) // https://github.com/gogits/go-gogs-client/wiki/Repositories#get-branch -func GetBranch(ctx *context.APIContext) { - branch, err := ctx.Repo.Repository.GetBranch(ctx.Params("*")) +func GetBranch(c *context.APIContext) { + branch, err := c.Repo.Repository.GetBranch(c.Params("*")) if err != nil { if models.IsErrBranchNotExist(err) { - ctx.Error(404, "GetBranch", err) + c.Error(404, "GetBranch", err) } else { - ctx.Error(500, "GetBranch", err) + c.Error(500, "GetBranch", err) } return } - c, err := branch.GetCommit() + commit, err := branch.GetCommit() if err != nil { - ctx.Error(500, "GetCommit", err) + c.Error(500, "GetCommit", err) return } - ctx.JSON(200, convert.ToBranch(branch, c)) + c.JSON(200, convert.ToBranch(branch, commit)) } // https://github.com/gogits/go-gogs-client/wiki/Repositories#list-branches -func ListBranches(ctx *context.APIContext) { - branches, err := ctx.Repo.Repository.GetBranches() +func ListBranches(c *context.APIContext) { + branches, err := c.Repo.Repository.GetBranches() if err != nil { - ctx.Error(500, "GetBranches", err) + c.Error(500, "GetBranches", err) return } apiBranches := make([]*api.Branch, len(branches)) for i := range branches { - c, err := branches[i].GetCommit() + commit, err := branches[i].GetCommit() if err != nil { - ctx.Error(500, "GetCommit", err) + c.Error(500, "GetCommit", err) return } - apiBranches[i] = convert.ToBranch(branches[i], c) + apiBranches[i] = convert.ToBranch(branches[i], commit) } - ctx.JSON(200, &apiBranches) + c.JSON(200, &apiBranches) } diff --git a/routers/api/v1/repo/collaborators.go b/routers/api/v1/repo/collaborators.go index ff109be5..d295ac0f 100644 --- a/routers/api/v1/repo/collaborators.go +++ b/routers/api/v1/repo/collaborators.go @@ -12,13 +12,13 @@ import ( "github.com/gogits/gogs/pkg/context" ) -func ListCollaborators(ctx *context.APIContext) { - collaborators, err := ctx.Repo.Repository.GetCollaborators() +func ListCollaborators(c *context.APIContext) { + collaborators, err := c.Repo.Repository.GetCollaborators() if err != nil { if errors.IsUserNotExist(err) { - ctx.Error(422, "", err) + c.Error(422, "", err) } else { - ctx.Error(500, "GetCollaborators", err) + c.Error(500, "GetCollaborators", err) } return } @@ -27,68 +27,68 @@ func ListCollaborators(ctx *context.APIContext) { for i := range collaborators { apiCollaborators[i] = collaborators[i].APIFormat() } - ctx.JSON(200, &apiCollaborators) + c.JSON(200, &apiCollaborators) } -func AddCollaborator(ctx *context.APIContext, form api.AddCollaboratorOption) { - collaborator, err := models.GetUserByName(ctx.Params(":collaborator")) +func AddCollaborator(c *context.APIContext, form api.AddCollaboratorOption) { + collaborator, err := models.GetUserByName(c.Params(":collaborator")) if err != nil { if errors.IsUserNotExist(err) { - ctx.Error(422, "", err) + c.Error(422, "", err) } else { - ctx.Error(500, "GetUserByName", err) + c.Error(500, "GetUserByName", err) } return } - if err := ctx.Repo.Repository.AddCollaborator(collaborator); err != nil { - ctx.Error(500, "AddCollaborator", err) + if err := c.Repo.Repository.AddCollaborator(collaborator); err != nil { + c.Error(500, "AddCollaborator", err) return } if form.Permission != nil { - if err := ctx.Repo.Repository.ChangeCollaborationAccessMode(collaborator.ID, models.ParseAccessMode(*form.Permission)); err != nil { - ctx.Error(500, "ChangeCollaborationAccessMode", err) + if err := c.Repo.Repository.ChangeCollaborationAccessMode(collaborator.ID, models.ParseAccessMode(*form.Permission)); err != nil { + c.Error(500, "ChangeCollaborationAccessMode", err) return } } - ctx.Status(204) + c.Status(204) } -func IsCollaborator(ctx *context.APIContext) { - collaborator, err := models.GetUserByName(ctx.Params(":collaborator")) +func IsCollaborator(c *context.APIContext) { + collaborator, err := models.GetUserByName(c.Params(":collaborator")) if err != nil { if errors.IsUserNotExist(err) { - ctx.Error(422, "", err) + c.Error(422, "", err) } else { - ctx.Error(500, "GetUserByName", err) + c.Error(500, "GetUserByName", err) } return } - if !ctx.Repo.Repository.IsCollaborator(collaborator.ID) { - ctx.Status(404) + if !c.Repo.Repository.IsCollaborator(collaborator.ID) { + c.Status(404) } else { - ctx.Status(204) + c.Status(204) } } -func DeleteCollaborator(ctx *context.APIContext) { - collaborator, err := models.GetUserByName(ctx.Params(":collaborator")) +func DeleteCollaborator(c *context.APIContext) { + collaborator, err := models.GetUserByName(c.Params(":collaborator")) if err != nil { if errors.IsUserNotExist(err) { - ctx.Error(422, "", err) + c.Error(422, "", err) } else { - ctx.Error(500, "GetUserByName", err) + c.Error(500, "GetUserByName", err) } return } - if err := ctx.Repo.Repository.DeleteCollaboration(collaborator.ID); err != nil { - ctx.Error(500, "DeleteCollaboration", err) + if err := c.Repo.Repository.DeleteCollaboration(collaborator.ID); err != nil { + c.Error(500, "DeleteCollaboration", err) return } - ctx.Status(204) + c.Status(204) } diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index 81c399f6..df6a4857 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -13,60 +13,60 @@ import ( ) // https://github.com/gogits/go-gogs-client/wiki/Repositories-Contents#download-raw-content -func GetRawFile(ctx *context.APIContext) { - if !ctx.Repo.HasAccess() { - ctx.Status(404) +func GetRawFile(c *context.APIContext) { + if !c.Repo.HasAccess() { + c.Status(404) return } - if ctx.Repo.Repository.IsBare { - ctx.Status(404) + if c.Repo.Repository.IsBare { + c.Status(404) return } - blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath) + blob, err := c.Repo.Commit.GetBlobByPath(c.Repo.TreePath) if err != nil { if git.IsErrNotExist(err) { - ctx.Status(404) + c.Status(404) } else { - ctx.Error(500, "GetBlobByPath", err) + c.Error(500, "GetBlobByPath", err) } return } - if err = repo.ServeBlob(ctx.Context, blob); err != nil { - ctx.Error(500, "ServeBlob", err) + if err = repo.ServeBlob(c.Context, blob); err != nil { + c.Error(500, "ServeBlob", err) } } // https://github.com/gogits/go-gogs-client/wiki/Repositories-Contents#download-archive -func GetArchive(ctx *context.APIContext) { - repoPath := models.RepoPath(ctx.Params(":username"), ctx.Params(":reponame")) +func GetArchive(c *context.APIContext) { + repoPath := models.RepoPath(c.Params(":username"), c.Params(":reponame")) gitRepo, err := git.OpenRepository(repoPath) if err != nil { - ctx.Error(500, "OpenRepository", err) + c.Error(500, "OpenRepository", err) return } - ctx.Repo.GitRepo = gitRepo + c.Repo.GitRepo = gitRepo - repo.Download(ctx.Context) + repo.Download(c.Context) } -func GetEditorconfig(ctx *context.APIContext) { - ec, err := ctx.Repo.GetEditorconfig() +func GetEditorconfig(c *context.APIContext) { + ec, err := c.Repo.GetEditorconfig() if err != nil { if git.IsErrNotExist(err) { - ctx.Error(404, "GetEditorconfig", err) + c.Error(404, "GetEditorconfig", err) } else { - ctx.Error(500, "GetEditorconfig", err) + c.Error(500, "GetEditorconfig", err) } return } - fileName := ctx.Params("filename") + fileName := c.Params("filename") def := ec.GetDefinitionForFilename(fileName) if def == nil { - ctx.Error(404, "GetDefinitionForFilename", err) + c.Error(404, "GetDefinitionForFilename", err) return } - ctx.JSON(200, def) + c.JSON(200, def) } diff --git a/routers/api/v1/repo/hook.go b/routers/api/v1/repo/hook.go index 6a4ec85f..7c2b293e 100644 --- a/routers/api/v1/repo/hook.go +++ b/routers/api/v1/repo/hook.go @@ -18,34 +18,34 @@ import ( ) // https://github.com/gogits/go-gogs-client/wiki/Repositories#list-hooks -func ListHooks(ctx *context.APIContext) { - hooks, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID) +func ListHooks(c *context.APIContext) { + hooks, err := models.GetWebhooksByRepoID(c.Repo.Repository.ID) if err != nil { - ctx.Error(500, "GetWebhooksByRepoID", err) + c.Error(500, "GetWebhooksByRepoID", err) return } apiHooks := make([]*api.Hook, len(hooks)) for i := range hooks { - apiHooks[i] = convert.ToHook(ctx.Repo.RepoLink, hooks[i]) + apiHooks[i] = convert.ToHook(c.Repo.RepoLink, hooks[i]) } - ctx.JSON(200, &apiHooks) + c.JSON(200, &apiHooks) } // https://github.com/gogits/go-gogs-client/wiki/Repositories#create-a-hook -func CreateHook(ctx *context.APIContext, form api.CreateHookOption) { +func CreateHook(c *context.APIContext, form api.CreateHookOption) { if !models.IsValidHookTaskType(form.Type) { - ctx.Error(422, "", "Invalid hook type") + c.Error(422, "", "Invalid hook type") return } for _, name := range []string{"url", "content_type"} { if _, ok := form.Config[name]; !ok { - ctx.Error(422, "", "Missing config option: "+name) + c.Error(422, "", "Missing config option: "+name) return } } if !models.IsValidHookContentType(form.Config["content_type"]) { - ctx.Error(422, "", "Invalid content type") + c.Error(422, "", "Invalid content type") return } @@ -53,7 +53,7 @@ func CreateHook(ctx *context.APIContext, form api.CreateHookOption) { form.Events = []string{"push"} } w := &models.Webhook{ - RepoID: ctx.Repo.Repository.ID, + RepoID: c.Repo.Repository.ID, URL: form.Config["url"], ContentType: models.ToHookContentType(form.Config["content_type"]), Secret: form.Config["secret"], @@ -76,7 +76,7 @@ func CreateHook(ctx *context.APIContext, form api.CreateHookOption) { if w.HookTaskType == models.SLACK { channel, ok := form.Config["channel"] if !ok { - ctx.Error(422, "", "Missing config option: channel") + c.Error(422, "", "Missing config option: channel") return } meta, err := json.Marshal(&models.SlackMeta{ @@ -86,31 +86,31 @@ func CreateHook(ctx *context.APIContext, form api.CreateHookOption) { Color: form.Config["color"], }) if err != nil { - ctx.Error(500, "slack: JSON marshal failed", err) + c.Error(500, "slack: JSON marshal failed", err) return } w.Meta = string(meta) } if err := w.UpdateEvent(); err != nil { - ctx.Error(500, "UpdateEvent", err) + c.Error(500, "UpdateEvent", err) return } else if err := models.CreateWebhook(w); err != nil { - ctx.Error(500, "CreateWebhook", err) + c.Error(500, "CreateWebhook", err) return } - ctx.JSON(201, convert.ToHook(ctx.Repo.RepoLink, w)) + c.JSON(201, convert.ToHook(c.Repo.RepoLink, w)) } // https://github.com/gogits/go-gogs-client/wiki/Repositories#edit-a-hook -func EditHook(ctx *context.APIContext, form api.EditHookOption) { - w, err := models.GetWebhookOfRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) +func EditHook(c *context.APIContext, form api.EditHookOption) { + w, err := models.GetWebhookOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id")) if err != nil { if errors.IsWebhookNotExist(err) { - ctx.Status(404) + c.Status(404) } else { - ctx.Error(500, "GetWebhookOfRepoByID", err) + c.Error(500, "GetWebhookOfRepoByID", err) } return } @@ -121,7 +121,7 @@ func EditHook(ctx *context.APIContext, form api.EditHookOption) { } if ct, ok := form.Config["content_type"]; ok { if !models.IsValidHookContentType(ct) { - ctx.Error(422, "", "Invalid content type") + c.Error(422, "", "Invalid content type") return } w.ContentType = models.ToHookContentType(ct) @@ -136,7 +136,7 @@ func EditHook(ctx *context.APIContext, form api.EditHookOption) { Color: form.Config["color"], }) if err != nil { - ctx.Error(500, "slack: JSON marshal failed", err) + c.Error(500, "slack: JSON marshal failed", err) return } w.Meta = string(meta) @@ -160,7 +160,7 @@ func EditHook(ctx *context.APIContext, form api.EditHookOption) { w.PullRequest = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_PULL_REQUEST)) w.Release = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_RELEASE)) if err = w.UpdateEvent(); err != nil { - ctx.Error(500, "UpdateEvent", err) + c.Error(500, "UpdateEvent", err) return } @@ -169,18 +169,18 @@ func EditHook(ctx *context.APIContext, form api.EditHookOption) { } if err := models.UpdateWebhook(w); err != nil { - ctx.Error(500, "UpdateWebhook", err) + c.Error(500, "UpdateWebhook", err) return } - ctx.JSON(200, convert.ToHook(ctx.Repo.RepoLink, w)) + c.JSON(200, convert.ToHook(c.Repo.RepoLink, w)) } -func DeleteHook(ctx *context.APIContext) { - if err := models.DeleteWebhookOfRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil { - ctx.Error(500, "DeleteWebhookByRepoID", err) +func DeleteHook(c *context.APIContext) { + if err := models.DeleteWebhookOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id")); err != nil { + c.Error(500, "DeleteWebhookByRepoID", err) return } - ctx.Status(204) + c.Status(204) } diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go index 5e233a20..4a072633 100644 --- a/routers/api/v1/repo/issue.go +++ b/routers/api/v1/repo/issue.go @@ -16,16 +16,16 @@ import ( "github.com/gogits/gogs/pkg/setting" ) -func listIssues(ctx *context.APIContext, opts *models.IssuesOptions) { +func listIssues(c *context.APIContext, opts *models.IssuesOptions) { issues, err := models.Issues(opts) if err != nil { - ctx.Error(500, "Issues", err) + c.Error(500, "Issues", err) return } count, err := models.IssuesCount(opts) if err != nil { - ctx.Error(500, "IssuesCount", err) + c.Error(500, "IssuesCount", err) return } @@ -33,64 +33,64 @@ func listIssues(ctx *context.APIContext, opts *models.IssuesOptions) { apiIssues := make([]*api.Issue, len(issues)) for i := range issues { if err = issues[i].LoadAttributes(); err != nil { - ctx.Error(500, "LoadAttributes", err) + c.Error(500, "LoadAttributes", err) return } apiIssues[i] = issues[i].APIFormat() } - ctx.SetLinkHeader(int(count), setting.UI.IssuePagingNum) - ctx.JSON(200, &apiIssues) + c.SetLinkHeader(int(count), setting.UI.IssuePagingNum) + c.JSON(200, &apiIssues) } -func ListUserIssues(ctx *context.APIContext) { +func ListUserIssues(c *context.APIContext) { opts := models.IssuesOptions{ - AssigneeID: ctx.User.ID, - Page: ctx.QueryInt("page"), + AssigneeID: c.User.ID, + Page: c.QueryInt("page"), } - listIssues(ctx, &opts) + listIssues(c, &opts) } -func ListIssues(ctx *context.APIContext) { +func ListIssues(c *context.APIContext) { opts := models.IssuesOptions{ - RepoID: ctx.Repo.Repository.ID, - Page: ctx.QueryInt("page"), + RepoID: c.Repo.Repository.ID, + Page: c.QueryInt("page"), } - listIssues(ctx, &opts) + listIssues(c, &opts) } -func GetIssue(ctx *context.APIContext) { - issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) +func GetIssue(c *context.APIContext) { + issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index")) if err != nil { if errors.IsIssueNotExist(err) { - ctx.Status(404) + c.Status(404) } else { - ctx.Error(500, "GetIssueByIndex", err) + c.Error(500, "GetIssueByIndex", err) } return } - ctx.JSON(200, issue.APIFormat()) + c.JSON(200, issue.APIFormat()) } -func CreateIssue(ctx *context.APIContext, form api.CreateIssueOption) { +func CreateIssue(c *context.APIContext, form api.CreateIssueOption) { issue := &models.Issue{ - RepoID: ctx.Repo.Repository.ID, + RepoID: c.Repo.Repository.ID, Title: form.Title, - PosterID: ctx.User.ID, - Poster: ctx.User, + PosterID: c.User.ID, + Poster: c.User, Content: form.Body, } - if ctx.Repo.IsWriter() { + if c.Repo.IsWriter() { if len(form.Assignee) > 0 { assignee, err := models.GetUserByName(form.Assignee) if err != nil { if errors.IsUserNotExist(err) { - ctx.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", form.Assignee)) + c.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", form.Assignee)) } else { - ctx.Error(500, "GetUserByName", err) + c.Error(500, "GetUserByName", err) } return } @@ -101,14 +101,14 @@ func CreateIssue(ctx *context.APIContext, form api.CreateIssueOption) { form.Labels = nil } - if err := models.NewIssue(ctx.Repo.Repository, issue, form.Labels, nil); err != nil { - ctx.Error(500, "NewIssue", err) + if err := models.NewIssue(c.Repo.Repository, issue, form.Labels, nil); err != nil { + c.Error(500, "NewIssue", err) return } if form.Closed { - if err := issue.ChangeStatus(ctx.User, ctx.Repo.Repository, true); err != nil { - ctx.Error(500, "ChangeStatus", err) + if err := issue.ChangeStatus(c.User, c.Repo.Repository, true); err != nil { + c.Error(500, "ChangeStatus", err) return } } @@ -117,25 +117,25 @@ func CreateIssue(ctx *context.APIContext, form api.CreateIssueOption) { var err error issue, err = models.GetIssueByID(issue.ID) if err != nil { - ctx.Error(500, "GetIssueByID", err) + c.Error(500, "GetIssueByID", err) return } - ctx.JSON(201, issue.APIFormat()) + c.JSON(201, issue.APIFormat()) } -func EditIssue(ctx *context.APIContext, form api.EditIssueOption) { - issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) +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) { - ctx.Status(404) + c.Status(404) } else { - ctx.Error(500, "GetIssueByIndex", err) + c.Error(500, "GetIssueByIndex", err) } return } - if !issue.IsPoster(ctx.User.ID) && !ctx.Repo.IsWriter() { - ctx.Status(403) + if !issue.IsPoster(c.User.ID) && !c.Repo.IsWriter() { + c.Status(403) return } @@ -146,7 +146,7 @@ func EditIssue(ctx *context.APIContext, form api.EditIssueOption) { issue.Content = *form.Body } - if ctx.Repo.IsWriter() && form.Assignee != nil && + if c.Repo.IsWriter() && form.Assignee != nil && (issue.Assignee == nil || issue.Assignee.LowerName != strings.ToLower(*form.Assignee)) { if len(*form.Assignee) == 0 { issue.AssigneeID = 0 @@ -154,9 +154,9 @@ func EditIssue(ctx *context.APIContext, form api.EditIssueOption) { assignee, err := models.GetUserByName(*form.Assignee) if err != nil { if errors.IsUserNotExist(err) { - ctx.Error(422, "", fmt.Sprintf("assignee does not exist: [name: %s]", *form.Assignee)) + c.Error(422, "", fmt.Sprintf("assignee does not exist: [name: %s]", *form.Assignee)) } else { - ctx.Error(500, "GetUserByName", err) + c.Error(500, "GetUserByName", err) } return } @@ -164,27 +164,27 @@ func EditIssue(ctx *context.APIContext, form api.EditIssueOption) { } if err = models.UpdateIssueUserByAssignee(issue); err != nil { - ctx.Error(500, "UpdateIssueUserByAssignee", err) + c.Error(500, "UpdateIssueUserByAssignee", err) return } } - if ctx.Repo.IsWriter() && form.Milestone != nil && + if c.Repo.IsWriter() && form.Milestone != nil && issue.MilestoneID != *form.Milestone { oldMilestoneID := issue.MilestoneID issue.MilestoneID = *form.Milestone - if err = models.ChangeMilestoneAssign(ctx.User, issue, oldMilestoneID); err != nil { - ctx.Error(500, "ChangeMilestoneAssign", err) + if err = models.ChangeMilestoneAssign(c.User, issue, oldMilestoneID); err != nil { + c.Error(500, "ChangeMilestoneAssign", err) return } } if err = models.UpdateIssue(issue); err != nil { - ctx.Error(500, "UpdateIssue", err) + c.Error(500, "UpdateIssue", err) return } if form.State != nil { - if err = issue.ChangeStatus(ctx.User, ctx.Repo.Repository, api.STATE_CLOSED == api.StateType(*form.State)); err != nil { - ctx.Error(500, "ChangeStatus", err) + if err = issue.ChangeStatus(c.User, c.Repo.Repository, api.STATE_CLOSED == api.StateType(*form.State)); err != nil { + c.Error(500, "ChangeStatus", err) return } } @@ -192,8 +192,8 @@ func EditIssue(ctx *context.APIContext, form api.EditIssueOption) { // Refetch from database to assign some automatic values issue, err = models.GetIssueByID(issue.ID) if err != nil { - ctx.Error(500, "GetIssueByID", err) + c.Error(500, "GetIssueByID", err) return } - ctx.JSON(201, issue.APIFormat()) + c.JSON(201, issue.APIFormat()) } diff --git a/routers/api/v1/repo/issue_comment.go b/routers/api/v1/repo/issue_comment.go index 04ecd216..4a057d76 100644 --- a/routers/api/v1/repo/issue_comment.go +++ b/routers/api/v1/repo/issue_comment.go @@ -12,22 +12,22 @@ import ( "github.com/gogits/gogs/pkg/context" ) -func ListIssueComments(ctx *context.APIContext) { +func ListIssueComments(c *context.APIContext) { var since time.Time - if len(ctx.Query("since")) > 0 { - since, _ = time.Parse(time.RFC3339, ctx.Query("since")) + if len(c.Query("since")) > 0 { + since, _ = time.Parse(time.RFC3339, c.Query("since")) } // comments,err:=models.GetCommentsByIssueIDSince(, since) - issue, err := models.GetRawIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) + issue, err := models.GetRawIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index")) if err != nil { - ctx.Error(500, "GetRawIssueByIndex", err) + c.Error(500, "GetRawIssueByIndex", err) return } comments, err := models.GetCommentsByIssueIDSince(issue.ID, since.Unix()) if err != nil { - ctx.Error(500, "GetCommentsByIssueIDSince", err) + c.Error(500, "GetCommentsByIssueIDSince", err) return } @@ -35,18 +35,18 @@ func ListIssueComments(ctx *context.APIContext) { for i := range comments { apiComments[i] = comments[i].APIFormat() } - ctx.JSON(200, &apiComments) + c.JSON(200, &apiComments) } -func ListRepoIssueComments(ctx *context.APIContext) { +func ListRepoIssueComments(c *context.APIContext) { var since time.Time - if len(ctx.Query("since")) > 0 { - since, _ = time.Parse(time.RFC3339, ctx.Query("since")) + if len(c.Query("since")) > 0 { + since, _ = time.Parse(time.RFC3339, c.Query("since")) } - comments, err := models.GetCommentsByRepoIDSince(ctx.Repo.Repository.ID, since.Unix()) + comments, err := models.GetCommentsByRepoIDSince(c.Repo.Repository.ID, since.Unix()) if err != nil { - ctx.Error(500, "GetCommentsByRepoIDSince", err) + c.Error(500, "GetCommentsByRepoIDSince", err) return } @@ -54,75 +54,75 @@ func ListRepoIssueComments(ctx *context.APIContext) { for i := range comments { apiComments[i] = comments[i].APIFormat() } - ctx.JSON(200, &apiComments) + c.JSON(200, &apiComments) } -func CreateIssueComment(ctx *context.APIContext, form api.CreateIssueCommentOption) { - issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) +func CreateIssueComment(c *context.APIContext, form api.CreateIssueCommentOption) { + issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index")) if err != nil { - ctx.Error(500, "GetIssueByIndex", err) + c.Error(500, "GetIssueByIndex", err) return } - comment, err := models.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Body, nil) + comment, err := models.CreateIssueComment(c.User, c.Repo.Repository, issue, form.Body, nil) if err != nil { - ctx.Error(500, "CreateIssueComment", err) + c.Error(500, "CreateIssueComment", err) return } - ctx.JSON(201, comment.APIFormat()) + c.JSON(201, comment.APIFormat()) } -func EditIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption) { - comment, err := models.GetCommentByID(ctx.ParamsInt64(":id")) +func EditIssueComment(c *context.APIContext, form api.EditIssueCommentOption) { + comment, err := models.GetCommentByID(c.ParamsInt64(":id")) if err != nil { if models.IsErrCommentNotExist(err) { - ctx.Error(404, "GetCommentByID", err) + c.Error(404, "GetCommentByID", err) } else { - ctx.Error(500, "GetCommentByID", err) + c.Error(500, "GetCommentByID", err) } return } - if ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin() { - ctx.Status(403) + if c.User.ID != comment.PosterID && !c.Repo.IsAdmin() { + c.Status(403) return } else if comment.Type != models.COMMENT_TYPE_COMMENT { - ctx.Status(204) + c.Status(204) return } oldContent := comment.Content comment.Content = form.Body - if err := models.UpdateComment(ctx.User, comment, oldContent); err != nil { - ctx.Error(500, "UpdateComment", err) + if err := models.UpdateComment(c.User, comment, oldContent); err != nil { + c.Error(500, "UpdateComment", err) return } - ctx.JSON(200, comment.APIFormat()) + c.JSON(200, comment.APIFormat()) } -func DeleteIssueComment(ctx *context.APIContext) { - comment, err := models.GetCommentByID(ctx.ParamsInt64(":id")) +func DeleteIssueComment(c *context.APIContext) { + comment, err := models.GetCommentByID(c.ParamsInt64(":id")) if err != nil { if models.IsErrCommentNotExist(err) { - ctx.Error(404, "GetCommentByID", err) + c.Error(404, "GetCommentByID", err) } else { - ctx.Error(500, "GetCommentByID", err) + c.Error(500, "GetCommentByID", err) } return } - if ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin() { - ctx.Status(403) + if c.User.ID != comment.PosterID && !c.Repo.IsAdmin() { + c.Status(403) return } else if comment.Type != models.COMMENT_TYPE_COMMENT { - ctx.Status(204) + c.Status(204) return } - if err = models.DeleteCommentByID(ctx.User, comment.ID); err != nil { - ctx.Error(500, "DeleteCommentByID", err) + if err = models.DeleteCommentByID(c.User, comment.ID); err != nil { + c.Error(500, "DeleteCommentByID", err) return } - ctx.Status(204) + c.Status(204) } diff --git a/routers/api/v1/repo/issue_label.go b/routers/api/v1/repo/issue_label.go index 06c52dc5..f3f2d730 100644 --- a/routers/api/v1/repo/issue_label.go +++ b/routers/api/v1/repo/issue_label.go @@ -12,13 +12,13 @@ import ( "github.com/gogits/gogs/pkg/context" ) -func ListIssueLabels(ctx *context.APIContext) { - issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) +func ListIssueLabels(c *context.APIContext) { + issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index")) if err != nil { if errors.IsIssueNotExist(err) { - ctx.Status(404) + c.Status(404) } else { - ctx.Error(500, "GetIssueByIndex", err) + c.Error(500, "GetIssueByIndex", err) } return } @@ -27,39 +27,39 @@ func ListIssueLabels(ctx *context.APIContext) { for i := range issue.Labels { apiLabels[i] = issue.Labels[i].APIFormat() } - ctx.JSON(200, &apiLabels) + c.JSON(200, &apiLabels) } -func AddIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) { - if !ctx.Repo.IsWriter() { - ctx.Status(403) +func AddIssueLabels(c *context.APIContext, form api.IssueLabelsOption) { + if !c.Repo.IsWriter() { + c.Status(403) return } - issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) + issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index")) if err != nil { if errors.IsIssueNotExist(err) { - ctx.Status(404) + c.Status(404) } else { - ctx.Error(500, "GetIssueByIndex", err) + c.Error(500, "GetIssueByIndex", err) } return } - labels, err := models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels) + labels, err := models.GetLabelsInRepoByIDs(c.Repo.Repository.ID, form.Labels) if err != nil { - ctx.Error(500, "GetLabelsInRepoByIDs", err) + c.Error(500, "GetLabelsInRepoByIDs", err) return } - if err = issue.AddLabels(ctx.User, labels); err != nil { - ctx.Error(500, "AddLabels", err) + if err = issue.AddLabels(c.User, labels); err != nil { + c.Error(500, "AddLabels", err) return } labels, err = models.GetLabelsByIssueID(issue.ID) if err != nil { - ctx.Error(500, "GetLabelsByIssueID", err) + c.Error(500, "GetLabelsByIssueID", err) return } @@ -67,73 +67,73 @@ func AddIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) { for i := range labels { apiLabels[i] = issue.Labels[i].APIFormat() } - ctx.JSON(200, &apiLabels) + c.JSON(200, &apiLabels) } -func DeleteIssueLabel(ctx *context.APIContext) { - if !ctx.Repo.IsWriter() { - ctx.Status(403) +func DeleteIssueLabel(c *context.APIContext) { + if !c.Repo.IsWriter() { + c.Status(403) return } - issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) + issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index")) if err != nil { if errors.IsIssueNotExist(err) { - ctx.Status(404) + c.Status(404) } else { - ctx.Error(500, "GetIssueByIndex", err) + c.Error(500, "GetIssueByIndex", err) } return } - label, err := models.GetLabelOfRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) + label, err := models.GetLabelOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id")) if err != nil { if models.IsErrLabelNotExist(err) { - ctx.Error(422, "", err) + c.Error(422, "", err) } else { - ctx.Error(500, "GetLabelInRepoByID", err) + c.Error(500, "GetLabelInRepoByID", err) } return } if err := models.DeleteIssueLabel(issue, label); err != nil { - ctx.Error(500, "DeleteIssueLabel", err) + c.Error(500, "DeleteIssueLabel", err) return } - ctx.Status(204) + c.Status(204) } -func ReplaceIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) { - if !ctx.Repo.IsWriter() { - ctx.Status(403) +func ReplaceIssueLabels(c *context.APIContext, form api.IssueLabelsOption) { + if !c.Repo.IsWriter() { + c.Status(403) return } - issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) + issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index")) if err != nil { if errors.IsIssueNotExist(err) { - ctx.Status(404) + c.Status(404) } else { - ctx.Error(500, "GetIssueByIndex", err) + c.Error(500, "GetIssueByIndex", err) } return } - labels, err := models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels) + labels, err := models.GetLabelsInRepoByIDs(c.Repo.Repository.ID, form.Labels) if err != nil { - ctx.Error(500, "GetLabelsInRepoByIDs", err) + c.Error(500, "GetLabelsInRepoByIDs", err) return } if err := issue.ReplaceLabels(labels); err != nil { - ctx.Error(500, "ReplaceLabels", err) + c.Error(500, "ReplaceLabels", err) return } labels, err = models.GetLabelsByIssueID(issue.ID) if err != nil { - ctx.Error(500, "GetLabelsByIssueID", err) + c.Error(500, "GetLabelsByIssueID", err) return } @@ -141,29 +141,29 @@ func ReplaceIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) { for i := range labels { apiLabels[i] = issue.Labels[i].APIFormat() } - ctx.JSON(200, &apiLabels) + c.JSON(200, &apiLabels) } -func ClearIssueLabels(ctx *context.APIContext) { - if !ctx.Repo.IsWriter() { - ctx.Status(403) +func ClearIssueLabels(c *context.APIContext) { + if !c.Repo.IsWriter() { + c.Status(403) return } - issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) + issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index")) if err != nil { if errors.IsIssueNotExist(err) { - ctx.Status(404) + c.Status(404) } else { - ctx.Error(500, "GetIssueByIndex", err) + c.Error(500, "GetIssueByIndex", err) } return } - if err := issue.ClearLabels(ctx.User); err != nil { - ctx.Error(500, "ClearLabels", err) + if err := issue.ClearLabels(c.User); err != nil { + c.Error(500, "ClearLabels", err) return } - ctx.Status(204) + c.Status(204) } diff --git a/routers/api/v1/repo/key.go b/routers/api/v1/repo/key.go index 4ac180f1..901df4c7 100644 --- a/routers/api/v1/repo/key.go +++ b/routers/api/v1/repo/key.go @@ -20,95 +20,95 @@ func composeDeployKeysAPILink(repoPath string) string { } // https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#list-deploy-keys -func ListDeployKeys(ctx *context.APIContext) { - keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID) +func ListDeployKeys(c *context.APIContext) { + keys, err := models.ListDeployKeys(c.Repo.Repository.ID) if err != nil { - ctx.Error(500, "ListDeployKeys", err) + c.Error(500, "ListDeployKeys", err) return } - apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name) + apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name) apiKeys := make([]*api.DeployKey, len(keys)) for i := range keys { if err = keys[i].GetContent(); err != nil { - ctx.Error(500, "GetContent", err) + c.Error(500, "GetContent", err) return } apiKeys[i] = convert.ToDeployKey(apiLink, keys[i]) } - ctx.JSON(200, &apiKeys) + c.JSON(200, &apiKeys) } // https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#get-a-deploy-key -func GetDeployKey(ctx *context.APIContext) { - key, err := models.GetDeployKeyByID(ctx.ParamsInt64(":id")) +func GetDeployKey(c *context.APIContext) { + key, err := models.GetDeployKeyByID(c.ParamsInt64(":id")) if err != nil { if models.IsErrDeployKeyNotExist(err) { - ctx.Status(404) + c.Status(404) } else { - ctx.Error(500, "GetDeployKeyByID", err) + c.Error(500, "GetDeployKeyByID", err) } return } if err = key.GetContent(); err != nil { - ctx.Error(500, "GetContent", err) + c.Error(500, "GetContent", err) return } - apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name) - ctx.JSON(200, convert.ToDeployKey(apiLink, key)) + apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name) + c.JSON(200, convert.ToDeployKey(apiLink, key)) } -func HandleCheckKeyStringError(ctx *context.APIContext, err error) { +func HandleCheckKeyStringError(c *context.APIContext, err error) { if models.IsErrKeyUnableVerify(err) { - ctx.Error(422, "", "Unable to verify key content") + c.Error(422, "", "Unable to verify key content") } else { - ctx.Error(422, "", fmt.Errorf("Invalid key content: %v", err)) + c.Error(422, "", fmt.Errorf("Invalid key content: %v", err)) } } -func HandleAddKeyError(ctx *context.APIContext, err error) { +func HandleAddKeyError(c *context.APIContext, err error) { switch { case models.IsErrKeyAlreadyExist(err): - ctx.Error(422, "", "Key content has been used as non-deploy key") + c.Error(422, "", "Key content has been used as non-deploy key") case models.IsErrKeyNameAlreadyUsed(err): - ctx.Error(422, "", "Key title has been used") + c.Error(422, "", "Key title has been used") default: - ctx.Error(500, "AddKey", err) + c.Error(500, "AddKey", err) } } // https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#add-a-new-deploy-key -func CreateDeployKey(ctx *context.APIContext, form api.CreateKeyOption) { +func CreateDeployKey(c *context.APIContext, form api.CreateKeyOption) { content, err := models.CheckPublicKeyString(form.Key) if err != nil { - HandleCheckKeyStringError(ctx, err) + HandleCheckKeyStringError(c, err) return } - key, err := models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content) + key, err := models.AddDeployKey(c.Repo.Repository.ID, form.Title, content) if err != nil { - HandleAddKeyError(ctx, err) + HandleAddKeyError(c, err) return } key.Content = content - apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name) - ctx.JSON(201, convert.ToDeployKey(apiLink, key)) + apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name) + c.JSON(201, convert.ToDeployKey(apiLink, key)) } // https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#remove-a-deploy-key -func DeleteDeploykey(ctx *context.APIContext) { - if err := models.DeleteDeployKey(ctx.User, ctx.ParamsInt64(":id")); err != nil { +func DeleteDeploykey(c *context.APIContext) { + if err := models.DeleteDeployKey(c.User, c.ParamsInt64(":id")); err != nil { if models.IsErrKeyAccessDenied(err) { - ctx.Error(403, "", "You do not have access to this key") + c.Error(403, "", "You do not have access to this key") } else { - ctx.Error(500, "DeleteDeployKey", err) + c.Error(500, "DeleteDeployKey", err) } return } - ctx.Status(204) + c.Status(204) } diff --git a/routers/api/v1/repo/label.go b/routers/api/v1/repo/label.go index 32c1924b..289f40a4 100644 --- a/routers/api/v1/repo/label.go +++ b/routers/api/v1/repo/label.go @@ -11,10 +11,10 @@ import ( "github.com/gogits/gogs/pkg/context" ) -func ListLabels(ctx *context.APIContext) { - labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID) +func ListLabels(c *context.APIContext) { + labels, err := models.GetLabelsByRepoID(c.Repo.Repository.ID) if err != nil { - ctx.Error(500, "GetLabelsByRepoID", err) + c.Error(500, "GetLabelsByRepoID", err) return } @@ -22,53 +22,53 @@ func ListLabels(ctx *context.APIContext) { for i := range labels { apiLabels[i] = labels[i].APIFormat() } - ctx.JSON(200, &apiLabels) + c.JSON(200, &apiLabels) } -func GetLabel(ctx *context.APIContext) { - label, err := models.GetLabelOfRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) +func GetLabel(c *context.APIContext) { + label, err := models.GetLabelOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id")) if err != nil { if models.IsErrLabelNotExist(err) { - ctx.Status(404) + c.Status(404) } else { - ctx.Error(500, "GetLabelByRepoID", err) + c.Error(500, "GetLabelByRepoID", err) } return } - ctx.JSON(200, label.APIFormat()) + c.JSON(200, label.APIFormat()) } -func CreateLabel(ctx *context.APIContext, form api.CreateLabelOption) { - if !ctx.Repo.IsWriter() { - ctx.Status(403) +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: ctx.Repo.Repository.ID, + RepoID: c.Repo.Repository.ID, } if err := models.NewLabels(label); err != nil { - ctx.Error(500, "NewLabel", err) + c.Error(500, "NewLabel", err) return } - ctx.JSON(201, label.APIFormat()) + c.JSON(201, label.APIFormat()) } -func EditLabel(ctx *context.APIContext, form api.EditLabelOption) { - if !ctx.Repo.IsWriter() { - ctx.Status(403) +func EditLabel(c *context.APIContext, form api.EditLabelOption) { + if !c.Repo.IsWriter() { + c.Status(403) return } - label, err := models.GetLabelOfRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) + label, err := models.GetLabelOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id")) if err != nil { if models.IsErrLabelNotExist(err) { - ctx.Status(404) + c.Status(404) } else { - ctx.Error(500, "GetLabelByRepoID", err) + c.Error(500, "GetLabelByRepoID", err) } return } @@ -80,22 +80,22 @@ func EditLabel(ctx *context.APIContext, form api.EditLabelOption) { label.Color = *form.Color } if err := models.UpdateLabel(label); err != nil { - ctx.Handle(500, "UpdateLabel", err) + c.Handle(500, "UpdateLabel", err) return } - ctx.JSON(200, label.APIFormat()) + c.JSON(200, label.APIFormat()) } -func DeleteLabel(ctx *context.APIContext) { - if !ctx.Repo.IsWriter() { - ctx.Status(403) +func DeleteLabel(c *context.APIContext) { + if !c.Repo.IsWriter() { + c.Status(403) return } - if err := models.DeleteLabel(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil { - ctx.Error(500, "DeleteLabel", err) + if err := models.DeleteLabel(c.Repo.Repository.ID, c.ParamsInt64(":id")); err != nil { + c.Error(500, "DeleteLabel", err) return } - ctx.Status(204) + c.Status(204) } diff --git a/routers/api/v1/repo/milestone.go b/routers/api/v1/repo/milestone.go index 5133d19b..baf8eb2f 100644 --- a/routers/api/v1/repo/milestone.go +++ b/routers/api/v1/repo/milestone.go @@ -13,10 +13,10 @@ import ( "github.com/gogits/gogs/pkg/context" ) -func ListMilestones(ctx *context.APIContext) { - milestones, err := models.GetMilestonesByRepoID(ctx.Repo.Repository.ID) +func ListMilestones(c *context.APIContext) { + milestones, err := models.GetMilestonesByRepoID(c.Repo.Repository.ID) if err != nil { - ctx.Error(500, "GetMilestonesByRepoID", err) + c.Error(500, "GetMilestonesByRepoID", err) return } @@ -24,49 +24,49 @@ func ListMilestones(ctx *context.APIContext) { for i := range milestones { apiMilestones[i] = milestones[i].APIFormat() } - ctx.JSON(200, &apiMilestones) + c.JSON(200, &apiMilestones) } -func GetMilestone(ctx *context.APIContext) { - milestone, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) +func GetMilestone(c *context.APIContext) { + milestone, err := models.GetMilestoneByRepoID(c.Repo.Repository.ID, c.ParamsInt64(":id")) if err != nil { if models.IsErrMilestoneNotExist(err) { - ctx.Status(404) + c.Status(404) } else { - ctx.Error(500, "GetMilestoneByRepoID", err) + c.Error(500, "GetMilestoneByRepoID", err) } return } - ctx.JSON(200, milestone.APIFormat()) + c.JSON(200, milestone.APIFormat()) } -func CreateMilestone(ctx *context.APIContext, form api.CreateMilestoneOption) { +func CreateMilestone(c *context.APIContext, form api.CreateMilestoneOption) { if form.Deadline == nil { defaultDeadline, _ := time.ParseInLocation("2006-01-02", "9999-12-31", time.Local) form.Deadline = &defaultDeadline } milestone := &models.Milestone{ - RepoID: ctx.Repo.Repository.ID, + RepoID: c.Repo.Repository.ID, Name: form.Title, Content: form.Description, Deadline: *form.Deadline, } if err := models.NewMilestone(milestone); err != nil { - ctx.Error(500, "NewMilestone", err) + c.Error(500, "NewMilestone", err) return } - ctx.JSON(201, milestone.APIFormat()) + c.JSON(201, milestone.APIFormat()) } -func EditMilestone(ctx *context.APIContext, form api.EditMilestoneOption) { - milestone, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) +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) { - ctx.Status(404) + c.Status(404) } else { - ctx.Error(500, "GetMilestoneByRepoID", err) + c.Error(500, "GetMilestoneByRepoID", err) } return } @@ -83,21 +83,21 @@ func EditMilestone(ctx *context.APIContext, form api.EditMilestoneOption) { if form.State != nil { if err = milestone.ChangeStatus(api.STATE_CLOSED == api.StateType(*form.State)); err != nil { - ctx.Error(500, "ChangeStatus", err) + c.Error(500, "ChangeStatus", err) return } } else if err = models.UpdateMilestone(milestone); err != nil { - ctx.Handle(500, "UpdateMilestone", err) + c.Handle(500, "UpdateMilestone", err) return } - ctx.JSON(200, milestone.APIFormat()) + c.JSON(200, milestone.APIFormat()) } -func DeleteMilestone(ctx *context.APIContext) { - if err := models.DeleteMilestoneOfRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil { - ctx.Error(500, "DeleteMilestoneByRepoID", err) +func DeleteMilestone(c *context.APIContext) { + if err := models.DeleteMilestoneOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id")); err != nil { + c.Error(500, "DeleteMilestoneByRepoID", err) return } - ctx.Status(204) + c.Status(204) } diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index f86905d4..1095f08d 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -20,27 +20,27 @@ import ( ) // https://github.com/gogits/go-gogs-client/wiki/Repositories#search-repositories -func Search(ctx *context.APIContext) { +func Search(c *context.APIContext) { opts := &models.SearchRepoOptions{ - Keyword: path.Base(ctx.Query("q")), - OwnerID: ctx.QueryInt64("uid"), - PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")), + Keyword: path.Base(c.Query("q")), + OwnerID: c.QueryInt64("uid"), + PageSize: convert.ToCorrectPageSize(c.QueryInt("limit")), } // Check visibility. - if ctx.IsLogged && opts.OwnerID > 0 { - if ctx.User.ID == opts.OwnerID { + if c.IsLogged && opts.OwnerID > 0 { + if c.User.ID == opts.OwnerID { opts.Private = true } else { u, err := models.GetUserByID(opts.OwnerID) if err != nil { - ctx.JSON(500, map[string]interface{}{ + c.JSON(500, map[string]interface{}{ "ok": false, "error": err.Error(), }) return } - if u.IsOrganization() && u.IsOwnedBy(ctx.User.ID) { + if u.IsOrganization() && u.IsOwnedBy(c.User.ID) { opts.Private = true } // FIXME: how about collaborators? @@ -49,7 +49,7 @@ func Search(ctx *context.APIContext) { repos, count, err := models.SearchRepositoryByName(opts) if err != nil { - ctx.JSON(500, map[string]interface{}{ + c.JSON(500, map[string]interface{}{ "ok": false, "error": err.Error(), }) @@ -59,7 +59,7 @@ func Search(ctx *context.APIContext) { results := make([]*api.Repository, len(repos)) for i := range repos { if err = repos[i].GetOwner(); err != nil { - ctx.JSON(500, map[string]interface{}{ + c.JSON(500, map[string]interface{}{ "ok": false, "error": err.Error(), }) @@ -71,17 +71,17 @@ func Search(ctx *context.APIContext) { } } - ctx.SetLinkHeader(int(count), setting.API.MaxResponseItems) - ctx.JSON(200, map[string]interface{}{ + c.SetLinkHeader(int(count), setting.API.MaxResponseItems) + c.JSON(200, map[string]interface{}{ "ok": true, "data": results, }) } -func listUserRepositories(ctx *context.APIContext, username string) { +func listUserRepositories(c *context.APIContext, username string) { user, err := models.GetUserByName(username) if err != nil { - ctx.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err) + c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err) return } @@ -89,32 +89,32 @@ func listUserRepositories(ctx *context.APIContext, username string) { // or an organization isn't a member of. var ownRepos []*models.Repository if user.IsOrganization() { - ownRepos, _, err = user.GetUserRepositories(ctx.User.ID, 1, user.NumRepos) + ownRepos, _, err = user.GetUserRepositories(c.User.ID, 1, user.NumRepos) } else { ownRepos, err = models.GetUserRepositories(&models.UserRepoOptions{ UserID: user.ID, - Private: ctx.User.ID == user.ID, + Private: c.User.ID == user.ID, Page: 1, PageSize: user.NumRepos, }) } if err != nil { - ctx.Error(500, "GetUserRepositories", err) + c.Error(500, "GetUserRepositories", err) return } - if ctx.User.ID != user.ID { + if c.User.ID != user.ID { repos := make([]*api.Repository, len(ownRepos)) for i := range ownRepos { repos[i] = ownRepos[i].APIFormat(&api.Permission{true, true, true}) } - ctx.JSON(200, &repos) + c.JSON(200, &repos) return } accessibleRepos, err := user.GetRepositoryAccesses() if err != nil { - ctx.Error(500, "GetRepositoryAccesses", err) + c.Error(500, "GetRepositoryAccesses", err) return } @@ -134,23 +134,23 @@ func listUserRepositories(ctx *context.APIContext, username string) { i++ } - ctx.JSON(200, &repos) + c.JSON(200, &repos) } -func ListMyRepos(ctx *context.APIContext) { - listUserRepositories(ctx, ctx.User.Name) +func ListMyRepos(c *context.APIContext) { + listUserRepositories(c, c.User.Name) } -func ListUserRepositories(ctx *context.APIContext) { - listUserRepositories(ctx, ctx.Params(":username")) +func ListUserRepositories(c *context.APIContext) { + listUserRepositories(c, c.Params(":username")) } -func ListOrgRepositories(ctx *context.APIContext) { - listUserRepositories(ctx, ctx.Params(":org")) +func ListOrgRepositories(c *context.APIContext) { + listUserRepositories(c, c.Params(":org")) } -func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateRepoOption) { - repo, err := models.CreateRepository(ctx.User, owner, models.CreateRepoOptions{ +func CreateUserRepo(c *context.APIContext, owner *models.User, opt api.CreateRepoOption) { + repo, err := models.CreateRepository(c.User, owner, models.CreateRepoOptions{ Name: opt.Name, Description: opt.Description, Gitignores: opt.Gitignores, @@ -163,104 +163,104 @@ func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateR if models.IsErrRepoAlreadyExist(err) || models.IsErrNameReserved(err) || models.IsErrNamePatternNotAllowed(err) { - ctx.Error(422, "", err) + c.Error(422, "", err) } else { if repo != nil { - if err = models.DeleteRepository(ctx.User.ID, repo.ID); err != nil { + if err = models.DeleteRepository(c.User.ID, repo.ID); err != nil { log.Error(2, "DeleteRepository: %v", err) } } - ctx.Error(500, "CreateRepository", err) + c.Error(500, "CreateRepository", err) } return } - ctx.JSON(201, repo.APIFormat(&api.Permission{true, true, true})) + c.JSON(201, repo.APIFormat(&api.Permission{true, true, true})) } // https://github.com/gogits/go-gogs-client/wiki/Repositories#create -func Create(ctx *context.APIContext, opt api.CreateRepoOption) { +func Create(c *context.APIContext, opt api.CreateRepoOption) { // Shouldn't reach this condition, but just in case. - if ctx.User.IsOrganization() { - ctx.Error(422, "", "not allowed creating repository for organization") + if c.User.IsOrganization() { + c.Error(422, "", "not allowed creating repository for organization") return } - CreateUserRepo(ctx, ctx.User, opt) + CreateUserRepo(c, c.User, opt) } -func CreateOrgRepo(ctx *context.APIContext, opt api.CreateRepoOption) { - org, err := models.GetOrgByName(ctx.Params(":org")) +func CreateOrgRepo(c *context.APIContext, opt api.CreateRepoOption) { + org, err := models.GetOrgByName(c.Params(":org")) if err != nil { if errors.IsUserNotExist(err) { - ctx.Error(422, "", err) + c.Error(422, "", err) } else { - ctx.Error(500, "GetOrgByName", err) + c.Error(500, "GetOrgByName", err) } return } - if !org.IsOwnedBy(ctx.User.ID) { - ctx.Error(403, "", "Given user is not owner of organization.") + if !org.IsOwnedBy(c.User.ID) { + c.Error(403, "", "Given user is not owner of organization.") return } - CreateUserRepo(ctx, org, opt) + CreateUserRepo(c, org, opt) } // https://github.com/gogits/go-gogs-client/wiki/Repositories#migrate -func Migrate(ctx *context.APIContext, f form.MigrateRepo) { - ctxUser := ctx.User +func Migrate(c *context.APIContext, f form.MigrateRepo) { + ctxUser := c.User // Not equal means context user is an organization, // or is another user/organization if current user is admin. if f.Uid != ctxUser.ID { org, err := models.GetUserByID(f.Uid) if err != nil { if errors.IsUserNotExist(err) { - ctx.Error(422, "", err) + c.Error(422, "", err) } else { - ctx.Error(500, "GetUserByID", err) + c.Error(500, "GetUserByID", err) } return } else if !org.IsOrganization() { - ctx.Error(403, "", "Given user is not an organization") + c.Error(403, "", "Given user is not an organization") return } ctxUser = org } - if ctx.HasError() { - ctx.Error(422, "", ctx.GetErrMsg()) + if c.HasError() { + c.Error(422, "", c.GetErrMsg()) return } - if ctxUser.IsOrganization() && !ctx.User.IsAdmin { + if ctxUser.IsOrganization() && !c.User.IsAdmin { // Check ownership of organization. - if !ctxUser.IsOwnedBy(ctx.User.ID) { - ctx.Error(403, "", "Given user is not owner of organization") + if !ctxUser.IsOwnedBy(c.User.ID) { + c.Error(403, "", "Given user is not owner of organization") return } } - remoteAddr, err := f.ParseRemoteAddr(ctx.User) + remoteAddr, err := f.ParseRemoteAddr(c.User) if err != nil { if models.IsErrInvalidCloneAddr(err) { addrErr := err.(models.ErrInvalidCloneAddr) switch { case addrErr.IsURLError: - ctx.Error(422, "", err) + c.Error(422, "", err) case addrErr.IsPermissionDenied: - ctx.Error(422, "", "You are not allowed to import local repositories") + c.Error(422, "", "You are not allowed to import local repositories") case addrErr.IsInvalidPath: - ctx.Error(422, "", "Invalid local path, it does not exist or not a directory") + c.Error(422, "", "Invalid local path, it does not exist or not a directory") default: - ctx.Error(500, "ParseRemoteAddr", "Unknown error type (ErrInvalidCloneAddr): "+err.Error()) + c.Error(500, "ParseRemoteAddr", "Unknown error type (ErrInvalidCloneAddr): "+err.Error()) } } else { - ctx.Error(500, "ParseRemoteAddr", err) + c.Error(500, "ParseRemoteAddr", err) } return } - repo, err := models.MigrateRepository(ctx.User, ctxUser, models.MigrateRepoOptions{ + repo, err := models.MigrateRepository(c.User, ctxUser, models.MigrateRepoOptions{ Name: f.RepoName, Description: f.Description, IsPrivate: f.Private || setting.Repository.ForcePrivate, @@ -275,34 +275,34 @@ func Migrate(ctx *context.APIContext, f form.MigrateRepo) { } if errors.IsReachLimitOfRepo(err) { - ctx.Error(422, "", err) + c.Error(422, "", err) } else { - ctx.Error(500, "MigrateRepository", models.HandleMirrorCredentials(err.Error(), true)) + c.Error(500, "MigrateRepository", models.HandleMirrorCredentials(err.Error(), true)) } return } log.Trace("Repository migrated: %s/%s", ctxUser.Name, f.RepoName) - ctx.JSON(201, repo.APIFormat(&api.Permission{true, true, true})) + c.JSON(201, repo.APIFormat(&api.Permission{true, true, true})) } -func parseOwnerAndRepo(ctx *context.APIContext) (*models.User, *models.Repository) { - owner, err := models.GetUserByName(ctx.Params(":username")) +func parseOwnerAndRepo(c *context.APIContext) (*models.User, *models.Repository) { + owner, err := models.GetUserByName(c.Params(":username")) if err != nil { if errors.IsUserNotExist(err) { - ctx.Error(422, "", err) + c.Error(422, "", err) } else { - ctx.Error(500, "GetUserByName", err) + c.Error(500, "GetUserByName", err) } return nil, nil } - repo, err := models.GetRepositoryByName(owner.ID, ctx.Params(":reponame")) + repo, err := models.GetRepositoryByName(owner.ID, c.Params(":reponame")) if err != nil { if errors.IsRepoNotExist(err) { - ctx.Status(404) + c.Status(404) } else { - ctx.Error(500, "GetRepositoryByName", err) + c.Error(500, "GetRepositoryByName", err) } return nil, nil } @@ -311,72 +311,72 @@ func parseOwnerAndRepo(ctx *context.APIContext) (*models.User, *models.Repositor } // https://github.com/gogits/go-gogs-client/wiki/Repositories#get -func Get(ctx *context.APIContext) { - _, repo := parseOwnerAndRepo(ctx) - if ctx.Written() { +func Get(c *context.APIContext) { + _, repo := parseOwnerAndRepo(c) + if c.Written() { return } - ctx.JSON(200, repo.APIFormat(&api.Permission{ - Admin: ctx.Repo.IsAdmin(), - Push: ctx.Repo.IsWriter(), + c.JSON(200, repo.APIFormat(&api.Permission{ + Admin: c.Repo.IsAdmin(), + Push: c.Repo.IsWriter(), Pull: true, })) } // https://github.com/gogits/go-gogs-client/wiki/Repositories#delete -func Delete(ctx *context.APIContext) { - owner, repo := parseOwnerAndRepo(ctx) - if ctx.Written() { +func Delete(c *context.APIContext) { + owner, repo := parseOwnerAndRepo(c) + if c.Written() { return } - if owner.IsOrganization() && !owner.IsOwnedBy(ctx.User.ID) { - ctx.Error(403, "", "Given user is not owner of organization.") + if owner.IsOrganization() && !owner.IsOwnedBy(c.User.ID) { + c.Error(403, "", "Given user is not owner of organization.") return } if err := models.DeleteRepository(owner.ID, repo.ID); err != nil { - ctx.Error(500, "DeleteRepository", err) + c.Error(500, "DeleteRepository", err) return } log.Trace("Repository deleted: %s/%s", owner.Name, repo.Name) - ctx.Status(204) + c.Status(204) } -func ListForks(ctx *context.APIContext) { - forks, err := ctx.Repo.Repository.GetForks() +func ListForks(c *context.APIContext) { + forks, err := c.Repo.Repository.GetForks() if err != nil { - ctx.Error(500, "GetForks", err) + c.Error(500, "GetForks", err) return } apiForks := make([]*api.Repository, len(forks)) for i := range forks { if err := forks[i].GetOwner(); err != nil { - ctx.Error(500, "GetOwner", err) + c.Error(500, "GetOwner", err) return } apiForks[i] = forks[i].APIFormat(&api.Permission{ - Admin: ctx.User.IsAdminOfRepo(forks[i]), - Push: ctx.User.IsWriterOfRepo(forks[i]), + Admin: c.User.IsAdminOfRepo(forks[i]), + Push: c.User.IsWriterOfRepo(forks[i]), Pull: true, }) } - ctx.JSON(200, &apiForks) + c.JSON(200, &apiForks) } -func MirrorSync(ctx *context.APIContext) { - _, repo := parseOwnerAndRepo(ctx) - if ctx.Written() { +func MirrorSync(c *context.APIContext) { + _, repo := parseOwnerAndRepo(c) + if c.Written() { return } else if !repo.IsMirror { - ctx.Status(404) + c.Status(404) return } go models.MirrorQueue.Add(repo.ID) - ctx.Status(202) + c.Status(202) } |