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 /models | |
parent | 94bccbb148a4ed897eb8332fc746aa75486c5c4d (diff) | |
download | gitea-e573855a4f040abd4aa6a2afa9ce610a1ec2670f.tar.gz gitea-e573855a4f040abd4aa6a2afa9ce610a1ec2670f.zip |
Fix #98, support web hook
Diffstat (limited to 'models')
-rw-r--r-- | models/action.go | 62 | ||||
-rw-r--r-- | models/update.go | 2 | ||||
-rw-r--r-- | models/webhook.go | 15 |
3 files changed, 65 insertions, 14 deletions
diff --git a/models/action.go b/models/action.go index 8d8713b8a6..231a7a5485 100644 --- a/models/action.go +++ b/models/action.go @@ -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 + } + + 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 } diff --git a/models/update.go b/models/update.go index 3ae5510a84..b7242cde85 100644 --- a/models/update.go +++ b/models/update.go @@ -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) } } diff --git a/models/webhook.go b/models/webhook.go index daa4510f2d..f10fa2131e 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -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}) |