2020-01-10 04:34:21 -05:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-10 04:34:21 -05:00
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2020-03-27 07:34:39 -05:00
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
2023-05-13 13:59:11 -05:00
|
|
|
"strconv"
|
2020-01-10 04:34:21 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 08:37:34 -05:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-09 20:27:50 -05:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2022-10-16 18:29:26 -05:00
|
|
|
system_model "code.gitea.io/gitea/models/system"
|
2021-11-12 09:36:47 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2020-01-10 04:34:21 -05:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2022-10-16 18:29:26 -05:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-11-17 07:34:35 -05:00
|
|
|
|
2020-01-10 04:34:21 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2020-01-10 04:34:21 -05:00
|
|
|
|
|
|
|
pushCommits := NewPushCommits()
|
|
|
|
pushCommits.Commits = []*PushCommit{
|
|
|
|
{
|
|
|
|
Sha1: "69554a6",
|
|
|
|
CommitterEmail: "user2@example.com",
|
|
|
|
CommitterName: "User2",
|
|
|
|
AuthorEmail: "user2@example.com",
|
|
|
|
AuthorName: "User2",
|
|
|
|
Message: "not signed commit",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Sha1: "27566bd",
|
|
|
|
CommitterEmail: "user2@example.com",
|
|
|
|
CommitterName: "User2",
|
|
|
|
AuthorEmail: "user2@example.com",
|
|
|
|
AuthorName: "User2",
|
|
|
|
Message: "good signed commit (with not yet validated email)",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Sha1: "5099b81",
|
|
|
|
CommitterEmail: "user2@example.com",
|
|
|
|
CommitterName: "User2",
|
|
|
|
AuthorEmail: "user2@example.com",
|
|
|
|
AuthorName: "User2",
|
|
|
|
Message: "good signed commit",
|
|
|
|
},
|
|
|
|
}
|
2021-06-29 08:34:03 -05:00
|
|
|
pushCommits.HeadCommit = &PushCommit{Sha1: "69554a6"}
|
2020-01-10 04:34:21 -05:00
|
|
|
|
2022-08-15 21:22:25 -05:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 16})
|
2022-01-19 18:26:57 -05:00
|
|
|
payloadCommits, headCommit, err := pushCommits.ToAPIPayloadCommits(git.DefaultContext, repo.RepoPath(), "/user2/repo16")
|
2020-01-10 04:34:21 -05:00
|
|
|
assert.NoError(t, err)
|
2021-06-07 00:27:09 -05:00
|
|
|
assert.Len(t, payloadCommits, 3)
|
2021-06-29 08:34:03 -05:00
|
|
|
assert.NotNil(t, headCommit)
|
2020-01-10 04:34:21 -05:00
|
|
|
|
|
|
|
assert.Equal(t, "69554a6", payloadCommits[0].ID)
|
|
|
|
assert.Equal(t, "not signed commit", payloadCommits[0].Message)
|
|
|
|
assert.Equal(t, "/user2/repo16/commit/69554a6", payloadCommits[0].URL)
|
|
|
|
assert.Equal(t, "User2", payloadCommits[0].Committer.Name)
|
|
|
|
assert.Equal(t, "user2", payloadCommits[0].Committer.UserName)
|
|
|
|
assert.Equal(t, "User2", payloadCommits[0].Author.Name)
|
|
|
|
assert.Equal(t, "user2", payloadCommits[0].Author.UserName)
|
|
|
|
assert.EqualValues(t, []string{}, payloadCommits[0].Added)
|
|
|
|
assert.EqualValues(t, []string{}, payloadCommits[0].Removed)
|
|
|
|
assert.EqualValues(t, []string{"readme.md"}, payloadCommits[0].Modified)
|
|
|
|
|
|
|
|
assert.Equal(t, "27566bd", payloadCommits[1].ID)
|
|
|
|
assert.Equal(t, "good signed commit (with not yet validated email)", payloadCommits[1].Message)
|
|
|
|
assert.Equal(t, "/user2/repo16/commit/27566bd", payloadCommits[1].URL)
|
|
|
|
assert.Equal(t, "User2", payloadCommits[1].Committer.Name)
|
|
|
|
assert.Equal(t, "user2", payloadCommits[1].Committer.UserName)
|
|
|
|
assert.Equal(t, "User2", payloadCommits[1].Author.Name)
|
|
|
|
assert.Equal(t, "user2", payloadCommits[1].Author.UserName)
|
|
|
|
assert.EqualValues(t, []string{}, payloadCommits[1].Added)
|
|
|
|
assert.EqualValues(t, []string{}, payloadCommits[1].Removed)
|
|
|
|
assert.EqualValues(t, []string{"readme.md"}, payloadCommits[1].Modified)
|
|
|
|
|
|
|
|
assert.Equal(t, "5099b81", payloadCommits[2].ID)
|
|
|
|
assert.Equal(t, "good signed commit", payloadCommits[2].Message)
|
|
|
|
assert.Equal(t, "/user2/repo16/commit/5099b81", payloadCommits[2].URL)
|
|
|
|
assert.Equal(t, "User2", payloadCommits[2].Committer.Name)
|
|
|
|
assert.Equal(t, "user2", payloadCommits[2].Committer.UserName)
|
|
|
|
assert.Equal(t, "User2", payloadCommits[2].Author.Name)
|
|
|
|
assert.Equal(t, "user2", payloadCommits[2].Author.UserName)
|
|
|
|
assert.EqualValues(t, []string{"readme.md"}, payloadCommits[2].Added)
|
|
|
|
assert.EqualValues(t, []string{}, payloadCommits[2].Removed)
|
|
|
|
assert.EqualValues(t, []string{}, payloadCommits[2].Modified)
|
2021-06-29 08:34:03 -05:00
|
|
|
|
|
|
|
assert.Equal(t, "69554a6", headCommit.ID)
|
|
|
|
assert.Equal(t, "not signed commit", headCommit.Message)
|
|
|
|
assert.Equal(t, "/user2/repo16/commit/69554a6", headCommit.URL)
|
|
|
|
assert.Equal(t, "User2", headCommit.Committer.Name)
|
|
|
|
assert.Equal(t, "user2", headCommit.Committer.UserName)
|
|
|
|
assert.Equal(t, "User2", headCommit.Author.Name)
|
|
|
|
assert.Equal(t, "user2", headCommit.Author.UserName)
|
|
|
|
assert.EqualValues(t, []string{}, headCommit.Added)
|
|
|
|
assert.EqualValues(t, []string{}, headCommit.Removed)
|
|
|
|
assert.EqualValues(t, []string{"readme.md"}, headCommit.Modified)
|
2020-01-10 04:34:21 -05:00
|
|
|
}
|
|
|
|
|
2022-10-16 18:29:26 -05:00
|
|
|
func enableGravatar(t *testing.T) {
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 08:37:34 -05:00
|
|
|
err := system_model.SetSettingNoVersion(db.DefaultContext, system_model.KeyPictureDisableGravatar, "false")
|
2022-10-16 18:29:26 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
setting.GravatarSource = "https://secure.gravatar.com/avatar"
|
2023-02-24 05:23:13 -05:00
|
|
|
err = system_model.Init(db.DefaultContext)
|
2022-10-16 18:29:26 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-01-10 04:34:21 -05:00
|
|
|
func TestPushCommits_AvatarLink(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2020-01-10 04:34:21 -05:00
|
|
|
|
|
|
|
pushCommits := NewPushCommits()
|
|
|
|
pushCommits.Commits = []*PushCommit{
|
|
|
|
{
|
|
|
|
Sha1: "abcdef1",
|
|
|
|
CommitterEmail: "user2@example.com",
|
|
|
|
CommitterName: "User Two",
|
|
|
|
AuthorEmail: "user4@example.com",
|
|
|
|
AuthorName: "User Four",
|
|
|
|
Message: "message1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Sha1: "abcdef2",
|
|
|
|
CommitterEmail: "user2@example.com",
|
|
|
|
CommitterName: "User Two",
|
|
|
|
AuthorEmail: "user2@example.com",
|
|
|
|
AuthorName: "User Two",
|
|
|
|
Message: "message2",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-10-16 18:29:26 -05:00
|
|
|
enableGravatar(t)
|
|
|
|
|
2020-01-10 04:34:21 -05:00
|
|
|
assert.Equal(t,
|
2023-05-13 13:59:11 -05:00
|
|
|
"https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon&s="+strconv.Itoa(28*setting.Avatar.RenderedSizeFactor),
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 08:37:34 -05:00
|
|
|
pushCommits.AvatarLink(db.DefaultContext, "user2@example.com"))
|
2020-01-10 04:34:21 -05:00
|
|
|
|
|
|
|
assert.Equal(t,
|
2023-05-13 13:59:11 -05:00
|
|
|
fmt.Sprintf("https://secure.gravatar.com/avatar/%x?d=identicon&s=%d", md5.Sum([]byte("nonexistent@example.com")), 28*setting.Avatar.RenderedSizeFactor),
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 08:37:34 -05:00
|
|
|
pushCommits.AvatarLink(db.DefaultContext, "nonexistent@example.com"))
|
2020-01-10 04:34:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommitToPushCommit(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
sig := &git.Signature{
|
|
|
|
Email: "example@example.com",
|
|
|
|
Name: "John Doe",
|
|
|
|
When: now,
|
|
|
|
}
|
|
|
|
const hexString = "0123456789abcdef0123456789abcdef01234567"
|
|
|
|
sha1, err := git.NewIDFromString(hexString)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
pushCommit := CommitToPushCommit(&git.Commit{
|
|
|
|
ID: sha1,
|
|
|
|
Author: sig,
|
|
|
|
Committer: sig,
|
|
|
|
CommitMessage: "Commit Message",
|
|
|
|
})
|
|
|
|
assert.Equal(t, hexString, pushCommit.Sha1)
|
|
|
|
assert.Equal(t, "Commit Message", pushCommit.Message)
|
|
|
|
assert.Equal(t, "example@example.com", pushCommit.AuthorEmail)
|
|
|
|
assert.Equal(t, "John Doe", pushCommit.AuthorName)
|
|
|
|
assert.Equal(t, "example@example.com", pushCommit.CommitterEmail)
|
|
|
|
assert.Equal(t, "John Doe", pushCommit.CommitterName)
|
|
|
|
assert.Equal(t, now, pushCommit.Timestamp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListToPushCommits(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
sig := &git.Signature{
|
|
|
|
Email: "example@example.com",
|
|
|
|
Name: "John Doe",
|
|
|
|
When: now,
|
|
|
|
}
|
|
|
|
|
|
|
|
const hexString1 = "0123456789abcdef0123456789abcdef01234567"
|
|
|
|
hash1, err := git.NewIDFromString(hexString1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
const hexString2 = "fedcba9876543210fedcba9876543210fedcba98"
|
|
|
|
hash2, err := git.NewIDFromString(hexString2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-08-09 13:08:51 -05:00
|
|
|
l := []*git.Commit{
|
|
|
|
{
|
|
|
|
ID: hash1,
|
|
|
|
Author: sig,
|
|
|
|
Committer: sig,
|
|
|
|
CommitMessage: "Message1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: hash2,
|
|
|
|
Author: sig,
|
|
|
|
Committer: sig,
|
|
|
|
CommitMessage: "Message2",
|
|
|
|
},
|
|
|
|
}
|
2020-01-10 04:34:21 -05:00
|
|
|
|
2021-08-09 13:08:51 -05:00
|
|
|
pushCommits := GitToPushCommits(l)
|
2020-01-10 04:34:21 -05:00
|
|
|
if assert.Len(t, pushCommits.Commits, 2) {
|
|
|
|
assert.Equal(t, "Message1", pushCommits.Commits[0].Message)
|
|
|
|
assert.Equal(t, hexString1, pushCommits.Commits[0].Sha1)
|
|
|
|
assert.Equal(t, "example@example.com", pushCommits.Commits[0].AuthorEmail)
|
|
|
|
assert.Equal(t, now, pushCommits.Commits[0].Timestamp)
|
|
|
|
|
|
|
|
assert.Equal(t, "Message2", pushCommits.Commits[1].Message)
|
|
|
|
assert.Equal(t, hexString2, pushCommits.Commits[1].Sha1)
|
|
|
|
assert.Equal(t, "example@example.com", pushCommits.Commits[1].AuthorEmail)
|
|
|
|
assert.Equal(t, now, pushCommits.Commits[1].Timestamp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO TestPushUpdate
|