2019-10-15 10:33:05 +05:30
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-10-15 10:33:05 +05:30
|
|
|
|
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
2022-11-19 13:42:33 +05:30
|
|
|
"context"
|
2022-01-20 04:56:57 +05:30
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2022-03-30 14:12:47 +05:30
|
|
|
packages_model "code.gitea.io/gitea/models/packages"
|
2021-11-28 17:28:28 +05:30
|
|
|
"code.gitea.io/gitea/models/perm"
|
2022-05-11 15:39:36 +05:30
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-10 01:27:58 +05:30
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-11-06 12:13:03 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
2019-10-15 10:33:05 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2023-01-01 20:53:15 +05:30
|
|
|
"code.gitea.io/gitea/modules/notification"
|
2019-10-15 10:33:05 +05:30
|
|
|
"code.gitea.io/gitea/modules/notification/base"
|
2020-01-10 15:04:21 +05:30
|
|
|
"code.gitea.io/gitea/modules/repository"
|
2019-11-03 12:29:26 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-10-15 10:33:05 +05:30
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2023-01-01 20:53:15 +05:30
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
2022-12-29 08:27:15 +05:30
|
|
|
"code.gitea.io/gitea/services/convert"
|
2019-10-15 10:33:05 +05:30
|
|
|
)
|
|
|
|
|
2023-01-01 20:53:15 +05:30
|
|
|
func init() {
|
|
|
|
notification.RegisterNotifier(&webhookNotifier{})
|
|
|
|
}
|
|
|
|
|
2019-10-15 10:33:05 +05:30
|
|
|
type webhookNotifier struct {
|
|
|
|
base.NullNotifier
|
|
|
|
}
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
var _ base.Notifier = &webhookNotifier{}
|
2019-10-15 10:33:05 +05:30
|
|
|
|
|
|
|
// NewNotifier create a new webhookNotifier notifier
|
|
|
|
func NewNotifier() base.Notifier {
|
|
|
|
return &webhookNotifier{}
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyIssueClearLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue) {
|
|
|
|
if err := issue.LoadPoster(ctx); err != nil {
|
|
|
|
log.Error("LoadPoster: %v", err)
|
2019-10-15 10:33:05 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-08 14:41:15 +05:30
|
|
|
if err := issue.LoadRepo(ctx); err != nil {
|
2019-10-15 10:33:05 +05:30
|
|
|
log.Error("LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo)
|
2019-10-15 10:33:05 +05:30
|
|
|
var err error
|
|
|
|
if issue.IsPull {
|
2022-11-19 13:42:33 +05:30
|
|
|
if err = issue.LoadPullRequest(ctx); err != nil {
|
2019-10-15 10:33:05 +05:30
|
|
|
log.Error("LoadPullRequest: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-01 20:53:15 +05:30
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequestLabel, &api.PullRequestPayload{
|
2019-10-15 10:33:05 +05:30
|
|
|
Action: api.HookIssueLabelCleared,
|
|
|
|
Index: issue.Index,
|
2022-01-20 04:56:57 +05:30
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-10-15 10:33:05 +05:30
|
|
|
})
|
|
|
|
} else {
|
2023-01-01 20:53:15 +05:30
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueLabel, &api.IssuePayload{
|
2019-10-15 10:33:05 +05:30
|
|
|
Action: api.HookIssueLabelCleared,
|
|
|
|
Index: issue.Index,
|
2022-11-19 13:42:33 +05:30
|
|
|
Issue: convert.ToAPIIssue(ctx, issue),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-10-15 10:33:05 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-26 12:24:11 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyForkRepository(ctx context.Context, doer *user_model.User, oldRepo, repo *repo_model.Repository) {
|
|
|
|
oldMode, _ := access_model.AccessLevel(ctx, doer, oldRepo)
|
|
|
|
mode, _ := access_model.AccessLevel(ctx, doer, repo)
|
2019-10-26 12:24:11 +05:30
|
|
|
|
|
|
|
// forked webhook
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: oldRepo}, webhook_module.HookEventFork, &api.ForkPayload{
|
2022-12-03 08:18:26 +05:30
|
|
|
Forkee: convert.ToRepo(ctx, oldRepo, oldMode),
|
|
|
|
Repo: convert.ToRepo(ctx, repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-10-26 12:24:11 +05:30
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", oldRepo.ID, err)
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
u := repo.MustOwner(ctx)
|
2019-10-26 12:24:11 +05:30
|
|
|
|
|
|
|
// Add to hook queue for created repo after session commit.
|
|
|
|
if u.IsOrganization() {
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{
|
2019-10-26 12:24:11 +05:30
|
|
|
Action: api.HookRepoCreated,
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner),
|
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
|
|
|
Organization: convert.ToUser(ctx, u, nil),
|
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-10-26 12:24:11 +05:30
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyCreateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
|
2019-10-26 12:24:11 +05:30
|
|
|
// Add to hook queue for created repo after session commit.
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{
|
2020-10-02 15:07:46 +05:30
|
|
|
Action: api.HookRepoCreated,
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner),
|
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
|
|
|
Organization: convert.ToUser(ctx, u, nil),
|
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2020-10-02 15:07:46 +05:30
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
2019-10-26 12:24:11 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyDeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository) {
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{
|
2020-10-02 15:07:46 +05:30
|
|
|
Action: api.HookRepoDeleted,
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner),
|
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
|
|
|
Organization: convert.ToUser(ctx, repo.MustOwner(ctx), nil),
|
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2020-10-02 15:07:46 +05:30
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
2019-10-26 12:24:11 +05:30
|
|
|
}
|
|
|
|
}
|
2019-10-28 07:41:50 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyMigrateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
|
2020-12-17 17:56:22 +05:30
|
|
|
// Add to hook queue for created repo after session commit.
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{
|
2020-12-17 17:56:22 +05:30
|
|
|
Action: api.HookRepoCreated,
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner),
|
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
|
|
|
Organization: convert.ToUser(ctx, u, nil),
|
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2020-12-17 17:56:22 +05:30
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyIssueChangeAssignee(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, assignee *user_model.User, removed bool, comment *issues_model.Comment) {
|
2019-10-28 07:41:50 +05:30
|
|
|
if issue.IsPull {
|
2022-11-19 13:42:33 +05:30
|
|
|
mode, _ := access_model.AccessLevelUnit(ctx, doer, issue.Repo, unit.TypePullRequests)
|
2019-10-28 07:41:50 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
if err := issue.LoadPullRequest(ctx); err != nil {
|
2019-10-28 07:41:50 +05:30
|
|
|
log.Error("LoadPullRequest failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
apiPullRequest := &api.PullRequestPayload{
|
|
|
|
Index: issue.Index,
|
2022-01-20 04:56:57 +05:30
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-10-28 07:41:50 +05:30
|
|
|
}
|
|
|
|
if removed {
|
|
|
|
apiPullRequest.Action = api.HookIssueUnassigned
|
|
|
|
} else {
|
|
|
|
apiPullRequest.Action = api.HookIssueAssigned
|
|
|
|
}
|
|
|
|
// Assignee comment triggers a webhook
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequestAssign, apiPullRequest); err != nil {
|
2019-10-28 07:41:50 +05:30
|
|
|
log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2022-11-19 13:42:33 +05:30
|
|
|
mode, _ := access_model.AccessLevelUnit(ctx, doer, issue.Repo, unit.TypeIssues)
|
2019-10-28 07:41:50 +05:30
|
|
|
apiIssue := &api.IssuePayload{
|
|
|
|
Index: issue.Index,
|
2022-11-19 13:42:33 +05:30
|
|
|
Issue: convert.ToAPIIssue(ctx, issue),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-10-28 07:41:50 +05:30
|
|
|
}
|
|
|
|
if removed {
|
|
|
|
apiIssue.Action = api.HookIssueUnassigned
|
|
|
|
} else {
|
|
|
|
apiIssue.Action = api.HookIssueAssigned
|
|
|
|
}
|
|
|
|
// Assignee comment triggers a webhook
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueAssign, apiIssue); err != nil {
|
2019-10-28 07:41:50 +05:30
|
|
|
log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyIssueChangeTitle(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldTitle string) {
|
|
|
|
mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo)
|
2019-10-28 07:41:50 +05:30
|
|
|
var err error
|
|
|
|
if issue.IsPull {
|
2022-11-19 13:42:33 +05:30
|
|
|
if err = issue.LoadPullRequest(ctx); err != nil {
|
2019-10-28 07:41:50 +05:30
|
|
|
log.Error("LoadPullRequest failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2023-01-01 20:53:15 +05:30
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{
|
2019-10-28 07:41:50 +05:30
|
|
|
Action: api.HookIssueEdited,
|
|
|
|
Index: issue.Index,
|
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Title: &api.ChangesFromPayload{
|
|
|
|
From: oldTitle,
|
|
|
|
},
|
|
|
|
},
|
2022-01-20 04:56:57 +05:30
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-10-28 07:41:50 +05:30
|
|
|
})
|
|
|
|
} else {
|
2023-01-01 20:53:15 +05:30
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, &api.IssuePayload{
|
2019-10-28 07:41:50 +05:30
|
|
|
Action: api.HookIssueEdited,
|
|
|
|
Index: issue.Index,
|
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Title: &api.ChangesFromPayload{
|
|
|
|
From: oldTitle,
|
|
|
|
},
|
|
|
|
},
|
2022-11-19 13:42:33 +05:30
|
|
|
Issue: convert.ToAPIIssue(ctx, issue),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-10-28 07:41:50 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-28 10:56:46 +05:30
|
|
|
|
2023-01-25 10:17:53 +05:30
|
|
|
func (m *webhookNotifier) NotifyIssueChangeStatus(ctx context.Context, doer *user_model.User, commitID string, issue *issues_model.Issue, actionComment *issues_model.Comment, isClosed bool) {
|
2022-11-19 13:42:33 +05:30
|
|
|
mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo)
|
2019-10-28 10:56:46 +05:30
|
|
|
var err error
|
|
|
|
if issue.IsPull {
|
2022-11-19 13:42:33 +05:30
|
|
|
if err = issue.LoadPullRequest(ctx); err != nil {
|
2019-10-28 10:56:46 +05:30
|
|
|
log.Error("LoadPullRequest: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Merge pull request calls issue.changeStatus so we need to handle separately.
|
|
|
|
apiPullRequest := &api.PullRequestPayload{
|
|
|
|
Index: issue.Index,
|
2022-01-20 04:56:57 +05:30
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2023-01-25 10:17:53 +05:30
|
|
|
CommitID: commitID,
|
2019-10-28 10:56:46 +05:30
|
|
|
}
|
|
|
|
if isClosed {
|
|
|
|
apiPullRequest.Action = api.HookIssueClosed
|
|
|
|
} else {
|
|
|
|
apiPullRequest.Action = api.HookIssueReOpened
|
|
|
|
}
|
2023-01-01 20:53:15 +05:30
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequest, apiPullRequest)
|
2019-10-28 10:56:46 +05:30
|
|
|
} else {
|
|
|
|
apiIssue := &api.IssuePayload{
|
|
|
|
Index: issue.Index,
|
2022-11-19 13:42:33 +05:30
|
|
|
Issue: convert.ToAPIIssue(ctx, issue),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2023-01-25 10:17:53 +05:30
|
|
|
CommitID: commitID,
|
2019-10-28 10:56:46 +05:30
|
|
|
}
|
|
|
|
if isClosed {
|
|
|
|
apiIssue.Action = api.HookIssueClosed
|
|
|
|
} else {
|
|
|
|
apiIssue.Action = api.HookIssueReOpened
|
|
|
|
}
|
2023-01-01 20:53:15 +05:30
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, apiIssue)
|
2019-10-28 10:56:46 +05:30
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v, is_closed: %v]: %v", issue.IsPull, isClosed, err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-28 22:15:43 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyNewIssue(ctx context.Context, issue *issues_model.Issue, mentions []*user_model.User) {
|
|
|
|
if err := issue.LoadRepo(ctx); err != nil {
|
2019-11-04 02:29:09 +05:30
|
|
|
log.Error("issue.LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 13:42:33 +05:30
|
|
|
if err := issue.LoadPoster(ctx); err != nil {
|
2019-11-04 02:29:09 +05:30
|
|
|
log.Error("issue.LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo)
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, &api.IssuePayload{
|
2019-10-28 22:15:43 +05:30
|
|
|
Action: api.HookIssueOpened,
|
|
|
|
Index: issue.Index,
|
2022-11-19 13:42:33 +05:30
|
|
|
Issue: convert.ToAPIIssue(ctx, issue),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, issue.Poster, nil),
|
2019-10-28 22:15:43 +05:30
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-30 14:06:25 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyNewPullRequest(ctx context.Context, pull *issues_model.PullRequest, mentions []*user_model.User) {
|
|
|
|
if err := pull.LoadIssue(ctx); err != nil {
|
2019-11-04 02:29:09 +05:30
|
|
|
log.Error("pull.LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-04-08 14:41:15 +05:30
|
|
|
if err := pull.Issue.LoadRepo(ctx); err != nil {
|
2019-11-04 02:29:09 +05:30
|
|
|
log.Error("pull.Issue.LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 13:42:33 +05:30
|
|
|
if err := pull.Issue.LoadPoster(ctx); err != nil {
|
2019-11-04 02:29:09 +05:30
|
|
|
log.Error("pull.Issue.LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
mode, _ := access_model.AccessLevel(ctx, pull.Issue.Poster, pull.Issue.Repo)
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: pull.Issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{
|
2019-11-04 02:29:09 +05:30
|
|
|
Action: api.HookIssueOpened,
|
|
|
|
Index: pull.Issue.Index,
|
2022-01-20 04:56:57 +05:30
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, pull, nil),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, pull.Issue.Repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, pull.Issue.Poster, nil),
|
2019-11-04 02:29:09 +05:30
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyIssueChangeContent(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldContent string) {
|
2022-12-09 12:05:56 +05:30
|
|
|
if err := issue.LoadRepo(ctx); err != nil {
|
|
|
|
log.Error("LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo)
|
2019-10-30 14:06:25 +05:30
|
|
|
var err error
|
|
|
|
if issue.IsPull {
|
2023-02-21 05:45:49 +05:30
|
|
|
if err := issue.LoadPullRequest(ctx); err != nil {
|
|
|
|
log.Error("LoadPullRequest: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2023-01-01 20:53:15 +05:30
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{
|
2019-10-30 14:06:25 +05:30
|
|
|
Action: api.HookIssueEdited,
|
|
|
|
Index: issue.Index,
|
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Body: &api.ChangesFromPayload{
|
|
|
|
From: oldContent,
|
|
|
|
},
|
|
|
|
},
|
2022-01-20 04:56:57 +05:30
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-10-30 14:06:25 +05:30
|
|
|
})
|
|
|
|
} else {
|
2023-01-01 20:53:15 +05:30
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, &api.IssuePayload{
|
2019-10-30 14:06:25 +05:30
|
|
|
Action: api.HookIssueEdited,
|
|
|
|
Index: issue.Index,
|
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Body: &api.ChangesFromPayload{
|
|
|
|
From: oldContent,
|
|
|
|
},
|
|
|
|
},
|
2022-11-19 13:42:33 +05:30
|
|
|
Issue: convert.ToAPIIssue(ctx, issue),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-10-30 14:06:25 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-30 15:32:46 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyUpdateComment(ctx context.Context, doer *user_model.User, c *issues_model.Comment, oldContent string) {
|
|
|
|
if err := c.LoadPoster(ctx); err != nil {
|
2019-10-30 15:32:46 +05:30
|
|
|
log.Error("LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 13:42:33 +05:30
|
|
|
if err := c.LoadIssue(ctx); err != nil {
|
2019-10-30 15:32:46 +05:30
|
|
|
log.Error("LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
if err := c.Issue.LoadAttributes(ctx); err != nil {
|
2019-10-30 15:32:46 +05:30
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-01 20:53:15 +05:30
|
|
|
var eventType webhook_module.HookEventType
|
2020-03-06 10:40:48 +05:30
|
|
|
if c.Issue.IsPull {
|
2023-01-01 20:53:15 +05:30
|
|
|
eventType = webhook_module.HookEventPullRequestComment
|
2020-03-06 10:40:48 +05:30
|
|
|
} else {
|
2023-01-01 20:53:15 +05:30
|
|
|
eventType = webhook_module.HookEventIssueComment
|
2020-03-06 10:40:48 +05:30
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
mode, _ := access_model.AccessLevel(ctx, doer, c.Issue.Repo)
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: c.Issue.Repo}, eventType, &api.IssueCommentPayload{
|
2022-10-21 21:51:56 +05:30
|
|
|
Action: api.HookIssueCommentEdited,
|
2022-11-19 13:42:33 +05:30
|
|
|
Issue: convert.ToAPIIssue(ctx, c.Issue),
|
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
|
|
|
Comment: convert.ToComment(ctx, c),
|
2022-10-21 21:51:56 +05:30
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Body: &api.ChangesFromPayload{
|
|
|
|
From: oldContent,
|
|
|
|
},
|
|
|
|
},
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, c.Issue.Repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2022-10-21 21:51:56 +05:30
|
|
|
IsPull: c.Issue.IsPull,
|
|
|
|
}); err != nil {
|
2019-10-30 15:32:46 +05:30
|
|
|
log.Error("PrepareWebhooks [comment_id: %d]: %v", c.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyCreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository,
|
2022-06-13 15:07:59 +05:30
|
|
|
issue *issues_model.Issue, comment *issues_model.Comment, mentions []*user_model.User,
|
2022-02-24 01:46:07 +05:30
|
|
|
) {
|
2023-01-01 20:53:15 +05:30
|
|
|
var eventType webhook_module.HookEventType
|
2020-03-06 10:40:48 +05:30
|
|
|
if issue.IsPull {
|
2023-01-01 20:53:15 +05:30
|
|
|
eventType = webhook_module.HookEventPullRequestComment
|
2020-03-06 10:40:48 +05:30
|
|
|
} else {
|
2023-01-01 20:53:15 +05:30
|
|
|
eventType = webhook_module.HookEventIssueComment
|
2020-03-06 10:40:48 +05:30
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
mode, _ := access_model.AccessLevel(ctx, doer, repo)
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, eventType, &api.IssueCommentPayload{
|
2022-10-21 21:51:56 +05:30
|
|
|
Action: api.HookIssueCommentCreated,
|
2022-11-19 13:42:33 +05:30
|
|
|
Issue: convert.ToAPIIssue(ctx, issue),
|
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
|
|
|
Comment: convert.ToComment(ctx, comment),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2022-10-21 21:51:56 +05:30
|
|
|
IsPull: issue.IsPull,
|
|
|
|
}); err != nil {
|
2019-10-30 15:32:46 +05:30
|
|
|
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyDeleteComment(ctx context.Context, doer *user_model.User, comment *issues_model.Comment) {
|
2020-03-06 10:40:48 +05:30
|
|
|
var err error
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
if err = comment.LoadPoster(ctx); err != nil {
|
2019-10-30 15:32:46 +05:30
|
|
|
log.Error("LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 13:42:33 +05:30
|
|
|
if err = comment.LoadIssue(ctx); err != nil {
|
2019-10-30 15:32:46 +05:30
|
|
|
log.Error("LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
if err = comment.Issue.LoadAttributes(ctx); err != nil {
|
2019-10-30 15:32:46 +05:30
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-01 20:53:15 +05:30
|
|
|
var eventType webhook_module.HookEventType
|
2020-03-06 10:40:48 +05:30
|
|
|
if comment.Issue.IsPull {
|
2023-01-01 20:53:15 +05:30
|
|
|
eventType = webhook_module.HookEventPullRequestComment
|
2020-03-06 10:40:48 +05:30
|
|
|
} else {
|
2023-01-01 20:53:15 +05:30
|
|
|
eventType = webhook_module.HookEventIssueComment
|
2020-03-06 10:40:48 +05:30
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
mode, _ := access_model.AccessLevel(ctx, doer, comment.Issue.Repo)
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: comment.Issue.Repo}, eventType, &api.IssueCommentPayload{
|
2022-10-21 21:51:56 +05:30
|
|
|
Action: api.HookIssueCommentDeleted,
|
2022-11-19 13:42:33 +05:30
|
|
|
Issue: convert.ToAPIIssue(ctx, comment.Issue),
|
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
|
|
|
Comment: convert.ToComment(ctx, comment),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, comment.Issue.Repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2022-10-21 21:51:56 +05:30
|
|
|
IsPull: comment.Issue.IsPull,
|
|
|
|
}); err != nil {
|
2019-10-30 15:32:46 +05:30
|
|
|
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-02 07:19:57 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyNewWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) {
|
2022-09-05 01:24:23 +05:30
|
|
|
// Add to hook queue for created wiki page.
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventWiki, &api.WikiPayload{
|
2022-09-05 01:24:23 +05:30
|
|
|
Action: api.HookWikiCreated,
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2022-09-05 01:24:23 +05:30
|
|
|
Page: page,
|
|
|
|
Comment: comment,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyEditWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) {
|
2022-09-05 01:24:23 +05:30
|
|
|
// Add to hook queue for edit wiki page.
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventWiki, &api.WikiPayload{
|
2022-09-05 01:24:23 +05:30
|
|
|
Action: api.HookWikiEdited,
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2022-09-05 01:24:23 +05:30
|
|
|
Page: page,
|
|
|
|
Comment: comment,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyDeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page string) {
|
2022-09-05 01:24:23 +05:30
|
|
|
// Add to hook queue for edit wiki page.
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventWiki, &api.WikiPayload{
|
2022-09-05 01:24:23 +05:30
|
|
|
Action: api.HookWikiDeleted,
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2022-09-05 01:24:23 +05:30
|
|
|
Page: page,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyIssueChangeLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue,
|
2022-06-13 15:07:59 +05:30
|
|
|
addedLabels, removedLabels []*issues_model.Label,
|
2022-02-24 01:46:07 +05:30
|
|
|
) {
|
2019-11-02 07:19:57 +05:30
|
|
|
var err error
|
|
|
|
|
2022-04-08 14:41:15 +05:30
|
|
|
if err = issue.LoadRepo(ctx); err != nil {
|
2019-11-02 07:19:57 +05:30
|
|
|
log.Error("LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
if err = issue.LoadPoster(ctx); err != nil {
|
2019-11-02 07:19:57 +05:30
|
|
|
log.Error("LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo)
|
2019-11-02 07:19:57 +05:30
|
|
|
if issue.IsPull {
|
2022-11-19 13:42:33 +05:30
|
|
|
if err = issue.LoadPullRequest(ctx); err != nil {
|
2019-11-02 07:19:57 +05:30
|
|
|
log.Error("loadPullRequest: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 13:42:33 +05:30
|
|
|
if err = issue.PullRequest.LoadIssue(ctx); err != nil {
|
2019-11-02 07:19:57 +05:30
|
|
|
log.Error("LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2023-01-01 20:53:15 +05:30
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequestLabel, &api.PullRequestPayload{
|
2019-11-02 07:19:57 +05:30
|
|
|
Action: api.HookIssueLabelUpdated,
|
|
|
|
Index: issue.Index,
|
2022-01-20 04:56:57 +05:30
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, perm.AccessModeNone),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-11-02 07:19:57 +05:30
|
|
|
})
|
|
|
|
} else {
|
2023-01-01 20:53:15 +05:30
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueLabel, &api.IssuePayload{
|
2019-11-02 07:19:57 +05:30
|
|
|
Action: api.HookIssueLabelUpdated,
|
|
|
|
Index: issue.Index,
|
2022-11-19 13:42:33 +05:30
|
|
|
Issue: convert.ToAPIIssue(ctx, issue),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-11-02 07:19:57 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-02 09:03:20 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyIssueChangeMilestone(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldMilestoneID int64) {
|
2019-11-02 09:03:20 +05:30
|
|
|
var hookAction api.HookIssueAction
|
|
|
|
var err error
|
|
|
|
if issue.MilestoneID > 0 {
|
|
|
|
hookAction = api.HookIssueMilestoned
|
|
|
|
} else {
|
|
|
|
hookAction = api.HookIssueDemilestoned
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
if err = issue.LoadAttributes(ctx); err != nil {
|
2019-11-02 09:03:20 +05:30
|
|
|
log.Error("issue.LoadAttributes failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
mode, _ := access_model.AccessLevel(ctx, doer, issue.Repo)
|
2019-11-02 09:03:20 +05:30
|
|
|
if issue.IsPull {
|
2022-11-19 13:42:33 +05:30
|
|
|
err = issue.PullRequest.LoadIssue(ctx)
|
2019-11-02 09:03:20 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2023-01-01 20:53:15 +05:30
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequestMilestone, &api.PullRequestPayload{
|
2019-11-02 09:03:20 +05:30
|
|
|
Action: hookAction,
|
|
|
|
Index: issue.Index,
|
2022-01-20 04:56:57 +05:30
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-11-02 09:03:20 +05:30
|
|
|
})
|
|
|
|
} else {
|
2023-01-01 20:53:15 +05:30
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueMilestone, &api.IssuePayload{
|
2019-11-02 09:03:20 +05:30
|
|
|
Action: hookAction,
|
|
|
|
Index: issue.Index,
|
2022-11-19 13:42:33 +05:30
|
|
|
Issue: convert.ToAPIIssue(ctx, issue),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-11-02 09:03:20 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-03 12:29:26 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
|
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
|
|
|
apiPusher := convert.ToUser(ctx, pusher, nil)
|
2022-01-20 04:56:57 +05:30
|
|
|
apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(ctx, repo.RepoPath(), repo.HTMLURL())
|
2019-11-03 12:29:26 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("commits.ToAPIPayloadCommits failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{
|
2022-10-16 21:52:34 +05:30
|
|
|
Ref: opts.RefFullName,
|
|
|
|
Before: opts.OldCommitID,
|
|
|
|
After: opts.NewCommitID,
|
|
|
|
CompareURL: setting.AppURL + commits.CompareURL,
|
|
|
|
Commits: apiCommits,
|
|
|
|
TotalCommits: commits.Len,
|
|
|
|
HeadCommit: apiHeadCommit,
|
2022-12-03 08:18:26 +05:30
|
|
|
Repo: convert.ToRepo(ctx, repo, perm.AccessModeOwner),
|
2022-10-16 21:52:34 +05:30
|
|
|
Pusher: apiPusher,
|
|
|
|
Sender: apiPusher,
|
2019-11-03 12:29:26 +05:30
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-05 16:34:08 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyAutoMergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
|
2022-11-03 21:19:00 +05:30
|
|
|
// just redirect to the NotifyMergePullRequest
|
2022-11-19 13:42:33 +05:30
|
|
|
m.NotifyMergePullRequest(ctx, doer, pr)
|
2022-11-03 21:19:00 +05:30
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (*webhookNotifier) NotifyMergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
|
2019-11-21 22:38:42 +05:30
|
|
|
// Reload pull request information.
|
2022-11-19 13:42:33 +05:30
|
|
|
if err := pr.LoadAttributes(ctx); err != nil {
|
2019-11-21 22:38:42 +05:30
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
if err := pr.LoadIssue(ctx); err != nil {
|
|
|
|
log.Error("LoadIssue: %v", err)
|
2019-11-21 22:38:42 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-08 14:41:15 +05:30
|
|
|
if err := pr.Issue.LoadRepo(ctx); err != nil {
|
2019-11-21 22:38:42 +05:30
|
|
|
log.Error("pr.Issue.LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
mode, err := access_model.AccessLevel(ctx, doer, pr.Issue.Repo)
|
2019-11-21 22:38:42 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("models.AccessLevel: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge pull request calls issue.changeStatus so we need to handle separately.
|
|
|
|
apiPullRequest := &api.PullRequestPayload{
|
|
|
|
Index: pr.Issue.Index,
|
2022-01-20 04:56:57 +05:30
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, pr.Issue.Repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-11-21 22:38:42 +05:30
|
|
|
Action: api.HookIssueClosed,
|
|
|
|
}
|
|
|
|
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: pr.Issue.Repo}, webhook_module.HookEventPullRequest, apiPullRequest); err != nil {
|
2019-11-21 22:38:42 +05:30
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyPullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch string) {
|
|
|
|
if err := pr.LoadIssue(ctx); err != nil {
|
|
|
|
log.Error("LoadIssue: %v", err)
|
2019-12-16 11:50:25 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
issue := pr.Issue
|
|
|
|
|
|
|
|
mode, _ := access_model.AccessLevel(ctx, issue.Poster, issue.Repo)
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{
|
2019-12-16 11:50:25 +05:30
|
|
|
Action: api.HookIssueEdited,
|
|
|
|
Index: issue.Index,
|
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Ref: &api.ChangesFromPayload{
|
|
|
|
From: oldBranch,
|
|
|
|
},
|
|
|
|
},
|
2022-11-19 13:42:33 +05:30
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2022-11-19 13:42:33 +05:30
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [pr: %d]: %v", pr.ID, err)
|
2019-12-16 11:50:25 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyPullRequestReview(ctx context.Context, pr *issues_model.PullRequest, review *issues_model.Review, comment *issues_model.Comment, mentions []*user_model.User) {
|
2023-01-01 20:53:15 +05:30
|
|
|
var reviewHookType webhook_module.HookEventType
|
2019-11-05 16:34:08 +05:30
|
|
|
|
|
|
|
switch review.Type {
|
2022-06-13 15:07:59 +05:30
|
|
|
case issues_model.ReviewTypeApprove:
|
2023-01-01 20:53:15 +05:30
|
|
|
reviewHookType = webhook_module.HookEventPullRequestReviewApproved
|
2022-06-13 15:07:59 +05:30
|
|
|
case issues_model.ReviewTypeComment:
|
2023-01-01 20:53:15 +05:30
|
|
|
reviewHookType = webhook_module.HookEventPullRequestComment
|
2022-06-13 15:07:59 +05:30
|
|
|
case issues_model.ReviewTypeReject:
|
2023-01-01 20:53:15 +05:30
|
|
|
reviewHookType = webhook_module.HookEventPullRequestReviewRejected
|
2019-11-05 16:34:08 +05:30
|
|
|
default:
|
|
|
|
// unsupported review webhook type here
|
|
|
|
log.Error("Unsupported review webhook type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
if err := pr.LoadIssue(ctx); err != nil {
|
|
|
|
log.Error("LoadIssue: %v", err)
|
2019-11-05 16:34:08 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
mode, err := access_model.AccessLevel(ctx, review.Issue.Poster, review.Issue.Repo)
|
2019-11-05 16:34:08 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("models.AccessLevel: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: review.Issue.Repo}, reviewHookType, &api.PullRequestPayload{
|
2020-03-06 10:40:48 +05:30
|
|
|
Action: api.HookIssueReviewed,
|
2019-11-05 16:34:08 +05:30
|
|
|
Index: review.Issue.Index,
|
2022-01-20 04:56:57 +05:30
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, review.Issue.Repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, review.Reviewer, nil),
|
2019-11-05 16:34:08 +05:30
|
|
|
Review: &api.ReviewPayload{
|
|
|
|
Type: string(reviewHookType),
|
|
|
|
Content: review.Content,
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) {
|
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
|
|
|
apiPusher := convert.ToUser(ctx, pusher, nil)
|
2022-12-03 08:18:26 +05:30
|
|
|
apiRepo := convert.ToRepo(ctx, repo, perm.AccessModeNone)
|
2019-11-06 12:13:03 +05:30
|
|
|
refName := git.RefEndName(refFullName)
|
|
|
|
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventCreate, &api.CreatePayload{
|
2019-11-06 12:13:03 +05:30
|
|
|
Ref: refName,
|
2022-01-20 04:56:57 +05:30
|
|
|
Sha: refID,
|
2019-11-06 12:13:03 +05:30
|
|
|
RefType: refType,
|
|
|
|
Repo: apiRepo,
|
|
|
|
Sender: apiPusher,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyPullRequestSynchronized(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
|
|
|
|
if err := pr.LoadIssue(ctx); err != nil {
|
|
|
|
log.Error("LoadIssue: %v", err)
|
2019-11-05 16:34:08 +05:30
|
|
|
return
|
|
|
|
}
|
2022-11-19 13:42:33 +05:30
|
|
|
if err := pr.Issue.LoadAttributes(ctx); err != nil {
|
2019-11-05 16:34:08 +05:30
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: pr.Issue.Repo}, webhook_module.HookEventPullRequestSync, &api.PullRequestPayload{
|
2019-11-05 16:34:08 +05:30
|
|
|
Action: api.HookIssueSynchronized,
|
|
|
|
Index: pr.Issue.Index,
|
2022-01-20 04:56:57 +05:30
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, pr.Issue.Repo, perm.AccessModeNone),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-11-05 16:34:08 +05:30
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [pull_id: %v]: %v", pr.ID, err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-06 12:13:03 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) {
|
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
|
|
|
apiPusher := convert.ToUser(ctx, pusher, nil)
|
2022-12-03 08:18:26 +05:30
|
|
|
apiRepo := convert.ToRepo(ctx, repo, perm.AccessModeNone)
|
2019-11-06 12:13:03 +05:30
|
|
|
refName := git.RefEndName(refFullName)
|
|
|
|
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventDelete, &api.DeletePayload{
|
2019-11-06 12:13:03 +05:30
|
|
|
Ref: refName,
|
2020-02-24 00:19:40 +05:30
|
|
|
RefType: refType,
|
2019-11-06 12:13:03 +05:30
|
|
|
PusherType: api.PusherTypeUser,
|
|
|
|
Repo: apiRepo,
|
|
|
|
Sender: apiPusher,
|
|
|
|
}); err != nil {
|
2020-02-24 00:19:40 +05:30
|
|
|
log.Error("PrepareWebhooks.(delete %s): %v", refType, err)
|
2019-11-06 12:13:03 +05:30
|
|
|
}
|
|
|
|
}
|
2019-11-06 13:55:50 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func sendReleaseHook(ctx context.Context, doer *user_model.User, rel *repo_model.Release, action api.HookReleaseAction) {
|
|
|
|
if err := rel.LoadAttributes(ctx); err != nil {
|
2019-11-06 13:55:50 +05:30
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
mode, _ := access_model.AccessLevel(ctx, doer, rel.Repo)
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: rel.Repo}, webhook_module.HookEventRelease, &api.ReleasePayload{
|
2019-11-06 13:55:50 +05:30
|
|
|
Action: action,
|
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
|
|
|
Release: convert.ToRelease(ctx, rel),
|
2022-12-03 08:18:26 +05:30
|
|
|
Repository: convert.ToRepo(ctx, rel.Repo, mode),
|
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
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-11-06 13:55:50 +05:30
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyNewRelease(ctx context.Context, rel *repo_model.Release) {
|
|
|
|
sendReleaseHook(ctx, rel.Publisher, rel, api.HookReleasePublished)
|
2019-11-06 13:55:50 +05:30
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyUpdateRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) {
|
|
|
|
sendReleaseHook(ctx, doer, rel, api.HookReleaseUpdated)
|
2019-11-06 13:55:50 +05:30
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyDeleteRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) {
|
|
|
|
sendReleaseHook(ctx, doer, rel, api.HookReleaseDeleted)
|
2019-11-06 13:55:50 +05:30
|
|
|
}
|
2019-11-24 10:46:59 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifySyncPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
|
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
|
|
|
apiPusher := convert.ToUser(ctx, pusher, nil)
|
2022-01-20 04:56:57 +05:30
|
|
|
apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(ctx, repo.RepoPath(), repo.HTMLURL())
|
2019-11-24 10:46:59 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("commits.ToAPIPayloadCommits failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{
|
2022-10-16 21:52:34 +05:30
|
|
|
Ref: opts.RefFullName,
|
|
|
|
Before: opts.OldCommitID,
|
|
|
|
After: opts.NewCommitID,
|
|
|
|
CompareURL: setting.AppURL + commits.CompareURL,
|
|
|
|
Commits: apiCommits,
|
|
|
|
TotalCommits: commits.Len,
|
|
|
|
HeadCommit: apiHeadCommit,
|
2022-12-03 08:18:26 +05:30
|
|
|
Repo: convert.ToRepo(ctx, repo, perm.AccessModeOwner),
|
2022-10-16 21:52:34 +05:30
|
|
|
Pusher: apiPusher,
|
|
|
|
Sender: apiPusher,
|
2019-11-24 10:46:59 +05:30
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2020-11-14 00:42:33 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifySyncCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) {
|
|
|
|
m.NotifyCreateRef(ctx, pusher, repo, refType, refFullName, refID)
|
2020-11-14 00:42:33 +05:30
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifySyncDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) {
|
|
|
|
m.NotifyDeleteRef(ctx, pusher, repo, refType, refFullName)
|
2020-11-14 00:42:33 +05:30
|
|
|
}
|
2022-03-30 14:12:47 +05:30
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyPackageCreate(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) {
|
|
|
|
notifyPackage(ctx, doer, pd, api.HookPackageCreated)
|
2022-03-30 14:12:47 +05:30
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func (m *webhookNotifier) NotifyPackageDelete(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) {
|
|
|
|
notifyPackage(ctx, doer, pd, api.HookPackageDeleted)
|
2022-03-30 14:12:47 +05:30
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
func notifyPackage(ctx context.Context, sender *user_model.User, pd *packages_model.PackageDescriptor, action api.HookPackageAction) {
|
2023-01-01 20:53:15 +05:30
|
|
|
source := EventSource{
|
2022-10-21 21:51:56 +05:30
|
|
|
Repository: pd.Repository,
|
|
|
|
Owner: pd.Owner,
|
2022-03-30 14:12:47 +05:30
|
|
|
}
|
|
|
|
|
2022-05-07 21:51:15 +05:30
|
|
|
apiPackage, err := convert.ToPackage(ctx, pd, sender)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error converting package: %v", err)
|
|
|
|
return
|
2022-03-30 14:12:47 +05:30
|
|
|
}
|
|
|
|
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := PrepareWebhooks(ctx, source, webhook_module.HookEventPackage, &api.PackagePayload{
|
2022-05-07 21:51:15 +05:30
|
|
|
Action: action,
|
|
|
|
Package: apiPackage,
|
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
|
|
|
Sender: convert.ToUser(ctx, sender, nil),
|
2022-03-30 14:12:47 +05:30
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|