![](public/img/gogs-large-resize.png) | ![](public/img/gogs-large-resize.png) | ||||
##### Current version: 0.7.17 Beta | |||||
##### Current version: 0.7.18 Beta | |||||
<table> | <table> | ||||
<tr> | <tr> |
m.Patch("/hooks/:id:int", bind(api.EditHookOption{}), v1.EditRepoHook) | m.Patch("/hooks/:id:int", bind(api.EditHookOption{}), v1.EditRepoHook) | ||||
m.Get("/raw/*", middleware.RepoRef(), v1.GetRepoRawFile) | m.Get("/raw/*", middleware.RepoRef(), v1.GetRepoRawFile) | ||||
m.Get("/archive/*", v1.GetRepoArchive) | m.Get("/archive/*", v1.GetRepoArchive) | ||||
m.Group("/keys", func() { | |||||
m.Combo("").Get(v1.ListRepoDeployKeys). | |||||
Post(bind(api.CreateDeployKeyOption{}), v1.CreateRepoDeployKey) | |||||
m.Combo("/:id").Get(v1.GetRepoDeployKey). | |||||
Delete(v1.DeleteRepoDeploykey) | |||||
}) | |||||
}, middleware.ApiRepoAssignment()) | }, middleware.ApiRepoAssignment()) | ||||
}, middleware.ApiReqToken()) | }, middleware.ApiReqToken()) | ||||
"github.com/gogits/gogs/modules/setting" | "github.com/gogits/gogs/modules/setting" | ||||
) | ) | ||||
const APP_VER = "0.7.17.1118 Beta" | |||||
const APP_VER = "0.7.18.1118 Beta" | |||||
func init() { | func init() { | ||||
runtime.GOMAXPROCS(runtime.NumCPU()) | runtime.GOMAXPROCS(runtime.NumCPU()) |
// |____| |____/|___ /____/__|\___ > |____|__ \___ > ____| | // |____| |____/|___ /____/__|\___ > |____|__ \___ > ____| | ||||
// \/ \/ \/ \/\/ | // \/ \/ \/ \/\/ | ||||
type ErrKeyUnableVerify struct { | |||||
Result string | |||||
} | |||||
func IsErrKeyUnableVerify(err error) bool { | |||||
_, ok := err.(ErrKeyUnableVerify) | |||||
return ok | |||||
} | |||||
func (err ErrKeyUnableVerify) Error() string { | |||||
return fmt.Sprintf("Unable to verify key content [result: %s]", err.Result) | |||||
} | |||||
type ErrKeyNotExist struct { | type ErrKeyNotExist struct { | ||||
ID int64 | ID int64 | ||||
} | } | ||||
return fmt.Sprintf("public key already exists [owner_id: %d, name: %s]", err.OwnerID, err.Name) | return fmt.Sprintf("public key already exists [owner_id: %d, name: %s]", err.OwnerID, err.Name) | ||||
} | } | ||||
type ErrDeployKeyNotExist struct { | |||||
ID int64 | |||||
KeyID int64 | |||||
RepoID int64 | |||||
} | |||||
func IsErrDeployKeyNotExist(err error) bool { | |||||
_, ok := err.(ErrDeployKeyNotExist) | |||||
return ok | |||||
} | |||||
func (err ErrDeployKeyNotExist) Error() string { | |||||
return fmt.Sprintf("Deploy key does not exist [id: %d, key_id: %d, repo_id: %d]", err.ID, err.KeyID, err.RepoID) | |||||
} | |||||
type ErrDeployKeyAlreadyExist struct { | type ErrDeployKeyAlreadyExist struct { | ||||
KeyID int64 | KeyID int64 | ||||
RepoID int64 | RepoID int64 |
_TPL_PUBLICK_KEY = `command="%s serv key-%d --config='%s'",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty %s` + "\n" | _TPL_PUBLICK_KEY = `command="%s serv key-%d --config='%s'",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty %s` + "\n" | ||||
) | ) | ||||
var ( | |||||
ErrKeyUnableVerify = errors.New("Unable to verify public key") | |||||
) | |||||
var sshOpLocker = sync.Mutex{} | var sshOpLocker = sync.Mutex{} | ||||
var SSHPath string // SSH directory. | var SSHPath string // SSH directory. | ||||
sshKeygenOutput := strings.Split(stdout, " ") | sshKeygenOutput := strings.Split(stdout, " ") | ||||
if len(sshKeygenOutput) < 4 { | if len(sshKeygenOutput) < 4 { | ||||
return content, ErrKeyUnableVerify | |||||
return content, ErrKeyUnableVerify{stdout} | |||||
} | } | ||||
// Check if key type and key size match. | // Check if key type and key size match. | ||||
RepoID int64 `xorm:"UNIQUE(s) INDEX"` | RepoID int64 `xorm:"UNIQUE(s) INDEX"` | ||||
Name string | Name string | ||||
Fingerprint string | Fingerprint string | ||||
Content string `xorm:"-"` | |||||
Created time.Time `xorm:"CREATED"` | Created time.Time `xorm:"CREATED"` | ||||
Updated time.Time // Note: Updated must below Created for AfterSet. | Updated time.Time // Note: Updated must below Created for AfterSet. | ||||
HasRecentActivity bool `xorm:"-"` | HasRecentActivity bool `xorm:"-"` | ||||
} | } | ||||
} | } | ||||
// GetContent gets associated public key content. | |||||
func (k *DeployKey) GetContent() error { | |||||
pkey, err := GetPublicKeyByID(k.KeyID) | |||||
if err != nil { | |||||
return err | |||||
} | |||||
k.Content = pkey.Content | |||||
return nil | |||||
} | |||||
func checkDeployKey(e Engine, keyID, repoID int64, name string) error { | func checkDeployKey(e Engine, keyID, repoID int64, name string) error { | ||||
// Note: We want error detail, not just true or false here. | // Note: We want error detail, not just true or false here. | ||||
has, err := e.Where("key_id=? AND repo_id=?", keyID, repoID).Get(new(DeployKey)) | has, err := e.Where("key_id=? AND repo_id=?", keyID, repoID).Get(new(DeployKey)) | ||||
} | } | ||||
// addDeployKey adds new key-repo relation. | // addDeployKey adds new key-repo relation. | ||||
func addDeployKey(e *xorm.Session, keyID, repoID int64, name, fingerprint string) (err error) { | |||||
if err = checkDeployKey(e, keyID, repoID, name); err != nil { | |||||
return err | |||||
func addDeployKey(e *xorm.Session, keyID, repoID int64, name, fingerprint string) (*DeployKey, error) { | |||||
if err := checkDeployKey(e, keyID, repoID, name); err != nil { | |||||
return nil, err | |||||
} | } | ||||
_, err = e.Insert(&DeployKey{ | |||||
key := &DeployKey{ | |||||
KeyID: keyID, | KeyID: keyID, | ||||
RepoID: repoID, | RepoID: repoID, | ||||
Name: name, | Name: name, | ||||
Fingerprint: fingerprint, | Fingerprint: fingerprint, | ||||
}) | |||||
return err | |||||
} | |||||
_, err := e.Insert(key) | |||||
return key, err | |||||
} | } | ||||
// HasDeployKey returns true if public key is a deploy key of given repository. | // HasDeployKey returns true if public key is a deploy key of given repository. | ||||
} | } | ||||
// AddDeployKey add new deploy key to database and authorized_keys file. | // AddDeployKey add new deploy key to database and authorized_keys file. | ||||
func AddDeployKey(repoID int64, name, content string) (err error) { | |||||
if err = checkKeyContent(content); err != nil { | |||||
return err | |||||
func AddDeployKey(repoID int64, name, content string) (*DeployKey, error) { | |||||
if err := checkKeyContent(content); err != nil { | |||||
return nil, err | |||||
} | } | ||||
key := &PublicKey{ | |||||
pkey := &PublicKey{ | |||||
Content: content, | Content: content, | ||||
Mode: ACCESS_MODE_READ, | Mode: ACCESS_MODE_READ, | ||||
Type: KEY_TYPE_DEPLOY, | Type: KEY_TYPE_DEPLOY, | ||||
} | } | ||||
has, err := x.Get(key) | |||||
has, err := x.Get(pkey) | |||||
if err != nil { | if err != nil { | ||||
return err | |||||
return nil, err | |||||
} | } | ||||
sess := x.NewSession() | sess := x.NewSession() | ||||
defer sessionRelease(sess) | defer sessionRelease(sess) | ||||
if err = sess.Begin(); err != nil { | if err = sess.Begin(); err != nil { | ||||
return err | |||||
return nil, err | |||||
} | } | ||||
// First time use this deploy key. | // First time use this deploy key. | ||||
if !has { | if !has { | ||||
if err = addKey(sess, key); err != nil { | |||||
return nil | |||||
if err = addKey(sess, pkey); err != nil { | |||||
return nil, fmt.Errorf("addKey: %v", err) | |||||
} | } | ||||
} | } | ||||
if err = addDeployKey(sess, key.ID, repoID, name, key.Fingerprint); err != nil { | |||||
return err | |||||
key, err := addDeployKey(sess, pkey.ID, repoID, name, pkey.Fingerprint) | |||||
if err != nil { | |||||
return nil, fmt.Errorf("addDeployKey: %v", err) | |||||
} | } | ||||
return sess.Commit() | |||||
return key, sess.Commit() | |||||
} | |||||
// GetDeployKeyByID returns deploy key by given ID. | |||||
func GetDeployKeyByID(id int64) (*DeployKey, error) { | |||||
key := new(DeployKey) | |||||
has, err := x.Id(id).Get(key) | |||||
if err != nil { | |||||
return nil, err | |||||
} else if !has { | |||||
return nil, ErrDeployKeyNotExist{id, 0, 0} | |||||
} | |||||
return key, nil | |||||
} | } | ||||
// GetDeployKeyByRepo returns deploy key by given public key ID and repository ID. | // GetDeployKeyByRepo returns deploy key by given public key ID and repository ID. | ||||
KeyID: keyID, | KeyID: keyID, | ||||
RepoID: repoID, | RepoID: repoID, | ||||
} | } | ||||
_, err := x.Get(key) | |||||
return key, err | |||||
has, err := x.Get(key) | |||||
if err != nil { | |||||
return nil, err | |||||
} else if !has { | |||||
return nil, ErrDeployKeyNotExist{0, keyID, repoID} | |||||
} | |||||
return key, nil | |||||
} | } | ||||
// UpdateDeployKey updates deploy key information. | // UpdateDeployKey updates deploy key information. |
return ws, err | return ws, err | ||||
} | } | ||||
// GetWebhooksByRepoId returns all webhooks of repository. | |||||
func GetWebhooksByRepoId(repoID int64) (ws []*Webhook, err error) { | |||||
// GetWebhooksByRepoID returns all webhooks of repository. | |||||
func GetWebhooksByRepoID(repoID int64) (ws []*Webhook, err error) { | |||||
err = x.Find(&ws, &Webhook{RepoID: repoID}) | err = x.Find(&ws, &Webhook{RepoID: repoID}) | ||||
return ws, err | return ws, err | ||||
} | } |
// https://github.com/gogits/go-gogs-client/wiki/Repositories#list-hooks | // https://github.com/gogits/go-gogs-client/wiki/Repositories#list-hooks | ||||
func ListRepoHooks(ctx *middleware.Context) { | func ListRepoHooks(ctx *middleware.Context) { | ||||
hooks, err := models.GetWebhooksByRepoId(ctx.Repo.Repository.ID) | |||||
hooks, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID) | |||||
if err != nil { | if err != nil { | ||||
ctx.APIError(500, "GetWebhooksByRepoId", err) | |||||
ctx.APIError(500, "GetWebhooksByRepoID", err) | |||||
return | return | ||||
} | } | ||||
func EditRepoHook(ctx *middleware.Context, form api.EditHookOption) { | func EditRepoHook(ctx *middleware.Context, form api.EditHookOption) { | ||||
w, err := models.GetWebhookByID(ctx.ParamsInt64(":id")) | w, err := models.GetWebhookByID(ctx.ParamsInt64(":id")) | ||||
if err != nil { | if err != nil { | ||||
ctx.APIError(500, "GetWebhookById", err) | |||||
if models.IsErrWebhookNotExist(err) { | |||||
ctx.Error(404) | |||||
} else { | |||||
ctx.APIError(500, "GetWebhookById", err) | |||||
} | |||||
return | return | ||||
} | } | ||||
// 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) | |||||
} |
ctx.Data["BaseLink"] = ctx.Repo.RepoLink | 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") | 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 { | if err != nil { | ||||
ctx.Handle(500, "GetWebhooksByRepoId", err) | |||||
ctx.Handle(500, "GetWebhooksByRepoID", err) | |||||
return | return | ||||
} | } | ||||
ctx.Data["Webhooks"] = ws | ctx.Data["Webhooks"] = ws | ||||
content, err := models.CheckPublicKeyString(form.Content) | content, err := models.CheckPublicKeyString(form.Content) | ||||
if err != nil { | if err != nil { | ||||
if err == models.ErrKeyUnableVerify { | |||||
if models.IsErrKeyUnableVerify(err) { | |||||
ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key")) | ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key")) | ||||
} else { | } else { | ||||
ctx.Data["HasError"] = true | ctx.Data["HasError"] = true | ||||
} | } | ||||
} | } | ||||
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 | ctx.Data["HasError"] = true | ||||
switch { | switch { | ||||
case models.IsErrKeyAlreadyExist(err): | case models.IsErrKeyAlreadyExist(err): | ||||
} | } | ||||
log.Trace("Deploy key added: %d", ctx.Repo.Repository.ID) | 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") | ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys") | ||||
} | } | ||||
content, err := models.CheckPublicKeyString(form.Content) | content, err := models.CheckPublicKeyString(form.Content) | ||||
if err != nil { | if err != nil { | ||||
if err == models.ErrKeyUnableVerify { | |||||
if models.IsErrKeyUnableVerify(err) { | |||||
ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key")) | ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key")) | ||||
} else { | } else { | ||||
ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error())) | ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error())) |
0.7.17.1118 Beta | |||||
0.7.18.1118 Beta |