diff options
Diffstat (limited to 'models')
-rw-r--r-- | models/action.go | 6 | ||||
-rw-r--r-- | models/user.go | 14 | ||||
-rw-r--r-- | models/webhook.go | 58 |
3 files changed, 55 insertions, 23 deletions
diff --git a/models/action.go b/models/action.go index 2beb42e576..8405084bbe 100644 --- a/models/action.go +++ b/models/action.go @@ -445,13 +445,13 @@ func CommitRepoAction(userID, repoUserID int64, userName, actEmail string, if err = CreateHookTask(&HookTask{ RepoID: repo.ID, - HookID: w.Id, + HookID: w.ID, Type: w.HookTaskType, - Url: w.Url, + Url: w.URL, BasePayload: payload, ContentType: w.ContentType, EventType: HOOK_EVENT_PUSH, - IsSsl: w.IsSsl, + IsSsl: w.IsSSL, }); err != nil { return fmt.Errorf("CreateHookTask: %v", err) } diff --git a/models/user.go b/models/user.go index f2fc480e1b..3d54b79e76 100644 --- a/models/user.go +++ b/models/user.go @@ -111,8 +111,11 @@ func (u *User) DashboardLink() string { return setting.AppSubUrl + "/" } -// HomeLink returns the user home page link. +// HomeLink returns the user or organization home page link. func (u *User) HomeLink() string { + if u.IsOrganization() { + return setting.AppSubUrl + "/org/" + u.Name + } return setting.AppSubUrl + "/" + u.Name } @@ -162,6 +165,15 @@ func (u *User) AvatarLink() string { return setting.GravatarSource + u.Avatar } +// DisplayName returns full name if it's not empty, +// returns username otherwise. +func (u *User) DisplayName() string { + if len(u.FullName) > 0 { + return u.FullName + } + return u.Name +} + // NewGitSig generates and returns the signature of given user. func (u *User) NewGitSig() *git.Signature { return &git.Signature{ diff --git a/models/webhook.go b/models/webhook.go index 18cda74871..6211b215f2 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -60,36 +60,45 @@ type HookEvent struct { PushOnly bool `json:"push_only"` } +type HookStatus int + +const ( + HOOK_STATUS_NONE = iota + HOOK_STATUS_SUCCEED + HOOK_STATUS_FAILED +) + // Webhook represents a web hook object. type Webhook struct { - Id int64 - RepoId int64 - Url string `xorm:"TEXT"` + ID int64 `xorm:"pk autoincr"` + RepoID int64 + OrgID int64 + URL string `xorm:"url TEXT"` ContentType HookContentType Secret string `xorm:"TEXT"` Events string `xorm:"TEXT"` *HookEvent `xorm:"-"` - IsSsl bool + IsSSL bool `xorm:"is_ssl"` IsActive bool HookTaskType HookTaskType - Meta string `xorm:"TEXT"` // store hook-specific attributes - OrgId int64 - Created time.Time `xorm:"CREATED"` - Updated time.Time `xorm:"UPDATED"` + Meta string `xorm:"TEXT"` // store hook-specific attributes + LastStatus HookStatus // Last delivery status + Created time.Time `xorm:"CREATED"` + 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) + log.Error(4, "webhook.GetEvent(%d): %v", w.ID, err) } } func (w *Webhook) GetSlackHook() *Slack { s := &Slack{} if err := json.Unmarshal([]byte(w.Meta), s); err != nil { - log.Error(4, "webhook.GetSlackHook(%d): %v", w.Id, err) + log.Error(4, "webhook.GetSlackHook(%d): %v", w.ID, err) } return s } @@ -117,7 +126,7 @@ func CreateWebhook(w *Webhook) error { // GetWebhookById returns webhook by given ID. func GetWebhookById(hookId int64) (*Webhook, error) { - w := &Webhook{Id: hookId} + w := &Webhook{ID: hookId} has, err := x.Get(w) if err != nil { return nil, err @@ -134,26 +143,37 @@ func GetActiveWebhooksByRepoId(repoId int64) (ws []*Webhook, err error) { } // GetWebhooksByRepoId returns all webhooks of repository. -func GetWebhooksByRepoId(repoId int64) (ws []*Webhook, err error) { - err = x.Find(&ws, &Webhook{RepoId: repoId}) +func GetWebhooksByRepoId(repoID int64) (ws []*Webhook, err error) { + err = x.Find(&ws, &Webhook{RepoID: repoID}) return ws, err } // UpdateWebhook updates information of webhook. func UpdateWebhook(w *Webhook) error { - _, err := x.Id(w.Id).AllCols().Update(w) + _, err := x.Id(w.ID).AllCols().Update(w) return err } // DeleteWebhook deletes webhook of repository. -func DeleteWebhook(hookId int64) error { - _, err := x.Delete(&Webhook{Id: hookId}) - return err +func DeleteWebhook(id int64) (err error) { + sess := x.NewSession() + defer sessionRelease(sess) + if err = sess.Begin(); err != nil { + return err + } + + if _, err = sess.Delete(&Webhook{ID: id}); err != nil { + return err + } else if _, err = sess.Delete(&HookTask{HookID: id}); err != nil { + return err + } + + return sess.Commit() } // GetWebhooksByOrgId returns all webhooks for an organization. -func GetWebhooksByOrgId(orgId int64) (ws []*Webhook, err error) { - err = x.Find(&ws, &Webhook{OrgId: orgId}) +func GetWebhooksByOrgId(orgID int64) (ws []*Webhook, err error) { + err = x.Find(&ws, &Webhook{OrgID: orgID}) return ws, err } |