2020-01-11 08:29:41 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2020-01-11 08:29:41 +05:30
|
|
|
|
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
2022-11-19 13:42:33 +05:30
|
|
|
"context"
|
2021-09-10 21:33:16 +05:30
|
|
|
"fmt"
|
2021-11-16 23:48:25 +05:30
|
|
|
"net/url"
|
2020-02-29 08:19:50 +05:30
|
|
|
"strings"
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-04-08 14:41:15 +05:30
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-09-10 21:33:16 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-01-11 08:29:41 +05:30
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
2020-02-29 08:19:50 +05:30
|
|
|
// ToAPIIssue converts an Issue to API format
|
|
|
|
// it assumes some fields assigned with values:
|
|
|
|
// Required - Poster, Labels,
|
|
|
|
// Optional - Milestone, Assignee, PullRequest
|
2022-11-19 13:42:33 +05:30
|
|
|
func ToAPIIssue(ctx context.Context, issue *issues_model.Issue) *api.Issue {
|
|
|
|
if err := issue.LoadLabels(ctx); err != nil {
|
2020-02-29 08:19:50 +05:30
|
|
|
return &api.Issue{}
|
|
|
|
}
|
2022-11-19 13:42:33 +05:30
|
|
|
if err := issue.LoadPoster(ctx); err != nil {
|
2020-02-29 08:19:50 +05:30
|
|
|
return &api.Issue{}
|
|
|
|
}
|
2022-11-19 13:42:33 +05:30
|
|
|
if err := issue.LoadRepo(ctx); err != nil {
|
2020-02-29 08:19:50 +05:30
|
|
|
return &api.Issue{}
|
|
|
|
}
|
2022-11-19 13:42:33 +05:30
|
|
|
if err := issue.Repo.GetOwner(ctx); err != nil {
|
2021-09-10 21:33:16 +05:30
|
|
|
return &api.Issue{}
|
|
|
|
}
|
2020-02-29 08:19:50 +05:30
|
|
|
|
|
|
|
apiIssue := &api.Issue{
|
2022-12-09 12:05:56 +05:30
|
|
|
ID: issue.ID,
|
|
|
|
URL: issue.APIURL(),
|
|
|
|
HTMLURL: issue.HTMLURL(),
|
|
|
|
Index: issue.Index,
|
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 19:07:34 +05:30
|
|
|
Poster: ToUser(ctx, issue.Poster, nil),
|
2022-12-09 12:05:56 +05:30
|
|
|
Title: issue.Title,
|
|
|
|
Body: issue.Content,
|
|
|
|
Attachments: ToAttachments(issue.Attachments),
|
|
|
|
Ref: issue.Ref,
|
|
|
|
Labels: ToLabelList(issue.Labels, issue.Repo, issue.Repo.Owner),
|
|
|
|
State: issue.State(),
|
|
|
|
IsLocked: issue.IsLocked,
|
|
|
|
Comments: issue.NumComments,
|
|
|
|
Created: issue.CreatedUnix.AsTime(),
|
|
|
|
Updated: issue.UpdatedUnix.AsTime(),
|
2020-02-29 08:19:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
apiIssue.Repo = &api.RepositoryMeta{
|
|
|
|
ID: issue.Repo.ID,
|
|
|
|
Name: issue.Repo.Name,
|
|
|
|
Owner: issue.Repo.OwnerName,
|
|
|
|
FullName: issue.Repo.FullName(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if issue.ClosedUnix != 0 {
|
|
|
|
apiIssue.Closed = issue.ClosedUnix.AsTimePtr()
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
if err := issue.LoadMilestone(ctx); err != nil {
|
2020-02-29 08:19:50 +05:30
|
|
|
return &api.Issue{}
|
|
|
|
}
|
|
|
|
if issue.Milestone != nil {
|
2020-05-13 03:24:35 +05:30
|
|
|
apiIssue.Milestone = ToAPIMilestone(issue.Milestone)
|
2020-02-29 08:19:50 +05:30
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
if err := issue.LoadAssignees(ctx); err != nil {
|
2020-02-29 08:19:50 +05:30
|
|
|
return &api.Issue{}
|
|
|
|
}
|
|
|
|
if len(issue.Assignees) > 0 {
|
|
|
|
for _, assignee := range issue.Assignees {
|
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 19:07:34 +05:30
|
|
|
apiIssue.Assignees = append(apiIssue.Assignees, ToUser(ctx, assignee, nil))
|
2020-02-29 08:19:50 +05:30
|
|
|
}
|
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 19:07:34 +05:30
|
|
|
apiIssue.Assignee = ToUser(ctx, issue.Assignees[0], nil) // For compatibility, we're keeping the first assignee as `apiIssue.Assignee`
|
2020-02-29 08:19:50 +05:30
|
|
|
}
|
|
|
|
if issue.IsPull {
|
2022-11-19 13:42:33 +05:30
|
|
|
if err := issue.LoadPullRequest(ctx); err != nil {
|
2020-02-29 08:19:50 +05:30
|
|
|
return &api.Issue{}
|
|
|
|
}
|
|
|
|
apiIssue.PullRequest = &api.PullRequestMeta{
|
|
|
|
HasMerged: issue.PullRequest.HasMerged,
|
|
|
|
}
|
|
|
|
if issue.PullRequest.HasMerged {
|
|
|
|
apiIssue.PullRequest.Merged = issue.PullRequest.MergedUnix.AsTimePtr()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if issue.DeadlineUnix != 0 {
|
|
|
|
apiIssue.Deadline = issue.DeadlineUnix.AsTimePtr()
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiIssue
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToAPIIssueList converts an IssueList to API format
|
2022-11-19 13:42:33 +05:30
|
|
|
func ToAPIIssueList(ctx context.Context, il issues_model.IssueList) []*api.Issue {
|
2020-02-29 08:19:50 +05:30
|
|
|
result := make([]*api.Issue, len(il))
|
|
|
|
for i := range il {
|
2022-11-19 13:42:33 +05:30
|
|
|
result[i] = ToAPIIssue(ctx, il[i])
|
2020-02-29 08:19:50 +05:30
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-01-11 08:29:41 +05:30
|
|
|
// ToTrackedTime converts TrackedTime to API format
|
2022-11-19 13:42:33 +05:30
|
|
|
func ToTrackedTime(ctx context.Context, t *issues_model.TrackedTime) (apiT *api.TrackedTime) {
|
2020-01-11 08:29:41 +05:30
|
|
|
apiT = &api.TrackedTime{
|
2022-12-04 23:18:18 +05:30
|
|
|
ID: t.ID,
|
|
|
|
IssueID: t.IssueID,
|
|
|
|
UserID: t.UserID,
|
|
|
|
Time: t.Time,
|
|
|
|
Created: t.Created,
|
2020-01-11 08:29:41 +05:30
|
|
|
}
|
|
|
|
if t.Issue != nil {
|
2022-11-19 13:42:33 +05:30
|
|
|
apiT.Issue = ToAPIIssue(ctx, t.Issue)
|
2020-01-11 08:29:41 +05:30
|
|
|
}
|
|
|
|
if t.User != nil {
|
|
|
|
apiT.UserName = t.User.Name
|
|
|
|
}
|
2022-06-20 15:32:49 +05:30
|
|
|
return apiT
|
2020-01-11 08:29:41 +05:30
|
|
|
}
|
|
|
|
|
2020-09-18 17:39:26 +05:30
|
|
|
// ToStopWatches convert Stopwatch list to api.StopWatches
|
2022-06-13 15:07:59 +05:30
|
|
|
func ToStopWatches(sws []*issues_model.Stopwatch) (api.StopWatches, error) {
|
2020-09-18 17:39:26 +05:30
|
|
|
result := api.StopWatches(make([]api.StopWatch, 0, len(sws)))
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
issueCache := make(map[int64]*issues_model.Issue)
|
2021-12-10 06:57:50 +05:30
|
|
|
repoCache := make(map[int64]*repo_model.Repository)
|
2020-09-18 17:39:26 +05:30
|
|
|
var (
|
2022-06-13 15:07:59 +05:30
|
|
|
issue *issues_model.Issue
|
2021-12-10 06:57:50 +05:30
|
|
|
repo *repo_model.Repository
|
2020-09-18 17:39:26 +05:30
|
|
|
ok bool
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, sw := range sws {
|
|
|
|
issue, ok = issueCache[sw.IssueID]
|
|
|
|
if !ok {
|
2022-06-13 15:07:59 +05:30
|
|
|
issue, err = issues_model.GetIssueByID(db.DefaultContext, sw.IssueID)
|
2020-09-18 17:39:26 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
repo, ok = repoCache[issue.RepoID]
|
|
|
|
if !ok {
|
2022-12-03 08:18:26 +05:30
|
|
|
repo, err = repo_model.GetRepositoryByID(db.DefaultContext, issue.RepoID)
|
2020-09-18 17:39:26 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, api.StopWatch{
|
|
|
|
Created: sw.CreatedUnix.AsTime(),
|
2021-01-21 20:21:52 +05:30
|
|
|
Seconds: sw.Seconds(),
|
|
|
|
Duration: sw.Duration(),
|
2020-09-18 17:39:26 +05:30
|
|
|
IssueIndex: issue.Index,
|
|
|
|
IssueTitle: issue.Title,
|
|
|
|
RepoOwnerName: repo.OwnerName,
|
|
|
|
RepoName: repo.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-01-11 08:29:41 +05:30
|
|
|
// ToTrackedTimeList converts TrackedTimeList to API format
|
2022-11-19 13:42:33 +05:30
|
|
|
func ToTrackedTimeList(ctx context.Context, tl issues_model.TrackedTimeList) api.TrackedTimeList {
|
2020-01-11 08:29:41 +05:30
|
|
|
result := make([]*api.TrackedTime, 0, len(tl))
|
|
|
|
for _, t := range tl {
|
2022-11-19 13:42:33 +05:30
|
|
|
result = append(result, ToTrackedTime(ctx, t))
|
2020-01-11 08:29:41 +05:30
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2020-02-29 08:19:50 +05:30
|
|
|
|
|
|
|
// ToLabel converts Label to API format
|
2022-06-13 15:07:59 +05:30
|
|
|
func ToLabel(label *issues_model.Label, repo *repo_model.Repository, org *user_model.User) *api.Label {
|
2021-09-10 21:33:16 +05:30
|
|
|
result := &api.Label{
|
2020-02-29 08:19:50 +05:30
|
|
|
ID: label.ID,
|
|
|
|
Name: label.Name,
|
|
|
|
Color: strings.TrimLeft(label.Color, "#"),
|
|
|
|
Description: label.Description,
|
|
|
|
}
|
2021-09-10 21:33:16 +05:30
|
|
|
|
|
|
|
// calculate URL
|
|
|
|
if label.BelongsToRepo() && repo != nil {
|
|
|
|
if repo != nil {
|
|
|
|
result.URL = fmt.Sprintf("%s/labels/%d", repo.APIURL(), label.ID)
|
|
|
|
} else {
|
|
|
|
log.Error("ToLabel did not get repo to calculate url for label with id '%d'", label.ID)
|
|
|
|
}
|
|
|
|
} else { // BelongsToOrg
|
|
|
|
if org != nil {
|
2021-11-16 23:48:25 +05:30
|
|
|
result.URL = fmt.Sprintf("%sapi/v1/orgs/%s/labels/%d", setting.AppURL, url.PathEscape(org.Name), label.ID)
|
2021-09-10 21:33:16 +05:30
|
|
|
} else {
|
|
|
|
log.Error("ToLabel did not get org to calculate url for label with id '%d'", label.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2020-02-29 08:19:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// ToLabelList converts list of Label to API format
|
2022-06-13 15:07:59 +05:30
|
|
|
func ToLabelList(labels []*issues_model.Label, repo *repo_model.Repository, org *user_model.User) []*api.Label {
|
2020-02-29 08:19:50 +05:30
|
|
|
result := make([]*api.Label, len(labels))
|
|
|
|
for i := range labels {
|
2021-09-10 21:33:16 +05:30
|
|
|
result[i] = ToLabel(labels[i], repo, org)
|
2020-02-29 08:19:50 +05:30
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2020-05-13 03:24:35 +05:30
|
|
|
|
|
|
|
// ToAPIMilestone converts Milestone into API Format
|
2022-04-08 14:41:15 +05:30
|
|
|
func ToAPIMilestone(m *issues_model.Milestone) *api.Milestone {
|
2020-05-13 03:24:35 +05:30
|
|
|
apiMilestone := &api.Milestone{
|
|
|
|
ID: m.ID,
|
|
|
|
State: m.State(),
|
|
|
|
Title: m.Name,
|
|
|
|
Description: m.Content,
|
|
|
|
OpenIssues: m.NumOpenIssues,
|
|
|
|
ClosedIssues: m.NumClosedIssues,
|
2020-09-05 23:08:54 +05:30
|
|
|
Created: m.CreatedUnix.AsTime(),
|
|
|
|
Updated: m.UpdatedUnix.AsTimePtr(),
|
2020-05-13 03:24:35 +05:30
|
|
|
}
|
|
|
|
if m.IsClosed {
|
|
|
|
apiMilestone.Closed = m.ClosedDateUnix.AsTimePtr()
|
|
|
|
}
|
|
|
|
if m.DeadlineUnix.Year() < 9999 {
|
|
|
|
apiMilestone.Deadline = m.DeadlineUnix.AsTimePtr()
|
|
|
|
}
|
|
|
|
return apiMilestone
|
|
|
|
}
|