Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Jul 8, 2024
1 parent d7c7a78 commit 1ca153b
Show file tree
Hide file tree
Showing 17 changed files with 172 additions and 183 deletions.
9 changes: 7 additions & 2 deletions models/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/httplib"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/optional"
Expand Down Expand Up @@ -321,8 +322,12 @@ func (repo *Repository) FullName() string {
}

// HTMLURL returns the repository HTML URL
func (repo *Repository) HTMLURL() string {
return setting.AppURL + url.PathEscape(repo.OwnerName) + "/" + url.PathEscape(repo.Name)
func (repo *Repository) HTMLURL(ctxs ...context.Context) string {
ctx := context.TODO()
if len(ctxs) > 0 {
ctx = ctxs[0]
}
return httplib.MakeAbsoluteURL(ctx, repo.Link())
}

// CommitLink make link to by commit full ID
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ func SearchRepo(ctx *context.Context) {
Template: repo.IsTemplate,
Mirror: repo.IsMirror,
Stars: repo.NumStars,
HTMLURL: repo.HTMLURL(),
HTMLURL: repo.HTMLURL(ctx),
Link: repo.Link(),
Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
},
Expand Down
2 changes: 1 addition & 1 deletion services/convert/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func innerToRepo(ctx context.Context, repo *repo_model.Repository, permissionInR
Fork: repo.IsFork,
Parent: parent,
Mirror: repo.IsMirror,
HTMLURL: repo.HTMLURL(),
HTMLURL: repo.HTMLURL(ctx),
URL: repoAPIURL,
SSHURL: cloneLink.SSH,
CloneURL: cloneLink.HTTPS,
Expand Down
23 changes: 10 additions & 13 deletions services/webhook/dingtalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
dingtalk "gitea.com/lunny/dingtalk_webhook"
)

type (
// DingtalkPayload represents
DingtalkPayload dingtalk.Payload
)
// DingtalkPayload represents
type DingtalkPayload dingtalk.Payload

type dingtalkConvertor struct{}

// Create implements PayloadConvertor Create method
func (dc dingtalkConvertor) Create(p *api.CreatePayload) (DingtalkPayload, error) {
Expand Down Expand Up @@ -92,9 +92,9 @@ func (dc dingtalkConvertor) Push(p *api.PushPayload) (DingtalkPayload, error) {

// Issue implements PayloadConvertor Issue method
func (dc dingtalkConvertor) Issue(p *api.IssuePayload) (DingtalkPayload, error) {
text, issueTitle, attachmentText, _ := getIssuesPayloadInfo(p, noneLinkFormatter, true)
text, issueTitle, extraMarkdown, _ := getIssuesPayloadInfo(p, noneLinkFormatter, true)

return createDingtalkPayload(issueTitle, text+"\r\n\r\n"+attachmentText, "view issue", p.Issue.HTMLURL), nil
return createDingtalkPayload(issueTitle, text+"\r\n\r\n"+extraMarkdown, "view issue", p.Issue.HTMLURL), nil
}

// Wiki implements PayloadConvertor Wiki method
Expand All @@ -114,9 +114,9 @@ func (dc dingtalkConvertor) IssueComment(p *api.IssueCommentPayload) (DingtalkPa

// PullRequest implements PayloadConvertor PullRequest method
func (dc dingtalkConvertor) PullRequest(p *api.PullRequestPayload) (DingtalkPayload, error) {
text, issueTitle, attachmentText, _ := getPullRequestPayloadInfo(p, noneLinkFormatter, true)
text, issueTitle, extraMarkdown, _ := getPullRequestPayloadInfo(p, noneLinkFormatter, true)

return createDingtalkPayload(issueTitle, text+"\r\n\r\n"+attachmentText, "view pull request", p.PullRequest.HTMLURL), nil
return createDingtalkPayload(issueTitle, text+"\r\n\r\n"+extraMarkdown, "view pull request", p.PullRequest.HTMLURL), nil
}

// Review implements PayloadConvertor Review method
Expand Down Expand Up @@ -186,10 +186,7 @@ func createDingtalkPayload(title, text, singleTitle, singleURL string) DingtalkP
}
}

type dingtalkConvertor struct{}

var _ payloadConvertor[DingtalkPayload] = dingtalkConvertor{}

func newDingtalkRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
return newJSONRequest(dingtalkConvertor{}, w, t, true)
var pc payloadConvertor[DingtalkPayload] = dingtalkConvertor{}
return newJSONRequest(pc, w, t, true)
}
24 changes: 11 additions & 13 deletions services/webhook/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ var (
redColor = color("ff3232")
)

type discordConvertor struct {
Username string
AvatarURL string
}

// Create implements PayloadConvertor Create method
func (d discordConvertor) Create(p *api.CreatePayload) (DiscordPayload, error) {
// created tag/branch
Expand Down Expand Up @@ -162,9 +167,9 @@ func (d discordConvertor) Push(p *api.PushPayload) (DiscordPayload, error) {

// Issue implements PayloadConvertor Issue method
func (d discordConvertor) Issue(p *api.IssuePayload) (DiscordPayload, error) {
title, _, text, color := getIssuesPayloadInfo(p, noneLinkFormatter, false)
title, _, extraMarkdown, color := getIssuesPayloadInfo(p, noneLinkFormatter, false)

return d.createPayload(p.Sender, title, text, p.Issue.HTMLURL, color), nil
return d.createPayload(p.Sender, title, extraMarkdown, p.Issue.HTMLURL, color), nil
}

// IssueComment implements PayloadConvertor IssueComment method
Expand All @@ -176,9 +181,9 @@ func (d discordConvertor) IssueComment(p *api.IssueCommentPayload) (DiscordPaylo

// PullRequest implements PayloadConvertor PullRequest method
func (d discordConvertor) PullRequest(p *api.PullRequestPayload) (DiscordPayload, error) {
title, _, text, color := getPullRequestPayloadInfo(p, noneLinkFormatter, false)
title, _, extraMarkdown, color := getPullRequestPayloadInfo(p, noneLinkFormatter, false)

return d.createPayload(p.Sender, title, text, p.PullRequest.HTMLURL, color), nil
return d.createPayload(p.Sender, title, extraMarkdown, p.PullRequest.HTMLURL, color), nil
}

// Review implements PayloadConvertor Review method
Expand Down Expand Up @@ -253,23 +258,16 @@ func (d discordConvertor) Package(p *api.PackagePayload) (DiscordPayload, error)
return d.createPayload(p.Sender, text, "", p.Package.HTMLURL, color), nil
}

type discordConvertor struct {
Username string
AvatarURL string
}

var _ payloadConvertor[DiscordPayload] = discordConvertor{}

func newDiscordRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
meta := &DiscordMeta{}
if err := json.Unmarshal([]byte(w.Meta), meta); err != nil {
return nil, nil, fmt.Errorf("newDiscordRequest meta json: %w", err)
}
sc := discordConvertor{
var pc payloadConvertor[DiscordPayload] = discordConvertor{
Username: meta.Username,
AvatarURL: meta.IconURL,
}
return newJSONRequest(sc, w, t, true)
return newJSONRequest(pc, w, t, true)
}

func parseHookPullRequestEventType(event webhook_module.HookEventType) (string, error) {
Expand Down
9 changes: 4 additions & 5 deletions services/webhook/feishu.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func newFeishuTextPayload(text string) FeishuPayload {
}
}

type feishuConvertor struct{}

// Create implements PayloadConvertor Create method
func (fc feishuConvertor) Create(p *api.CreatePayload) (FeishuPayload, error) {
// created tag/branch
Expand Down Expand Up @@ -164,10 +166,7 @@ func (fc feishuConvertor) Package(p *api.PackagePayload) (FeishuPayload, error)
return newFeishuTextPayload(text), nil
}

type feishuConvertor struct{}

var _ payloadConvertor[FeishuPayload] = feishuConvertor{}

func newFeishuRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
return newJSONRequest(feishuConvertor{}, w, t, true)
var pc payloadConvertor[FeishuPayload] = feishuConvertor{}
return newJSONRequest(pc, w, t, true)
}
32 changes: 14 additions & 18 deletions services/webhook/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,11 @@ func getIssuesCommentInfo(p *api.IssueCommentPayload) (title, link, by, operator
return title, link, by, operator
}

func getIssuesPayloadInfo(p *api.IssuePayload, linkFormatter linkFormatter, withSender bool) (string, string, string, int) {
repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
issueTitle := fmt.Sprintf("#%d %s", p.Index, p.Issue.Title)
func getIssuesPayloadInfo(p *api.IssuePayload, linkFormatter linkFormatter, withSender bool) (text, issueTitle, extraMarkdown string, color int) {
color = yellowColor
issueTitle = fmt.Sprintf("#%d %s", p.Index, p.Issue.Title)
titleLink := linkFormatter(fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Index), issueTitle)
var text string
color := yellowColor
repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)

switch p.Action {
case api.HookIssueOpened:
Expand Down Expand Up @@ -135,26 +134,23 @@ func getIssuesPayloadInfo(p *api.IssuePayload, linkFormatter linkFormatter, with
text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName))
}

var attachmentText string
if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited {
attachmentText = p.Issue.Body
extraMarkdown = p.Issue.Body
}

return text, issueTitle, attachmentText, color
return text, issueTitle, extraMarkdown, color
}

