mirror of https://github.com/gogs/gogs.git
golangci-lint: add `unparam` linter (#6839)
parent
775901058d
commit
aeb5e34490
|
@ -19,3 +19,4 @@ linters:
|
|||
- rowserrcheck
|
||||
- unconvert
|
||||
- goimports
|
||||
- unparam
|
||||
|
|
|
@ -54,21 +54,21 @@ func test_repos_create(t *testing.T, db *repos) {
|
|||
})
|
||||
|
||||
t.Run("already exists", func(t *testing.T) {
|
||||
_, err := db.create(1, createRepoOpts{
|
||||
_, err := db.create(2, createRepoOpts{
|
||||
Name: "repo1",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = db.create(1, createRepoOpts{
|
||||
_, err = db.create(2, createRepoOpts{
|
||||
Name: "repo1",
|
||||
})
|
||||
expErr := ErrRepoAlreadyExist{args: errutil.Args{"ownerID": int64(1), "name": "repo1"}}
|
||||
expErr := ErrRepoAlreadyExist{args: errutil.Args{"ownerID": int64(2), "name": "repo1"}}
|
||||
assert.Equal(t, expErr, err)
|
||||
})
|
||||
|
||||
repo, err := db.create(1, createRepoOpts{
|
||||
repo, err := db.create(3, createRepoOpts{
|
||||
Name: "repo2",
|
||||
})
|
||||
if err != nil {
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"strings"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/gogs/git-module"
|
||||
api "github.com/gogs/go-gogs-client"
|
||||
|
@ -61,67 +62,70 @@ func NewDingtalkActionCard(singleTitle, singleURL string) DingtalkActionCard {
|
|||
func GetDingtalkPayload(p api.Payloader, event HookEventType) (payload *DingtalkPayload, err error) {
|
||||
switch event {
|
||||
case HOOK_EVENT_CREATE:
|
||||
payload, err = getDingtalkCreatePayload(p.(*api.CreatePayload))
|
||||
payload = getDingtalkCreatePayload(p.(*api.CreatePayload))
|
||||
case HOOK_EVENT_DELETE:
|
||||
payload, err = getDingtalkDeletePayload(p.(*api.DeletePayload))
|
||||
payload = getDingtalkDeletePayload(p.(*api.DeletePayload))
|
||||
case HOOK_EVENT_FORK:
|
||||
payload, err = getDingtalkForkPayload(p.(*api.ForkPayload))
|
||||
payload = getDingtalkForkPayload(p.(*api.ForkPayload))
|
||||
case HOOK_EVENT_PUSH:
|
||||
payload, err = getDingtalkPushPayload(p.(*api.PushPayload))
|
||||
payload = getDingtalkPushPayload(p.(*api.PushPayload))
|
||||
case HOOK_EVENT_ISSUES:
|
||||
payload, err = getDingtalkIssuesPayload(p.(*api.IssuesPayload))
|
||||
payload = getDingtalkIssuesPayload(p.(*api.IssuesPayload))
|
||||
case HOOK_EVENT_ISSUE_COMMENT:
|
||||
payload, err = getDingtalkIssueCommentPayload(p.(*api.IssueCommentPayload))
|
||||
payload = getDingtalkIssueCommentPayload(p.(*api.IssueCommentPayload))
|
||||
case HOOK_EVENT_PULL_REQUEST:
|
||||
payload, err = getDingtalkPullRequestPayload(p.(*api.PullRequestPayload))
|
||||
payload = getDingtalkPullRequestPayload(p.(*api.PullRequestPayload))
|
||||
case HOOK_EVENT_RELEASE:
|
||||
payload, err = getDingtalkReleasePayload(p.(*api.ReleasePayload))
|
||||
payload = getDingtalkReleasePayload(p.(*api.ReleasePayload))
|
||||
default:
|
||||
return nil, errors.Errorf("unexpected event %q", event)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("event '%s': %v", event, err)
|
||||
}
|
||||
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
func getDingtalkCreatePayload(p *api.CreatePayload) (*DingtalkPayload, error) {
|
||||
func getDingtalkCreatePayload(p *api.CreatePayload) *DingtalkPayload {
|
||||
refName := git.RefShortName(p.Ref)
|
||||
refType := strings.Title(p.RefType)
|
||||
|
||||
actionCard := NewDingtalkActionCard("View "+refType, p.Repo.HTMLURL+"/src/"+refName)
|
||||
|
||||
actionCard.Text += "# New " + refType + " Create Event"
|
||||
actionCard.Text += "\n- Repo: **" + MarkdownLinkFormatter(p.Repo.HTMLURL, p.Repo.Name) + "**"
|
||||
actionCard.Text += "\n- New " + refType + ": **" + MarkdownLinkFormatter(p.Repo.HTMLURL+"/src/"+refName, refName) + "**"
|
||||
|
||||
return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
|
||||
return &DingtalkPayload{
|
||||
MsgType: "actionCard",
|
||||
ActionCard: actionCard,
|
||||
}
|
||||
}
|
||||
|
||||
func getDingtalkDeletePayload(p *api.DeletePayload) (*DingtalkPayload, error) {
|
||||
func getDingtalkDeletePayload(p *api.DeletePayload) *DingtalkPayload {
|
||||
refName := git.RefShortName(p.Ref)
|
||||
refType := strings.Title(p.RefType)
|
||||
|
||||
actionCard := NewDingtalkActionCard("View Repo", p.Repo.HTMLURL)
|
||||
|
||||
actionCard.Text += "# " + refType + " Delete Event"
|
||||
actionCard.Text += "\n- Repo: **" + MarkdownLinkFormatter(p.Repo.HTMLURL, p.Repo.Name) + "**"
|
||||
actionCard.Text += "\n- " + refType + ": **" + refName + "**"
|
||||
|
||||
return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
|
||||
return &DingtalkPayload{
|
||||
MsgType: "actionCard",
|
||||
ActionCard: actionCard,
|
||||
}
|
||||
}
|
||||
|
||||
func getDingtalkForkPayload(p *api.ForkPayload) (*DingtalkPayload, error) {
|
||||
actionCard := NewDingtalkActionCard("View Forkee", p.Forkee.HTMLURL)
|
||||
|
||||
func getDingtalkForkPayload(p *api.ForkPayload) *DingtalkPayload {
|
||||
actionCard := NewDingtalkActionCard("View Fork", p.Forkee.HTMLURL)
|
||||
actionCard.Text += "# Repo Fork Event"
|
||||
actionCard.Text += "\n- From Repo: **" + MarkdownLinkFormatter(p.Repo.HTMLURL, p.Repo.Name) + "**"
|
||||
actionCard.Text += "\n- To Repo: **" + MarkdownLinkFormatter(p.Forkee.HTMLURL, p.Forkee.FullName) + "**"
|
||||
|
||||
return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
|
||||
return &DingtalkPayload{
|
||||
MsgType: "actionCard",
|
||||
ActionCard: actionCard,
|
||||
}
|
||||
}
|
||||
|
||||
func getDingtalkPushPayload(p *api.PushPayload) (*DingtalkPayload, error) {
|
||||
func getDingtalkPushPayload(p *api.PushPayload) *DingtalkPayload {
|
||||
refName := git.RefShortName(p.Ref)
|
||||
|
||||
pusher := p.Pusher.FullName
|
||||
|
@ -137,7 +141,6 @@ func getDingtalkPushPayload(p *api.PushPayload) (*DingtalkPayload, error) {
|
|||
}
|
||||
|
||||
actionCard := NewDingtalkActionCard("View Changes", p.CompareURL)
|
||||
|
||||
actionCard.Text += "# Repo Push Event"
|
||||
actionCard.Text += "\n- Repo: **" + MarkdownLinkFormatter(p.Repo.HTMLURL, p.Repo.Name) + "**"
|
||||
actionCard.Text += "\n- Ref: **" + MarkdownLinkFormatter(p.Repo.HTMLURL+"/src/"+refName, refName) + "**"
|
||||
|
@ -145,15 +148,17 @@ func getDingtalkPushPayload(p *api.PushPayload) (*DingtalkPayload, error) {
|
|||
actionCard.Text += "\n## " + fmt.Sprintf("Total %d commits(s)", len(p.Commits))
|
||||
actionCard.Text += "\n" + detail
|
||||
|
||||
return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
|
||||
return &DingtalkPayload{
|
||||
MsgType: "actionCard",
|
||||
ActionCard: actionCard,
|
||||
}
|
||||
}
|
||||
|
||||
func getDingtalkIssuesPayload(p *api.IssuesPayload) (*DingtalkPayload, error) {
|
||||
func getDingtalkIssuesPayload(p *api.IssuesPayload) *DingtalkPayload {
|
||||
issueName := fmt.Sprintf("#%d %s", p.Index, p.Issue.Title)
|
||||
issueURL := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Index)
|
||||
|
||||
actionCard := NewDingtalkActionCard("View Issue", issueURL)
|
||||
|
||||
actionCard.Text += "# Issue Event " + strings.Title(string(p.Action))
|
||||
actionCard.Text += "\n- Issue: **" + MarkdownLinkFormatter(issueURL, issueName) + "**"
|
||||
|
||||
|
@ -177,10 +182,13 @@ func getDingtalkIssuesPayload(p *api.IssuesPayload) (*DingtalkPayload, error) {
|
|||
actionCard.Text += "\n> " + p.Issue.Body
|
||||
}
|
||||
|
||||
return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
|
||||
return &DingtalkPayload{
|
||||
MsgType: "actionCard",
|
||||
ActionCard: actionCard,
|
||||
}
|
||||
}
|
||||
|
||||
func getDingtalkIssueCommentPayload(p *api.IssueCommentPayload) (*DingtalkPayload, error) {
|
||||
func getDingtalkIssueCommentPayload(p *api.IssueCommentPayload) *DingtalkPayload {
|
||||
issueName := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
|
||||
commentURL := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
|
||||
if p.Action != api.HOOK_ISSUE_COMMENT_DELETED {
|
||||
|
@ -190,16 +198,18 @@ func getDingtalkIssueCommentPayload(p *api.IssueCommentPayload) (*DingtalkPayloa
|
|||
issueURL := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index)
|
||||
|
||||
actionCard := NewDingtalkActionCard("View Issue Comment", commentURL)
|
||||
|
||||
actionCard.Text += "# Issue Comment " + strings.Title(string(p.Action))
|
||||
actionCard.Text += "\n- Issue: " + MarkdownLinkFormatter(issueURL, issueName)
|
||||
actionCard.Text += "\n- Comment content: "
|
||||
actionCard.Text += "\n> " + p.Comment.Body
|
||||
|
||||
return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
|
||||
return &DingtalkPayload{
|
||||
MsgType: "actionCard",
|
||||
ActionCard: actionCard,
|
||||
}
|
||||
}
|
||||
|
||||
func getDingtalkPullRequestPayload(p *api.PullRequestPayload) (*DingtalkPayload, error) {
|
||||
func getDingtalkPullRequestPayload(p *api.PullRequestPayload) *DingtalkPayload {
|
||||
title := "# Pull Request " + strings.Title(string(p.Action))
|
||||
if p.Action == api.HOOK_ISSUE_CLOSED && p.PullRequest.HasMerged {
|
||||
title = "# Pull Request Merged"
|
||||
|
@ -227,10 +237,13 @@ func getDingtalkPullRequestPayload(p *api.PullRequestPayload) (*DingtalkPayload,
|
|||
actionCard.Text += "\n> " + p.PullRequest.Body
|
||||
}
|
||||
|
||||
return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
|
||||
return &DingtalkPayload{
|
||||
MsgType: "actionCard",
|
||||
ActionCard: actionCard,
|
||||
}
|
||||
}
|
||||
|
||||
func getDingtalkReleasePayload(p *api.ReleasePayload) (*DingtalkPayload, error) {
|
||||
func getDingtalkReleasePayload(p *api.ReleasePayload) *DingtalkPayload {
|
||||
releaseURL := p.Repository.HTMLURL + "/src/" + p.Release.TagName
|
||||
|
||||
author := p.Release.Author.FullName
|
||||
|
@ -239,7 +252,6 @@ func getDingtalkReleasePayload(p *api.ReleasePayload) (*DingtalkPayload, error)
|
|||
}
|
||||
|
||||
actionCard := NewDingtalkActionCard("View Release", releaseURL)
|
||||
|
||||
actionCard.Text += "# New Release Published"
|
||||
actionCard.Text += "\n- Repo: " + MarkdownLinkFormatter(p.Repository.HTMLURL, p.Repository.Name)
|
||||
actionCard.Text += "\n- Tag: " + MarkdownLinkFormatter(releaseURL, p.Release.TagName)
|
||||
|
@ -252,10 +264,13 @@ func getDingtalkReleasePayload(p *api.ReleasePayload) (*DingtalkPayload, error)
|
|||
actionCard.Text += "\n- Note: " + p.Release.Body
|
||||
}
|
||||
|
||||
return &DingtalkPayload{MsgType: "actionCard", ActionCard: actionCard}, nil
|
||||
return &DingtalkPayload{
|
||||
MsgType: "actionCard",
|
||||
ActionCard: actionCard,
|
||||
}
|
||||
}
|
||||
|
||||
// Format link addr and title into markdown style
|
||||
// MarkdownLinkFormatter formats link address and title into Markdown style.
|
||||
func MarkdownLinkFormatter(link, text string) string {
|
||||
return "[" + text + "](" + link + ")"
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ import (
|
|||
"strings"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/gogs/git-module"
|
||||
api "github.com/gogs/go-gogs-client"
|
||||
|
@ -70,7 +71,7 @@ func DiscordSHALinkFormatter(url, text string) string {
|
|||
}
|
||||
|
||||
// getDiscordCreatePayload composes Discord payload for create new branch or tag.
|
||||
func getDiscordCreatePayload(p *api.CreatePayload) (*DiscordPayload, error) {
|
||||
func getDiscordCreatePayload(p *api.CreatePayload) *DiscordPayload {
|
||||
refName := git.RefShortName(p.Ref)
|
||||
repoLink := DiscordLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
|
||||
refLink := DiscordLinkFormatter(p.Repo.HTMLURL+"/src/"+refName, refName)
|
||||
|
@ -84,11 +85,11 @@ func getDiscordCreatePayload(p *api.CreatePayload) (*DiscordPayload, error) {
|
|||
IconURL: p.Sender.AvatarUrl,
|
||||
},
|
||||
}},
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// getDiscordDeletePayload composes Discord payload for delete a branch or tag.
|
||||
func getDiscordDeletePayload(p *api.DeletePayload) (*DiscordPayload, error) {
|
||||
func getDiscordDeletePayload(p *api.DeletePayload) *DiscordPayload {
|
||||
refName := git.RefShortName(p.Ref)
|
||||
repoLink := DiscordLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
|
||||
content := fmt.Sprintf("Deleted %s: %s/%s", p.RefType, repoLink, refName)
|
||||
|
@ -101,11 +102,11 @@ func getDiscordDeletePayload(p *api.DeletePayload) (*DiscordPayload, error) {
|
|||
IconURL: p.Sender.AvatarUrl,
|
||||
},
|
||||
}},
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// getDiscordForkPayload composes Discord payload for forked by a repository.
|
||||
func getDiscordForkPayload(p *api.ForkPayload) (*DiscordPayload, error) {
|
||||
func getDiscordForkPayload(p *api.ForkPayload) *DiscordPayload {
|
||||
baseLink := DiscordLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
|
||||
forkLink := DiscordLinkFormatter(p.Forkee.HTMLURL, p.Forkee.FullName)
|
||||
content := fmt.Sprintf("%s is forked to %s", baseLink, forkLink)
|
||||
|
@ -118,10 +119,10 @@ func getDiscordForkPayload(p *api.ForkPayload) (*DiscordPayload, error) {
|
|||
IconURL: p.Sender.AvatarUrl,
|
||||
},
|
||||
}},
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func getDiscordPushPayload(p *api.PushPayload, slack *SlackMeta) (*DiscordPayload, error) {
|
||||
func getDiscordPushPayload(p *api.PushPayload, slack *SlackMeta) *DiscordPayload {
|
||||
// n new commits
|
||||
var (
|
||||
branchName = git.RefShortName(p.Ref)
|
||||
|
@ -167,10 +168,10 @@ func getDiscordPushPayload(p *api.PushPayload, slack *SlackMeta) (*DiscordPayloa
|
|||
IconURL: p.Sender.AvatarUrl,
|
||||
},
|
||||
}},
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func getDiscordIssuesPayload(p *api.IssuesPayload, slack *SlackMeta) (*DiscordPayload, error) {
|
||||
func getDiscordIssuesPayload(p *api.IssuesPayload, slack *SlackMeta) *DiscordPayload {
|
||||
title := fmt.Sprintf("#%d %s", p.Index, p.Issue.Title)
|
||||
url := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Index)
|
||||
content := ""
|
||||
|
@ -239,10 +240,10 @@ func getDiscordIssuesPayload(p *api.IssuesPayload, slack *SlackMeta) (*DiscordPa
|
|||
},
|
||||
Fields: fields,
|
||||
}},
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func getDiscordIssueCommentPayload(p *api.IssueCommentPayload, slack *SlackMeta) (*DiscordPayload, error) {
|
||||
func getDiscordIssueCommentPayload(p *api.IssueCommentPayload, slack *SlackMeta) *DiscordPayload {
|
||||
title := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)
|
||||
url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, CommentHashTag(p.Comment.ID))
|
||||
content := ""
|
||||
|
@ -278,10 +279,10 @@ func getDiscordIssueCommentPayload(p *api.IssueCommentPayload, slack *SlackMeta)
|
|||
},
|
||||
Fields: fields,
|
||||
}},
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func getDiscordPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (*DiscordPayload, error) {
|
||||
func getDiscordPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) *DiscordPayload {
|
||||
title := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title)
|
||||
url := fmt.Sprintf("%s/pulls/%d", p.Repository.HTMLURL, p.Index)
|
||||
content := ""
|
||||
|
@ -351,10 +352,10 @@ func getDiscordPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (
|
|||
},
|
||||
Fields: fields,
|
||||
}},
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func getDiscordReleasePayload(p *api.ReleasePayload) (*DiscordPayload, error) {
|
||||
func getDiscordReleasePayload(p *api.ReleasePayload) *DiscordPayload {
|
||||
repoLink := DiscordLinkFormatter(p.Repository.HTMLURL, p.Repository.Name)
|
||||
refLink := DiscordLinkFormatter(p.Repository.HTMLURL+"/src/"+p.Release.TagName, p.Release.TagName)
|
||||
content := fmt.Sprintf("Published new release %s of %s", refLink, repoLink)
|
||||
|
@ -367,7 +368,7 @@ func getDiscordReleasePayload(p *api.ReleasePayload) (*DiscordPayload, error) {
|
|||
IconURL: p.Sender.AvatarUrl,
|
||||
},
|
||||
}},
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func GetDiscordPayload(p api.Payloader, event HookEventType, meta string) (payload *DiscordPayload, err error) {
|
||||
|
@ -378,24 +379,23 @@ func GetDiscordPayload(p api.Payloader, event HookEventType, meta string) (paylo
|
|||
|
||||
switch event {
|
||||
case HOOK_EVENT_CREATE:
|
||||
payload, err = getDiscordCreatePayload(p.(*api.CreatePayload))
|
||||
payload = getDiscordCreatePayload(p.(*api.CreatePayload))
|
||||
case HOOK_EVENT_DELETE:
|
||||
payload, err = getDiscordDeletePayload(p.(*api.DeletePayload))
|
||||
payload = getDiscordDeletePayload(p.(*api.DeletePayload))
|
||||
case HOOK_EVENT_FORK:
|
||||
payload, err = getDiscordForkPayload(p.(*api.ForkPayload))
|
||||
payload = getDiscordForkPayload(p.(*api.ForkPayload))
|
||||
case HOOK_EVENT_PUSH:
|
||||
payload, err = getDiscordPushPayload(p.(*api.PushPayload), slack)
|
||||
payload = getDiscordPushPayload(p.(*api.PushPayload), slack)
|
||||
case HOOK_EVENT_ISSUES:
|
||||
payload, err = getDiscordIssuesPayload(p.(*api.IssuesPayload), slack)
|
||||
payload = getDiscordIssuesPayload(p.(*api.IssuesPayload), slack)
|
||||
case HOOK_EVENT_ISSUE_COMMENT:
|
||||
payload, err = getDiscordIssueCommentPayload(p.(*api.IssueCommentPayload), slack)
|
||||
payload = getDiscordIssueCommentPayload(p.(*api.IssueCommentPayload), slack)
|
||||
case HOOK_EVENT_PULL_REQUEST:
|
||||
payload, err = getDiscordPullRequestPayload(p.(*api.PullRequestPayload), slack)
|
||||
payload = getDiscordPullRequestPayload(p.(*api.PullRequestPayload), slack)
|
||||
case HOOK_EVENT_RELEASE:
|
||||
payload, err = getDiscordReleasePayload(p.(*api.ReleasePayload))
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("event '%s': %v", event, err)
|
||||
payload = getDiscordReleasePayload(p.(*api.ReleasePayload))
|
||||
default:
|
||||
return nil, errors.Errorf("unexpected event %q", event)
|
||||
}
|
||||
|
||||
payload.Username = slack.Username
|
||||
|
@ -404,6 +404,5 @@ func GetDiscordPayload(p api.Payloader, event HookEventType, meta string) (paylo
|
|||
color, _ := strconv.ParseInt(strings.TrimLeft(slack.Color, "#"), 16, 32)
|
||||
payload.Embeds[0].Color = int(color)
|
||||
}
|
||||
|
||||
return payload, nil
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"strings"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/gogs/git-module"
|
||||
api "github.com/gogs/go-gogs-client"
|
||||
|
@ -71,37 +72,37 @@ func SlackLinkFormatter(url, text string) string {
|
|||
}
|
||||
|
||||
// getSlackCreatePayload composes Slack payload for create new branch or tag.
|
||||
func getSlackCreatePayload(p *api.CreatePayload) (*SlackPayload, error) {
|
||||
func getSlackCreatePayload(p *api.CreatePayload) *SlackPayload {
|
||||
refName := git.RefShortName(p.Ref)
|
||||
repoLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
|
||||
refLink := SlackLinkFormatter(p.Repo.HTMLURL+"/src/"+refName, refName)
|
||||
text := fmt.Sprintf("[%s:%s] %s created by %s", repoLink, refLink, p.RefType, p.Sender.UserName)
|
||||
return &SlackPayload{
|
||||
Text: text,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// getSlackDeletePayload composes Slack payload for delete a branch or tag.
|
||||
func getSlackDeletePayload(p *api.DeletePayload) (*SlackPayload, error) {
|
||||
func getSlackDeletePayload(p *api.DeletePayload) *SlackPayload {
|
||||
refName := git.RefShortName(p.Ref)
|
||||
repoLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
|
||||
text := fmt.Sprintf("[%s:%s] %s deleted by %s", repoLink, refName, p.RefType, p.Sender.UserName)
|
||||
return &SlackPayload{
|
||||
Text: text,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// getSlackForkPayload composes Slack payload for forked by a repository.
|
||||
func getSlackForkPayload(p *api.ForkPayload) (*SlackPayload, error) {
|
||||
func getSlackForkPayload(p *api.ForkPayload) *SlackPayload {
|
||||
baseLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.Name)
|
||||
forkLink := SlackLinkFormatter(p.Forkee.HTMLURL, p.Forkee.FullName)
|
||||
text := fmt.Sprintf("%s is forked to %s", baseLink, forkLink)
|
||||
return &SlackPayload{
|
||||
Text: text,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func getSlackPushPayload(p *api.PushPayload, slack *SlackMeta) (*SlackPayload, error) {
|
||||
func getSlackPushPayload(p *api.PushPayload, slack *SlackMeta) *SlackPayload {
|
||||
// n new commits
|
||||
var (
|
||||
branchName = git.RefShortName(p.Ref)
|
||||
|
@ -143,10 +144,10 @@ func getSlackPushPayload(p *api.PushPayload, slack *SlackMeta) (*SlackPayload, e
|
|||
Color: slack.Color,
|
||||
Text: attachmentText,
|
||||
}},
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func getSlackIssuesPayload(p *api.IssuesPayload, slack *SlackMeta) (*SlackPayload, error) {
|
||||
func getSlackIssuesPayload(p *api.IssuesPayload, slack *SlackMeta) *SlackPayload {
|
||||
senderLink := SlackLinkFormatter(conf.Server.ExternalURL+p.Sender.UserName, p.Sender.UserName)
|
||||
titleLink := SlackLinkFormatter(fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Index),
|
||||
fmt.Sprintf("#%d %s", p.Index, p.Issue.Title))
|
||||
|
@ -189,10 +190,10 @@ func getSlackIssuesPayload(p *api.IssuesPayload, slack *SlackMeta) (*SlackPayloa
|
|||
Title: title,
|
||||
Text: attachmentText,
|
||||
}},
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func getSlackIssueCommentPayload(p *api.IssueCommentPayload, slack *SlackMeta) (*SlackPayload, error) {
|
||||
func getSlackIssueCommentPayload(p *api.IssueCommentPayload, slack *SlackMeta) *SlackPayload {
|
||||
senderLink := SlackLinkFormatter(conf.Server.ExternalURL+p.Sender.UserName, p.Sender.UserName)
|
||||
titleLink := SlackLinkFormatter(fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, CommentHashTag(p.Comment.ID)),
|
||||
fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title))
|
||||
|
@ -223,10 +224,10 @@ func getSlackIssueCommentPayload(p *api.IssueCommentPayload, slack *SlackMeta) (
|
|||
Title: title,
|
||||
Text: attachmentText,
|
||||
}},
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func getSlackPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (*SlackPayload, error) {
|
||||
func getSlackPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) *SlackPayload {
|
||||
senderLink := SlackLinkFormatter(conf.Server.ExternalURL+p.Sender.UserName, p.Sender.UserName)
|
||||
titleLink := SlackLinkFormatter(fmt.Sprintf("%s/pulls/%d", p.Repository.HTMLURL, p.Index),
|
||||
fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title))
|
||||
|
@ -275,16 +276,16 @@ func getSlackPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (*S
|
|||
Title: title,
|
||||
Text: attachmentText,
|
||||
}},
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func getSlackReleasePayload(p *api.ReleasePayload) (*SlackPayload, error) {
|
||||
func getSlackReleasePayload(p *api.ReleasePayload) *SlackPayload {
|
||||
repoLink := SlackLinkFormatter(p.Repository.HTMLURL, p.Repository.Name)
|
||||
refLink := SlackLinkFormatter(p.Repository.HTMLURL+"/src/"+p.Release.TagName, p.Release.TagName)
|
||||
text := fmt.Sprintf("[%s] new release %s published by %s", repoLink, refLink, p.Sender.UserName)
|
||||
return &SlackPayload{
|
||||
Text: text,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func GetSlackPayload(p api.Payloader, event HookEventType, meta string) (payload *SlackPayload, err error) {
|
||||
|
@ -295,24 +296,23 @@ func GetSlackPayload(p api.Payloader, event HookEventType, meta string) (payload
|
|||
|
||||
switch event {
|
||||
case HOOK_EVENT_CREATE:
|
||||
payload, err = getSlackCreatePayload(p.(*api.CreatePayload))
|
||||
payload = getSlackCreatePayload(p.(*api.CreatePayload))
|
||||
case HOOK_EVENT_DELETE:
|
||||
payload, err = getSlackDeletePayload(p.(*api.DeletePayload))
|
||||
payload = getSlackDeletePayload(p.(*api.DeletePayload))
|
||||
case HOOK_EVENT_FORK:
|
||||
payload, err = getSlackForkPayload(p.(*api.ForkPayload))
|
||||
payload = getSlackForkPayload(p.(*api.ForkPayload))
|
||||
case HOOK_EVENT_PUSH:
|
||||
payload, err = getSlackPushPayload(p.(*api.PushPayload), slack)
|
||||
payload = getSlackPushPayload(p.(*api.PushPayload), slack)
|
||||
case HOOK_EVENT_ISSUES:
|
||||
payload, err = getSlackIssuesPayload(p.(*api.IssuesPayload), slack)
|
||||
payload = getSlackIssuesPayload(p.(*api.IssuesPayload), slack)
|
||||
case HOOK_EVENT_ISSUE_COMMENT:
|
||||
payload, err = getSlackIssueCommentPayload(p.(*api.IssueCommentPayload), slack)
|
||||
payload = getSlackIssueCommentPayload(p.(*api.IssueCommentPayload), slack)
|
||||
case HOOK_EVENT_PULL_REQUEST:
|
||||
payload, err = getSlackPullRequestPayload(p.(*api.PullRequestPayload), slack)
|
||||
payload = getSlackPullRequestPayload(p.(*api.PullRequestPayload), slack)
|
||||
case HOOK_EVENT_RELEASE:
|
||||
payload, err = getSlackReleasePayload(p.(*api.ReleasePayload))
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("event '%s': %v", event, err)
|
||||
payload = getSlackReleasePayload(p.(*api.ReleasePayload))
|
||||
default:
|
||||
return nil, errors.Errorf("unexpected event %q", event)
|
||||
}
|
||||
|
||||
payload.Channel = slack.Channel
|
||||
|
@ -321,6 +321,5 @@ func GetSlackPayload(p api.Payloader, event HookEventType, meta string) (payload
|
|||
if len(payload.Attachments) > 0 {
|
||||
payload.Attachments[0].Color = slack.Color
|
||||
}
|
||||
|
||||
return payload, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue