diff options
author | Unknown <joe2010xtmf@163.com> | 2014-05-06 11:50:31 -0400 |
---|---|---|
committer | Unknown <joe2010xtmf@163.com> | 2014-05-06 11:50:31 -0400 |
commit | e573855a4f040abd4aa6a2afa9ce610a1ec2670f (patch) | |
tree | 7ead7dafd3c228305e83a19dec0bca5b60d92217 /modules/hooks | |
parent | 94bccbb148a4ed897eb8332fc746aa75486c5c4d (diff) | |
download | gitea-e573855a4f040abd4aa6a2afa9ce610a1ec2670f.tar.gz gitea-e573855a4f040abd4aa6a2afa9ce610a1ec2670f.zip |
Fix #98, support web hook
Diffstat (limited to 'modules/hooks')
-rw-r--r-- | modules/hooks/hooks.go | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/modules/hooks/hooks.go b/modules/hooks/hooks.go new file mode 100644 index 0000000000..2b53dbfbbd --- /dev/null +++ b/modules/hooks/hooks.go @@ -0,0 +1,83 @@ +// 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 hooks + +import ( + "encoding/json" + "time" + + "github.com/gogits/gogs/modules/httplib" + "github.com/gogits/gogs/modules/log" +) + +// Hook task types. +const ( + HTT_WEBHOOK = iota + 1 + HTT_SERVICE +) + +type PayloadAuthor struct { + Name string `json:"name"` + Email string `json:"email"` +} + +type PayloadCommit struct { + Id string `json:"id"` + Message string `json:"message"` + Url string `json:"url"` + Author *PayloadAuthor `json:"author"` +} + +// Payload represents payload information of hook. +type Payload struct { + Secret string `json:"secret"` + Ref string `json:"ref"` + Commits []*PayloadCommit `json:"commits"` + Pusher *PayloadAuthor `json:"pusher"` +} + +// HookTask represents hook task. +type HookTask struct { + Type int + Url string + *Payload + ContentType int + IsSsl bool +} + +var ( + taskQueue = make(chan *HookTask, 1000) +) + +// AddHookTask adds new hook task to task queue. +func AddHookTask(t *HookTask) { + taskQueue <- t +} + +func init() { + go handleQueue() +} + +func handleQueue() { + for { + select { + case t := <-taskQueue: + // Only support JSON now. + data, err := json.MarshalIndent(t.Payload, "", "\t") + if err != nil { + log.Error("hooks.handleQueue(json): %v", err) + continue + } + + _, err = httplib.Post(t.Url).SetTimeout(5*time.Second, 5*time.Second). + Body(data).Response() + if err != nil { + log.Error("hooks.handleQueue: Fail to deliver hook: %v", err) + continue + } + log.Info("Hook delivered") + } + } +} |