aboutsummaryrefslogtreecommitdiff
path: root/routers/api/v1
diff options
context:
space:
mode:
Diffstat (limited to 'routers/api/v1')
-rw-r--r--routers/api/v1/repo.go37
-rw-r--r--routers/api/v1/repo_hooks.go10
-rw-r--r--routers/api/v1/repo_keys.go115
-rw-r--r--routers/api/v1/user.go2
4 files changed, 141 insertions, 23 deletions
diff --git a/routers/api/v1/repo.go b/routers/api/v1/repo.go
index 5a002677..b4da4c6f 100644
--- a/routers/api/v1/repo.go
+++ b/routers/api/v1/repo.go
@@ -5,9 +5,7 @@
package v1
import (
- "net/url"
"path"
- "strings"
"github.com/Unknwon/com"
@@ -108,9 +106,9 @@ func ListMyRepos(ctx *middleware.Context) {
}
numOwnRepos := len(ownRepos)
- accessibleRepos, err := ctx.User.GetAccessibleRepositories()
+ accessibleRepos, err := ctx.User.GetRepositoryAccesses()
if err != nil {
- ctx.APIError(500, "GetAccessibleRepositories", err)
+ ctx.APIError(500, "GetRepositoryAccesses", err)
return
}
@@ -218,22 +216,23 @@ func MigrateRepo(ctx *middleware.Context, form auth.MigrateRepoForm) {
}
}
- // Remote address can be HTTP/HTTPS/Git URL or local path.
- remoteAddr := form.CloneAddr
- if strings.HasPrefix(form.CloneAddr, "http://") ||
- strings.HasPrefix(form.CloneAddr, "https://") ||
- strings.HasPrefix(form.CloneAddr, "git://") {
- u, err := url.Parse(form.CloneAddr)
- if err != nil {
- ctx.APIError(422, "", err)
- return
- }
- if len(form.AuthUsername) > 0 || len(form.AuthPassword) > 0 {
- u.User = url.UserPassword(form.AuthUsername, form.AuthPassword)
+ remoteAddr, err := form.ParseRemoteAddr(ctx.User)
+ if err != nil {
+ if models.IsErrInvalidCloneAddr(err) {
+ addrErr := err.(models.ErrInvalidCloneAddr)
+ switch {
+ case addrErr.IsURLError:
+ ctx.APIError(422, "", err)
+ case addrErr.IsPermissionDenied:
+ ctx.APIError(422, "", "You are not allowed to import local repositories.")
+ case addrErr.IsInvalidPath:
+ ctx.APIError(422, "", "Invalid local path, it does not exist or not a directory.")
+ default:
+ ctx.APIError(500, "ParseRemoteAddr", "Unknown error type (ErrInvalidCloneAddr): "+err.Error())
+ }
+ } else {
+ ctx.APIError(500, "ParseRemoteAddr", err)
}
- remoteAddr = u.String()
- } else if !com.IsDir(remoteAddr) {
- ctx.APIError(422, "", "Invalid local path, it does not exist or not a directory.")
return
}
diff --git a/routers/api/v1/repo_hooks.go b/routers/api/v1/repo_hooks.go
index 91547cf1..77c2ba66 100644
--- a/routers/api/v1/repo_hooks.go
+++ b/routers/api/v1/repo_hooks.go
@@ -44,9 +44,9 @@ func ToApiHook(repoLink string, w *models.Webhook) *api.Hook {
// https://github.com/gogits/go-gogs-client/wiki/Repositories#list-hooks
func ListRepoHooks(ctx *middleware.Context) {
- hooks, err := models.GetWebhooksByRepoId(ctx.Repo.Repository.ID)
+ hooks, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID)
if err != nil {
- ctx.APIError(500, "GetWebhooksByRepoId", err)
+ ctx.APIError(500, "GetWebhooksByRepoID", err)
return
}
@@ -127,7 +127,11 @@ func CreateRepoHook(ctx *middleware.Context, form api.CreateHookOption) {
func EditRepoHook(ctx *middleware.Context, form api.EditHookOption) {
w, err := models.GetWebhookByID(ctx.ParamsInt64(":id"))
if err != nil {
- ctx.APIError(500, "GetWebhookById", err)
+ if models.IsErrWebhookNotExist(err) {
+ ctx.Error(404)
+ } else {
+ ctx.APIError(500, "GetWebhookById", err)
+ }
return
}
diff --git a/routers/api/v1/repo_keys.go b/routers/api/v1/repo_keys.go
new file mode 100644
index 00000000..7016c55e
--- /dev/null
+++ b/routers/api/v1/repo_keys.go
@@ -0,0 +1,115 @@
+// 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 v1
+
+import (
+ "fmt"
+
+ "github.com/Unknwon/com"
+
+ api "github.com/gogits/go-gogs-client"
+
+ "github.com/gogits/gogs/models"
+ "github.com/gogits/gogs/modules/middleware"
+ "github.com/gogits/gogs/modules/setting"
+)
+
+func ToApiDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey {
+ return &api.DeployKey{
+ ID: key.ID,
+ Key: key.Content,
+ URL: apiLink + com.ToStr(key.ID),
+ Title: key.Name,
+ Created: key.Created,
+ ReadOnly: true, // All deploy keys are read-only.
+ }
+}
+
+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 ListRepoDeployKeys(ctx *middleware.Context) {
+ keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
+ if err != nil {
+ ctx.Handle(500, "ListDeployKeys", err)
+ return
+ }
+
+ apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name)
+ apiKeys := make([]*api.DeployKey, len(keys))
+ for i := range keys {
+ if err = keys[i].GetContent(); err != nil {
+ ctx.APIError(500, "GetContent", err)
+ return
+ }
+ apiKeys[i] = ToApiDeployKey(apiLink, keys[i])
+ }
+
+ ctx.JSON(200, &apiKeys)
+}
+
+// https://github.com/gogits/go-gogs-client/wiki/Repositories---Deploy-Keys#get-a-deploy-key
+func GetRepoDeployKey(ctx *middleware.Context) {
+ key, err := models.GetDeployKeyByID(ctx.ParamsInt64(":id"))
+ if err != nil {
+ if models.IsErrDeployKeyNotExist(err) {
+ ctx.Error(404)
+ } else {
+ ctx.Handle(500, "GetDeployKeyByID", err)
+ }
+ return
+ }
+
+ if err = key.GetContent(); err != nil {
+ ctx.APIError(500, "GetContent", err)
+ return
+ }
+
+ apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name)
+ ctx.JSON(200, ToApiDeployKey(apiLink, key))
+}
+
+// https://github.com/gogits/go-gogs-client/wiki/Repositories---Deploy-Keys#add-a-new-deploy-key
+func CreateRepoDeployKey(ctx *middleware.Context, form api.CreateDeployKeyOption) {
+ content, err := models.CheckPublicKeyString(form.Key)
+ if err != nil {
+ if models.IsErrKeyUnableVerify(err) {
+ ctx.APIError(422, "", "Unable to verify key content")
+ } else {
+ ctx.APIError(422, "", fmt.Errorf("Invalid key content: %v", err))
+ }
+ return
+ }
+
+ key, err := models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content)
+ if err != nil {
+ ctx.Data["HasError"] = true
+ switch {
+ case models.IsErrKeyAlreadyExist(err):
+ ctx.APIError(422, "", "Key content has been used as non-deploy key")
+ case models.IsErrKeyNameAlreadyUsed(err):
+ ctx.APIError(422, "", "Key title has been used")
+ default:
+ ctx.APIError(500, "AddDeployKey", err)
+ }
+ return
+ }
+
+ key.Content = content
+ apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name)
+ ctx.JSON(201, ToApiDeployKey(apiLink, key))
+}
+
+// https://github.com/gogits/go-gogs-client/wiki/Repositories---Deploy-Keys#remove-a-deploy-key
+func DeleteRepoDeploykey(ctx *middleware.Context) {
+ if err := models.DeleteDeployKey(ctx.ParamsInt64(":id")); err != nil {
+ ctx.APIError(500, "DeleteDeployKey", err)
+ return
+ }
+
+ ctx.Status(204)
+}
diff --git a/routers/api/v1/user.go b/routers/api/v1/user.go
index f27cd3ae..ec4f720c 100644
--- a/routers/api/v1/user.go
+++ b/routers/api/v1/user.go
@@ -55,7 +55,7 @@ func SearchUsers(ctx *middleware.Context) {
}
}
- ctx.Render.JSON(200, map[string]interface{}{
+ ctx.JSON(200, map[string]interface{}{
"ok": true,
"data": results,
})