diff options
author | Unknwon <u@gogs.io> | 2015-08-29 11:49:59 +0800 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2015-08-29 11:49:59 +0800 |
commit | 55ddf225bb8aba269a407b1d13ab00a497df4f9b (patch) | |
tree | f8609999ba624a7a9cbb4b9fd00ca2bddde38959 /models | |
parent | 2f08e9f04880aedd82be24e191198eff700ddc08 (diff) | |
download | gitea-55ddf225bb8aba269a407b1d13ab00a497df4f9b.tar.gz gitea-55ddf225bb8aba269a407b1d13ab00a497df4f9b.zip |
webhook APIs
Diffstat (limited to 'models')
-rw-r--r-- | models/webhook.go | 28 | ||||
-rw-r--r-- | models/webhook_slack.go | 22 |
2 files changed, 31 insertions, 19 deletions
diff --git a/models/webhook.go b/models/webhook.go index b2fecb8d3c..75380d17c1 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -97,11 +97,16 @@ type Webhook struct { Updated time.Time `xorm:"UPDATED"` } -// GetEvent handles conversion from Events to HookEvent. -func (w *Webhook) GetEvent() { - w.HookEvent = &HookEvent{} - if err := json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil { - log.Error(4, "webhook.GetEvent(%d): %v", w.ID, err) +func (w *Webhook) AfterSet(colName string, _ xorm.Cell) { + var err error + switch colName { + case "events": + w.HookEvent = &HookEvent{} + if err = json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil { + log.Error(3, "Unmarshal[%d]: %v", w.ID, err) + } + case "created": + w.Created = regulateTimeZone(w.Created) } } @@ -137,6 +142,17 @@ func (w *Webhook) HasPushEvent() bool { (w.ChooseEvents && w.HookEvents.Push) } +func (w *Webhook) EventsArray() []string { + events := make([]string, 0, 2) + if w.HasCreateEvent() { + events = append(events, "create") + } + if w.HasPushEvent() { + events = append(events, "push") + } + return events +} + // CreateWebhook creates a new web hook. func CreateWebhook(w *Webhook) error { _, err := x.Insert(w) @@ -382,8 +398,6 @@ func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) err } for _, w := range ws { - w.GetEvent() - switch event { case HOOK_EVENT_CREATE: if !w.HasCreateEvent() { diff --git a/models/webhook_slack.go b/models/webhook_slack.go index b9a39b64e0..45766a51ba 100644 --- a/models/webhook_slack.go +++ b/models/webhook_slack.go @@ -13,22 +13,20 @@ import ( api "github.com/gogits/go-gogs-client" "github.com/gogits/gogs/modules/git" - "github.com/gogits/gogs/modules/setting" -) - -const ( - SLACK_COLOR string = "#dd4b39" ) type SlackMeta struct { - Channel string `json:"channel"` + Channel string `json:"channel"` + Username string `json:"username"` + IconURL string `json:"icon_url"` + Color string `json:"color"` } type SlackPayload struct { Channel string `json:"channel"` Text string `json:"text"` Username string `json:"username"` - IconUrl string `json:"icon_url"` + IconURL string `json:"icon_url"` UnfurlLinks int `json:"unfurl_links"` LinkNames int `json:"link_names"` Attachments []SlackAttachment `json:"attachments"` @@ -75,8 +73,8 @@ func getSlackCreatePayload(p *api.CreatePayload, slack *SlackMeta) (*SlackPayloa return &SlackPayload{ Channel: slack.Channel, Text: text, - Username: setting.AppName, - IconUrl: setting.AppUrl + "/img/favicon.png", + Username: slack.Username, + IconURL: slack.IconURL, }, nil } @@ -113,13 +111,13 @@ func getSlackPushPayload(p *api.PushPayload, slack *SlackMeta) (*SlackPayload, e } } - slackAttachments := []SlackAttachment{{Color: SLACK_COLOR, Text: attachmentText}} + slackAttachments := []SlackAttachment{{Color: slack.Color, Text: attachmentText}} return &SlackPayload{ Channel: slack.Channel, Text: text, - Username: setting.AppName, - IconUrl: setting.AppUrl + "/img/favicon.png", + Username: slack.Username, + IconURL: slack.IconURL, Attachments: slackAttachments, }, nil } |