aboutsummaryrefslogtreecommitdiff
path: root/routers/api/v1/repo
diff options
context:
space:
mode:
Diffstat (limited to 'routers/api/v1/repo')
-rw-r--r--routers/api/v1/repo/branch.go55
-rw-r--r--routers/api/v1/repo/collaborators.go94
-rw-r--r--routers/api/v1/repo/file.go72
-rw-r--r--routers/api/v1/repo/hook.go186
-rw-r--r--routers/api/v1/repo/issue.go201
-rw-r--r--routers/api/v1/repo/issue_comment.go128
-rw-r--r--routers/api/v1/repo/issue_label.go169
-rw-r--r--routers/api/v1/repo/key.go114
-rw-r--r--routers/api/v1/repo/label.go110
-rw-r--r--routers/api/v1/repo/milestone.go103
-rw-r--r--routers/api/v1/repo/repo.go380
11 files changed, 0 insertions, 1612 deletions
diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go
deleted file mode 100644
index 3d5e646c..00000000
--- a/routers/api/v1/repo/branch.go
+++ /dev/null
@@ -1,55 +0,0 @@
-// Copyright 2016 The Gogs Authors. All rights reserved.
-// Use of this source code is governed by a MIT-style
-// license that can be found in the LICENSE file.
-
-package repo
-
-import (
- api "github.com/gogits/go-gogs-client"
-
- "github.com/gogits/gogs/models"
- "github.com/gogits/gogs/pkg/context"
- "github.com/gogits/gogs/routers/api/v1/convert"
-)
-
-// https://github.com/gogits/go-gogs-client/wiki/Repositories#get-branch
-func GetBranch(c *context.APIContext) {
- branch, err := c.Repo.Repository.GetBranch(c.Params("*"))
- if err != nil {
- if models.IsErrBranchNotExist(err) {
- c.Error(404, "GetBranch", err)
- } else {
- c.Error(500, "GetBranch", err)
- }
- return
- }
-
- commit, err := branch.GetCommit()
- if err != nil {
- c.Error(500, "GetCommit", err)
- return
- }
-
- c.JSON(200, convert.ToBranch(branch, commit))
-}
-
-// https://github.com/gogits/go-gogs-client/wiki/Repositories#list-branches
-func ListBranches(c *context.APIContext) {
- branches, err := c.Repo.Repository.GetBranches()
- if err != nil {
- c.Error(500, "GetBranches", err)
- return
- }
-
- apiBranches := make([]*api.Branch, len(branches))
- for i := range branches {
- commit, err := branches[i].GetCommit()
- if err != nil {
- c.Error(500, "GetCommit", err)
- return
- }
- apiBranches[i] = convert.ToBranch(branches[i], commit)
- }
-
- c.JSON(200, &apiBranches)
-}
diff --git a/routers/api/v1/repo/collaborators.go b/routers/api/v1/repo/collaborators.go
deleted file mode 100644
index d295ac0f..00000000
--- a/routers/api/v1/repo/collaborators.go
+++ /dev/null
@@ -1,94 +0,0 @@
-// Copyright 2016 The Gogs Authors. All rights reserved.
-// Use of this source code is governed by a MIT-style
-// license that can be found in the LICENSE file.
-
-package repo
-
-import (
- api "github.com/gogits/go-gogs-client"
-
- "github.com/gogits/gogs/models"
- "github.com/gogits/gogs/models/errors"
- "github.com/gogits/gogs/pkg/context"
-)
-
-func ListCollaborators(c *context.APIContext) {
- collaborators, err := c.Repo.Repository.GetCollaborators()
- if err != nil {
- if errors.IsUserNotExist(err) {
- c.Error(422, "", err)
- } else {
- c.Error(500, "GetCollaborators", err)
- }
- return
- }
-
- apiCollaborators := make([]*api.Collaborator, len(collaborators))
- for i := range collaborators {
- apiCollaborators[i] = collaborators[i].APIFormat()
- }
- c.JSON(200, &apiCollaborators)
-}
-
-func AddCollaborator(c *context.APIContext, form api.AddCollaboratorOption) {
- collaborator, err := models.GetUserByName(c.Params(":collaborator"))
- if err != nil {
- if errors.IsUserNotExist(err) {
- c.Error(422, "", err)
- } else {
- c.Error(500, "GetUserByName", err)
- }
- return
- }
-
- if err := c.Repo.Repository.AddCollaborator(collaborator); err != nil {
- c.Error(500, "AddCollaborator", err)
- return
- }
-
- if form.Permission != nil {
- if err := c.Repo.Repository.ChangeCollaborationAccessMode(collaborator.ID, models.ParseAccessMode(*form.Permission)); err != nil {
- c.Error(500, "ChangeCollaborationAccessMode", err)
- return
- }
- }
-
- c.Status(204)
-}
-
-func IsCollaborator(c *context.APIContext) {
- collaborator, err := models.GetUserByName(c.Params(":collaborator"))
- if err != nil {
- if errors.IsUserNotExist(err) {
- c.Error(422, "", err)
- } else {
- c.Error(500, "GetUserByName", err)
- }
- return
- }
-
- if !c.Repo.Repository.IsCollaborator(collaborator.ID) {
- c.Status(404)
- } else {
- c.Status(204)
- }
-}
-
-func DeleteCollaborator(c *context.APIContext) {
- collaborator, err := models.GetUserByName(c.Params(":collaborator"))
- if err != nil {
- if errors.IsUserNotExist(err) {
- c.Error(422, "", err)
- } else {
- c.Error(500, "GetUserByName", err)
- }
- return
- }
-
- if err := c.Repo.Repository.DeleteCollaboration(collaborator.ID); err != nil {
- c.Error(500, "DeleteCollaboration", err)
- return
- }
-
- c.Status(204)
-}
diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go
deleted file mode 100644
index df6a4857..00000000
--- a/routers/api/v1/repo/file.go
+++ /dev/null
@@ -1,72 +0,0 @@
-// Copyright 2014 The Gogs Authors. All rights reserved.
-// Use of this source code is governed by a MIT-style
-// license that can be found in the LICENSE file.
-
-package repo
-
-import (
- "github.com/gogits/git-module"
-
- "github.com/gogits/gogs/models"
- "github.com/gogits/gogs/pkg/context"
- "github.com/gogits/gogs/routers/repo"
-)
-
-// https://github.com/gogits/go-gogs-client/wiki/Repositories-Contents#download-raw-content
-func GetRawFile(c *context.APIContext) {
- if !c.Repo.HasAccess() {
- c.Status(404)
- return
- }
-
- if c.Repo.Repository.IsBare {
- c.Status(404)
- 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)
- }
- return
- }
- 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(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)
- return
- }
- c.Repo.GitRepo = gitRepo
-
- repo.Download(c.Context)
-}
-
-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)
- }
- return
- }
-
- fileName := c.Params("filename")
- def := ec.GetDefinitionForFilename(fileName)
- if def == nil {
- c.Error(404, "GetDefinitionForFilename", err)
- return
- }
- c.JSON(200, def)
-}
diff --git a/routers/api/v1/repo/hook.go b/routers/api/v1/repo/hook.go
deleted file mode 100644
index 7c2b293e..00000000
--- a/routers/api/v1/repo/hook.go
+++ /dev/null
@@ -1,186 +0,0 @@
-// Copyright 2014 The Gogs Authors. All rights reserved.
-// Use of this source code is governed by a MIT-style
-// license that can be found in the LICENSE file.
-
-package repo
-
-import (
- "encoding/json"
-
- "github.com/Unknwon/com"
-
- api "github.com/gogits/go-gogs-client"
-
- "github.com/gogits/gogs/models"
- "github.com/gogits/gogs/models/errors"
- "github.com/gogits/gogs/pkg/context"
- "github.com/gogits/gogs/routers/api/v1/convert"
-)
-
-// https://github.com/gogits/go-gogs-client/wiki/Repositories#list-hooks
-func ListHooks(c *context.APIContext) {
- hooks, err := models.GetWebhooksByRepoID(c.Repo.Repository.ID)
- if err != nil {
- c.Error(500, "GetWebhooksByRepoID", err)
- return
- }
-
- apiHooks := make([]*api.Hook, len(hooks))
- for i := range hooks {
- apiHooks[i] = convert.ToHook(c.Repo.RepoLink, hooks[i])
- }
- c.JSON(200, &apiHooks)
-}
-
-// https://github.com/gogits/go-gogs-client/wiki/Repositories#create-a-hook
-func CreateHook(c *context.APIContext, form api.CreateHookOption) {
- if !models.IsValidHookTaskType(form.Type) {
- c.Error(422, "", "Invalid hook type")
- return
- }
- for _, name := range []string{"url", "content_type"} {
- if _, ok := form.Config[name]; !ok {
- c.Error(422, "", "Missing config option: "+name)
- return
- }
- }
- if !models.IsValidHookContentType(form.Config["content_type"]) {
- c.Error(422, "", "Invalid content type")
- return
- }
-
- if len(form.Events) == 0 {
- form.Events = []string{"push"}
- }
- w := &models.Webhook{
- RepoID: c.Repo.Repository.ID,
- URL: form.Config["url"],
- ContentType: models.ToHookContentType(form.Config["content_type"]),
- Secret: form.Config["secret"],
- HookEvent: &models.HookEvent{
- ChooseEvents: true,
- HookEvents: models.HookEvents{
- Create: com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_CREATE)),
- Delete: com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_DELETE)),
- Fork: com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_FORK)),
- Push: com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_PUSH)),
- Issues: com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_ISSUES)),
- IssueComment: com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_ISSUE_COMMENT)),
- PullRequest: com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_PULL_REQUEST)),
- Release: com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_RELEASE)),
- },
- },
- IsActive: form.Active,
- HookTaskType: models.ToHookTaskType(form.Type),
- }
- if w.HookTaskType == models.SLACK {
- channel, ok := form.Config["channel"]
- if !ok {
- c.Error(422, "", "Missing config option: channel")
- return
- }
- meta, err := json.Marshal(&models.SlackMeta{
- Channel: channel,
- Username: form.Config["username"],
- IconURL: form.Config["icon_url"],
- Color: form.Config["color"],
- })
- if err != nil {
- c.Error(500, "slack: JSON marshal failed", err)
- return
- }
- w.Meta = string(meta)
- }
-
- if err := w.UpdateEvent(); err != nil {
- c.Error(500, "UpdateEvent", err)
- return
- } else if err := models.CreateWebhook(w); err != nil {
- c.Error(500, "CreateWebhook", err)
- return
- }
-
- c.JSON(201, convert.ToHook(c.Repo.RepoLink, w))
-}
-
-// https://github.com/gogits/go-gogs-client/wiki/Repositories#edit-a-hook
-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) {
- c.Status(404)
- } else {
- c.Error(500, "GetWebhookOfRepoByID", err)
- }
- return
- }
-
- if form.Config != nil {
- if url, ok := form.Config["url"]; ok {
- w.URL = url
- }
- if ct, ok := form.Config["content_type"]; ok {
- if !models.IsValidHookContentType(ct) {
- c.Error(422, "", "Invalid content type")
- return
- }
- w.ContentType = models.ToHookContentType(ct)
- }
-
- if w.HookTaskType == models.SLACK {
- if channel, ok := form.Config["channel"]; ok {
- meta, err := json.Marshal(&models.SlackMeta{
- Channel: channel,
- Username: form.Config["username"],
- IconURL: form.Config["icon_url"],
- Color: form.Config["color"],
- })
- if err != nil {
- c.Error(500, "slack: JSON marshal failed", err)
- return
- }
- w.Meta = string(meta)
- }
- }
- }
-
- // Update events
- if len(form.Events) == 0 {
- form.Events = []string{"push"}
- }
- w.PushOnly = false
- w.SendEverything = false
- w.ChooseEvents = true
- w.Create = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_CREATE))
- w.Delete = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_DELETE))
- w.Fork = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_FORK))
- w.Push = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_PUSH))
- w.Issues = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_ISSUES))
- w.IssueComment = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_ISSUE_COMMENT))
- 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 {
- c.Error(500, "UpdateEvent", err)
- return
- }
-
- if form.Active != nil {
- w.IsActive = *form.Active
- }
-
- if err := models.UpdateWebhook(w); err != nil {
- c.Error(500, "UpdateWebhook", err)
- return
- }
-
- c.JSON(200, convert.ToHook(c.Repo.RepoLink, w))
-}
-
-func DeleteHook(c *context.APIContext) {
- if err := models.DeleteWebhookOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id")); err != nil {
- c.Error(500, "DeleteWebhookByRepoID", err)
- return
- }
-
- c.Status(204)
-}
diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go
deleted file mode 100644
index d6ae7b4d..00000000
--- a/routers/api/v1/repo/issue.go
+++ /dev/null
@@ -1,201 +0,0 @@
-// Copyright 2016 The Gogs Authors. All rights reserved.
-// Use of this source code is governed by a MIT-style
-// license that can be found in the LICENSE file.
-
-package repo
-
-import (
- "fmt"
- "strings"
-
- api "github.com/gogits/go-gogs-client"
-
- "github.com/gogits/gogs/models"
- "github.com/gogits/gogs/models/errors"
- "github.com/gogits/gogs/pkg/context"
- "github.com/gogits/gogs/pkg/setting"
-)
-
-func listIssues(c *context.APIContext, opts *models.IssuesOptions) {
- issues, err := models.Issues(opts)
- if err != nil {
- c.Error(500, "Issues", err)
- return
- }
-
- count, err := models.IssuesCount(opts)
- if err != nil {
- c.Error(500, "IssuesCount", err)
- return
- }
-
- // FIXME: use IssueList to improve performance.
- apiIssues := make([]*api.Issue, len(issues))
- for i := range issues {
- if err = issues[i].LoadAttributes(); err != nil {
- c.Error(500, "LoadAttributes", err)
- return
- }
- apiIssues[i] = issues[i].APIFormat()
- }
-
- c.SetLinkHeader(int(count), setting.UI.IssuePagingNum)
- c.JSON(200, &apiIssues)
-}
-
-func ListUserIssues(c *context.APIContext) {
- opts := models.IssuesOptions{
- AssigneeID: c.User.ID,
- Page: c.QueryInt("page"),
- IsClosed: api.StateType(c.Query("state")) == api.STATE_CLOSED,
- }
-
- listIssues(c, &opts)
-}
-
-func ListIssues(c *context.APIContext) {
- opts := models.IssuesOptions{
- RepoID: c.Repo.Repository.ID,
- Page: c.QueryInt("page"),
- IsClosed: api.StateType(c.Query("state")) == api.STATE_CLOSED,
- }
-
- listIssues(c, &opts)
-}
-
-func GetIssue(c *context.APIContext) {
- issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
- if err != nil {
- if errors.IsIssueNotExist(err) {
- c.Status(404)
- } else {
- c.Error(500, "GetIssueByIndex", err)
- }
- return
- }
- c.JSON(200, issue.APIFormat())
-}
-
-func CreateIssue(c *context.APIContext, form api.CreateIssueOption) {
- issue := &models.Issue{
- RepoID: c.Repo.Repository.ID,
- Title: form.Title,
- PosterID: c.User.ID,
- Poster: c.User,
- Content: form.Body,
- }
-
- if c.Repo.IsWriter() {
- if len(form.Assignee) > 0 {
- assignee, err := models.GetUserByName(form.Assignee)
- if err != nil {
- if errors.IsUserNotExist(err) {
- c.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", form.Assignee))
- } else {
- c.Error(500, "GetUserByName", err)
- }
- return
- }
- issue.AssigneeID = assignee.ID
- }
- issue.MilestoneID = form.Milestone
- } else {
- form.Labels = nil
- }
-
- 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(c.User, c.Repo.Repository, true); err != nil {
- c.Error(500, "ChangeStatus", err)
- return
- }
- }
-
- // Refetch from database to assign some automatic values
- var err error
- issue, err = models.GetIssueByID(issue.ID)
- if err != nil {
- c.Error(500, "GetIssueByID", err)
- return
- }
- c.JSON(201, issue.APIFormat())
-}
-
-func EditIssue(c *context.APIContext, form api.EditIssueOption) {
- issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
- if err != nil {
- if errors.IsIssueNotExist(err) {
- c.Status(404)
- } else {
- c.Error(500, "GetIssueByIndex", err)
- }
- return
- }
-
- if !issue.IsPoster(c.User.ID) && !c.Repo.IsWriter() {
- c.Status(403)
- return
- }
-
- if len(form.Title) > 0 {
- issue.Title = form.Title
- }
- if form.Body != nil {
- issue.Content = *form.Body
- }
-
- 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
- } else {
- assignee, err := models.GetUserByName(*form.Assignee)
- if err != nil {
- if errors.IsUserNotExist(err) {
- c.Error(422, "", fmt.Sprintf("assignee does not exist: [name: %s]", *form.Assignee))
- } else {
- c.Error(500, "GetUserByName", err)
- }
- return
- }
- issue.AssigneeID = assignee.ID
- }
-
- if err = models.UpdateIssueUserByAssignee(issue); err != nil {
- c.Error(500, "UpdateIssueUserByAssignee", err)
- return
- }
- }
- if c.Repo.IsWriter() && form.Milestone != nil &&
- issue.MilestoneID != *form.Milestone {
- oldMilestoneID := issue.MilestoneID
- issue.MilestoneID = *form.Milestone
- if err = models.ChangeMilestoneAssign(c.User, issue, oldMilestoneID); err != nil {
- c.Error(500, "ChangeMilestoneAssign", err)
- return
- }
- }
-
- if err = models.UpdateIssue(issue); err != nil {
- c.Error(500, "UpdateIssue", err)
- return
- }
- if form.State != nil {
- if err = issue.ChangeStatus(c.User, c.Repo.Repository, api.STATE_CLOSED == api.StateType(*form.State)); err != nil {
- c.Error(500, "ChangeStatus", err)
- return
- }
- }
-
- // Refetch from database to assign some automatic values
- issue, err = models.GetIssueByID(issue.ID)
- if err != nil {
- c.Error(500, "GetIssueByID", err)
- return
- }
- c.JSON(201, issue.APIFormat())
-}
diff --git a/routers/api/v1/repo/issue_comment.go b/routers/api/v1/repo/issue_comment.go
deleted file mode 100644
index 4a057d76..00000000
--- a/routers/api/v1/repo/issue_comment.go
+++ /dev/null
@@ -1,128 +0,0 @@
-// Copyright 2015 The Gogs Authors. All rights reserved.
-// Use of this source code is governed by a MIT-style
-// license that can be found in the LICENSE file.
-package repo
-
-import (
- "time"
-
- api "github.com/gogits/go-gogs-client"
-
- "github.com/gogits/gogs/models"
- "github.com/gogits/gogs/pkg/context"
-)
-
-func ListIssueComments(c *context.APIContext) {
- var since time.Time
- if len(c.Query("since")) > 0 {
- since, _ = time.Parse(time.RFC3339, c.Query("since"))
- }
-
- // comments,err:=models.GetCommentsByIssueIDSince(, since)
- issue, err := models.GetRawIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
- if err != nil {
- c.Error(500, "GetRawIssueByIndex", err)
- return
- }
-
- comments, err := models.GetCommentsByIssueIDSince(issue.ID, since.Unix())
- if err != nil {
- c.Error(500, "GetCommentsByIssueIDSince", err)
- return
- }
-
- apiComments := make([]*api.Comment, len(comments))
- for i := range comments {
- apiComments[i] = comments[i].APIFormat()
- }
- c.JSON(200, &apiComments)
-}
-
-func ListRepoIssueComments(c *context.APIContext) {
- var since time.Time
- if len(c.Query("since")) > 0 {
- since, _ = time.Parse(time.RFC3339, c.Query("since"))
- }
-
- comments, err := models.GetCommentsByRepoIDSince(c.Repo.Repository.ID, since.Unix())
- if err != nil {
- c.Error(500, "GetCommentsByRepoIDSince", err)
- return
- }
-
- apiComments := make([]*api.Comment, len(comments))
- for i := range comments {
- apiComments[i] = comments[i].APIFormat()
- }
- c.JSON(200, &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)
- return
- }
-
- comment, err := models.CreateIssueComment(c.User, c.Repo.Repository, issue, form.Body, nil)
- if err != nil {
- c.Error(500, "CreateIssueComment", err)
- return
- }
-
- c.JSON(201, 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)
- }
- return
- }
-
- if c.User.ID != comment.PosterID && !c.Repo.IsAdmin() {
- c.Status(403)
- return
- } else if comment.Type != models.COMMENT_TYPE_COMMENT {
- c.Status(204)
- return
- }
-
- oldContent := comment.Content
- comment.Content = form.Body
- if err := models.UpdateComment(c.User, comment, oldContent); err != nil {
- c.Error(500, "UpdateComment", err)
- return
- }
- c.JSON(200, 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)
- }
- return
- }
-
- if c.User.ID != comment.PosterID && !c.Repo.IsAdmin() {
- c.Status(403)
- return
- } else if comment.Type != models.COMMENT_TYPE_COMMENT {
- c.Status(204)
- return
- }
-
- if err = models.DeleteCommentByID(c.User, comment.ID); err != nil {
- c.Error(500, "DeleteCommentByID", err)
- return
- }
- c.Status(204)
-}
diff --git a/routers/api/v1/repo/issue_label.go b/routers/api/v1/repo/issue_label.go
deleted file mode 100644
index f3f2d730..00000000
--- a/routers/api/v1/repo/issue_label.go
+++ /dev/null
@@ -1,169 +0,0 @@
-// Copyright 2016 The Gogs Authors. All rights reserved.
-// Use of this source code is governed by a MIT-style
-// license that can be found in the LICENSE file.
-
-package repo
-
-import (
- api "github.com/gogits/go-gogs-client"
-
- "github.com/gogits/gogs/models"
- "github.com/gogits/gogs/models/errors"
- "github.com/gogits/gogs/pkg/context"
-)
-
-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)
- }
- return
- }
-
- apiLabels := make([]*api.Label, len(issue.Labels))
- for i := range issue.Labels {
- apiLabels[i] = issue.Labels[i].APIFormat()
- }
- c.JSON(200, &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)
- }
- return
- }
-
- labels, err := models.GetLabelsInRepoByIDs(c.Repo.Repository.ID, form.Labels)
- if err != nil {
- c.Error(500, "GetLabelsInRepoByIDs", err)
- return
- }
-
- if err = issue.AddLabels(c.User, labels); err != nil {
- c.Error(500, "AddLabels", err)
- return
- }
-
- labels, err = models.GetLabelsByIssueID(issue.ID)
- if err != nil {
- c.Error(500, "GetLabelsByIssueID", err)
- return
- }
-
- apiLabels := make([]*api.Label, len(labels))
- for i := range labels {
- apiLabels[i] = issue.Labels[i].APIFormat()
- }
- c.JSON(200, &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)
- }
- return
- }
-
- label, err := models.GetLabelOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
- if err != nil {
- if models.IsErrLabelNotExist(err) {
- c.Error(422, "", err)
- } else {
- c.Error(500, "GetLabelInRepoByID", err)
- }
- return
- }
-
- if err := models.DeleteIssueLabel(issue, label); err != nil {
- c.Error(500, "DeleteIssueLabel", err)
- return
- }
-
- c.Status(204)
-}
-
-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)
- }
- return
- }
-
- labels, err := models.GetLabelsInRepoByIDs(c.Repo.Repository.ID, form.Labels)
- if err != nil {
- c.Error(500, "GetLabelsInRepoByIDs", err)
- return
- }
-
- if err := issue.ReplaceLabels(labels); err != nil {
- c.Error(500, "ReplaceLabels", err)
- return
- }
-
- labels, err = models.GetLabelsByIssueID(issue.ID)
- if err != nil {
- c.Error(500, "GetLabelsByIssueID", err)
- return
- }
-
- apiLabels := make([]*api.Label, len(labels))
- for i := range labels {
- apiLabels[i] = issue.Labels[i].APIFormat()
- }
- c.JSON(200, &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)
- }
- return
- }
-
- if err := issue.ClearLabels(c.User); err != nil {
- c.Error(500, "ClearLabels", err)
- return
- }
-
- c.Status(204)
-}
diff --git a/routers/api/v1/repo/key.go b/routers/api/v1/repo/key.go
deleted file mode 100644
index 901df4c7..00000000
--- a/routers/api/v1/repo/key.go
+++ /dev/null
@@ -1,114 +0,0 @@
-// Copyright 2015 The Gogs Authors. All rights reserved.
-// Use of this source code is governed by a MIT-style
-// license that can be found in the LICENSE file.
-
-package repo
-
-import (
- "fmt"
-
- api "github.com/gogits/go-gogs-client"
-
- "github.com/gogits/gogs/models"
- "github.com/gogits/gogs/pkg/context"
- "github.com/gogits/gogs/pkg/setting"
- "github.com/gogits/gogs/routers/api/v1/convert"
-)
-
-func composeDeployKeysAPILink(repoPath string) string {
- return setting.AppURL + "api/v1/repos/" + repoPath + "/keys/"
-}
-
-// https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#list-deploy-keys
-func ListDeployKeys(c *context.APIContext) {
- keys, err := models.ListDeployKeys(c.Repo.Repository.ID)
- if err != nil {
- c.Error(500, "ListDeployKeys", err)
- return
- }
-
- 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 {
- c.Error(500, "GetContent", err)
- return
- }
- apiKeys[i] = convert.ToDeployKey(apiLink, keys[i])
- }
-
- c.JSON(200, &apiKeys)
-}
-
-// https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#get-a-deploy-key
-func GetDeployKey(c *context.APIContext) {
- key, err := models.GetDeployKeyByID(c.ParamsInt64(":id"))
- if err != nil {
- if models.IsErrDeployKeyNotExist(err) {
- c.Status(404)
- } else {
- c.Error(500, "GetDeployKeyByID", err)
- }
- return
- }
-
- if err = key.GetContent(); err != nil {
- c.Error(500, "GetContent", err)
- return
- }
-
- apiLink := composeDeployKeysAPILink(c.Repo.Owner.Name + "/" + c.Repo.Repository.Name)
- c.JSON(200, convert.ToDeployKey(apiLink, key))
-}
-
-func HandleCheckKeyStringError(c *context.APIContext, err error) {
- if models.IsErrKeyUnableVerify(err) {
- c.Error(422, "", "Unable to verify key content")
- } else {
- c.Error(422, "", fmt.Errorf("Invalid key content: %v", err))
- }
-}
-
-func HandleAddKeyError(c *context.APIContext, err error) {
- switch {
- case models.IsErrKeyAlreadyExist(err):
- c.Error(422, "", "Key content has been used as non-deploy key")
- case models.IsErrKeyNameAlreadyUsed(err):
- c.Error(422, "", "Key title has been used")
- default:
- c.Error(500, "AddKey", err)
- }
-}
-
-// https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#add-a-new-deploy-key
-func CreateDeployKey(c *context.APIContext, form api.CreateKeyOption) {
- content, err := models.CheckPublicKeyString(form.Key)
- if err != nil {
- HandleCheckKeyStringError(c, err)
- return
- }
-
- key, err := models.AddDeployKey(c.Repo.Repository.ID, form.Title, content)
- if err != nil {
- HandleAddKeyError(c, err)
- return
- }
-
- key.Content = content
- 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(c *context.APIContext) {
- if err := models.DeleteDeployKey(c.User, c.ParamsInt64(":id")); err != nil {
- if models.IsErrKeyAccessDenied(err) {
- c.Error(403, "", "You do not have access to this key")
- } else {
- c.Error(500, "DeleteDeployKey", err)
- }
- return
- }
-
- c.Status(204)
-}
diff --git a/routers/api/v1/repo/label.go b/routers/api/v1/repo/label.go
deleted file mode 100644
index 1161d633..00000000
--- a/routers/api/v1/repo/label.go
+++ /dev/null
@@ -1,110 +0,0 @@
-// Copyright 2016 The Gogs Authors. All rights reserved.
-// Use of this source code is governed by a MIT-style
-// license that can be found in the LICENSE file.
-
-package repo
-
-import (
- "github.com/Unknwon/com"
-
- api "github.com/gogits/go-gogs-client"
-
- "github.com/gogits/gogs/models"
- "github.com/gogits/gogs/pkg/context"
-)
-
-func ListLabels(c *context.APIContext) {
- labels, err := models.GetLabelsByRepoID(c.Repo.Repository.ID)
- if err != nil {
- c.Error(500, "GetLabelsByRepoID", err)
- return
- }
-
- apiLabels := make([]*api.Label, len(labels))
- for i := range labels {
- apiLabels[i] = labels[i].APIFormat()
- }
- c.JSON(200, &apiLabels)
-}
-
-func GetLabel(c *context.APIContext) {
- var label *models.Label
- var err error
- idStr := c.Params(":id")
- if id := com.StrTo(idStr).MustInt64(); id > 0 {
- label, err = models.GetLabelOfRepoByID(c.Repo.Repository.ID, id)
- } else {
- 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)
- }
- return
- }
-
- c.JSON(200, 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)
- return
- }
- c.JSON(201, 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)
- }
- return
- }
-
- if form.Name != nil {
- label.Name = *form.Name
- }
- if form.Color != nil {
- label.Color = *form.Color
- }
- if err := models.UpdateLabel(label); err != nil {
- c.Handle(500, "UpdateLabel", err)
- return
- }
- c.JSON(200, 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)
- return
- }
-
- c.Status(204)
-}
diff --git a/routers/api/v1/repo/milestone.go b/routers/api/v1/repo/milestone.go
deleted file mode 100644
index baf8eb2f..00000000
--- a/routers/api/v1/repo/milestone.go
+++ /dev/null
@@ -1,103 +0,0 @@
-// Copyright 2016 The Gogs Authors. All rights reserved.
-// Use of this source code is governed by a MIT-style
-// license that can be found in the LICENSE file.
-
-package repo
-
-import (
- "time"
-
- api "github.com/gogits/go-gogs-client"
-
- "github.com/gogits/gogs/models"
- "github.com/gogits/gogs/pkg/context"
-)
-
-func ListMilestones(c *context.APIContext) {
- milestones, err := models.GetMilestonesByRepoID(c.Repo.Repository.ID)
- if err != nil {
- c.Error(500, "GetMilestonesByRepoID", err)
- return
- }
-
- apiMilestones := make([]*api.Milestone, len(milestones))
- for i := range milestones {
- apiMilestones[i] = milestones[i].APIFormat()
- }
- c.JSON(200, &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)
- }
- return
- }
- c.JSON(200, milestone.APIFormat())
-}
-
-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: c.Repo.Repository.ID,
- Name: form.Title,
- Content: form.Description,
- Deadline: *form.Deadline,
- }
-
- if err := models.NewMilestone(milestone); err != nil {
- c.Error(500, "NewMilestone", err)
- return
- }
- c.JSON(201, 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)
- }
- return
- }
-
- if len(form.Title) > 0 {
- milestone.Name = form.Title
- }
- if form.Description != nil {
- milestone.Content = *form.Description
- }
- if form.Deadline != nil && !form.Deadline.IsZero() {
- milestone.Deadline = *form.Deadline
- }
-
- if form.State != nil {
- if err = milestone.ChangeStatus(api.STATE_CLOSED == api.StateType(*form.State)); err != nil {
- c.Error(500, "ChangeStatus", err)
- return
- }
- } else if err = models.UpdateMilestone(milestone); err != nil {
- c.Handle(500, "UpdateMilestone", err)
- return
- }
-
- c.JSON(200, 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)
- return
- }
- c.Status(204)
-}
diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go
deleted file mode 100644
index 727e1678..00000000
--- a/routers/api/v1/repo/repo.go
+++ /dev/null
@@ -1,380 +0,0 @@
-// Copyright 2014 The Gogs Authors. All rights reserved.
-// Use of this source code is governed by a MIT-style
-// license that can be found in the LICENSE file.
-
-package repo
-
-import (
- "path"
-
- log "gopkg.in/clog.v1"
-
- api "github.com/gogits/go-gogs-client"
-
- "github.com/gogits/gogs/models"
- "github.com/gogits/gogs/models/errors"
- "github.com/gogits/gogs/pkg/context"
- "github.com/gogits/gogs/pkg/form"
- "github.com/gogits/gogs/pkg/setting"
- "github.com/gogits/gogs/routers/api/v1/convert"
-)
-
-// https://github.com/gogits/go-gogs-client/wiki/Repositories#search-repositories
-func Search(c *context.APIContext) {
- opts := &models.SearchRepoOptions{
- Keyword: path.Base(c.Query("q")),
- OwnerID: c.QueryInt64("uid"),
- PageSize: convert.ToCorrectPageSize(c.QueryInt("limit")),
- }
-
- // Check visibility.
- 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 {
- c.JSON(500, map[string]interface{}{
- "ok": false,
- "error": err.Error(),
- })
- return
- }
- if u.IsOrganization() && u.IsOwnedBy(c.User.ID) {
- opts.Private = true
- }
- // FIXME: how about collaborators?
- }
- }
-
- repos, count, err := models.SearchRepositoryByName(opts)
- if err != nil {
- c.JSON(500, map[string]interface{}{
- "ok": false,
- "error": err.Error(),
- })
- return
- }
-
- if err = models.RepositoryList(repos).LoadAttributes(); err != nil {
- c.JSON(500, map[string]interface{}{
- "ok": false,
- "error": err.Error(),
- })
- return
- }
-
- results := make([]*api.Repository, len(repos))
- for i := range repos {
- results[i] = repos[i].APIFormat(nil)
- }
-
- c.SetLinkHeader(int(count), setting.API.MaxResponseItems)
- c.JSON(200, map[string]interface{}{
- "ok": true,
- "data": results,
- })
-}
-
-func listUserRepositories(c *context.APIContext, username string) {
- user, err := models.GetUserByName(username)
- if err != nil {
- c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
- return
- }
-
- // Only list public repositories if user requests someone else's repository list,
- // or an organization isn't a member of.
- var ownRepos []*models.Repository
- if user.IsOrganization() {
- ownRepos, _, err = user.GetUserRepositories(c.User.ID, 1, user.NumRepos)
- } else {
- ownRepos, err = models.GetUserRepositories(&models.UserRepoOptions{
- UserID: user.ID,
- Private: c.User.ID == user.ID,
- Page: 1,
- PageSize: user.NumRepos,
- })
- }
- if err != nil {
- c.Error(500, "GetUserRepositories", err)
- return
- }
-
- 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})
- }
- c.JSON(200, &repos)
- return
- }
-
- accessibleRepos, err := user.GetRepositoryAccesses()
- if err != nil {
- c.Error(500, "GetRepositoryAccesses", err)
- return
- }
-
- numOwnRepos := len(ownRepos)
- repos := make([]*api.Repository, numOwnRepos+len(accessibleRepos))
- for i := range ownRepos {
- repos[i] = ownRepos[i].APIFormat(&api.Permission{true, true, true})
- }
-
- i := numOwnRepos
- for repo, access := range accessibleRepos {
- repos[i] = repo.APIFormat(&api.Permission{
- Admin: access >= models.ACCESS_MODE_ADMIN,
- Push: access >= models.ACCESS_MODE_WRITE,
- Pull: true,
- })
- i++
- }
-
- c.JSON(200, &repos)
-}
-
-func ListMyRepos(c *context.APIContext) {
- listUserRepositories(c, c.User.Name)
-}
-
-func ListUserRepositories(c *context.APIContext) {
- listUserRepositories(c, c.Params(":username"))
-}
-
-func ListOrgRepositories(c *context.APIContext) {
- listUserRepositories(c, c.Params(":org"))
-}
-
-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,
- License: opt.License,
- Readme: opt.Readme,
- IsPrivate: opt.Private,
- AutoInit: opt.AutoInit,
- })
- if err != nil {
- if models.IsErrRepoAlreadyExist(err) ||
- models.IsErrNameReserved(err) ||
- models.IsErrNamePatternNotAllowed(err) {
- c.Error(422, "", err)
- } else {
- if repo != nil {
- if err = models.DeleteRepository(c.User.ID, repo.ID); err != nil {
- log.Error(2, "DeleteRepository: %v", err)
- }
- }
- c.Error(500, "CreateRepository", err)
- }
- return
- }
-
- c.JSON(201, repo.APIFormat(&api.Permission{true, true, true}))
-}
-
-// https://github.com/gogits/go-gogs-client/wiki/Repositories#create
-func Create(c *context.APIContext, opt api.CreateRepoOption) {
- // Shouldn't reach this condition, but just in case.
- if c.User.IsOrganization() {
- c.Error(422, "", "not allowed creating repository for organization")
- return
- }
- CreateUserRepo(c, c.User, opt)
-}
-
-func CreateOrgRepo(c *context.APIContext, opt api.CreateRepoOption) {
- org, err := models.GetOrgByName(c.Params(":org"))
- if err != nil {
- if errors.IsUserNotExist(err) {
- c.Error(422, "", err)
- } else {
- c.Error(500, "GetOrgByName", err)
- }
- return
- }
-
- if !org.IsOwnedBy(c.User.ID) {
- c.Error(403, "", "Given user is not owner of organization.")
- return
- }
- CreateUserRepo(c, org, opt)
-}
-
-// https://github.com/gogits/go-gogs-client/wiki/Repositories#migrate
-func Migrate(c *context.APIContext, f form.MigrateRepo) {
- ctxUser := c.User
- // Not equal means context user is an organization,
- // 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) {
- c.Error(422, "", err)
- } else {
- c.Error(500, "GetUserByID", err)
- }
- return
- } else if !org.IsOrganization() && !c.User.IsAdmin {
- c.Error(403, "", "Given user is not an organization")
- return
- }
- ctxUser = org
- }
-
- if c.HasError() {
- c.Error(422, "", c.GetErrMsg())
- return
- }
-
- if ctxUser.IsOrganization() && !c.User.IsAdmin {
- // Check ownership of organization.
- if !ctxUser.IsOwnedBy(c.User.ID) {
- c.Error(403, "", "Given user is not owner of organization")
- return
- }
- }
-
- remoteAddr, err := f.ParseRemoteAddr(c.User)
- if err != nil {
- if models.IsErrInvalidCloneAddr(err) {
- addrErr := err.(models.ErrInvalidCloneAddr)
- switch {
- case addrErr.IsURLError:
- c.Error(422, "", err)
- case addrErr.IsPermissionDenied:
- c.Error(422, "", "You are not allowed to import local repositories")
- case addrErr.IsInvalidPath:
- c.Error(422, "", "Invalid local path, it does not exist or not a directory")
- default:
- c.Error(500, "ParseRemoteAddr", "Unknown error type (ErrInvalidCloneAddr): "+err.Error())
- }
- } else {
- c.Error(500, "ParseRemoteAddr", err)
- }
- return
- }
-
- repo, err := models.MigrateRepository(c.User, ctxUser, models.MigrateRepoOptions{
- Name: f.RepoName,
- Description: f.Description,
- IsPrivate: f.Private || setting.Repository.ForcePrivate,
- IsMirror: f.Mirror,
- RemoteAddr: remoteAddr,
- })
- if err != nil {
- if repo != nil {
- if errDelete := models.DeleteRepository(ctxUser.ID, repo.ID); errDelete != nil {
- log.Error(2, "DeleteRepository: %v", errDelete)
- }
- }
-
- if errors.IsReachLimitOfRepo(err) {
- c.Error(422, "", err)
- } else {
- c.Error(500, "MigrateRepository", models.HandleMirrorCredentials(err.Error(), true))
- }
- return
- }
-
- log.Trace("Repository migrated: %s/%s", ctxUser.Name, f.RepoName)
- c.JSON(201, repo.APIFormat(&api.Permission{true, true, true}))
-}
-
-func parseOwnerAndRepo(c *context.APIContext) (*models.User, *models.Repository) {
- owner, err := models.GetUserByName(c.Params(":username"))
- if err != nil {
- if errors.IsUserNotExist(err) {
- c.Error(422, "", err)
- } else {
- c.Error(500, "GetUserByName", err)
- }
- return nil, nil
- }
-
- repo, err := models.GetRepositoryByName(owner.ID, c.Params(":reponame"))
- if err != nil {
- if errors.IsRepoNotExist(err) {
- c.Status(404)
- } else {
- c.Error(500, "GetRepositoryByName", err)
- }
- return nil, nil
- }
-
- return owner, repo
-}
-
-// https://github.com/gogits/go-gogs-client/wiki/Repositories#get
-func Get(c *context.APIContext) {
- _, repo := parseOwnerAndRepo(c)
- if c.Written() {
- return
- }
-
- c.JSON(200, repo.APIFormat(&api.Permission{
- Admin: c.Repo.IsAdmin(),
- Push: c.Repo.IsWriter(),
- Pull: true,
- }))
-}
-
-// https://github.com/gogits/go-gogs-client/wiki/Repositories#delete
-func Delete(c *context.APIContext) {
- owner, repo := parseOwnerAndRepo(c)
- if c.Written() {
- return
- }
-
- 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 {
- c.Error(500, "DeleteRepository", err)
- return
- }
-
- log.Trace("Repository deleted: %s/%s", owner.Name, repo.Name)
- c.Status(204)
-}
-
-func ListForks(c *context.APIContext) {
- forks, err := c.Repo.Repository.GetForks()
- if err != nil {
- c.Error(500, "GetForks", err)
- return
- }
-
- apiForks := make([]*api.Repository, len(forks))
- for i := range forks {
- if err := forks[i].GetOwner(); err != nil {
- c.Error(500, "GetOwner", err)
- return
- }
- apiForks[i] = forks[i].APIFormat(&api.Permission{
- Admin: c.User.IsAdminOfRepo(forks[i]),
- Push: c.User.IsWriterOfRepo(forks[i]),
- Pull: true,
- })
- }
-
- c.JSON(200, &apiForks)
-}
-
-func MirrorSync(c *context.APIContext) {
- _, repo := parseOwnerAndRepo(c)
- if c.Written() {
- return
- } else if !repo.IsMirror {
- c.Status(404)
- return
- }
-
- go models.MirrorQueue.Add(repo.ID)
- c.Status(202)
-}