2020-05-01 20:20:51 -04:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-05-01 20:20:51 -04:00
|
|
|
|
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
2022-01-19 18:26:57 -05:00
|
|
|
"context"
|
2020-05-01 20:20:51 -04:00
|
|
|
"strings"
|
|
|
|
|
2022-06-13 05:37:59 -04:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-05-01 20:20:51 -04:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ToPullReview convert a review to api format
|
2022-06-13 05:37:59 -04:00
|
|
|
func ToPullReview(ctx context.Context, r *issues_model.Review, doer *user_model.User) (*api.PullReview, error) {
|
2022-01-19 18:26:57 -05:00
|
|
|
if err := r.LoadAttributes(ctx); err != nil {
|
2021-11-24 04:49:20 -05:00
|
|
|
if !user_model.IsErrUserNotExist(err) {
|
2020-05-01 20:20:51 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-24 04:49:20 -05:00
|
|
|
r.Reviewer = user_model.NewGhostUser()
|
2020-05-01 20:20:51 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
apiTeam, err := ToTeam(ctx, r.ReviewerTeam)
|
2022-05-13 13:27:58 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-01 20:20:51 -04: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 08:37:34 -05:00
|
|
|
Reviewer: ToUser(ctx, r.Reviewer, doer),
|
2022-05-13 13:27:58 -04:00
|
|
|
ReviewerTeam: apiTeam,
|
2020-05-01 20:20:51 -04:00
|
|
|
State: api.ReviewStateUnknown,
|
|
|
|
Body: r.Content,
|
|
|
|
CommitID: r.CommitID,
|
|
|
|
Stale: r.Stale,
|
|
|
|
Official: r.Official,
|
2021-02-11 12:32:25 -05:00
|
|
|
Dismissed: r.Dismissed,
|
2020-05-01 20:20:51 -04:00
|
|
|
CodeCommentsCount: r.GetCodeCommentsCount(),
|
|
|
|
Submitted: r.CreatedUnix.AsTime(),
|
2022-11-15 04:33:52 -05:00
|
|
|
Updated: r.UpdatedUnix.AsTime(),
|
2020-05-01 20:20:51 -04:00
|
|
|
HTMLURL: r.HTMLURL(),
|
|
|
|
HTMLPullURL: r.Issue.HTMLURL(),
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.Type {
|
2022-06-13 05:37:59 -04:00
|
|
|
case issues_model.ReviewTypeApprove:
|
2020-05-01 20:20:51 -04:00
|
|
|
result.State = api.ReviewStateApproved
|
2022-06-13 05:37:59 -04:00
|
|
|
case issues_model.ReviewTypeReject:
|
2020-05-01 20:20:51 -04:00
|
|
|
result.State = api.ReviewStateRequestChanges
|
2022-06-13 05:37:59 -04:00
|
|
|
case issues_model.ReviewTypeComment:
|
2020-05-01 20:20:51 -04:00
|
|
|
result.State = api.ReviewStateComment
|
2022-06-13 05:37:59 -04:00
|
|
|
case issues_model.ReviewTypePending:
|
2020-05-01 20:20:51 -04:00
|
|
|
result.State = api.ReviewStatePending
|
2022-06-13 05:37:59 -04:00
|
|
|
case issues_model.ReviewTypeRequest:
|
2020-05-01 20:20:51 -04:00
|
|
|
result.State = api.ReviewStateRequestReview
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToPullReviewList convert a list of review to it's api format
|
2022-06-13 05:37:59 -04:00
|
|
|
func ToPullReviewList(ctx context.Context, rl []*issues_model.Review, doer *user_model.User) ([]*api.PullReview, error) {
|
2020-05-01 20:20:51 -04:00
|
|
|
result := make([]*api.PullReview, 0, len(rl))
|
|
|
|
for i := range rl {
|
|
|
|
// show pending reviews only for the user who created them
|
2022-06-13 05:37:59 -04:00
|
|
|
if rl[i].Type == issues_model.ReviewTypePending && !(doer.IsAdmin || doer.ID == rl[i].ReviewerID) {
|
2020-05-01 20:20:51 -04:00
|
|
|
continue
|
|
|
|
}
|
2022-01-19 18:26:57 -05:00
|
|
|
r, err := ToPullReview(ctx, rl[i], doer)
|
2020-05-01 20:20:51 -04: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 05:37:59 -04:00
|
|
|
func ToPullReviewCommentList(ctx context.Context, review *issues_model.Review, doer *user_model.User) ([]*api.PullReviewComment, error) {
|
2022-01-19 18:26:57 -05:00
|
|
|
if err := review.LoadAttributes(ctx); err != nil {
|
2021-11-24 04:49:20 -05:00
|
|
|
if !user_model.IsErrUserNotExist(err) {
|
2020-05-01 20:20:51 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-24 04:49:20 -05:00
|
|
|
review.Reviewer = user_model.NewGhostUser()
|
2020-05-01 20:20:51 -04: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 08:37:34 -05:00
|
|
|
Poster: ToUser(ctx, comment.Poster, doer),
|
|
|
|
Resolver: ToUser(ctx, comment.ResolveDoer, doer),
|
2021-03-25 22:46:41 -04:00
|
|
|
ReviewID: review.ID,
|
2020-05-01 20:20:51 -04:00
|
|
|
Created: comment.CreatedUnix.AsTime(),
|
|
|
|
Updated: comment.UpdatedUnix.AsTime(),
|
|
|
|
Path: comment.TreePath,
|
|
|
|
CommitID: comment.CommitSHA,
|
|
|
|
OrigCommitID: comment.OldRef,
|
F3: Forgejo driver and CLI
user, topic, project, label, milestone, repository, pull_request,
release, asset, comment, reaction, review providers
Signed-off-by: Earl Warren <contact@earl-warren.org>
Preserve file size when creating attachments
Introduced in c6f50297084ebd9ec8b8c25370b9b963167274eb
repoList.LoadAttributes has a ctx argument now
Rename `repo.GetOwner` to `repo.LoadOwner`
bd66fa586a0da58c4cf2f5f8390aef4bac9d0527
upgrade to the latest gof3
(cherry picked from commit c77071365629984c1dc39a7a83e7252fd5b298e2)
[F3] ID remapping logic is in place, remove workaround
(cherry picked from commit d0fee301670c37c0e73afb271e0a8dd6b622f6f6)
[F3] it is experimental, do not enable by default
(cherry picked from commit de325b21d0adad199ec05652cb8d9fff19248ddb)
(cherry picked from commit 547e7b3c40f15766deb569cf2acface3290cf092)
(cherry picked from commit 820df3a56bc194645b482ef77a8845255d1185fe)
(cherry picked from commit eaba87689bbea84a215558033fc7d514b1b44f3e)
(cherry picked from commit 1b86896b3b4144254ed27064a167650b4e12c690)
(cherry picked from commit 0046aac1c639e021e719408e374cfc84fcbaa1d8)
(cherry picked from commit f14220df8ff692bdcfdcc94660acf64c77e732f5)
(cherry picked from commit 559b73100149978173b0ca8085280cc7fb79982f)
2022-09-06 00:35:43 -04:00
|
|
|
DiffHunk: Patch2diff(comment.Patch),
|
2020-05-01 20:20:51 -04:00
|
|
|
HTMLURL: comment.HTMLURL(),
|
2020-05-20 22:41:30 -04:00
|
|
|
HTMLPullURL: review.Issue.HTMLURL(),
|
2020-05-01 20:20:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if comment.Line < 0 {
|
|
|
|
apiComment.OldLineNum = comment.UnsignedLine()
|
|
|
|
} else {
|
|
|
|
apiComment.LineNum = comment.UnsignedLine()
|
|
|
|
}
|
|
|
|
apiComments = append(apiComments, apiComment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return apiComments, nil
|
|
|
|
}
|
|
|
|
|
F3: Forgejo driver and CLI
user, topic, project, label, milestone, repository, pull_request,
release, asset, comment, reaction, review providers
Signed-off-by: Earl Warren <contact@earl-warren.org>
Preserve file size when creating attachments
Introduced in c6f50297084ebd9ec8b8c25370b9b963167274eb
repoList.LoadAttributes has a ctx argument now
Rename `repo.GetOwner` to `repo.LoadOwner`
bd66fa586a0da58c4cf2f5f8390aef4bac9d0527
upgrade to the latest gof3
(cherry picked from commit c77071365629984c1dc39a7a83e7252fd5b298e2)
[F3] ID remapping logic is in place, remove workaround
(cherry picked from commit d0fee301670c37c0e73afb271e0a8dd6b622f6f6)
[F3] it is experimental, do not enable by default
(cherry picked from commit de325b21d0adad199ec05652cb8d9fff19248ddb)
(cherry picked from commit 547e7b3c40f15766deb569cf2acface3290cf092)
(cherry picked from commit 820df3a56bc194645b482ef77a8845255d1185fe)
(cherry picked from commit eaba87689bbea84a215558033fc7d514b1b44f3e)
(cherry picked from commit 1b86896b3b4144254ed27064a167650b4e12c690)
(cherry picked from commit 0046aac1c639e021e719408e374cfc84fcbaa1d8)
(cherry picked from commit f14220df8ff692bdcfdcc94660acf64c77e732f5)
(cherry picked from commit 559b73100149978173b0ca8085280cc7fb79982f)
2022-09-06 00:35:43 -04:00
|
|
|
func Patch2diff(patch string) string {
|
2020-05-01 20:20:51 -04:00
|
|
|
split := strings.Split(patch, "\n@@")
|
|
|
|
if len(split) == 2 {
|
|
|
|
return "@@" + split[1]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|