Skip to content

Commit 58f5fa6

Browse files
KN4CK3Rlunny
andauthored
Fix Matrix and MSTeams nil dereference (#28089)
Fixes #28088 Fixes #28094 Added missing tests. --------- Co-authored-by: Lunny Xiao <[email protected]>
1 parent 17d246c commit 58f5fa6

11 files changed

+155
-6
lines changed

services/webhook/dingtalk_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,21 @@ func TestDingTalkPayload(t *testing.T) {
188188
assert.Equal(t, "http://localhost:3000/test/repo", parseRealSingleURL(pl.(*DingtalkPayload).ActionCard.SingleURL))
189189
})
190190

191+
t.Run("Package", func(t *testing.T) {
192+
p := packageTestPayload()
193+
194+
d := new(DingtalkPayload)
195+
pl, err := d.Package(p)
196+
require.NoError(t, err)
197+
require.NotNil(t, pl)
198+
require.IsType(t, &DingtalkPayload{}, pl)
199+
200+
assert.Equal(t, "Package created: GiteaContainer:latest by user1", pl.(*DingtalkPayload).ActionCard.Text)
201+
assert.Equal(t, "Package created: GiteaContainer:latest by user1", pl.(*DingtalkPayload).ActionCard.Title)
202+
assert.Equal(t, "view package", pl.(*DingtalkPayload).ActionCard.SingleTitle)
203+
assert.Equal(t, "http://localhost:3000/user1/-/packages/container/GiteaContainer/latest", parseRealSingleURL(pl.(*DingtalkPayload).ActionCard.SingleURL))
204+
})
205+
191206
t.Run("Wiki", func(t *testing.T) {
192207
p := wikiTestPayload()
193208

services/webhook/discord_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,24 @@ func TestDiscordPayload(t *testing.T) {
211211
assert.Equal(t, p.Sender.AvatarURL, pl.(*DiscordPayload).Embeds[0].Author.IconURL)
212212
})
213213

214+
t.Run("Package", func(t *testing.T) {
215+
p := packageTestPayload()
216+
217+
d := new(DiscordPayload)
218+
pl, err := d.Package(p)
219+
require.NoError(t, err)
220+
require.NotNil(t, pl)
221+
require.IsType(t, &DiscordPayload{}, pl)
222+
223+
assert.Len(t, pl.(*DiscordPayload).Embeds, 1)
224+
assert.Equal(t, "Package created: GiteaContainer:latest", pl.(*DiscordPayload).Embeds[0].Title)
225+
assert.Empty(t, pl.(*DiscordPayload).Embeds[0].Description)
226+
assert.Equal(t, "http://localhost:3000/user1/-/packages/container/GiteaContainer/latest", pl.(*DiscordPayload).Embeds[0].URL)
227+
assert.Equal(t, p.Sender.UserName, pl.(*DiscordPayload).Embeds[0].Author.Name)
228+
assert.Equal(t, setting.AppURL+p.Sender.UserName, pl.(*DiscordPayload).Embeds[0].Author.URL)
229+
assert.Equal(t, p.Sender.AvatarURL, pl.(*DiscordPayload).Embeds[0].Author.IconURL)
230+
})
231+
214232
t.Run("Wiki", func(t *testing.T) {
215233
p := wikiTestPayload()
216234

services/webhook/feishu_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,18 @@ func TestFeishuPayload(t *testing.T) {
144144
assert.Equal(t, "[test/repo] Repository created", pl.(*FeishuPayload).Content.Text)
145145
})
146146

147+
t.Run("Package", func(t *testing.T) {
148+
p := packageTestPayload()
149+
150+
d := new(FeishuPayload)
151+
pl, err := d.Package(p)
152+
require.NoError(t, err)
153+
require.NotNil(t, pl)
154+
require.IsType(t, &FeishuPayload{}, pl)
155+
156+
assert.Equal(t, "Package created: GiteaContainer:latest by user1", pl.(*FeishuPayload).Content.Text)
157+
})
158+
147159
t.Run("Wiki", func(t *testing.T) {
148160
p := wikiTestPayload()
149161

services/webhook/general_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,36 @@ func repositoryTestPayload() *api.RepositoryPayload {
303303
}
304304
}
305305

306+
func packageTestPayload() *api.PackagePayload {
307+
return &api.PackagePayload{
308+
Action: api.HookPackageCreated,
309+
Sender: &api.User{
310+
UserName: "user1",
311+
AvatarURL: "http://localhost:3000/user1/avatar",
312+
},
313+
Repository: nil,
314+
Organization: &api.User{
315+
UserName: "org1",
316+
AvatarURL: "http://localhost:3000/org1/avatar",
317+
},
318+
Package: &api.Package{
319+
Owner: &api.User{
320+
UserName: "user1",
321+
AvatarURL: "http://localhost:3000/user1/avatar",
322+
},
323+
Repository: nil,
324+
Creator: &api.User{
325+
UserName: "user1",
326+
AvatarURL: "http://localhost:3000/user1/avatar",
327+
},
328+
Type: "container",
329+
Name: "GiteaContainer",
330+
Version: "latest",
331+
HTMLURL: "http://localhost:3000/user1/-/packages/container/GiteaContainer/latest",
332+
},
333+
}
334+
}
335+
306336
func TestGetIssuesPayloadInfo(t *testing.T) {
307337
p := issueTestPayload()
308338

services/webhook/matrix.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,14 @@ func (m *MatrixPayload) Repository(p *api.RepositoryPayload) (api.Payloader, err
212212

213213
func (m *MatrixPayload) Package(p *api.PackagePayload) (api.Payloader, error) {
214214
senderLink := MatrixLinkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName)
215-
repoLink := MatrixLinkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
215+
packageLink := MatrixLinkFormatter(p.Package.HTMLURL, p.Package.Name)
216216
var text string
217217

218218
switch p.Action {
219219
case api.HookPackageCreated:
220-
text = fmt.Sprintf("[%s] Package published by %s", repoLink, senderLink)
220+
text = fmt.Sprintf("[%s] Package published by %s", packageLink, senderLink)
221221
case api.HookPackageDeleted:
222-
text = fmt.Sprintf("[%s] Package deleted by %s", repoLink, senderLink)
222+
text = fmt.Sprintf("[%s] Package deleted by %s", packageLink, senderLink)
223223
}
224224

225225
return getMatrixPayload(text, nil, m.MsgType), nil

services/webhook/matrix_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,19 @@ func TestMatrixPayload(t *testing.T) {
155155
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Repository created by <a href="https://try.gitea.io/user1">user1</a>`, pl.(*MatrixPayload).FormattedBody)
156156
})
157157

158+
t.Run("Package", func(t *testing.T) {
159+
p := packageTestPayload()
160+
161+
d := new(MatrixPayload)
162+
pl, err := d.Package(p)
163+
require.NoError(t, err)
164+
require.NotNil(t, pl)
165+
require.IsType(t, &MatrixPayload{}, pl)
166+
167+
assert.Equal(t, `[[GiteaContainer](http://localhost:3000/user1/-/packages/container/GiteaContainer/latest)] Package published by [user1](https://try.gitea.io/user1)`, pl.(*MatrixPayload).Body)
168+
assert.Equal(t, `[<a href="http://localhost:3000/user1/-/packages/container/GiteaContainer/latest">GiteaContainer</a>] Package published by <a href="https://try.gitea.io/user1">user1</a>`, pl.(*MatrixPayload).FormattedBody)
169+
})
170+
158171
t.Run("Wiki", func(t *testing.T) {
159172
p := wikiTestPayload()
160173

services/webhook/msteams.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,12 @@ func GetMSTeamsPayload(p api.Payloader, event webhook_module.HookEventType, _ st
316316
}
317317

318318
func createMSTeamsPayload(r *api.Repository, s *api.User, title, text, actionTarget string, color int, fact *MSTeamsFact) *MSTeamsPayload {
319-
facts := []MSTeamsFact{
320-
{
319+
facts := make([]MSTeamsFact, 0, 2)
320+
if r != nil {
321+
facts = append(facts, MSTeamsFact{
321322
Name: "Repository:",
322323
Value: r.FullName,
323-
},
324+
})
324325
}
325326
if fact != nil {
326327
facts = append(facts, *fact)

services/webhook/msteams_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,33 @@ func TestMSTeamsPayload(t *testing.T) {
329329
assert.Equal(t, "http://localhost:3000/test/repo", pl.(*MSTeamsPayload).PotentialAction[0].Targets[0].URI)
330330
})
331331

332+
t.Run("Package", func(t *testing.T) {
333+
p := packageTestPayload()
334+
335+
d := new(MSTeamsPayload)
336+
pl, err := d.Package(p)
337+
require.NoError(t, err)
338+
require.NotNil(t, pl)
339+
require.IsType(t, &MSTeamsPayload{}, pl)
340+
341+
assert.Equal(t, "Package created: GiteaContainer:latest", pl.(*MSTeamsPayload).Title)
342+
assert.Equal(t, "Package created: GiteaContainer:latest", pl.(*MSTeamsPayload).Summary)
343+
assert.Len(t, pl.(*MSTeamsPayload).Sections, 1)
344+
assert.Equal(t, "user1", pl.(*MSTeamsPayload).Sections[0].ActivitySubtitle)
345+
assert.Empty(t, pl.(*MSTeamsPayload).Sections[0].Text)
346+
assert.Len(t, pl.(*MSTeamsPayload).Sections[0].Facts, 1)
347+
for _, fact := range pl.(*MSTeamsPayload).Sections[0].Facts {
348+
if fact.Name == "Package:" {
349+
assert.Equal(t, p.Package.Name, fact.Value)
350+
} else {
351+
t.Fail()
352+
}
353+
}
354+
assert.Len(t, pl.(*MSTeamsPayload).PotentialAction, 1)
355+
assert.Len(t, pl.(*MSTeamsPayload).PotentialAction[0].Targets, 1)
356+
assert.Equal(t, "http://localhost:3000/user1/-/packages/container/GiteaContainer/latest", pl.(*MSTeamsPayload).PotentialAction[0].Targets[0].URI)
357+
})
358+
332359
t.Run("Wiki", func(t *testing.T) {
333360
p := wikiTestPayload()
334361

services/webhook/packagist_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ func TestPackagistPayload(t *testing.T) {
115115
require.Nil(t, pl)
116116
})
117117

118+
t.Run("Package", func(t *testing.T) {
119+
p := packageTestPayload()
120+
121+
d := new(PackagistPayload)
122+
pl, err := d.Package(p)
123+
require.NoError(t, err)
124+
require.Nil(t, pl)
125+
})
126+
118127
t.Run("Wiki", func(t *testing.T) {
119128
p := wikiTestPayload()
120129

services/webhook/slack_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,18 @@ func TestSlackPayload(t *testing.T) {
144144
assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Repository created by <https://try.gitea.io/user1|user1>", pl.(*SlackPayload).Text)
145145
})
146146

147+
t.Run("Package", func(t *testing.T) {
148+
p := packageTestPayload()
149+
150+
d := new(SlackPayload)
151+
pl, err := d.Package(p)
152+
require.NoError(t, err)
153+
require.NotNil(t, pl)
154+
require.IsType(t, &SlackPayload{}, pl)
155+
156+
assert.Equal(t, "Package created: <http://localhost:3000/user1/-/packages/container/GiteaContainer/latest|GiteaContainer:latest> by <https://try.gitea.io/user1|user1>", pl.(*SlackPayload).Text)
157+
})
158+
147159
t.Run("Wiki", func(t *testing.T) {
148160
p := wikiTestPayload()
149161

services/webhook/telegram_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,18 @@ func TestTelegramPayload(t *testing.T) {
144144
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Repository created`, pl.(*TelegramPayload).Message)
145145
})
146146

147+
t.Run("Package", func(t *testing.T) {
148+
p := packageTestPayload()
149+
150+
d := new(TelegramPayload)
151+
pl, err := d.Package(p)
152+
require.NoError(t, err)
153+
require.NotNil(t, pl)
154+
require.IsType(t, &TelegramPayload{}, pl)
155+
156+
assert.Equal(t, `Package created: <a href="http://localhost:3000/user1/-/packages/container/GiteaContainer/latest">GiteaContainer:latest</a> by <a href="https://try.gitea.io/user1">user1</a>`, pl.(*TelegramPayload).Message)
157+
})
158+
147159
t.Run("Wiki", func(t *testing.T) {
148160
p := wikiTestPayload()
149161

0 commit comments

Comments
 (0)