Skip to content

Commit

Permalink
Fix #98, support web hook
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon committed May 6, 2014
1 parent 94bccbb commit e573855
Show file tree
Hide file tree
Showing 13 changed files with 581 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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).
Expand Down
4 changes: 2 additions & 2 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

## 开发目的

Expand All @@ -26,7 +26,7 @@ Gogs 完全使用 Go 语言来实现对 Git 数据的操作,实现 **零** 依
- SSH/HTTP(S) 协议支持
- 注册/删除/重命名用户
- 创建/迁移/镜像/删除/关注/重命名/转移 公开/私有 仓库
- 仓库 浏览器/发布/缺陷追踪
- 仓库 浏览器/发布/缺陷管理/Web 钩子
- 添加/删除 仓库协作者
- Gravatar 以及缓存支持
- 邮件服务(注册、Issue)
Expand Down
2 changes: 1 addition & 1 deletion gogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 50 additions & 12 deletions models/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ package models

import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"

"github.com/gogits/git"
qlog "github.com/qiniu/log"

"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/hooks"
"github.com/gogits/gogs/modules/log"
)

Expand Down Expand Up @@ -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
}

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
}

Expand Down
2 changes: 1 addition & 1 deletion models/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
15 changes: 14 additions & 1 deletion models/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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)
Expand All @@ -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})
Expand Down
83 changes: 83 additions & 0 deletions modules/hooks/hooks.go
Original file line number Diff line number Diff line change
@@ -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 changes: 62 additions & 0 deletions modules/httplib/README.md
Original file line number Diff line number Diff line change
@@ -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)

Loading

0 comments on commit e573855

Please sign in to comment.