Skip to content
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

Fix Matrix and MSTeams nil dereference #28089

Merged
merged 5 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions services/webhook/dingtalk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,21 @@ func TestDingTalkPayload(t *testing.T) {
assert.Equal(t, "http://localhost:3000/test/repo", parseRealSingleURL(pl.(*DingtalkPayload).ActionCard.SingleURL))
})

t.Run("Package", func(t *testing.T) {
p := packageTestPayload()

d := new(DingtalkPayload)
pl, err := d.Package(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &DingtalkPayload{}, pl)

assert.Equal(t, "Package created: GiteaContainer:latest by user1", pl.(*DingtalkPayload).ActionCard.Text)
assert.Equal(t, "Package created: GiteaContainer:latest by user1", pl.(*DingtalkPayload).ActionCard.Title)
assert.Equal(t, "view package", pl.(*DingtalkPayload).ActionCard.SingleTitle)
assert.Equal(t, "http://localhost:3000/user1/-/packages/container/GiteaContainer/latest", parseRealSingleURL(pl.(*DingtalkPayload).ActionCard.SingleURL))
})

t.Run("Wiki", func(t *testing.T) {
p := wikiTestPayload()

Expand Down
18 changes: 18 additions & 0 deletions services/webhook/discord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,24 @@ func TestDiscordPayload(t *testing.T) {
assert.Equal(t, p.Sender.AvatarURL, pl.(*DiscordPayload).Embeds[0].Author.IconURL)
})

t.Run("Package", func(t *testing.T) {
p := packageTestPayload()

d := new(DiscordPayload)
pl, err := d.Package(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &DiscordPayload{}, pl)

assert.Len(t, pl.(*DiscordPayload).Embeds, 1)
assert.Equal(t, "Package created: GiteaContainer:latest", pl.(*DiscordPayload).Embeds[0].Title)
assert.Empty(t, pl.(*DiscordPayload).Embeds[0].Description)
assert.Equal(t, "http://localhost:3000/user1/-/packages/container/GiteaContainer/latest", pl.(*DiscordPayload).Embeds[0].URL)
assert.Equal(t, p.Sender.UserName, pl.(*DiscordPayload).Embeds[0].Author.Name)
assert.Equal(t, setting.AppURL+p.Sender.UserName, pl.(*DiscordPayload).Embeds[0].Author.URL)
assert.Equal(t, p.Sender.AvatarURL, pl.(*DiscordPayload).Embeds[0].Author.IconURL)
})

t.Run("Wiki", func(t *testing.T) {
p := wikiTestPayload()

Expand Down
12 changes: 12 additions & 0 deletions services/webhook/feishu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,18 @@ func TestFeishuPayload(t *testing.T) {
assert.Equal(t, "[test/repo] Repository created", pl.(*FeishuPayload).Content.Text)
})

t.Run("Package", func(t *testing.T) {
p := packageTestPayload()

d := new(FeishuPayload)
pl, err := d.Package(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &FeishuPayload{}, pl)

assert.Equal(t, "Package created: GiteaContainer:latest by user1", pl.(*FeishuPayload).Content.Text)
})

t.Run("Wiki", func(t *testing.T) {
p := wikiTestPayload()

Expand Down
30 changes: 30 additions & 0 deletions services/webhook/general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,36 @@ func repositoryTestPayload() *api.RepositoryPayload {
}
}

func packageTestPayload() *api.PackagePayload {
return &api.PackagePayload{
Action: api.HookPackageCreated,
Sender: &api.User{
UserName: "user1",
AvatarURL: "http://localhost:3000/user1/avatar",
},
Repository: nil,
Organization: &api.User{
UserName: "org1",
AvatarURL: "http://localhost:3000/org1/avatar",
},
Package: &api.Package{
Owner: &api.User{
UserName: "user1",
AvatarURL: "http://localhost:3000/user1/avatar",
},
Repository: nil,
Creator: &api.User{
UserName: "user1",
AvatarURL: "http://localhost:3000/user1/avatar",
},
Type: "container",
Name: "GiteaContainer",
Version: "latest",
HTMLURL: "http://localhost:3000/user1/-/packages/container/GiteaContainer/latest",
},
}
}

func TestGetIssuesPayloadInfo(t *testing.T) {
p := issueTestPayload()

Expand Down
2 changes: 1 addition & 1 deletion services/webhook/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (m *MatrixPayload) Repository(p *api.RepositoryPayload) (api.Payloader, err

func (m *MatrixPayload) Package(p *api.PackagePayload) (api.Payloader, error) {
senderLink := MatrixLinkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName)
repoLink := MatrixLinkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
repoLink := MatrixLinkFormatter(p.Package.HTMLURL, p.Package.Name)
lunny marked this conversation as resolved.
Show resolved Hide resolved
var text string

switch p.Action {
Expand Down
13 changes: 13 additions & 0 deletions services/webhook/matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,19 @@ func TestMatrixPayload(t *testing.T) {
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)
})

t.Run("Package", func(t *testing.T) {
p := packageTestPayload()

d := new(MatrixPayload)
pl, err := d.Package(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &MatrixPayload{}, pl)

assert.Equal(t, `[[GiteaContainer](http://localhost:3000/user1/-/packages/container/GiteaContainer/latest)] Package published by [user1](https://try.gitea.io/user1)`, pl.(*MatrixPayload).Body)
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)
})

t.Run("Wiki", func(t *testing.T) {
p := wikiTestPayload()

Expand Down
7 changes: 4 additions & 3 deletions services/webhook/msteams.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,12 @@ func GetMSTeamsPayload(p api.Payloader, event webhook_module.HookEventType, _ st
}

func createMSTeamsPayload(r *api.Repository, s *api.User, title, text, actionTarget string, color int, fact *MSTeamsFact) *MSTeamsPayload {
facts := []MSTeamsFact{
{
facts := make([]MSTeamsFact, 0, 2)
if r != nil {
facts = append(facts, MSTeamsFact{
Name: "Repository:",
Value: r.FullName,
},
})
}
if fact != nil {
facts = append(facts, *fact)
Expand Down
27 changes: 27 additions & 0 deletions services/webhook/msteams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,33 @@ func TestMSTeamsPayload(t *testing.T) {
assert.Equal(t, "http://localhost:3000/test/repo", pl.(*MSTeamsPayload).PotentialAction[0].Targets[0].URI)
})

t.Run("Package", func(t *testing.T) {
p := packageTestPayload()

d := new(MSTeamsPayload)
pl, err := d.Package(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &MSTeamsPayload{}, pl)

assert.Equal(t, "Package created: GiteaContainer:latest", pl.(*MSTeamsPayload).Title)
assert.Equal(t, "Package created: GiteaContainer:latest", pl.(*MSTeamsPayload).Summary)
assert.Len(t, pl.(*MSTeamsPayload).Sections, 1)
assert.Equal(t, "user1", pl.(*MSTeamsPayload).Sections[0].ActivitySubtitle)
assert.Empty(t, pl.(*MSTeamsPayload).Sections[0].Text)
assert.Len(t, pl.(*MSTeamsPayload).Sections[0].Facts, 1)
for _, fact := range pl.(*MSTeamsPayload).Sections[0].Facts {
if fact.Name == "Package:" {
assert.Equal(t, p.Package.Name, fact.Value)
} else {
t.Fail()
}
}
assert.Len(t, pl.(*MSTeamsPayload).PotentialAction, 1)
assert.Len(t, pl.(*MSTeamsPayload).PotentialAction[0].Targets, 1)
assert.Equal(t, "http://localhost:3000/user1/-/packages/container/GiteaContainer/latest", pl.(*MSTeamsPayload).PotentialAction[0].Targets[0].URI)
})

t.Run("Wiki", func(t *testing.T) {
p := wikiTestPayload()

Expand Down
9 changes: 9 additions & 0 deletions services/webhook/packagist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ func TestPackagistPayload(t *testing.T) {
require.Nil(t, pl)
})

t.Run("Package", func(t *testing.T) {
p := packageTestPayload()

d := new(PackagistPayload)
pl, err := d.Package(p)
require.NoError(t, err)
require.Nil(t, pl)
})

t.Run("Wiki", func(t *testing.T) {
p := wikiTestPayload()

Expand Down
12 changes: 12 additions & 0 deletions services/webhook/slack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,18 @@ func TestSlackPayload(t *testing.T) {
assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Repository created by <https://try.gitea.io/user1|user1>", pl.(*SlackPayload).Text)
})

t.Run("Package", func(t *testing.T) {
p := packageTestPayload()

d := new(SlackPayload)
pl, err := d.Package(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &SlackPayload{}, pl)

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)
})

t.Run("Wiki", func(t *testing.T) {
p := wikiTestPayload()

Expand Down
12 changes: 12 additions & 0 deletions services/webhook/telegram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,18 @@ func TestTelegramPayload(t *testing.T) {
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Repository created`, pl.(*TelegramPayload).Message)
})

t.Run("Package", func(t *testing.T) {
p := packageTestPayload()

d := new(TelegramPayload)
pl, err := d.Package(p)
require.NoError(t, err)
require.NotNil(t, pl)
require.IsType(t, &TelegramPayload{}, pl)

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)
})

t.Run("Wiki", func(t *testing.T) {
p := wikiTestPayload()

Expand Down