func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkFormatter, withSender bool) (string, string, string, int) {
repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
issueTitle := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title)
func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkFormatter, withSender bool) (text, issueTitle, extraMarkdown string, color int) {
color = yellowColor
issueTitle = fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title)
titleLink := linkFormatter(p.PullRequest.URL, issueTitle)
var text string
var attachmentText string
color := yellowColor
repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)

switch p.Action {
case api.HookIssueOpened:
text = fmt.Sprintf("[%s] Pull request opened: %s", repoLink, titleLink)
attachmentText = p.PullRequest.Body
extraMarkdown = p.PullRequest.Body
color = greenColor
case api.HookIssueClosed:
if p.PullRequest.HasMerged {
Expand All @@ -168,7 +164,7 @@ func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkForm
text = fmt.Sprintf("[%s] Pull request re-opened: %s", repoLink, titleLink)
case api.HookIssueEdited:
text = fmt.Sprintf("[%s] Pull request edited: %s", repoLink, titleLink)
attachmentText = p.PullRequest.Body
extraMarkdown = p.PullRequest.Body
case api.HookIssueAssigned:
list := make([]string, len(p.PullRequest.Assignees))
for i, user := range p.PullRequest.Assignees {
Expand All @@ -193,7 +189,7 @@ func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkForm
text = fmt.Sprintf("[%s] Pull request milestone cleared: %s", repoLink, titleLink)
case api.HookIssueReviewed:
text = fmt.Sprintf("[%s] Pull request reviewed: %s", repoLink, titleLink)
attachmentText = p.Review.Content
extraMarkdown = p.Review.Content
case api.HookIssueReviewRequested:
text = fmt.Sprintf("[%s] Pull request review requested: %s", repoLink, titleLink)
case api.HookIssueReviewRequestRemoved:
Expand All @@ -203,7 +199,7 @@ func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkForm
text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName))
}

