diff options
author | Unknwon <u@gogs.io> | 2015-11-18 21:21:47 -0500 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2015-11-18 21:21:47 -0500 |
commit | f455125d4de8c788143a24edefb8c6b2cef0f524 (patch) | |
tree | b6a258d73a3e25362fd0005b2954ebe84869fede /routers | |
parent | df339ad8b0c4f8858a53a4890ded97a1e3208b13 (diff) | |
download | gitea-f455125d4de8c788143a24edefb8c6b2cef0f524.tar.gz gitea-f455125d4de8c788143a24edefb8c6b2cef0f524.zip |
fix #878
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/repo_hooks.go | 10 | ||||
-rw-r--r-- | routers/api/v1/repo_keys.go | 115 | ||||
-rw-r--r-- | routers/repo/setting.go | 11 | ||||
-rw-r--r-- | routers/user/setting.go | 2 |
4 files changed, 129 insertions, 9 deletions
diff --git a/routers/api/v1/repo_hooks.go b/routers/api/v1/repo_hooks.go index 91547cf16f..77c2ba6609 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 0000000000..7016c55ed4 --- /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/repo/setting.go b/routers/repo/setting.go index 019949b793..2d686cdd34 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -259,9 +259,9 @@ func Webhooks(ctx *middleware.Context) { ctx.Data["BaseLink"] = ctx.Repo.RepoLink ctx.Data["Description"] = ctx.Tr("repo.settings.hooks_desc", "https://github.com/gogits/go-gogs-client/wiki/Repositories---Webhooks") - ws, err := models.GetWebhooksByRepoId(ctx.Repo.Repository.ID) + ws, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID) if err != nil { - ctx.Handle(500, "GetWebhooksByRepoId", err) + ctx.Handle(500, "GetWebhooksByRepoID", err) return } ctx.Data["Webhooks"] = ws @@ -684,7 +684,7 @@ func DeployKeysPost(ctx *middleware.Context, form auth.AddSSHKeyForm) { content, err := models.CheckPublicKeyString(form.Content) if err != nil { - if err == models.ErrKeyUnableVerify { + if models.IsErrKeyUnableVerify(err) { ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key")) } else { ctx.Data["HasError"] = true @@ -695,7 +695,8 @@ func DeployKeysPost(ctx *middleware.Context, form auth.AddSSHKeyForm) { } } - if err = models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content); err != nil { + key, err := models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content) + if err != nil { ctx.Data["HasError"] = true switch { case models.IsErrKeyAlreadyExist(err): @@ -711,7 +712,7 @@ func DeployKeysPost(ctx *middleware.Context, form auth.AddSSHKeyForm) { } log.Trace("Deploy key added: %d", ctx.Repo.Repository.ID) - ctx.Flash.Success(ctx.Tr("repo.settings.add_key_success", form.Title)) + ctx.Flash.Success(ctx.Tr("repo.settings.add_key_success", key.Name)) ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys") } diff --git a/routers/user/setting.go b/routers/user/setting.go index 9be567a487..53b7fdf929 100644 --- a/routers/user/setting.go +++ b/routers/user/setting.go @@ -286,7 +286,7 @@ func SettingsSSHKeysPost(ctx *middleware.Context, form auth.AddSSHKeyForm) { content, err := models.CheckPublicKeyString(form.Content) if err != nil { - if err == models.ErrKeyUnableVerify { + if models.IsErrKeyUnableVerify(err) { ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key")) } else { ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error())) |