Skip to content

Lint models/update.go, release.go & webhook_slack.go #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions models/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ type Release struct {
CreatedUnix int64
}

// BeforeInsert is invoked from XORM before inserting an object of this type.
func (r *Release) BeforeInsert() {
if r.CreatedUnix == 0 {
r.CreatedUnix = time.Now().Unix()
}
}

// AfterSet is invoked from XORM after setting the value of a field of this object.
func (r *Release) AfterSet(colName string, _ xorm.Cell) {
switch colName {
case "created_unix":
Expand Down Expand Up @@ -151,29 +153,29 @@ func GetReleasesByRepoID(repoID int64, page, pageSize int) (rels []*Release, err
return rels, err
}

type ReleaseSorter struct {
type releaseSorter struct {
rels []*Release
}

func (rs *ReleaseSorter) Len() int {
func (rs *releaseSorter) Len() int {
return len(rs.rels)
}

func (rs *ReleaseSorter) Less(i, j int) bool {
func (rs *releaseSorter) Less(i, j int) bool {
diffNum := rs.rels[i].NumCommits - rs.rels[j].NumCommits
if diffNum != 0 {
return diffNum > 0
}
return rs.rels[i].Created.After(rs.rels[j].Created)
}

func (rs *ReleaseSorter) Swap(i, j int) {
func (rs *releaseSorter) Swap(i, j int) {
rs.rels[i], rs.rels[j] = rs.rels[j], rs.rels[i]
}

// SortReleases sorts releases by number of commits and created time.
func SortReleases(rels []*Release) {
sorter := &ReleaseSorter{rels: rels}
sorter := &releaseSorter{rels: rels}
sort.Sort(sorter)
}

Expand Down
5 changes: 5 additions & 0 deletions models/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/modules/log"
)

// UpdateTask defines an UpdateTask
type UpdateTask struct {
ID int64 `xorm:"pk autoincr"`
UUID string `xorm:"index"`
Expand All @@ -23,6 +24,7 @@ type UpdateTask struct {
NewCommitID string
}

// AddUpdateTask adds an UpdateTask
func AddUpdateTask(task *UpdateTask) error {
_, err := x.Insert(task)
return err
Expand All @@ -42,6 +44,7 @@ func GetUpdateTaskByUUID(uuid string) (*UpdateTask, error) {
return task, nil
}

// DeleteUpdateTaskByUUID deletes an UpdateTask from the database
func DeleteUpdateTaskByUUID(uuid string) error {
_, err := x.Delete(&UpdateTask{UUID: uuid})
return err
Expand All @@ -60,6 +63,7 @@ func CommitToPushCommit(commit *git.Commit) *PushCommit {
}
}

// ListToPushCommits transforms a list.List to PushCommits type.
func ListToPushCommits(l *list.List) *PushCommits {
commits := make([]*PushCommit, 0)
var actEmail string
Expand All @@ -73,6 +77,7 @@ func ListToPushCommits(l *list.List) *PushCommits {
return &PushCommits{l.Len(), commits, "", nil}
}

// PushUpdateOptions defines the push update options
type PushUpdateOptions struct {
PusherID int64
PusherName string
Expand Down
9 changes: 9 additions & 0 deletions models/webhook_slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import (
"code.gitea.io/gitea/modules/setting"
)

// SlackMeta contains the slack metdata
type SlackMeta struct {
Channel string `json:"channel"`
Username string `json:"username"`
IconURL string `json:"icon_url"`
Color string `json:"color"`
}

// SlackPayload contains the information about the slack channel
type SlackPayload struct {
Channel string `json:"channel"`
Text string `json:"text"`
Expand All @@ -33,15 +35,18 @@ type SlackPayload struct {
Attachments []SlackAttachment `json:"attachments"`
}

// SlackAttachment contains the slack message
type SlackAttachment struct {
Fallback string `json:"fallback"`
Color string `json:"color"`
Title string `json:"title"`
Text string `json:"text"`
}

// SetSecret sets the slack secret
func (p *SlackPayload) SetSecret(_ string) {}

// JSONPayload Marshals the SlackPayload to json
func (p *SlackPayload) JSONPayload() ([]byte, error) {
data, err := json.MarshalIndent(p, "", " ")
if err != nil {
Expand All @@ -50,6 +55,7 @@ func (p *SlackPayload) JSONPayload() ([]byte, error) {
return data, nil
}

// SlackTextFormatter replaces &, <, > with HTML characters
// see: https://api.slack.com/docs/formatting
func SlackTextFormatter(s string) string {
// replace & < >
Expand All @@ -59,6 +65,7 @@ func SlackTextFormatter(s string) string {
return s
}

// SlackShortTextFormatter replaces &, <, > with HTML characters
func SlackShortTextFormatter(s string) string {
s = strings.Split(s, "\n")[0]
// replace & < >
Expand All @@ -68,6 +75,7 @@ func SlackShortTextFormatter(s string) string {
return s
}

// SlackLinkFormatter creates a link compatablie with slack
func SlackLinkFormatter(url string, text string) string {
return fmt.Sprintf("<%s|%s>", url, SlackTextFormatter(text))
}
Expand Down Expand Up @@ -181,6 +189,7 @@ func getSlackPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (*S
}, nil
}

// GetSlackPayload converts a slack webhook into a SlackPayload
func GetSlackPayload(p api.Payloader, event HookEventType, meta string) (*SlackPayload, error) {
s := new(SlackPayload)

Expand Down