Skip to content

Commit 948ba3d

Browse files
committed
fix
1 parent d7c7a78 commit 948ba3d

17 files changed

+170
-181
lines changed

models/repo/repo.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
user_model "code.gitea.io/gitea/models/user"
1919
"code.gitea.io/gitea/modules/base"
2020
"code.gitea.io/gitea/modules/git"
21+
"code.gitea.io/gitea/modules/httplib"
2122
"code.gitea.io/gitea/modules/log"
2223
"code.gitea.io/gitea/modules/markup"
2324
"code.gitea.io/gitea/modules/optional"
@@ -321,8 +322,12 @@ func (repo *Repository) FullName() string {
321322
}
322323

323324
// HTMLURL returns the repository HTML URL
324-
func (repo *Repository) HTMLURL() string {
325-
return setting.AppURL + url.PathEscape(repo.OwnerName) + "/" + url.PathEscape(repo.Name)
325+
func (repo *Repository) HTMLURL(ctxs ...context.Context) string {
326+
ctx := context.TODO()
327+
if len(ctxs) > 0 {
328+
ctx = ctxs[0]
329+
}
330+
return httplib.MakeAbsoluteURL(ctx, repo.Link())
326331
}
327332

328333
// CommitLink make link to by commit full ID

routers/web/repo/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ func SearchRepo(ctx *context.Context) {
668668
Template: repo.IsTemplate,
669669
Mirror: repo.IsMirror,
670670
Stars: repo.NumStars,
671-
HTMLURL: repo.HTMLURL(),
671+
HTMLURL: repo.HTMLURL(ctx),
672672
Link: repo.Link(),
673673
Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate,
674674
},

services/convert/repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func innerToRepo(ctx context.Context, repo *repo_model.Repository, permissionInR
191191
Fork: repo.IsFork,
192192
Parent: parent,
193193
Mirror: repo.IsMirror,
194-
HTMLURL: repo.HTMLURL(),
194+
HTMLURL: repo.HTMLURL(ctx),
195195
URL: repoAPIURL,
196196
SSHURL: cloneLink.SSH,
197197
CloneURL: cloneLink.HTTPS,

services/webhook/dingtalk.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
)
2121

2222
type (
23-
// DingtalkPayload represents
24-
DingtalkPayload dingtalk.Payload
23+
DingtalkPayload dingtalk.Payload
24+
dingtalkConvertor struct{}
2525
)
2626

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

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

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

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

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

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

122122
// Review implements PayloadConvertor Review method
@@ -186,10 +186,7 @@ func createDingtalkPayload(title, text, singleTitle, singleURL string) DingtalkP
186186
}
187187
}
188188

189-
type dingtalkConvertor struct{}
190-
191-
var _ payloadConvertor[DingtalkPayload] = dingtalkConvertor{}
192-
193189
func newDingtalkRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
194-
return newJSONRequest(dingtalkConvertor{}, w, t, true)
190+
var pc payloadConvertor[DingtalkPayload] = dingtalkConvertor{}
191+
return newJSONRequest(pc, w, t, true)
195192
}

services/webhook/discord.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ var (
100100
redColor = color("ff3232")
101101
)
102102

103+
type discordConvertor struct {
104+
Username string
105+
AvatarURL string
106+
}
107+
103108
// Create implements PayloadConvertor Create method
104109
func (d discordConvertor) Create(p *api.CreatePayload) (DiscordPayload, error) {
105110
// created tag/branch
@@ -162,9 +167,9 @@ func (d discordConvertor) Push(p *api.PushPayload) (DiscordPayload, error) {
162167

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

167-
return d.createPayload(p.Sender, title, text, p.Issue.HTMLURL, color), nil
172+
return d.createPayload(p.Sender, title, extraMarkdown, p.Issue.HTMLURL, color), nil
168173
}
169174

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

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

181-
return d.createPayload(p.Sender, title, text, p.PullRequest.HTMLURL, color), nil
186+
return d.createPayload(p.Sender, title, extraMarkdown, p.PullRequest.HTMLURL, color), nil
182187
}
183188

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

256-
type discordConvertor struct {
257-
Username string
258-
AvatarURL string
259-
}
260-
261-
var _ payloadConvertor[DiscordPayload] = discordConvertor{}
262-
263261
func newDiscordRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
264262
meta := &DiscordMeta{}
265263
if err := json.Unmarshal([]byte(w.Meta), meta); err != nil {
266264
return nil, nil, fmt.Errorf("newDiscordRequest meta json: %w", err)
267265
}
268-
sc := discordConvertor{
266+
var pc payloadConvertor[DiscordPayload] = discordConvertor{
269267
Username: meta.Username,
270268
AvatarURL: meta.IconURL,
271269
}
272-
return newJSONRequest(sc, w, t, true)
270+
return newJSONRequest(pc, w, t, true)
273271
}
274272

275273
func parseHookPullRequestEventType(event webhook_module.HookEventType) (string, error) {

services/webhook/feishu.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ func newFeishuTextPayload(text string) FeishuPayload {
3636
}
3737
}
3838

