Lint models/update.go & webhook_slack.go
This commit is contained in:
parent
ece19f4a5e
commit
33a2ac3830
|
@ -15,6 +15,7 @@ import (
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// UpdateTask defines an UpdateTask
|
||||||
type UpdateTask struct {
|
type UpdateTask struct {
|
||||||
ID int64 `xorm:"pk autoincr"`
|
ID int64 `xorm:"pk autoincr"`
|
||||||
UUID string `xorm:"index"`
|
UUID string `xorm:"index"`
|
||||||
|
@ -23,6 +24,7 @@ type UpdateTask struct {
|
||||||
NewCommitID string
|
NewCommitID string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddUpdateTask adds an UpdateTask
|
||||||
func AddUpdateTask(task *UpdateTask) error {
|
func AddUpdateTask(task *UpdateTask) error {
|
||||||
_, err := x.Insert(task)
|
_, err := x.Insert(task)
|
||||||
return err
|
return err
|
||||||
|
@ -42,6 +44,7 @@ func GetUpdateTaskByUUID(uuid string) (*UpdateTask, error) {
|
||||||
return task, nil
|
return task, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteUpdateTaskByUUID deletes an UpdateTask from the database
|
||||||
func DeleteUpdateTaskByUUID(uuid string) error {
|
func DeleteUpdateTaskByUUID(uuid string) error {
|
||||||
_, err := x.Delete(&UpdateTask{UUID: uuid})
|
_, err := x.Delete(&UpdateTask{UUID: uuid})
|
||||||
return err
|
return err
|
||||||
|
@ -60,6 +63,7 @@ func CommitToPushCommit(commit *git.Commit) *PushCommit {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListToPushCommits transforms a list.List to PushCommits type.
|
||||||
func ListToPushCommits(l *list.List) *PushCommits {
|
func ListToPushCommits(l *list.List) *PushCommits {
|
||||||
commits := make([]*PushCommit, 0)
|
commits := make([]*PushCommit, 0)
|
||||||
var actEmail string
|
var actEmail string
|
||||||
|
@ -73,6 +77,7 @@ func ListToPushCommits(l *list.List) *PushCommits {
|
||||||
return &PushCommits{l.Len(), commits, "", nil}
|
return &PushCommits{l.Len(), commits, "", nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PushUpdateOptions defines the push update options
|
||||||
type PushUpdateOptions struct {
|
type PushUpdateOptions struct {
|
||||||
PusherID int64
|
PusherID int64
|
||||||
PusherName string
|
PusherName string
|
||||||
|
|
|
@ -16,6 +16,7 @@ import (
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// SlackMeta contains the slack metdata
|
||||||
type SlackMeta struct {
|
type SlackMeta struct {
|
||||||
Channel string `json:"channel"`
|
Channel string `json:"channel"`
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
|
@ -23,6 +24,7 @@ type SlackMeta struct {
|
||||||
Color string `json:"color"`
|
Color string `json:"color"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SlackPayload contains the information about the slack channel
|
||||||
type SlackPayload struct {
|
type SlackPayload struct {
|
||||||
Channel string `json:"channel"`
|
Channel string `json:"channel"`
|
||||||
Text string `json:"text"`
|
Text string `json:"text"`
|
||||||
|
@ -33,6 +35,7 @@ type SlackPayload struct {
|
||||||
Attachments []SlackAttachment `json:"attachments"`
|
Attachments []SlackAttachment `json:"attachments"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SlackAttachment contains the slack message
|
||||||
type SlackAttachment struct {
|
type SlackAttachment struct {
|
||||||
Fallback string `json:"fallback"`
|
Fallback string `json:"fallback"`
|
||||||
Color string `json:"color"`
|
Color string `json:"color"`
|
||||||
|
@ -40,8 +43,10 @@ type SlackAttachment struct {
|
||||||
Text string `json:"text"`
|
Text string `json:"text"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetSecret sets the slack secret
|
||||||
func (p *SlackPayload) SetSecret(_ string) {}
|
func (p *SlackPayload) SetSecret(_ string) {}
|
||||||
|
|
||||||
|
// JSONPayload Marshals the SlackPayload to json
|
||||||
func (p *SlackPayload) JSONPayload() ([]byte, error) {
|
func (p *SlackPayload) JSONPayload() ([]byte, error) {
|
||||||
data, err := json.MarshalIndent(p, "", " ")
|
data, err := json.MarshalIndent(p, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -50,6 +55,7 @@ func (p *SlackPayload) JSONPayload() ([]byte, error) {
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SlackTextFormatter replaces &, <, > with HTML characters
|
||||||
// see: https://api.slack.com/docs/formatting
|
// see: https://api.slack.com/docs/formatting
|
||||||
func SlackTextFormatter(s string) string {
|
func SlackTextFormatter(s string) string {
|
||||||
// replace & < >
|
// replace & < >
|
||||||
|
@ -59,6 +65,7 @@ func SlackTextFormatter(s string) string {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SlackShortTextFormatter replaces &, <, > with HTML characters
|
||||||
func SlackShortTextFormatter(s string) string {
|
func SlackShortTextFormatter(s string) string {
|
||||||
s = strings.Split(s, "\n")[0]
|
s = strings.Split(s, "\n")[0]
|
||||||
// replace & < >
|
// replace & < >
|
||||||
|
@ -68,6 +75,7 @@ func SlackShortTextFormatter(s string) string {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SlackLinkFormatter creates a link compatablie with slack
|
||||||
func SlackLinkFormatter(url string, text string) string {
|
func SlackLinkFormatter(url string, text string) string {
|
||||||
return fmt.Sprintf("<%s|%s>", url, SlackTextFormatter(text))
|
return fmt.Sprintf("<%s|%s>", url, SlackTextFormatter(text))
|
||||||
}
|
}
|
||||||
|
@ -181,6 +189,7 @@ func getSlackPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (*S
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetSlackPayload converts a slack webhook into a SlackPayload
|
||||||
func GetSlackPayload(p api.Payloader, event HookEventType, meta string) (*SlackPayload, error) {
|
func GetSlackPayload(p api.Payloader, event HookEventType, meta string) (*SlackPayload, error) {
|
||||||
s := new(SlackPayload)
|
s := new(SlackPayload)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue