2020-05-02 08:20:51 +08:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-05-02 08:20:51 +08:00
|
|
|
|
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
2022-01-20 07:26:57 +08:00
|
|
|
"context"
|
2020-05-02 08:20:51 +08:00
|
|
|
"strings"
|
|
|
|
|
2022-06-13 17:37:59 +08:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-05-02 08:20:51 +08:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ToPullReview convert a review to api format
|
2022-06-13 17:37:59 +08:00
|
|
|
func ToPullReview(ctx context.Context, r *issues_model.Review, doer *user_model.User) (*api.PullReview, error) {
|
2022-01-20 07:26:57 +08:00
|
|
|
if err := r.LoadAttributes(ctx); err != nil {
|
2021-11-24 17:49:20 +08:00
|
|
|
if !user_model.IsErrUserNotExist(err) {
|
2020-05-02 08:20:51 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-24 17:49:20 +08:00
|
|
|
r.Reviewer = user_model.NewGhostUser()
|
2020-05-02 08:20:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
result := &api.PullReview{
|
|
|
|
ID: r.ID,
|
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 21:37:34 +08:00
|
|
|
Reviewer: ToUser(ctx, r.Reviewer, doer),
|
2020-05-02 08:20:51 +08:00
|
|
|
State: api.ReviewStateUnknown,
|
|
|
|
Body: r.Content,
|
|
|
|
CommitID: r.CommitID,
|
|
|
|
Stale: r.Stale,
|
|
|
|
Official: r.Official,
|
2021-02-12 01:32:25 +08:00
|
|
|
Dismissed: r.Dismissed,
|
2023-09-29 21:35:01 +08:00
|
|
|
CodeCommentsCount: r.GetCodeCommentsCount(ctx),
|
2020-05-02 08:20:51 +08:00
|
|
|
Submitted: r.CreatedUnix.AsTime(),
|
2022-11-15 17:33:52 +08:00
|
|
|
Updated: r.UpdatedUnix.AsTime(),
|
2023-09-29 21:35:01 +08:00
|
|
|
HTMLURL: r.HTMLURL(ctx),
|
2020-05-02 08:20:51 +08:00
|
|
|
HTMLPullURL: r.Issue.HTMLURL(),
|
|
|
|
}
|
|
|
|
|
2024-01-12 15:12:18 +08:00
|
|
|
if r.ReviewerTeam != nil {
|
|
|
|
var err error
|
|
|
|
result.ReviewerTeam, err = ToTeam(ctx, r.ReviewerTeam)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 08:20:51 +08:00
|
|
|
switch r.Type {
|
2022-06-13 17:37:59 +08:00
|
|
|
case issues_model.ReviewTypeApprove:
|
2020-05-02 08:20:51 +08:00
|
|
|
result.State = api.ReviewStateApproved
|
2022-06-13 17:37:59 +08:00
|
|
|
case issues_model.ReviewTypeReject:
|
2020-05-02 08:20:51 +08:00
|
|
|
result.State = api.ReviewStateRequestChanges
|
2022-06-13 17:37:59 +08:00
|
|
|
case issues_model.ReviewTypeComment:
|
2020-05-02 08:20:51 +08:00
|
|
|
result.State = api.ReviewStateComment
|
2022-06-13 17:37:59 +08:00
|
|
|
case issues_model.ReviewTypePending:
|
2020-05-02 08:20:51 +08:00
|
|
|
result.State = api.ReviewStatePending
|
2022-06-13 17:37:59 +08:00
|
|
|
case issues_model.ReviewTypeRequest:
|
2020-05-02 08:20:51 +08:00
|
|
|
result.State = api.ReviewStateRequestReview
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToPullReviewList convert a list of review to it's api format
|
2022-06-13 17:37:59 +08:00
|
|
|
func ToPullReviewList(ctx context.Context, rl []*issues_model.Review, doer *user_model.User) ([]*api.PullReview, error) {
|
2020-05-02 08:20:51 +08:00
|
|
|
result := make([]*api.PullReview, 0, len(rl))
|
|
|
|
for i := range rl {
|
|
|
|
// show pending reviews only for the user who created them
|
2024-03-14 04:42:17 +08:00
|
|
|
if rl[i].Type == issues_model.ReviewTypePending && (doer == nil || (!doer.IsAdmin && doer.ID != rl[i].ReviewerID)) {
|
2020-05-02 08:20:51 +08:00
|
|
|
continue
|
|
|
|
}
|
2022-01-20 07:26:57 +08:00
|
|
|
r, err := ToPullReview(ctx, rl[i], doer)
|
2020-05-02 08:20:51 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, r)
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToPullReviewCommentList convert the CodeComments of an review to it's api format
|
2022-06-13 17:37:59 +08:00
|
|
|
func ToPullReviewCommentList(ctx context.Context, review *issues_model.Review, doer *user_model.User) ([]*api.PullReviewComment, error) {
|
2022-01-20 07:26:57 +08:00
|
|
|
if err := review.LoadAttributes(ctx); err != nil {
|
2021-11-24 17:49:20 +08:00
|
|
|
if !user_model.IsErrUserNotExist(err) {
|
2020-05-02 08:20:51 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-24 17:49:20 +08:00
|
|
|
review.Reviewer = user_model.NewGhostUser()
|
2020-05-02 08:20:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
apiComments := make([]*api.PullReviewComment, 0, len(review.CodeComments))
|
|
|
|
|
|
|
|
for _, lines := range review.CodeComments {
|
|
|
|
for _, comments := range lines {
|
|
|
|
for _, comment := range comments {
|
|
|
|
apiComment := &api.PullReviewComment{
|
|
|
|
ID: comment.ID,
|
|
|
|
Body: comment.Content,
|
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 21:37:34 +08:00
|
|
|
Poster: ToUser(ctx, comment.Poster, doer),
|
|
|
|
Resolver: ToUser(ctx, comment.ResolveDoer, doer),
|
2021-03-26 10:46:41 +08:00
|
|
|
ReviewID: review.ID,
|
2020-05-02 08:20:51 +08:00
|
|
|
Created: comment.CreatedUnix.AsTime(),
|
|
|
|
Updated: comment.UpdatedUnix.AsTime(),
|
|
|
|
Path: comment.TreePath,
|
|
|
|
CommitID: comment.CommitSHA,
|
|
|
|
OrigCommitID: comment.OldRef,
|
|
|
|
DiffHunk: patch2diff(comment.Patch),
|
2023-09-29 21:35:01 +08:00
|
|
|
HTMLURL: comment.HTMLURL(ctx),
|
2020-05-21 10:41:30 +08:00
|
|
|
HTMLPullURL: review.Issue.HTMLURL(),
|
2020-05-02 08:20:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if comment.Line < 0 {
|
|
|
|
apiComment.OldLineNum = comment.UnsignedLine()
|
|
|
|
} else {
|
|
|
|
apiComment.LineNum = comment.UnsignedLine()
|
|
|
|
}
|
|
|
|
apiComments = append(apiComments, apiComment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return apiComments, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func patch2diff(patch string) string {
|
|
|
|
split := strings.Split(patch, "\n@@")
|
|
|
|
if len(split) == 2 {
|
|
|
|
return "@@" + split[1]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|