39+
type feishuConvertor struct{}
40+
3941
// Create implements PayloadConvertor Create method
4042
func (fc feishuConvertor) Create(p *api.CreatePayload) (FeishuPayload, error) {
4143
// created tag/branch
@@ -164,10 +166,7 @@ func (fc feishuConvertor) Package(p *api.PackagePayload) (FeishuPayload, error)
164166
return newFeishuTextPayload(text), nil
165167
}
166168

167-
type feishuConvertor struct{}
168-
169-
var _ payloadConvertor[FeishuPayload] = feishuConvertor{}
170-
171169
func newFeishuRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
172-
return newJSONRequest(feishuConvertor{}, w, t, true)
170+
var pc payloadConvertor[FeishuPayload] = feishuConvertor{}
171+
return newJSONRequest(pc, w, t, true)
173172
}

services/webhook/general.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,11 @@ func getIssuesCommentInfo(p *api.IssueCommentPayload) (title, link, by, operator
9191
return title, link, by, operator
9292
}
9393

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

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

138-
var attachmentText string
139137
if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited {
140-
attachmentText = p.Issue.Body
138+
extraMarkdown = p.Issue.Body
141139
}
142140

143-
return text, issueTitle, attachmentText, color
141+
return text, issueTitle, extraMarkdown, color
144142
}
145143

146-
func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkFormatter, withSender bool) (string, string, string, int) {
147-
repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
148-
issueTitle := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title)
144+
func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkFormatter, withSender bool) (text, issueTitle, extraMarkdown string, color int) {
145+
color = yellowColor
146+
issueTitle = fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title)
149147
titleLink := linkFormatter(p.PullRequest.URL, issueTitle)
150-
var text string
151-
var attachmentText string
152-
color := yellowColor
148+
repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
153149

154150
switch p.Action {
155151
case api.HookIssueOpened:
156152
text = fmt.Sprintf("[%s] Pull request opened: %s", repoLink, titleLink)
157-
attachmentText = p.PullRequest.Body
153+
extraMarkdown = p.PullRequest.Body
158154
color = greenColor
159155
case api.HookIssueClosed:
160156
if p.PullRequest.HasMerged {
@@ -168,7 +164,7 @@ func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkForm
168164
text = fmt.Sprintf("[%s] Pull request re-opened: %s", repoLink, titleLink)
169165
case api.HookIssueEdited:
170166
text = fmt.Sprintf("[%s] Pull request edited: %s", repoLink, titleLink)
171-
attachmentText = p.PullRequest.Body
167+
extraMarkdown = p.PullRequest.Body
172168
case api.HookIssueAssigned:
173169
list := make([]string, len(p.PullRequest.Assignees))
174170
for i, user := range p.PullRequest.Assignees {
@@ -193,7 +189,7 @@ func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkForm
193189
text = fmt.Sprintf("[%s] Pull request milestone cleared: %s", repoLink, titleLink)
194190
case api.HookIssueReviewed:
195191
text = fmt.Sprintf("[%s] Pull request reviewed: %s", repoLink, titleLink)
196-
attachmentText = p.Review.Content
192+
extraMarkdown = p.Review.Content
197193
case api.HookIssueReviewRequested:
198194
text = fmt.Sprintf("[%s] Pull request review requested: %s", repoLink, titleLink)
199195
case api.HookIssueReviewRequestRemoved:
@@ -203,7 +199,7 @@ func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkForm
203199
text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName))
204200
}
205201

206-
return text, issueTitle, attachmentText, color
202+
return text, issueTitle, extraMarkdown, color
207203
}
208204

