Fix #98, support web hook

This commit is contained in:
Unknown
2014-05-06 11:50:31 -04:00
parent 94bccbb148
commit e573855a4f
13 changed files with 581 additions and 23 deletions

View File

@ -5,7 +5,7 @@ Gogs(Go Git Service) is a Self Hosted Git Service in the Go Programming Language
![Demo](http://gowalker.org/public/gogs_demo.gif)
##### Current version: 0.3.2 Alpha
##### Current version: 0.3.3 Alpha
### NOTICES
@ -35,7 +35,7 @@ More importantly, Gogs only needs one binary to setup your own project hosting o
- SSH/HTTP(S) protocol support.
- Register/delete/rename account.
- Create/migrate/mirror/delete/watch/rename/transfer public/private repository.
- Repository viewer/release/issue tracker.
- Repository viewer/release/issue tracker/webhooks.
- Add/remove repository collaborators.
- Gravatar and cache support.
- Mail service(register, issue).

View File

@ -5,7 +5,7 @@ Gogs(Go Git Service) 是一个由 Go 语言编写的自助 Git 托管服务。
![Demo](http://gowalker.org/public/gogs_demo.gif)
##### 当前版本0.3.2 Alpha
##### 当前版本0.3.3 Alpha
## 开发目的
@ -26,7 +26,7 @@ Gogs 完全使用 Go 语言来实现对 Git 数据的操作,实现 **零** 依
- SSH/HTTP(S) 协议支持
- 注册/删除/重命名用户
- 创建/迁移/镜像/删除/关注/重命名/转移 公开/私有 仓库
- 仓库 浏览器/发布/缺陷追踪
- 仓库 浏览器/发布/缺陷管理/Web 钩子
- 添加/删除 仓库协作者
- Gravatar 以及缓存支持
- 邮件服务注册、Issue

View File

@ -20,7 +20,7 @@ import (
// Test that go1.2 tag above is included in builds. main.go refers to this definition.
const go12tag = true
const APP_VER = "0.3.2.0505 Alpha"
const APP_VER = "0.3.3.0506 Alpha"
func init() {
base.AppVer = APP_VER

View File

@ -6,6 +6,8 @@ package models
import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"
@ -13,6 +15,7 @@ import (
qlog "github.com/qiniu/log"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/hooks"
"github.com/gogits/gogs/modules/log"
)
@ -73,45 +76,80 @@ func (a Action) GetContent() string {
// CommitRepoAction adds new action for committing repository.
func CommitRepoAction(userId, repoUserId int64, userName, actEmail string,
repoId int64, repoUserName, repoName string, refName string, commit *base.PushCommits) error {
repoId int64, repoUserName, repoName string, refFullName string, commit *base.PushCommits) error {
// log.Trace("action.CommitRepoAction(start): %d/%s", userId, repoName)
opType := OP_COMMIT_REPO
// Check it's tag push or branch.
if strings.HasPrefix(refName, "refs/tags/") {
if strings.HasPrefix(refFullName, "refs/tags/") {
opType = OP_PUSH_TAG
commit = &base.PushCommits{}
}
refName = git.RefEndName(refName)
refName := git.RefEndName(refFullName)
bs, err := json.Marshal(commit)
if err != nil {
qlog.Error("action.CommitRepoAction(json): %d/%s", repoUserId, repoName)
return err
return errors.New("action.CommitRepoAction(json): " + err.Error())
}
// Change repository bare status and update last updated time.
repo, err := GetRepositoryByName(repoUserId, repoName)
if err != nil {
qlog.Error("action.CommitRepoAction(GetRepositoryByName): %d/%s", repoUserId, repoName)
return err
return errors.New("action.CommitRepoAction(GetRepositoryByName): " + err.Error())
}
repo.IsBare = false
if err = UpdateRepository(repo); err != nil {
qlog.Error("action.CommitRepoAction(UpdateRepository): %d/%s", repoUserId, repoName)
return err
return errors.New("action.CommitRepoAction(UpdateRepository): " + err.Error())
}
if err = NotifyWatchers(&Action{ActUserId: userId, ActUserName: userName, ActEmail: actEmail,
OpType: opType, Content: string(bs), RepoId: repoId, RepoUserName: repoUserName,
RepoName: repoName, RefName: refName,
IsPrivate: repo.IsPrivate}); err != nil {
qlog.Error("action.CommitRepoAction(notify watchers): %d/%s", userId, repoName)
return err
return errors.New("action.CommitRepoAction(NotifyWatchers): " + err.Error())
}
qlog.Info("action.CommitRepoAction(end): %d/%s", repoUserId, repoName)
// New push event hook.
ws, err := GetActiveWebhooksByRepoId(repoId)
if err != nil {
return errors.New("action.CommitRepoAction(GetWebhooksByRepoId): " + err.Error())
} else if len(ws) == 0 {
return nil
}
qlog.Info("action.CommitRepoAction(end): %d/%s", repoUserId, repoName)
commits := make([]*hooks.PayloadCommit, len(commit.Commits))
for i, cmt := range commit.Commits {
commits[i] = &hooks.PayloadCommit{
Id: cmt.Sha1,
Message: cmt.Message,
Url: fmt.Sprintf("%s%s/%s/commit/%s", base.AppUrl, repoUserName, repoName, cmt.Sha1),
Author: &hooks.PayloadAuthor{
Name: cmt.AuthorName,
Email: cmt.AuthorEmail,
},
}
}
p := &hooks.Payload{
Ref: refFullName,
Commits: commits,
Pusher: &hooks.PayloadAuthor{
Name: userName,
Email: actEmail,
},
}
for _, w := range ws {
w.GetEvent()
if !w.HasPushEvent() {
continue
}
p.Secret = w.Secret
hooks.AddHookTask(&hooks.HookTask{hooks.HTT_WEBHOOK, w.Url, p, w.ContentType, w.IsSsl})
}
return nil
}

View File

@ -96,6 +96,6 @@ func Update(refName, oldCommitId, newCommitId, userName, repoUserName, repoName
//commits = append(commits, []string{lastCommit.Id().String(), lastCommit.Message()})
if err = CommitRepoAction(userId, ru.Id, userName, actEmail,
repos.Id, repoUserName, repoName, refName, &base.PushCommits{l.Len(), commits}); err != nil {
qlog.Fatalf("runUpdate.models.CommitRepoAction: %v", err)
qlog.Fatalf("runUpdate.models.CommitRepoAction: %s/%s:%v", repoUserName, repoName, err)
}
}

View File

@ -28,7 +28,7 @@ type HookEvent struct {
type Webhook struct {
Id int64
RepoId int64
Payload string `xorm:"TEXT"`
Url string `xorm:"TEXT"`
ContentType int
Secret string `xorm:"TEXT"`
Events string `xorm:"TEXT"`
@ -50,6 +50,13 @@ func (w *Webhook) SaveEvent() error {
return err
}
func (w *Webhook) HasPushEvent() bool {
if w.PushOnly {
return true
}
return false
}
// CreateWebhook creates new webhook.
func CreateWebhook(w *Webhook) error {
_, err := orm.Insert(w)
@ -74,6 +81,12 @@ func GetWebhookById(hookId int64) (*Webhook, error) {
return w, nil
}
// GetActiveWebhooksByRepoId returns all active webhooks of repository.
func GetActiveWebhooksByRepoId(repoId int64) (ws []*Webhook, err error) {
err = orm.Find(&ws, &Webhook{RepoId: repoId, IsActive: true})
return ws, err
}
// GetWebhooksByRepoId returns all webhooks of repository.
func GetWebhooksByRepoId(repoId int64) (ws []*Webhook, err error) {
err = orm.Find(&ws, &Webhook{RepoId: repoId})

83
modules/hooks/hooks.go Normal file
View File

@ -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")
}
}
}

62
modules/httplib/README.md Executable file
View File

@ -0,0 +1,62 @@
# httplib
httplib is an libs help you to curl remote url.
# How to use?
## GET
you can use Get to crawl data.
import "httplib"
str, err := httplib.Get("http://beego.me/").String()
if err != nil {
t.Fatal(err)
}
fmt.Println(str)
## POST
POST data to remote url
b:=httplib.Post("http://beego.me/")
b.Param("username","astaxie")
b.Param("password","123456")
str, err := b.String()
if err != nil {
t.Fatal(err)
}
fmt.Println(str)
## set timeout
you can set timeout in request.default is 60 seconds.
set Get timeout:
httplib.Get("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second)
set post timeout:
httplib.Post("http://beego.me/").SetTimeout(100 * time.Second, 30 * time.Second)
- first param is connectTimeout.
- second param is readWriteTimeout
## debug
if you want to debug the request info, set the debug on
httplib.Get("http://beego.me/").Debug(true)
## support HTTPS client
if request url is https. You can set the client support TSL:
httplib.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
more info about the tls.Config please visit http://golang.org/pkg/crypto/tls/#Config
## set cookie
some http request need setcookie. So set it like this:
cookie := &http.Cookie{}
cookie.Name = "username"
cookie.Value = "astaxie"
httplib.Get("http://beego.me/").SetCookie(cookie)

330
modules/httplib/httplib.go Executable file

File diff suppressed because it is too large Load Diff

32
modules/httplib/httplib_test.go Executable file
View File

@ -0,0 +1,32 @@
package httplib
import (
"io/ioutil"
"testing"
)
func TestGetUrl(t *testing.T) {
resp, err := Get("http://beego.me/").Debug(true).Response()
if err != nil {
t.Fatal(err)
}
if resp.Body == nil {
t.Fatal("body is nil")
}
data, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
t.Fatal(err)
}
if len(data) == 0 {
t.Fatal("data is no")
}
str, err := Get("http://beego.me/").String()
if err != nil {
t.Fatal(err)
}
if len(str) == 0 {
t.Fatal("has no info")
}
}

View File

@ -247,7 +247,7 @@ func WebHooksAddPost(ctx *middleware.Context, form auth.NewWebhookForm) {
w := &models.Webhook{
RepoId: ctx.Repo.Repository.Id,
Payload: form.Url,
Url: form.Url,
ContentType: ct,
Secret: form.Secret,
HookEvent: &models.HookEvent{
@ -315,7 +315,7 @@ func WebHooksEditPost(ctx *middleware.Context, params martini.Params, form auth.
w := &models.Webhook{
Id: hookId,
RepoId: ctx.Repo.Repository.Id,
Payload: form.Url,
Url: form.Url,
ContentType: ct,
Secret: form.Secret,
HookEvent: &models.HookEvent{

View File

@ -16,7 +16,7 @@
{{range .Webhooks}}
<li>
{{if .IsActive}}<span class="pull-left status text-success"><i class="fa fa-check"></i></span>{{else}}<span class="pull-left status"><i class="fa fa-times"></i></span>{{end}}
<a class="link" href="{{$.RepoLink}}/settings/hooks/{{.Id}}">{{.Payload}}</a>
<a class="link" href="{{$.RepoLink}}/settings/hooks/{{.Id}}">{{.Url}}</a>
<a href="{{$.RepoLink}}/settings/hooks?remove={{.Id}}" class="remove-hook pull-right"><i class="fa fa-times"></i></a>
<a href="{{$.RepoLink}}/settings/hooks/{{.Id}}" class="edit-hook pull-right"><i class="fa fa-pencil"></i></a>
</li>

View File

@ -19,7 +19,7 @@
<hr/>
<div class="form-group">
<label for="payload-url">Payload URL</label>
<input id="payload-url" name="url" class="form-control" type="url" required="required" value="{{.Webhook.Payload}}" />
<input id="payload-url" name="url" class="form-control" type="url" required="required" value="{{.Webhook.Url}}" />
</div>
<div class="form-group">