2020-01-10 09:34:21 +00:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-10 09:34:21 +00:00
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2022-01-19 23:26:57 +00:00
|
|
|
"context"
|
2020-01-10 09:34:21 +00:00
|
|
|
"fmt"
|
2021-11-16 18:18:25 +00:00
|
|
|
"net/url"
|
2020-01-10 09:34:21 +00:00
|
|
|
"time"
|
|
|
|
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-05 23:25:46 +00:00
|
|
|
"code.gitea.io/gitea/models/avatars"
|
2021-11-24 09:49:20 +00:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2023-09-13 07:15:00 +00:00
|
|
|
"code.gitea.io/gitea/modules/cache"
|
2020-01-10 09:34:21 +00:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-12-16 02:18:38 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-01-10 09:34:21 +00:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PushCommit represents a commit in a push operation.
|
|
|
|
type PushCommit struct {
|
|
|
|
Sha1 string
|
|
|
|
Message string
|
|
|
|
AuthorEmail string
|
|
|
|
AuthorName string
|
|
|
|
CommitterEmail string
|
|
|
|
CommitterName string
|
|
|
|
Timestamp time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// PushCommits represents list of commits in a push operation.
|
|
|
|
type PushCommits struct {
|
|
|
|
Commits []*PushCommit
|
2021-06-29 13:34:03 +00:00
|
|
|
HeadCommit *PushCommit
|
2020-01-10 09:34:21 +00:00
|
|
|
CompareURL string
|
2021-08-25 23:04:58 +00:00
|
|
|
Len int
|
2020-01-10 09:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPushCommits creates a new PushCommits object.
|
|
|
|
func NewPushCommits() *PushCommits {
|
2023-09-13 07:15:00 +00:00
|
|
|
return &PushCommits{}
|
2020-01-10 09:34:21 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 13:34:03 +00:00
|
|
|
// toAPIPayloadCommit converts a single PushCommit to an api.PayloadCommit object.
|
2023-09-13 07:15:00 +00:00
|
|
|
func (pc *PushCommits) toAPIPayloadCommit(ctx context.Context, emailUsers map[string]*user_model.User, repoPath, repoLink string, commit *PushCommit) (*api.PayloadCommit, error) {
|
2020-01-10 09:34:21 +00:00
|
|
|
var err error
|
2021-06-29 13:34:03 +00:00
|
|
|
authorUsername := ""
|
2023-09-13 07:15:00 +00:00
|
|
|
author, ok := emailUsers[commit.AuthorEmail]
|
2021-06-29 13:34:03 +00:00
|
|
|
if !ok {
|
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 13:37:34 +00:00
|
|
|
author, err = user_model.GetUserByEmail(ctx, commit.AuthorEmail)
|
2021-06-29 13:34:03 +00:00
|
|
|
if err == nil {
|
2020-01-10 09:34:21 +00:00
|
|
|
authorUsername = author.Name
|
2023-09-13 07:15:00 +00:00
|
|
|
emailUsers[commit.AuthorEmail] = author
|
2020-01-10 09:34:21 +00:00
|
|
|
}
|
2021-06-29 13:34:03 +00:00
|
|
|
} else {
|
|
|
|
authorUsername = author.Name
|
|
|
|
}
|
2020-01-10 09:34:21 +00:00
|
|
|
|
2021-06-29 13:34:03 +00:00
|
|
|
committerUsername := ""
|
2023-09-13 07:15:00 +00:00
|
|
|
committer, ok := emailUsers[commit.CommitterEmail]
|
2021-06-29 13:34:03 +00:00
|
|
|
if !ok {
|
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 13:37:34 +00:00
|
|
|
committer, err = user_model.GetUserByEmail(ctx, commit.CommitterEmail)
|
2021-06-29 13:34:03 +00:00
|
|
|
if err == nil {
|
|
|
|
// TODO: check errors other than email not found.
|
2020-01-10 09:34:21 +00:00
|
|
|
committerUsername = committer.Name
|
2023-09-13 07:15:00 +00:00
|
|
|
emailUsers[commit.CommitterEmail] = committer
|
2020-01-10 09:34:21 +00:00
|
|
|
}
|
2021-06-29 13:34:03 +00:00
|
|
|
} else {
|
|
|
|
committerUsername = committer.Name
|
|
|
|
}
|
2020-01-10 09:34:21 +00:00
|
|
|
|
2022-01-19 23:26:57 +00:00
|
|
|
fileStatus, err := git.GetCommitFileStatus(ctx, repoPath, commit.Sha1)
|
2021-06-29 13:34:03 +00:00
|
|
|
if err != nil {
|
2022-10-24 19:29:17 +00:00
|
|
|
return nil, fmt.Errorf("FileStatus [commit_sha1: %s]: %w", commit.Sha1, err)
|
2021-06-29 13:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &api.PayloadCommit{
|
|
|
|
ID: commit.Sha1,
|
|
|
|
Message: commit.Message,
|
2021-11-16 18:18:25 +00:00
|
|
|
URL: fmt.Sprintf("%s/commit/%s", repoLink, url.PathEscape(commit.Sha1)),
|
2021-06-29 13:34:03 +00:00
|
|
|
Author: &api.PayloadUser{
|
|
|
|
Name: commit.AuthorName,
|
|
|
|
Email: commit.AuthorEmail,
|
|
|
|
UserName: authorUsername,
|
|
|
|
},
|
|
|
|
Committer: &api.PayloadUser{
|
|
|
|
Name: commit.CommitterName,
|
|
|
|
Email: commit.CommitterEmail,
|
|
|
|
UserName: committerUsername,
|
|
|
|
},
|
|
|
|
Added: fileStatus.Added,
|
|
|
|
Removed: fileStatus.Removed,
|
|
|
|
Modified: fileStatus.Modified,
|
|
|
|
Timestamp: commit.Timestamp,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToAPIPayloadCommits converts a PushCommits object to api.PayloadCommit format.
|
|
|
|
// It returns all converted commits and, if provided, the head commit or an error otherwise.
|
2022-01-19 23:26:57 +00:00
|
|
|
func (pc *PushCommits) ToAPIPayloadCommits(ctx context.Context, repoPath, repoLink string) ([]*api.PayloadCommit, *api.PayloadCommit, error) {
|
2021-06-29 13:34:03 +00:00
|
|
|
commits := make([]*api.PayloadCommit, len(pc.Commits))
|
|
|
|
var headCommit *api.PayloadCommit
|
|
|
|
|
2023-09-13 07:15:00 +00:00
|
|
|
emailUsers := make(map[string]*user_model.User)
|
|
|
|
|
2021-06-29 13:34:03 +00:00
|
|
|
for i, commit := range pc.Commits {
|
2023-09-13 07:15:00 +00:00
|
|
|
apiCommit, err := pc.toAPIPayloadCommit(ctx, emailUsers, repoPath, repoLink, commit)
|
2020-01-10 09:34:21 +00:00
|
|
|
if err != nil {
|
2021-06-29 13:34:03 +00:00
|
|
|
return nil, nil, err
|
2020-01-10 09:34:21 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 13:34:03 +00:00
|
|
|
commits[i] = apiCommit
|
|
|
|
if pc.HeadCommit != nil && pc.HeadCommit.Sha1 == commits[i].ID {
|
|
|
|
headCommit = apiCommit
|
2020-01-10 09:34:21 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-29 13:34:03 +00:00
|
|
|
if pc.HeadCommit != nil && headCommit == nil {
|
|
|
|
var err error
|
2023-09-13 07:15:00 +00:00
|
|
|
headCommit, err = pc.toAPIPayloadCommit(ctx, emailUsers, repoPath, repoLink, pc.HeadCommit)
|
2021-06-29 13:34:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return commits, headCommit, nil
|
2020-01-10 09:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AvatarLink tries to match user in database with e-mail
|
|
|
|
// in order to show custom avatar, and falls back to general avatar link.
|
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 13:37:34 +00:00
|
|
|
func (pc *PushCommits) AvatarLink(ctx context.Context, email string) string {
|
2021-12-16 02:18:38 +00:00
|
|
|
size := avatars.DefaultAvatarPixelSize * setting.Avatar.RenderedSizeFactor
|
2020-12-09 00:12:15 +00:00
|
|
|
|
2023-09-13 07:15:00 +00:00
|
|
|
v, _ := cache.GetWithContextCache(ctx, "push_commits", email, func() (string, error) {
|
|
|
|
u, err := user_model.GetUserByEmail(ctx, email)
|
2020-01-10 09:34:21 +00:00
|
|
|
if err != nil {
|
2021-11-24 09:49:20 +00:00
|
|
|
if !user_model.IsErrUserNotExist(err) {
|
2020-01-10 09:34:21 +00:00
|
|
|
log.Error("GetUserByEmail: %v", err)
|
2023-09-13 07:15:00 +00:00
|
|
|
return "", err
|
2020-01-10 09:34:21 +00:00
|
|
|
}
|
2023-09-13 07:15:00 +00:00
|
|
|
return avatars.GenerateEmailAvatarFastLink(ctx, email, size), nil
|
2020-01-10 09:34:21 +00:00
|
|
|
}
|
2023-09-13 07:15:00 +00:00
|
|
|
return u.AvatarLinkWithSize(ctx, size), nil
|
|
|
|
})
|
2020-01-10 09:34:21 +00:00
|
|
|
|
2023-09-13 07:15:00 +00:00
|
|
|
return v
|
2020-01-10 09:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CommitToPushCommit transforms a git.Commit to PushCommit type.
|
|
|
|
func CommitToPushCommit(commit *git.Commit) *PushCommit {
|
|
|
|
return &PushCommit{
|
|
|
|
Sha1: commit.ID.String(),
|
|
|
|
Message: commit.Message(),
|
|
|
|
AuthorEmail: commit.Author.Email,
|
|
|
|
AuthorName: commit.Author.Name,
|
|
|
|
CommitterEmail: commit.Committer.Email,
|
|
|
|
CommitterName: commit.Committer.Name,
|
|
|
|
Timestamp: commit.Author.When,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-09 18:08:51 +00:00
|
|
|
// GitToPushCommits transforms a list of git.Commits to PushCommits type.
|
|
|
|
func GitToPushCommits(gitCommits []*git.Commit) *PushCommits {
|
|
|
|
commits := make([]*PushCommit, 0, len(gitCommits))
|
|
|
|
for _, commit := range gitCommits {
|
|
|
|
commits = append(commits, CommitToPushCommit(commit))
|
2020-01-10 09:34:21 +00:00
|
|
|
}
|
2021-08-25 23:04:58 +00:00
|
|
|
return &PushCommits{
|
|
|
|
Commits: commits,
|
|
|
|
HeadCommit: nil,
|
|
|
|
CompareURL: "",
|
|
|
|
Len: len(commits),
|
|
|
|
}
|
2020-01-10 09:34:21 +00:00
|
|
|
}
|