209205
func getReleasePayloadInfo(p *api.ReleasePayload, linkFormatter linkFormatter, withSender bool) (text string, color int) {

services/webhook/general_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,10 @@ func TestGetIssuesPayloadInfo(t *testing.T) {
424424

425425
for i, c := range cases {
426426
p.Action = c.action
427-
text, issueTitle, attachmentText, color := getIssuesPayloadInfo(p, noneLinkFormatter, true)
427+
text, issueTitle, extraMarkdown, color := getIssuesPayloadInfo(p, noneLinkFormatter, true)
428428
assert.Equal(t, c.text, text, "case %d", i)
429429
assert.Equal(t, c.issueTitle, issueTitle, "case %d", i)
430-
assert.Equal(t, c.attachmentText, attachmentText, "case %d", i)
430+
assert.Equal(t, c.attachmentText, extraMarkdown, "case %d", i)
431431
assert.Equal(t, c.color, color, "case %d", i)
432432
}
433433
}
@@ -523,10 +523,10 @@ func TestGetPullRequestPayloadInfo(t *testing.T) {
523523

524524
for i, c := range cases {
525525
p.Action = c.action
526-
text, issueTitle, attachmentText, color := getPullRequestPayloadInfo(p, noneLinkFormatter, true)
526+
text, issueTitle, extraMarkdown, color := getPullRequestPayloadInfo(p, noneLinkFormatter, true)
527527
assert.Equal(t, c.text, text, "case %d", i)
528528
assert.Equal(t, c.issueTitle, issueTitle, "case %d", i)
529-
assert.Equal(t, c.attachmentText, attachmentText, "case %d", i)
529+
assert.Equal(t, c.attachmentText, extraMarkdown, "case %d", i)
530530
assert.Equal(t, c.color, color, "case %d", i)
531531
}
532532
}

services/webhook/matrix.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ func newMatrixRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_mo
2929
if err := json.Unmarshal([]byte(w.Meta), meta); err != nil {
3030
return nil, nil, fmt.Errorf("GetMatrixPayload meta json: %w", err)
3131
}
32-
mc := matrixConvertor{
32+
var pc payloadConvertor[MatrixPayload] = matrixConvertor{
3333
MsgType: messageTypeText[meta.MessageType],
3434
}
35-
payload, err := newPayload(mc, []byte(t.PayloadContent), t.EventType)
35+
payload, err := newPayload(pc, []byte(t.PayloadContent), t.EventType)
3636
if err != nil {
3737
return nil, nil, err
3838
}
@@ -87,8 +87,6 @@ type MatrixPayload struct {
8787
Commits []*api.PayloadCommit `json:"io.gitea.commits,omitempty"`
8888
}
8989

90-
var _ payloadConvertor[MatrixPayload] = matrixConvertor{}
91-
9290
type matrixConvertor struct {
9391
MsgType string
9492
}

services/webhook/msteams.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ type (
5858
}
5959
)
6060

61+
type msteamsConvertor struct{}
62+
6163
// Create implements PayloadConvertor Create method
6264
func (m msteamsConvertor) Create(p *api.CreatePayload) (MSTeamsPayload, error) {
6365
// created tag/branch
@@ -152,13 +154,13 @@ func (m msteamsConvertor) Push(p *api.PushPayload) (MSTeamsPayload, error) {
152154

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

157159
return createMSTeamsPayload(
158160
p.Repository,
159161
p.Sender,
160162
title,
161-
attachmentText,
163+
extraMarkdown,
162164
p.Issue.HTMLURL,
163165
color,
164166
&MSTeamsFact{"Issue #:", fmt.Sprintf("%d", p.Issue.ID)},
@@ -182,13 +184,13 @@ func (m msteamsConvertor) IssueComment(p *api.IssueCommentPayload) (MSTeamsPaylo
182184

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

187189
return createMSTeamsPayload(
188190
p.Repository,
189191
p.Sender,
190192
title,
191-
attachmentText,
193+
extraMarkdown,
192194
p.PullRequest.HTMLURL,
193195
color,
194196
&MSTeamsFact{"Pull request #:", fmt.Sprintf("%d", p.PullRequest.ID)},
@@ -343,10 +345,7 @@ func createMSTeamsPayload(r *api.Repository, s *api.User, title, text, actionTar
343345
}
344346
}
345347

346-
type msteamsConvertor struct{}
347-
348-
var _ payloadConvertor[MSTeamsPayload] = msteamsConvertor{}
349-
350348
func newMSTeamsRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
351-
return newJSONRequest(msteamsConvertor{}, w, t, true)
349+
var pc payloadConvertor[MSTeamsPayload] = msteamsConvertor{}
350+
return newJSONRequest(pc, w, t, true)
352351
}

services/webhook/notifier.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
)
2424

2525
func init() {
26-
notify_service.RegisterNotifier(&webhookNotifier{})
26+
notify_service.RegisterNotifier(NewNotifier())
2727
}
2828

2929
type webhookNotifier struct {

services/webhook/packagist.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ func GetPackagistHook(w *webhook_model.Webhook) *PackagistMeta {
4040
return s
4141
}
4242

43+
type packagistConvertor struct {
44+
PackageURL string
45+
}
46+
4347
// Create implements PayloadConvertor Create method
4448
func (pc packagistConvertor) Create(_ *api.CreatePayload) (PackagistPayload, error) {
4549
return PackagistPayload{}, nil
@@ -106,18 +110,12 @@ func (pc packagistConvertor) Package(_ *api.PackagePayload) (PackagistPayload, e
106110
return PackagistPayload{}, nil
107111
}
108112

109-
type packagistConvertor struct {
110-
PackageURL string
111-
}
112-
113-
var _ payloadConvertor[PackagistPayload] = packagistConvertor{}
114-
115113
func newPackagistRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
116114
meta := &PackagistMeta{}
117115
if err := json.Unmarshal([]byte(w.Meta), meta); err != nil {
118116
return nil, nil, fmt.Errorf("newpackagistRequest meta json: %w", err)
119117
}
120-
pc := packagistConvertor{
118+
var pc payloadConvertor[PackagistPayload] = packagistConvertor{
121119
PackageURL: meta.PackageURL,
122120
}
123121
return newJSONRequest(pc, w, t, true)

0 commit comments

Comments
 (0)