Finish new web hook pages
This commit is contained in:
parent
9820b8e134
commit
9a1d5d2489
23 changed files with 249 additions and 265 deletions
|
@ -209,6 +209,18 @@ func AddPublicKey(key *PublicKey) (err error) {
|
|||
return nil
|
||||
}
|
||||
|
||||
// GetPublicKeyById returns public key by given ID.
|
||||
func GetPublicKeyById(keyId int64) (*PublicKey, error) {
|
||||
key := new(PublicKey)
|
||||
has, err := x.Id(keyId).Get(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, ErrKeyNotExist
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
|
||||
// ListPublicKey returns a list of all public keys that user has.
|
||||
func ListPublicKey(uid int64) ([]*PublicKey, error) {
|
||||
keys := make([]*PublicKey, 0, 5)
|
||||
|
@ -277,6 +289,12 @@ func rewriteAuthorizedKeys(key *PublicKey, p, tmpP string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// UpdatePublicKey updates given public key.
|
||||
func UpdatePublicKey(key *PublicKey) error {
|
||||
_, err := x.Id(key.Id).AllCols().Update(key)
|
||||
return err
|
||||
}
|
||||
|
||||
// DeletePublicKey deletes SSH key information both in database and authorized_keys file.
|
||||
func DeletePublicKey(key *PublicKey) error {
|
||||
has, err := x.Get(key)
|
||||
|
|
|
@ -12,6 +12,7 @@ import (
|
|||
"github.com/gogits/gogs/modules/httplib"
|
||||
"github.com/gogits/gogs/modules/log"
|
||||
"github.com/gogits/gogs/modules/setting"
|
||||
"github.com/gogits/gogs/modules/uuid"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -122,6 +123,12 @@ const (
|
|||
SERVICE
|
||||
)
|
||||
|
||||
type HookEventType string
|
||||
|
||||
const (
|
||||
PUSH HookEventType = "push"
|
||||
)
|
||||
|
||||
type PayloadAuthor struct {
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
|
@ -157,13 +164,16 @@ type Payload struct {
|
|||
// HookTask represents a hook task.
|
||||
type HookTask struct {
|
||||
Id int64
|
||||
Uuid string
|
||||
Type HookTaskType
|
||||
Url string
|
||||
*Payload `xorm:"-"`
|
||||
PayloadContent string `xorm:"TEXT"`
|
||||
ContentType HookContentType
|
||||
EventType HookEventType
|
||||
IsSsl bool
|
||||
IsDeliveried bool
|
||||
IsSucceed bool
|
||||
}
|
||||
|
||||
// CreateHookTask creates a new hook task,
|
||||
|
@ -173,6 +183,7 @@ func CreateHookTask(t *HookTask) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
t.Uuid = uuid.NewV4().String()
|
||||
t.PayloadContent = string(data)
|
||||
_, err = x.Insert(t)
|
||||
return err
|
||||
|
@ -190,20 +201,32 @@ func DeliverHooks() {
|
|||
x.Where("is_deliveried=?", false).Iterate(new(HookTask),
|
||||
func(idx int, bean interface{}) error {
|
||||
t := bean.(*HookTask)
|
||||
// Only support JSON now.
|
||||
if _, err := httplib.Post(t.Url).SetTimeout(timeout, timeout).
|
||||
Body([]byte(t.PayloadContent)).Response(); err != nil {
|
||||
log.Error(4, "webhook.DeliverHooks(Delivery): %v", err)
|
||||
return nil
|
||||
req := httplib.Post(t.Url).SetTimeout(timeout, timeout).
|
||||
Header("X-Gogs-Delivery", t.Uuid).
|
||||
Header("X-Gogs-Event", string(t.EventType))
|
||||
|
||||
switch t.ContentType {
|
||||
case JSON:
|
||||
req = req.Header("Content-Type", "application/json").Body(t.PayloadContent)
|
||||
case FORM:
|
||||
req.Param("payload", t.PayloadContent)
|
||||
}
|
||||
|
||||
t.IsDeliveried = true
|
||||
|
||||
// TODO: record response.
|
||||
if _, err := req.Response(); err != nil {
|
||||
log.Error(4, "Delivery: %v", err)
|
||||
} else {
|
||||
t.IsSucceed = true
|
||||
}
|
||||
|
||||
if err := UpdateHookTask(t); err != nil {
|
||||
log.Error(4, "webhook.DeliverHooks(UpdateHookTask): %v", err)
|
||||
log.Error(4, "UpdateHookTask: %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Trace("Hook delivered: %s", t.PayloadContent)
|
||||
log.Trace("Hook delivered(%s): %s", t.Uuid, t.PayloadContent)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue