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 all commits
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
18 changes: 10 additions & 8 deletions models/system/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,26 @@ func (d *dbConfigCachedGetter) GetValue(ctx context.Context, key string) (v stri

func (d *dbConfigCachedGetter) GetRevision(ctx context.Context) int {
d.mu.RLock()
defer d.mu.RUnlock()
if time.Since(d.cacheTime) < time.Second {
return d.revision
cachedDuration := time.Since(d.cacheTime)
cachedRevision := d.revision
d.mu.RUnlock()

if cachedDuration < time.Second {
return cachedRevision
}

d.mu.Lock()
defer d.mu.Unlock()
if GetRevision(ctx) != d.revision {
d.mu.RUnlock()
d.mu.Lock()
rev, set, err := GetAllSettings(ctx)
if err != nil {
log.Error("Unable to get all settings: %v", err)
} else {
d.cacheTime = time.Now()
d.revision = rev
d.settings = set
}
d.mu.Unlock()
d.mu.RLock()
}
d.cacheTime = time.Now()
return d.revision
}

Expand Down
5 changes: 3 additions & 2 deletions modules/setting/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func DBConnStr() (string, error) {
connStr = fmt.Sprintf("%s:%s@%s(%s)/%s%scharset=%s&parseTime=true&tls=%s",
Database.User, Database.Passwd, connType, Database.Host, Database.Name, paramSep, Database.MysqlCharset, tls)
case "postgres":
connStr = getPostgreSQLConnectionString(Database.Host, Database.User, Database.Passwd, Database.Name, paramSep, Database.SSLMode)
connStr = getPostgreSQLConnectionString(Database.Host, Database.User, Database.Passwd, Database.Name, Database.SSLMode)
case "mssql":
host, port := ParseMSSQLHostPort(Database.Host)
connStr = fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;", host, port, Database.Name, Database.User, Database.Passwd)
Expand Down Expand Up @@ -157,7 +157,8 @@ func parsePostgreSQLHostPort(info string) (host, port string) {
return host, port
}

func getPostgreSQLConnectionString(dbHost, dbUser, dbPasswd, dbName, dbParam, dbsslMode string) (connStr string) {
func getPostgreSQLConnectionString(dbHost, dbUser, dbPasswd, dbName, dbsslMode string) (connStr string) {
dbName, dbParam, _ := strings.Cut(dbName, "?")
host, port := parsePostgreSQLHostPort(dbHost)
connURL := url.URL{
Scheme: "postgres",
Expand Down
15 changes: 8 additions & 7 deletions modules/setting/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,38 +59,39 @@ func Test_parsePostgreSQLHostPort(t *testing.T) {
func Test_getPostgreSQLConnectionString(t *testing.T) {
tests := []struct {
Host string
Port string
User string
Passwd string
Name string
Param string
SSLMode string
Output string
}{
{
Host: "/tmp/pg.sock",
Port: "4321",
User: "testuser",
Passwd: "space space !#$%^^%^```-=?=",
Name: "gitea",
Param: "",
SSLMode: "false",
Output: "postgres://testuser:space%20space%20%21%23$%25%5E%5E%25%5E%60%60%60-=%3F=@:5432/gitea?host=%2Ftmp%2Fpg.sock&sslmode=false",
},
{
Host: "localhost",
Port: "1234",
User: "pgsqlusername",
Passwd: "I love Gitea!",
Name: "gitea",
Param: "",
SSLMode: "true",
Output: "postgres://pgsqlusername:I%20love%20Gitea%21@localhost:5432/gitea?sslmode=true",
},
{
Host: "localhost:1234",
User: "user",
Passwd: "pass",
Name: "gitea?param=1",
Output: "postgres://user:pass@localhost:1234/gitea?param=1&sslmode=",
},
}

for _, test := range tests {
connStr := getPostgreSQLConnectionString(test.Host, test.User, test.Passwd, test.Name, test.Param, test.SSLMode)
connStr := getPostgreSQLConnectionString(test.Host, test.User, test.Passwd, test.Name, test.SSLMode)
assert.Equal(t, test.Output, connStr)
}
}
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
6 changes: 3 additions & 3 deletions services/webhook/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,14 @@ 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)
packageLink := MatrixLinkFormatter(p.Package.HTMLURL, p.Package.Name)
var text string

switch p.Action {
case api.HookPackageCreated:
text = fmt.Sprintf("[%s] Package published by %s", repoLink, senderLink)
text = fmt.Sprintf("[%s] Package published by %s", packageLink, senderLink)
case api.HookPackageDeleted:
text = fmt.Sprintf("[%s] Package deleted by %s", repoLink, senderLink)
text = fmt.Sprintf("[%s] Package deleted by %s", packageLink, senderLink)
}

return getMatrixPayload(text, nil, m.MsgType), nil
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