return text, issueTitle, attachmentText, color
return text, issueTitle, extraMarkdown, color
}

func getReleasePayloadInfo(p *api.ReleasePayload, linkFormatter linkFormatter, withSender bool) (text string, color int) {
Expand Down
8 changes: 4 additions & 4 deletions services/webhook/general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,10 @@ func TestGetIssuesPayloadInfo(t *testing.T) {

for i, c := range cases {
p.Action = c.action
text, issueTitle, attachmentText, color := getIssuesPayloadInfo(p, noneLinkFormatter, true)
text, issueTitle, extraMarkdown, color := getIssuesPayloadInfo(p, noneLinkFormatter, true)
assert.Equal(t, c.text, text, "case %d", i)
assert.Equal(t, c.issueTitle, issueTitle, "case %d", i)
assert.Equal(t, c.attachmentText, attachmentText, "case %d", i)
assert.Equal(t, c.attachmentText, extraMarkdown, "case %d", i)
assert.Equal(t, c.color, color, "case %d", i)
}
}
Expand Down Expand Up @@ -523,10 +523,10 @@ func TestGetPullRequestPayloadInfo(t *testing.T) {

for i, c := range cases {
p.Action = c.action
text, issueTitle, attachmentText, color := getPullRequestPayloadInfo(p, noneLinkFormatter, true)
text, issueTitle, extraMarkdown, color := getPullRequestPayloadInfo(p, noneLinkFormatter, true)
assert.Equal(t, c.text, text, "case %d", i)
assert.Equal(t, c.issueTitle, issueTitle, "case %d", i)
assert.Equal(t, c.attachmentText, attachmentText, "case %d", i)
assert.Equal(t, c.attachmentText, extraMarkdown, "case %d", i)
assert.Equal(t, c.color, color, "case %d", i)
}
}
Expand Down
6 changes: 2 additions & 4 deletions services/webhook/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ func newMatrixRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_mo
if err := json.Unmarshal([]byte(w.Meta), meta); err != nil {
return nil, nil, fmt.Errorf("GetMatrixPayload meta json: %w", err)
}
mc := matrixConvertor{
var pc payloadConvertor[MatrixPayload] = matrixConvertor{
MsgType: messageTypeText[meta.MessageType],
}
payload, err := newPayload(mc, []byte(t.PayloadContent), t.EventType)
payload, err := newPayload(pc, []byte(t.PayloadContent), t.EventType)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -87,8 +87,6 @@ type MatrixPayload struct {
Commits []*api.PayloadCommit `json:"io.gitea.commits,omitempty"`
}

var _ payloadConvertor[MatrixPayload] = matrixConvertor{}

type matrixConvertor struct {
MsgType string
}
Expand Down
17 changes: 8 additions & 9 deletions services/webhook/msteams.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ type (
}
)

type msteamsConvertor struct{}

// Create implements PayloadConvertor Create method
func (m msteamsConvertor) Create(p *api.CreatePayload) (MSTeamsPayload, error) {
// created tag/branch
Expand Down Expand Up @@ -152,13 +154,13 @@ func (m msteamsConvertor) Push(p *api.PushPayload) (MSTeamsPayload, error) {

// Issue implements PayloadConvertor Issue method
func (m msteamsConvertor) Issue(p *api.IssuePayload) (MSTeamsPayload, error) {
title, _, attachmentText, color := getIssuesPayloadInfo(p, noneLinkFormatter, false)
title, _, extraMarkdown, color := getIssuesPayloadInfo(p, noneLinkFormatter, false)

return createMSTeamsPayload(
p.Repository,
p.Sender,
title,
attachmentText,
extraMarkdown,
p.Issue.HTMLURL,
color,
&MSTeamsFact{"Issue #:", fmt.Sprintf("%d", p.Issue.ID)},
Expand All @@ -182,13 +184,13 @@ func (m msteamsConvertor) IssueComment(p *api.IssueCommentPayload) (MSTeamsPaylo

// PullRequest implements PayloadConvertor PullRequest method
func (m msteamsConvertor) PullRequest(p *api.PullRequestPayload) (MSTeamsPayload, error) {
title, _, attachmentText, color := getPullRequestPayloadInfo(p, noneLinkFormatter, false)
title, _, extraMarkdown, color := getPullRequestPayloadInfo(p, noneLinkFormatter, false)

return createMSTeamsPayload(
p.Repository,
p.Sender,
title,
attachmentText,
extraMarkdown,
p.PullRequest.HTMLURL,
color,
&MSTeamsFact{"Pull request #:", fmt.Sprintf("%d", p.PullRequest.ID)},
Expand Down Expand Up @@ -343,10 +345,7 @@ func createMSTeamsPayload(r *api.Repository, s *api.User, title, text, actionTar
}
}

type msteamsConvertor struct{}

var _ payloadConvertor[MSTeamsPayload] = msteamsConvertor{}

func newMSTeamsRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
return newJSONRequest(msteamsConvertor{}, w, t, true)
var pc payloadConvertor[MSTeamsPayload] = msteamsConvertor{}
return newJSONRequest(pc, w, t, true)
}
2 changes: 1 addition & 1 deletion services/webhook/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

func init() {
notify_service.RegisterNotifier(&webhookNotifier{})
notify_service.RegisterNotifier(NewNotifier())
}

type webhookNotifier struct {
Expand Down
12 changes: 5 additions & 7 deletions services/webhook/packagist.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func GetPackagistHook(w *webhook_model.Webhook) *PackagistMeta {
return s
}

type packagistConvertor struct {
PackageURL string
}

// Create implements PayloadConvertor Create method
func (pc packagistConvertor) Create(_ *api.CreatePayload) (PackagistPayload, error) {
return PackagistPayload{}, nil
Expand Down Expand Up @@ -106,18 +110,12 @@ func (pc packagistConvertor) Package(_ *api.PackagePayload) (PackagistPayload, e
return PackagistPayload{}, nil
}

type packagistConvertor struct {
PackageURL string
}

var _ payloadConvertor[PackagistPayload] = packagistConvertor{}

func newPackagistRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
meta := &PackagistMeta{}
if err := json.Unmarshal([]byte(w.Meta), meta); err != nil {
return nil, nil, fmt.Errorf("newpackagistRequest meta json: %w", err)
}
pc := packagistConvertor{
var pc payloadConvertor[PackagistPayload] = packagistConvertor{
PackageURL: meta.PackageURL,
}
return newJSONRequest(pc, w, t, true)
Expand Down
Loading

0 comments on commit 1ca153b

Please sign in to comment.