summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
Diffstat (limited to 'models')
-rw-r--r--models/webhook.go28
-rw-r--r--models/webhook_slack.go22
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
}