2019-10-15 13:03:05 +08:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-10-15 13:03:05 +08:00
|
|
|
|
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
2022-11-19 09:12:33 +01:00
|
|
|
"context"
|
2022-01-19 23:26:57 +00:00
|
|
|
|
Actions Failure, Succes, Recover Webhooks (#7508)
Implement Actions Success, Failure and Recover webhooks for Forgejo, Gitea, Gogs, Slack, Discord, DingTalk, Telegram, Microsoft Teams, Feishu / Lark Suite, Matrix, WeCom (Wechat Work), Packagist. Some of these webhooks have not been manually tested.
Implement settings for these new webhooks.
## Checklist
The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).
### Tests
- I added test coverage for Go changes...
- [x] in their respective `*_test.go` for unit tests.
- [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I added test coverage for JavaScript changes...
- [ ] in `web_src/js/*.test.js` if it can be unit tested.
- [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)).
### Documentation
- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.
### Release notes
- [ ] I do not want this change to show in the release notes.
- [x] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.
<!--start release-notes-assistant-->
## Release notes
<!--URL:https://codeberg.org/forgejo/forgejo-->
- Features
- [PR](https://codeberg.org/forgejo/forgejo/pulls/7508): <!--number 7508 --><!--line 0 --><!--description QWN0aW9ucyBGYWlsdXJlLCBTdWNjZXMsIFJlY292ZXIgV2ViaG9va3M=-->Actions Failure, Succes, Recover Webhooks<!--description-->
<!--end release-notes-assistant-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7508
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: christopher-besch <mail@chris-besch.com>
Co-committed-by: christopher-besch <mail@chris-besch.com>
2025-06-03 14:29:19 +02:00
|
|
|
actions_model "forgejo.org/models/actions"
|
2025-03-27 19:40:14 +00:00
|
|
|
issues_model "forgejo.org/models/issues"
|
|
|
|
packages_model "forgejo.org/models/packages"
|
|
|
|
"forgejo.org/models/perm"
|
|
|
|
access_model "forgejo.org/models/perm/access"
|
|
|
|
repo_model "forgejo.org/models/repo"
|
|
|
|
user_model "forgejo.org/models/user"
|
|
|
|
"forgejo.org/modules/git"
|
|
|
|
"forgejo.org/modules/log"
|
|
|
|
"forgejo.org/modules/repository"
|
|
|
|
"forgejo.org/modules/setting"
|
|
|
|
api "forgejo.org/modules/structs"
|
|
|
|
webhook_module "forgejo.org/modules/webhook"
|
|
|
|
"forgejo.org/services/convert"
|
|
|
|
notify_service "forgejo.org/services/notify"
|
2019-10-15 13:03:05 +08:00
|
|
|
)
|
|
|
|
|
2023-01-01 16:23:15 +01:00
|
|
|
func init() {
|
2023-09-06 02:37:47 +08:00
|
|
|
notify_service.RegisterNotifier(&webhookNotifier{})
|
2023-01-01 16:23:15 +01:00
|
|
|
}
|
|
|
|
|
2019-10-15 13:03:05 +08:00
|
|
|
type webhookNotifier struct {
|
2023-09-06 02:37:47 +08:00
|
|
|
notify_service.NullNotifier
|
2019-10-15 13:03:05 +08:00
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
var _ notify_service.Notifier = &webhookNotifier{}
|
2019-10-15 13:03:05 +08:00
|
|
|
|
|
|
|
// NewNotifier create a new webhookNotifier notifier
|
2023-09-06 02:37:47 +08:00
|
|
|
func NewNotifier() notify_service.Notifier {
|
2019-10-15 13:03:05 +08:00
|
|
|
return &webhookNotifier{}
|
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) IssueClearLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue) {
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := issue.LoadPoster(ctx); err != nil {
|
|
|
|
log.Error("LoadPoster: %v", err)
|
2019-10-15 13:03:05 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-08 17:11:15 +08:00
|
|
|
if err := issue.LoadRepo(ctx); err != nil {
|
2019-10-15 13:03:05 +08:00
|
|
|
log.Error("LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-22 21:08:08 +08:00
|
|
|
permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster)
|
2019-10-15 13:03:05 +08:00
|
|
|
var err error
|
|
|
|
if issue.IsPull {
|
2022-11-19 09:12:33 +01:00
|
|
|
if err = issue.LoadPullRequest(ctx); err != nil {
|
2019-10-15 13:03:05 +08:00
|
|
|
log.Error("LoadPullRequest: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-01 16:23:15 +01:00
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequestLabel, &api.PullRequestPayload{
|
2019-10-15 13:03:05 +08:00
|
|
|
Action: api.HookIssueLabelCleared,
|
|
|
|
Index: issue.Index,
|
2024-10-16 17:10:05 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, doer),
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, permission),
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-10-15 13:03:05 +08:00
|
|
|
})
|
|
|
|
} else {
|
2023-01-01 16:23:15 +01:00
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueLabel, &api.IssuePayload{
|
2019-10-15 13:03:05 +08:00
|
|
|
Action: api.HookIssueLabelCleared,
|
|
|
|
Index: issue.Index,
|
2024-04-09 05:26:41 +08:00
|
|
|
Issue: convert.ToAPIIssue(ctx, doer, issue),
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, permission),
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-10-15 13:03:05 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-26 14:54:11 +08:00
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) ForkRepository(ctx context.Context, doer *user_model.User, oldRepo, repo *repo_model.Repository) {
|
2023-06-22 21:08:08 +08:00
|
|
|
oldPermission, _ := access_model.GetUserRepoPermission(ctx, oldRepo, doer)
|
|
|
|
permission, _ := access_model.GetUserRepoPermission(ctx, repo, doer)
|
2019-10-26 14:54:11 +08:00
|
|
|
|
|
|
|
// forked webhook
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: oldRepo}, webhook_module.HookEventFork, &api.ForkPayload{
|
2023-06-22 21:08:08 +08:00
|
|
|
Forkee: convert.ToRepo(ctx, oldRepo, oldPermission),
|
|
|
|
Repo: convert.ToRepo(ctx, repo, permission),
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-10-26 14:54:11 +08:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", oldRepo.ID, err)
|
|
|
|
}
|
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
u := repo.MustOwner(ctx)
|
2019-10-26 14:54:11 +08:00
|
|
|
|
|
|
|
// Add to hook queue for created repo after session commit.
|
|
|
|
if u.IsOrganization() {
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{
|
2019-10-26 14:54:11 +08:00
|
|
|
Action: api.HookRepoCreated,
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: 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 21:37:34 +08:00
|
|
|
Organization: convert.ToUser(ctx, u, nil),
|
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-10-26 14:54:11 +08:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) CreateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
|
2019-10-26 14:54:11 +08:00
|
|
|
// Add to hook queue for created repo after session commit.
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{
|
2020-10-02 09:37:46 +00:00
|
|
|
Action: api.HookRepoCreated,
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: 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 21:37:34 +08:00
|
|
|
Organization: convert.ToUser(ctx, u, nil),
|
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2020-10-02 09:37:46 +00:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
2019-10-26 14:54:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) DeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository) {
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{
|
2020-10-02 09:37:46 +00:00
|
|
|
Action: api.HookRepoDeleted,
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: 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 21:37:34 +08:00
|
|
|
Organization: convert.ToUser(ctx, repo.MustOwner(ctx), nil),
|
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2020-10-02 09:37:46 +00:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
2019-10-26 14:54:11 +08:00
|
|
|
}
|
|
|
|
}
|
2019-10-28 10:11:50 +08:00
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) MigrateRepository(ctx context.Context, doer, u *user_model.User, repo *repo_model.Repository) {
|
2020-12-17 12:26:22 +00:00
|
|
|
// Add to hook queue for created repo after session commit.
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventRepository, &api.RepositoryPayload{
|
2020-12-17 12:26:22 +00:00
|
|
|
Action: api.HookRepoCreated,
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: 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 21:37:34 +08:00
|
|
|
Organization: convert.ToUser(ctx, u, nil),
|
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2020-12-17 12:26:22 +00:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) IssueChangeAssignee(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, assignee *user_model.User, removed bool, comment *issues_model.Comment) {
|
2019-10-28 10:11:50 +08:00
|
|
|
if issue.IsPull {
|
2023-06-22 21:08:08 +08:00
|
|
|
permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, doer)
|
2019-10-28 10:11:50 +08:00
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := issue.LoadPullRequest(ctx); err != nil {
|
2019-10-28 10:11:50 +08:00
|
|
|
log.Error("LoadPullRequest failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
apiPullRequest := &api.PullRequestPayload{
|
|
|
|
Index: issue.Index,
|
2024-10-16 17:10:05 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, doer),
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, permission),
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-10-28 10:11:50 +08:00
|
|
|
}
|
|
|
|
if removed {
|
|
|
|
apiPullRequest.Action = api.HookIssueUnassigned
|
|
|
|
} else {
|
|
|
|
apiPullRequest.Action = api.HookIssueAssigned
|
|
|
|
}
|
|
|
|
// Assignee comment triggers a webhook
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequestAssign, apiPullRequest); err != nil {
|
2019-10-28 10:11:50 +08:00
|
|
|
log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2023-06-22 21:08:08 +08:00
|
|
|
permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, doer)
|
2019-10-28 10:11:50 +08:00
|
|
|
apiIssue := &api.IssuePayload{
|
|
|
|
Index: issue.Index,
|
2024-04-09 05:26:41 +08:00
|
|
|
Issue: convert.ToAPIIssue(ctx, doer, issue),
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, permission),
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-10-28 10:11:50 +08:00
|
|
|
}
|
|
|
|
if removed {
|
|
|
|
apiIssue.Action = api.HookIssueUnassigned
|
|
|
|
} else {
|
|
|
|
apiIssue.Action = api.HookIssueAssigned
|
|
|
|
}
|
|
|
|
// Assignee comment triggers a webhook
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueAssign, apiIssue); err != nil {
|
2019-10-28 10:11:50 +08:00
|
|
|
log.Error("PrepareWebhooks [is_pull: %v, remove_assignee: %v]: %v", issue.IsPull, removed, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) IssueChangeTitle(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldTitle string) {
|
2023-06-22 21:08:08 +08:00
|
|
|
permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster)
|
2019-10-28 10:11:50 +08:00
|
|
|
var err error
|
|
|
|
if issue.IsPull {
|
2022-11-19 09:12:33 +01:00
|
|
|
if err = issue.LoadPullRequest(ctx); err != nil {
|
2019-10-28 10:11:50 +08:00
|
|
|
log.Error("LoadPullRequest failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2023-01-01 16:23:15 +01:00
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{
|
2019-10-28 10:11:50 +08:00
|
|
|
Action: api.HookIssueEdited,
|
|
|
|
Index: issue.Index,
|
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Title: &api.ChangesFromPayload{
|
|
|
|
From: oldTitle,
|
|
|
|
},
|
|
|
|
},
|
2024-10-16 17:10:05 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, doer),
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, permission),
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-10-28 10:11:50 +08:00
|
|
|
})
|
|
|
|
} else {
|
2023-01-01 16:23:15 +01:00
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, &api.IssuePayload{
|
2019-10-28 10:11:50 +08:00
|
|
|
Action: api.HookIssueEdited,
|
|
|
|
Index: issue.Index,
|
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Title: &api.ChangesFromPayload{
|
|
|
|
From: oldTitle,
|
|
|
|
},
|
|
|
|
},
|
2024-04-09 05:26:41 +08:00
|
|
|
Issue: convert.ToAPIIssue(ctx, doer, issue),
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, permission),
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-10-28 10:11:50 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-28 13:26:46 +08:00
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) IssueChangeStatus(ctx context.Context, doer *user_model.User, commitID string, issue *issues_model.Issue, actionComment *issues_model.Comment, isClosed bool) {
|
2023-06-22 21:08:08 +08:00
|
|
|
permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster)
|
2019-10-28 13:26:46 +08:00
|
|
|
var err error
|
|
|
|
if issue.IsPull {
|
2022-11-19 09:12:33 +01:00
|
|
|
if err = issue.LoadPullRequest(ctx); err != nil {
|
2019-10-28 13:26:46 +08:00
|
|
|
log.Error("LoadPullRequest: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Merge pull request calls issue.changeStatus so we need to handle separately.
|
|
|
|
apiPullRequest := &api.PullRequestPayload{
|
|
|
|
Index: issue.Index,
|
2024-10-16 17:10:05 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, doer),
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, permission),
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2023-01-25 05:47:53 +01:00
|
|
|
CommitID: commitID,
|
2019-10-28 13:26:46 +08:00
|
|
|
}
|
|
|
|
if isClosed {
|
|
|
|
apiPullRequest.Action = api.HookIssueClosed
|
|
|
|
} else {
|
|
|
|
apiPullRequest.Action = api.HookIssueReOpened
|
|
|
|
}
|
2023-01-01 16:23:15 +01:00
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequest, apiPullRequest)
|
2019-10-28 13:26:46 +08:00
|
|
|
} else {
|
|
|
|
apiIssue := &api.IssuePayload{
|
|
|
|
Index: issue.Index,
|
2024-04-09 05:26:41 +08:00
|
|
|
Issue: convert.ToAPIIssue(ctx, doer, issue),
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, permission),
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2023-01-25 05:47:53 +01:00
|
|
|
CommitID: commitID,
|
2019-10-28 13:26:46 +08:00
|
|
|
}
|
|
|
|
if isClosed {
|
|
|
|
apiIssue.Action = api.HookIssueClosed
|
|
|
|
} else {
|
|
|
|
apiIssue.Action = api.HookIssueReOpened
|
|
|
|
}
|
2023-01-01 16:23:15 +01:00
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, apiIssue)
|
2019-10-28 13:26:46 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v, is_closed: %v]: %v", issue.IsPull, isClosed, err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 00:45:43 +08:00
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) NewIssue(ctx context.Context, issue *issues_model.Issue, mentions []*user_model.User) {
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := issue.LoadRepo(ctx); err != nil {
|
2019-11-04 04:59:09 +08:00
|
|
|
log.Error("issue.LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := issue.LoadPoster(ctx); err != nil {
|
2019-11-04 04:59:09 +08:00
|
|
|
log.Error("issue.LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-22 21:08:08 +08:00
|
|
|
permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster)
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, &api.IssuePayload{
|
2019-10-29 00:45:43 +08:00
|
|
|
Action: api.HookIssueOpened,
|
|
|
|
Index: issue.Index,
|
2024-04-09 05:26:41 +08:00
|
|
|
Issue: convert.ToAPIIssue(ctx, issue.Poster, issue),
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, permission),
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, issue.Poster, nil),
|
2019-10-29 00:45:43 +08:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-30 16:36:25 +08:00
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) NewPullRequest(ctx context.Context, pull *issues_model.PullRequest, mentions []*user_model.User) {
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := pull.LoadIssue(ctx); err != nil {
|
2019-11-04 04:59:09 +08:00
|
|
|
log.Error("pull.LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-04-08 17:11:15 +08:00
|
|
|
if err := pull.Issue.LoadRepo(ctx); err != nil {
|
2019-11-04 04:59:09 +08:00
|
|
|
log.Error("pull.Issue.LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := pull.Issue.LoadPoster(ctx); err != nil {
|
2019-11-04 04:59:09 +08:00
|
|
|
log.Error("pull.Issue.LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-22 21:08:08 +08:00
|
|
|
permission, _ := access_model.GetUserRepoPermission(ctx, pull.Issue.Repo, pull.Issue.Poster)
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: pull.Issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{
|
2019-11-04 04:59:09 +08:00
|
|
|
Action: api.HookIssueOpened,
|
|
|
|
Index: pull.Issue.Index,
|
2024-10-16 17:10:05 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, pull, pull.Issue.Poster),
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, pull.Issue.Repo, permission),
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, pull.Issue.Poster, nil),
|
2019-11-04 04:59:09 +08:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) IssueChangeContent(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldContent string) {
|
2022-12-09 07:35:56 +01:00
|
|
|
if err := issue.LoadRepo(ctx); err != nil {
|
|
|
|
log.Error("LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-22 21:08:08 +08:00
|
|
|
permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster)
|
2019-10-30 16:36:25 +08:00
|
|
|
var err error
|
|
|
|
if issue.IsPull {
|
2023-02-21 00:15:49 +00:00
|
|
|
if err := issue.LoadPullRequest(ctx); err != nil {
|
|
|
|
log.Error("LoadPullRequest: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2023-01-01 16:23:15 +01:00
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{
|
2019-10-30 16:36:25 +08:00
|
|
|
Action: api.HookIssueEdited,
|
|
|
|
Index: issue.Index,
|
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Body: &api.ChangesFromPayload{
|
|
|
|
From: oldContent,
|
|
|
|
},
|
|
|
|
},
|
2024-10-16 17:10:05 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, doer),
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, permission),
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-10-30 16:36:25 +08:00
|
|
|
})
|
|
|
|
} else {
|
2023-01-01 16:23:15 +01:00
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssues, &api.IssuePayload{
|
2019-10-30 16:36:25 +08:00
|
|
|
Action: api.HookIssueEdited,
|
|
|
|
Index: issue.Index,
|
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Body: &api.ChangesFromPayload{
|
|
|
|
From: oldContent,
|
|
|
|
},
|
|
|
|
},
|
2024-04-09 05:26:41 +08:00
|
|
|
Issue: convert.ToAPIIssue(ctx, doer, issue),
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, permission),
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-10-30 16:36:25 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
|
|
|
}
|
|
|
|
}
|
2019-10-30 18:02:46 +08:00
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) UpdateComment(ctx context.Context, doer *user_model.User, c *issues_model.Comment, oldContent string) {
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := c.LoadPoster(ctx); err != nil {
|
2019-10-30 18:02:46 +08:00
|
|
|
log.Error("LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := c.LoadIssue(ctx); err != nil {
|
2019-10-30 18:02:46 +08:00
|
|
|
log.Error("LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := c.Issue.LoadAttributes(ctx); err != nil {
|
2019-10-30 18:02:46 +08:00
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-01 16:23:15 +01:00
|
|
|
var eventType webhook_module.HookEventType
|
2024-10-16 17:10:05 +08:00
|
|
|
var pullRequest *api.PullRequest
|
2020-03-05 23:10:48 -06:00
|
|
|
if c.Issue.IsPull {
|
2023-01-01 16:23:15 +01:00
|
|
|
eventType = webhook_module.HookEventPullRequestComment
|
2024-10-16 17:10:05 +08:00
|
|
|
pullRequest = convert.ToAPIPullRequest(ctx, c.Issue.PullRequest, doer)
|
2020-03-05 23:10:48 -06:00
|
|
|
} else {
|
2023-01-01 16:23:15 +01:00
|
|
|
eventType = webhook_module.HookEventIssueComment
|
2020-03-05 23:10:48 -06:00
|
|
|
}
|
|
|
|
|
2023-06-22 21:08:08 +08:00
|
|
|
permission, _ := access_model.GetUserRepoPermission(ctx, c.Issue.Repo, doer)
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: c.Issue.Repo}, eventType, &api.IssueCommentPayload{
|
2024-10-16 17:10:05 +08:00
|
|
|
Action: api.HookIssueCommentEdited,
|
|
|
|
Issue: convert.ToAPIIssue(ctx, doer, c.Issue),
|
|
|
|
PullRequest: pullRequest,
|
|
|
|
Comment: convert.ToAPIComment(ctx, c.Issue.Repo, c),
|
2022-10-21 18:21:56 +02:00
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Body: &api.ChangesFromPayload{
|
|
|
|
From: oldContent,
|
|
|
|
},
|
|
|
|
},
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, c.Issue.Repo, permission),
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2022-10-21 18:21:56 +02:00
|
|
|
IsPull: c.Issue.IsPull,
|
|
|
|
}); err != nil {
|
2019-10-30 18:02:46 +08:00
|
|
|
log.Error("PrepareWebhooks [comment_id: %d]: %v", c.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) CreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository,
|
2022-06-13 17:37:59 +08:00
|
|
|
issue *issues_model.Issue, comment *issues_model.Comment, mentions []*user_model.User,
|
2022-02-23 21:16:07 +01:00
|
|
|
) {
|
2023-01-01 16:23:15 +01:00
|
|
|
var eventType webhook_module.HookEventType
|
2024-10-16 17:10:05 +08:00
|
|
|
var pullRequest *api.PullRequest
|
2020-03-05 23:10:48 -06:00
|
|
|
if issue.IsPull {
|
2023-01-01 16:23:15 +01:00
|
|
|
eventType = webhook_module.HookEventPullRequestComment
|
2024-12-18 22:10:08 -08:00
|
|
|
if err := issue.LoadPullRequest(ctx); err != nil {
|
|
|
|
log.Error("LoadPullRequest: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2024-10-16 17:10:05 +08:00
|
|
|
pullRequest = convert.ToAPIPullRequest(ctx, issue.PullRequest, doer)
|
2020-03-05 23:10:48 -06:00
|
|
|
} else {
|
2023-01-01 16:23:15 +01:00
|
|
|
eventType = webhook_module.HookEventIssueComment
|
2020-03-05 23:10:48 -06:00
|
|
|
}
|
|
|
|
|
2023-06-22 21:08:08 +08:00
|
|
|
permission, _ := access_model.GetUserRepoPermission(ctx, repo, doer)
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, eventType, &api.IssueCommentPayload{
|
2024-10-16 17:10:05 +08:00
|
|
|
Action: api.HookIssueCommentCreated,
|
|
|
|
Issue: convert.ToAPIIssue(ctx, doer, issue),
|
|
|
|
PullRequest: pullRequest,
|
|
|
|
Comment: convert.ToAPIComment(ctx, repo, comment),
|
|
|
|
Repository: convert.ToRepo(ctx, repo, permission),
|
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
|
|
IsPull: issue.IsPull,
|
2022-10-21 18:21:56 +02:00
|
|
|
}); err != nil {
|
2019-10-30 18:02:46 +08:00
|
|
|
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) DeleteComment(ctx context.Context, doer *user_model.User, comment *issues_model.Comment) {
|
2020-03-05 23:10:48 -06:00
|
|
|
var err error
|
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
if err = comment.LoadPoster(ctx); err != nil {
|
2019-10-30 18:02:46 +08:00
|
|
|
log.Error("LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 09:12:33 +01:00
|
|
|
if err = comment.LoadIssue(ctx); err != nil {
|
2019-10-30 18:02:46 +08:00
|
|
|
log.Error("LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
if err = comment.Issue.LoadAttributes(ctx); err != nil {
|
2019-10-30 18:02:46 +08:00
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-01 16:23:15 +01:00
|
|
|
var eventType webhook_module.HookEventType
|
2024-10-16 17:10:05 +08:00
|
|
|
var pullRequest *api.PullRequest
|
2020-03-05 23:10:48 -06:00
|
|
|
if comment.Issue.IsPull {
|
2023-01-01 16:23:15 +01:00
|
|
|
eventType = webhook_module.HookEventPullRequestComment
|
2024-10-16 17:10:05 +08:00
|
|
|
pullRequest = convert.ToAPIPullRequest(ctx, comment.Issue.PullRequest, doer)
|
2020-03-05 23:10:48 -06:00
|
|
|
} else {
|
2023-01-01 16:23:15 +01:00
|
|
|
eventType = webhook_module.HookEventIssueComment
|
2020-03-05 23:10:48 -06:00
|
|
|
}
|
|
|
|
|
2023-06-22 21:08:08 +08:00
|
|
|
permission, _ := access_model.GetUserRepoPermission(ctx, comment.Issue.Repo, doer)
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: comment.Issue.Repo}, eventType, &api.IssueCommentPayload{
|
2024-10-16 17:10:05 +08:00
|
|
|
Action: api.HookIssueCommentDeleted,
|
|
|
|
Issue: convert.ToAPIIssue(ctx, doer, comment.Issue),
|
|
|
|
PullRequest: pullRequest,
|
|
|
|
Comment: convert.ToAPIComment(ctx, comment.Issue.Repo, comment),
|
|
|
|
Repository: convert.ToRepo(ctx, comment.Issue.Repo, permission),
|
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
|
|
IsPull: comment.Issue.IsPull,
|
2022-10-21 18:21:56 +02:00
|
|
|
}); err != nil {
|
2019-10-30 18:02:46 +08:00
|
|
|
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-02 09:49:57 +08:00
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) NewWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) {
|
2022-09-04 21:54:23 +02:00
|
|
|
// Add to hook queue for created wiki page.
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventWiki, &api.WikiPayload{
|
2022-09-04 21:54:23 +02:00
|
|
|
Action: api.HookWikiCreated,
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: 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 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2022-09-04 21:54:23 +02:00
|
|
|
Page: page,
|
|
|
|
Comment: comment,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) EditWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) {
|
2022-09-04 21:54:23 +02:00
|
|
|
// Add to hook queue for edit wiki page.
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventWiki, &api.WikiPayload{
|
2022-09-04 21:54:23 +02:00
|
|
|
Action: api.HookWikiEdited,
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: 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 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2022-09-04 21:54:23 +02:00
|
|
|
Page: page,
|
|
|
|
Comment: comment,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page string) {
|
2022-09-04 21:54:23 +02:00
|
|
|
// Add to hook queue for edit wiki page.
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventWiki, &api.WikiPayload{
|
2022-09-04 21:54:23 +02:00
|
|
|
Action: api.HookWikiDeleted,
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: 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 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2022-09-04 21:54:23 +02:00
|
|
|
Page: page,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [repo_id: %d]: %v", repo.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) IssueChangeLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue,
|
2022-06-13 17:37:59 +08:00
|
|
|
addedLabels, removedLabels []*issues_model.Label,
|
2022-02-23 21:16:07 +01:00
|
|
|
) {
|
2019-11-02 09:49:57 +08:00
|
|
|
var err error
|
|
|
|
|
2022-04-08 17:11:15 +08:00
|
|
|
if err = issue.LoadRepo(ctx); err != nil {
|
2019-11-02 09:49:57 +08:00
|
|
|
log.Error("LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
if err = issue.LoadPoster(ctx); err != nil {
|
2019-11-02 09:49:57 +08:00
|
|
|
log.Error("LoadPoster: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-22 21:08:08 +08:00
|
|
|
permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster)
|
2019-11-02 09:49:57 +08:00
|
|
|
if issue.IsPull {
|
2022-11-19 09:12:33 +01:00
|
|
|
if err = issue.LoadPullRequest(ctx); err != nil {
|
2019-11-02 09:49:57 +08:00
|
|
|
log.Error("loadPullRequest: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2022-11-19 09:12:33 +01:00
|
|
|
if err = issue.PullRequest.LoadIssue(ctx); err != nil {
|
2019-11-02 09:49:57 +08:00
|
|
|
log.Error("LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2023-01-01 16:23:15 +01:00
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequestLabel, &api.PullRequestPayload{
|
2019-11-02 09:49:57 +08:00
|
|
|
Action: api.HookIssueLabelUpdated,
|
|
|
|
Index: issue.Index,
|
2024-10-16 17:10:05 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, doer),
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, access_model.Permission{AccessMode: 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 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-11-02 09:49:57 +08:00
|
|
|
})
|
|
|
|
} else {
|
2023-01-01 16:23:15 +01:00
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueLabel, &api.IssuePayload{
|
2019-11-02 09:49:57 +08:00
|
|
|
Action: api.HookIssueLabelUpdated,
|
|
|
|
Index: issue.Index,
|
2024-04-09 05:26:41 +08:00
|
|
|
Issue: convert.ToAPIIssue(ctx, doer, issue),
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, permission),
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-11-02 09:49:57 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-02 11:33:20 +08:00
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) IssueChangeMilestone(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldMilestoneID int64) {
|
2019-11-02 11:33:20 +08:00
|
|
|
var hookAction api.HookIssueAction
|
|
|
|
var err error
|
|
|
|
if issue.MilestoneID > 0 {
|
|
|
|
hookAction = api.HookIssueMilestoned
|
|
|
|
} else {
|
|
|
|
hookAction = api.HookIssueDemilestoned
|
|
|
|
}
|
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
if err = issue.LoadAttributes(ctx); err != nil {
|
2019-11-02 11:33:20 +08:00
|
|
|
log.Error("issue.LoadAttributes failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-22 21:08:08 +08:00
|
|
|
permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, doer)
|
2019-11-02 11:33:20 +08:00
|
|
|
if issue.IsPull {
|
2022-11-19 09:12:33 +01:00
|
|
|
err = issue.PullRequest.LoadIssue(ctx)
|
2019-11-02 11:33:20 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Error("LoadIssue: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2023-01-01 16:23:15 +01:00
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequestMilestone, &api.PullRequestPayload{
|
2019-11-02 11:33:20 +08:00
|
|
|
Action: hookAction,
|
|
|
|
Index: issue.Index,
|
2024-10-16 17:10:05 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, doer),
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, permission),
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-11-02 11:33:20 +08:00
|
|
|
})
|
|
|
|
} else {
|
2023-01-01 16:23:15 +01:00
|
|
|
err = PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventIssueMilestone, &api.IssuePayload{
|
2019-11-02 11:33:20 +08:00
|
|
|
Action: hookAction,
|
|
|
|
Index: issue.Index,
|
2024-04-09 05:26:41 +08:00
|
|
|
Issue: convert.ToAPIIssue(ctx, doer, issue),
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, permission),
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-11-02 11:33:20 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-03 14:59:26 +08:00
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) PushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
|
2025-02-07 19:26:50 +00:00
|
|
|
if len(commits.Commits) > setting.Webhook.PayloadCommitLimit {
|
|
|
|
commits.Commits = commits.Commits[:setting.Webhook.PayloadCommitLimit]
|
|
|
|
}
|
|
|
|
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
apiPusher := convert.ToUser(ctx, pusher, nil)
|
2022-01-19 23:26:57 +00:00
|
|
|
apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(ctx, repo.RepoPath(), repo.HTMLURL())
|
2019-11-03 14:59:26 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Error("commits.ToAPIPayloadCommits failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{
|
2023-05-26 09:04:48 +08:00
|
|
|
Ref: opts.RefFullName.String(),
|
2022-10-16 18:22:34 +02:00
|
|
|
Before: opts.OldCommitID,
|
|
|
|
After: opts.NewCommitID,
|
|
|
|
CompareURL: setting.AppURL + commits.CompareURL,
|
|
|
|
Commits: apiCommits,
|
|
|
|
TotalCommits: commits.Len,
|
|
|
|
HeadCommit: apiHeadCommit,
|
2023-06-22 21:08:08 +08:00
|
|
|
Repo: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm.AccessModeOwner}),
|
2022-10-16 18:22:34 +02:00
|
|
|
Pusher: apiPusher,
|
|
|
|
Sender: apiPusher,
|
2019-11-03 14:59:26 +08:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-05 19:04:08 +08:00
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) AutoMergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
|
|
|
|
// just redirect to the MergePullRequest
|
|
|
|
m.MergePullRequest(ctx, doer, pr)
|
2022-11-03 16:49:00 +01:00
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (*webhookNotifier) MergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
|
2019-11-22 01:08:42 +08:00
|
|
|
// Reload pull request information.
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := pr.LoadAttributes(ctx); err != nil {
|
2019-11-22 01:08:42 +08:00
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := pr.LoadIssue(ctx); err != nil {
|
|
|
|
log.Error("LoadIssue: %v", err)
|
2019-11-22 01:08:42 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-08 17:11:15 +08:00
|
|
|
if err := pr.Issue.LoadRepo(ctx); err != nil {
|
2019-11-22 01:08:42 +08:00
|
|
|
log.Error("pr.Issue.LoadRepo: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-22 21:08:08 +08:00
|
|
|
permission, err := access_model.GetUserRepoPermission(ctx, pr.Issue.Repo, doer)
|
2019-11-22 01:08:42 +08:00
|
|
|
if err != nil {
|
2023-06-22 21:08:08 +08:00
|
|
|
log.Error("models.GetUserRepoPermission: %v", err)
|
2019-11-22 01:08:42 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge pull request calls issue.changeStatus so we need to handle separately.
|
|
|
|
apiPullRequest := &api.PullRequestPayload{
|
|
|
|
Index: pr.Issue.Index,
|
2024-10-16 17:10:05 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, pr, doer),
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, pr.Issue.Repo, permission),
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-11-22 01:08:42 +08:00
|
|
|
Action: api.HookIssueClosed,
|
|
|
|
}
|
|
|
|
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: pr.Issue.Repo}, webhook_module.HookEventPullRequest, apiPullRequest); err != nil {
|
2019-11-22 01:08:42 +08:00
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) PullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch string) {
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := pr.LoadIssue(ctx); err != nil {
|
|
|
|
log.Error("LoadIssue: %v", err)
|
2019-12-16 07:20:25 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
issue := pr.Issue
|
|
|
|
|
2023-06-22 21:08:08 +08:00
|
|
|
mode, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, issue.Poster)
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequest, &api.PullRequestPayload{
|
2019-12-16 07:20:25 +01:00
|
|
|
Action: api.HookIssueEdited,
|
|
|
|
Index: issue.Index,
|
|
|
|
Changes: &api.ChangesPayload{
|
|
|
|
Ref: &api.ChangesFromPayload{
|
|
|
|
From: oldBranch,
|
|
|
|
},
|
|
|
|
},
|
2024-10-16 17:10:05 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, pr, doer),
|
2022-12-03 10:48:26 +08:00
|
|
|
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 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2022-11-19 09:12:33 +01:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [pr: %d]: %v", pr.ID, err)
|
2019-12-16 07:20:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) PullRequestReview(ctx context.Context, pr *issues_model.PullRequest, review *issues_model.Review, comment *issues_model.Comment, mentions []*user_model.User) {
|
2023-01-01 16:23:15 +01:00
|
|
|
var reviewHookType webhook_module.HookEventType
|
2019-11-05 19:04:08 +08:00
|
|
|
|
|
|
|
switch review.Type {
|
2022-06-13 17:37:59 +08:00
|
|
|
case issues_model.ReviewTypeApprove:
|
2023-01-01 16:23:15 +01:00
|
|
|
reviewHookType = webhook_module.HookEventPullRequestReviewApproved
|
2022-06-13 17:37:59 +08:00
|
|
|
case issues_model.ReviewTypeComment:
|
2023-03-24 13:13:04 +08:00
|
|
|
reviewHookType = webhook_module.HookEventPullRequestReviewComment
|
2022-06-13 17:37:59 +08:00
|
|
|
case issues_model.ReviewTypeReject:
|
2023-01-01 16:23:15 +01:00
|
|
|
reviewHookType = webhook_module.HookEventPullRequestReviewRejected
|
2019-11-05 19:04:08 +08:00
|
|
|
default:
|
|
|
|
// unsupported review webhook type here
|
|
|
|
log.Error("Unsupported review webhook type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := pr.LoadIssue(ctx); err != nil {
|
|
|
|
log.Error("LoadIssue: %v", err)
|
2019-11-05 19:04:08 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-22 21:08:08 +08:00
|
|
|
permission, err := access_model.GetUserRepoPermission(ctx, review.Issue.Repo, review.Issue.Poster)
|
2019-11-05 19:04:08 +08:00
|
|
|
if err != nil {
|
2023-06-22 21:08:08 +08:00
|
|
|
log.Error("models.GetUserRepoPermission: %v", err)
|
2019-11-05 19:04:08 +08:00
|
|
|
return
|
|
|
|
}
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: review.Issue.Repo}, reviewHookType, &api.PullRequestPayload{
|
2024-10-16 17:10:05 +08:00
|
|
|
Action: api.HookIssueReviewed,
|
|
|
|
Index: review.Issue.Index,
|
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, pr, review.Reviewer),
|
|
|
|
RequestedReviewer: convert.ToUser(ctx, review.Reviewer, nil),
|
|
|
|
Repository: convert.ToRepo(ctx, review.Issue.Repo, permission),
|
|
|
|
Sender: convert.ToUser(ctx, review.Reviewer, nil),
|
2019-11-05 19:04:08 +08:00
|
|
|
Review: &api.ReviewPayload{
|
|
|
|
Type: string(reviewHookType),
|
|
|
|
Content: review.Content,
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) PullRequestReviewRequest(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, reviewer *user_model.User, isRequest bool, comment *issues_model.Comment) {
|
2023-05-25 10:06:27 +08:00
|
|
|
if !issue.IsPull {
|
2023-09-06 02:37:47 +08:00
|
|
|
log.Warn("PullRequestReviewRequest: issue is not a pull request: %v", issue.ID)
|
2023-05-25 10:06:27 +08:00
|
|
|
return
|
|
|
|
}
|
2023-06-22 21:08:08 +08:00
|
|
|
permission, _ := access_model.GetUserRepoPermission(ctx, issue.Repo, doer)
|
2023-05-25 10:06:27 +08:00
|
|
|
if err := issue.LoadPullRequest(ctx); err != nil {
|
|
|
|
log.Error("LoadPullRequest failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
apiPullRequest := &api.PullRequestPayload{
|
|
|
|
Index: issue.Index,
|
2024-10-16 17:10:05 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, doer),
|
2023-05-25 10:06:27 +08:00
|
|
|
RequestedReviewer: convert.ToUser(ctx, reviewer, nil),
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, issue.Repo, permission),
|
2023-05-25 10:06:27 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
|
|
|
}
|
|
|
|
if isRequest {
|
|
|
|
apiPullRequest.Action = api.HookIssueReviewRequested
|
|
|
|
} else {
|
|
|
|
apiPullRequest.Action = api.HookIssueReviewRequestRemoved
|
|
|
|
}
|
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: issue.Repo}, webhook_module.HookEventPullRequestReviewRequest, apiPullRequest); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [review_requested: %v]: %v", isRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) CreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName, 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 21:37:34 +08:00
|
|
|
apiPusher := convert.ToUser(ctx, pusher, nil)
|
2023-06-22 21:08:08 +08:00
|
|
|
apiRepo := convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm.AccessModeNone})
|
2019-11-06 14:43:03 +08:00
|
|
|
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventCreate, &api.CreatePayload{
|
2024-04-05 11:33:55 +02:00
|
|
|
Ref: refFullName.String(),
|
2022-01-19 23:26:57 +00:00
|
|
|
Sha: refID,
|
2023-06-13 14:05:28 +08:00
|
|
|
RefType: refFullName.RefType(),
|
2019-11-06 14:43:03 +08:00
|
|
|
Repo: apiRepo,
|
|
|
|
Sender: apiPusher,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) PullRequestSynchronized(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := pr.LoadIssue(ctx); err != nil {
|
|
|
|
log.Error("LoadIssue: %v", err)
|
2019-11-05 19:04:08 +08:00
|
|
|
return
|
|
|
|
}
|
2022-11-19 09:12:33 +01:00
|
|
|
if err := pr.Issue.LoadAttributes(ctx); err != nil {
|
2019-11-05 19:04:08 +08:00
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: pr.Issue.Repo}, webhook_module.HookEventPullRequestSync, &api.PullRequestPayload{
|
2019-11-05 19:04:08 +08:00
|
|
|
Action: api.HookIssueSynchronized,
|
|
|
|
Index: pr.Issue.Index,
|
2024-10-16 17:10:05 +08:00
|
|
|
PullRequest: convert.ToAPIPullRequest(ctx, pr, doer),
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, pr.Issue.Repo, access_model.Permission{AccessMode: 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 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-11-05 19:04:08 +08:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks [pull_id: %v]: %v", pr.ID, err)
|
|
|
|
}
|
|
|
|
}
|
2019-11-06 14:43:03 +08:00
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) DeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName) {
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
apiPusher := convert.ToUser(ctx, pusher, nil)
|
2023-06-22 21:08:08 +08:00
|
|
|
apiRepo := convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm.AccessModeOwner})
|
2019-11-06 14:43:03 +08:00
|
|
|
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventDelete, &api.DeletePayload{
|
2024-04-05 11:33:55 +02:00
|
|
|
Ref: refFullName.String(),
|
2023-06-13 14:05:28 +08:00
|
|
|
RefType: refFullName.RefType(),
|
2019-11-06 14:43:03 +08:00
|
|
|
PusherType: api.PusherTypeUser,
|
|
|
|
Repo: apiRepo,
|
|
|
|
Sender: apiPusher,
|
|
|
|
}); err != nil {
|
2023-06-13 14:05:28 +08:00
|
|
|
log.Error("PrepareWebhooks.(delete %s): %v", refFullName.RefType(), err)
|
2019-11-06 14:43:03 +08:00
|
|
|
}
|
|
|
|
}
|
2019-11-06 16:25:50 +08:00
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
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 16:25:50 +08:00
|
|
|
log.Error("LoadAttributes: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-22 21:08:08 +08:00
|
|
|
permission, _ := access_model.GetUserRepoPermission(ctx, rel.Repo, doer)
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: rel.Repo}, webhook_module.HookEventRelease, &api.ReleasePayload{
|
2019-11-06 16:25:50 +08:00
|
|
|
Action: action,
|
2023-07-10 17:31:19 +08:00
|
|
|
Release: convert.ToAPIRelease(ctx, rel.Repo, rel),
|
2023-06-22 21:08:08 +08:00
|
|
|
Repository: convert.ToRepo(ctx, rel.Repo, permission),
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, doer, nil),
|
2019-11-06 16:25:50 +08:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) NewRelease(ctx context.Context, rel *repo_model.Release) {
|
2022-11-19 09:12:33 +01:00
|
|
|
sendReleaseHook(ctx, rel.Publisher, rel, api.HookReleasePublished)
|
2019-11-06 16:25:50 +08:00
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) UpdateRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) {
|
2022-11-19 09:12:33 +01:00
|
|
|
sendReleaseHook(ctx, doer, rel, api.HookReleaseUpdated)
|
2019-11-06 16:25:50 +08:00
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) DeleteRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) {
|
2022-11-19 09:12:33 +01:00
|
|
|
sendReleaseHook(ctx, doer, rel, api.HookReleaseDeleted)
|
2019-11-06 16:25:50 +08:00
|
|
|
}
|
2019-11-24 13:16:59 +08:00
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) SyncPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) {
|
2025-02-07 19:26:50 +00:00
|
|
|
if len(commits.Commits) > setting.Webhook.PayloadCommitLimit {
|
|
|
|
commits.Commits = commits.Commits[:setting.Webhook.PayloadCommitLimit]
|
|
|
|
}
|
|
|
|
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
apiPusher := convert.ToUser(ctx, pusher, nil)
|
2022-01-19 23:26:57 +00:00
|
|
|
apiCommits, apiHeadCommit, err := commits.ToAPIPayloadCommits(ctx, repo.RepoPath(), repo.HTMLURL())
|
2019-11-24 13:16:59 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Error("commits.ToAPIPayloadCommits failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventPush, &api.PushPayload{
|
2023-05-26 09:04:48 +08:00
|
|
|
Ref: opts.RefFullName.String(),
|
2022-10-16 18:22:34 +02:00
|
|
|
Before: opts.OldCommitID,
|
|
|
|
After: opts.NewCommitID,
|
|
|
|
CompareURL: setting.AppURL + commits.CompareURL,
|
|
|
|
Commits: apiCommits,
|
|
|
|
TotalCommits: commits.Len,
|
|
|
|
HeadCommit: apiHeadCommit,
|
2023-06-22 21:08:08 +08:00
|
|
|
Repo: convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm.AccessModeOwner}),
|
2022-10-16 18:22:34 +02:00
|
|
|
Pusher: apiPusher,
|
|
|
|
Sender: apiPusher,
|
2019-11-24 13:16:59 +08:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2020-11-14 03:12:33 +08:00
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) SyncCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName, refID string) {
|
|
|
|
m.CreateRef(ctx, pusher, repo, refFullName, refID)
|
2020-11-14 03:12:33 +08:00
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) SyncDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName) {
|
|
|
|
m.DeleteRef(ctx, pusher, repo, refFullName)
|
2020-11-14 03:12:33 +08:00
|
|
|
}
|
2022-03-30 10:42:47 +02:00
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) PackageCreate(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) {
|
2022-11-19 09:12:33 +01:00
|
|
|
notifyPackage(ctx, doer, pd, api.HookPackageCreated)
|
2022-03-30 10:42:47 +02:00
|
|
|
}
|
|
|
|
|
2023-09-06 02:37:47 +08:00
|
|
|
func (m *webhookNotifier) PackageDelete(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) {
|
2022-11-19 09:12:33 +01:00
|
|
|
notifyPackage(ctx, doer, pd, api.HookPackageDeleted)
|
2022-03-30 10:42:47 +02:00
|
|
|
}
|
|
|
|
|
Actions Failure, Succes, Recover Webhooks (#7508)
Implement Actions Success, Failure and Recover webhooks for Forgejo, Gitea, Gogs, Slack, Discord, DingTalk, Telegram, Microsoft Teams, Feishu / Lark Suite, Matrix, WeCom (Wechat Work), Packagist. Some of these webhooks have not been manually tested.
Implement settings for these new webhooks.
## Checklist
The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).
### Tests
- I added test coverage for Go changes...
- [x] in their respective `*_test.go` for unit tests.
- [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I added test coverage for JavaScript changes...
- [ ] in `web_src/js/*.test.js` if it can be unit tested.
- [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)).
### Documentation
- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.
### Release notes
- [ ] I do not want this change to show in the release notes.
- [x] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.
<!--start release-notes-assistant-->
## Release notes
<!--URL:https://codeberg.org/forgejo/forgejo-->
- Features
- [PR](https://codeberg.org/forgejo/forgejo/pulls/7508): <!--number 7508 --><!--line 0 --><!--description QWN0aW9ucyBGYWlsdXJlLCBTdWNjZXMsIFJlY292ZXIgV2ViaG9va3M=-->Actions Failure, Succes, Recover Webhooks<!--description-->
<!--end release-notes-assistant-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7508
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: christopher-besch <mail@chris-besch.com>
Co-committed-by: christopher-besch <mail@chris-besch.com>
2025-06-03 14:29:19 +02:00
|
|
|
func (m *webhookNotifier) ActionRunNowDone(ctx context.Context, run *actions_model.ActionRun, priorStatus actions_model.Status, lastRun *actions_model.ActionRun) {
|
|
|
|
source := EventSource{
|
|
|
|
Repository: run.Repo,
|
|
|
|
Owner: run.TriggerUser,
|
|
|
|
}
|
|
|
|
|
|
|
|
payload := &api.ActionPayload{
|
|
|
|
Run: convert.ToActionRun(ctx, run),
|
|
|
|
LastRun: convert.ToActionRun(ctx, lastRun),
|
|
|
|
PriorStatus: priorStatus.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if run.Status.IsSuccess() {
|
|
|
|
payload.Action = api.HookActionSuccess
|
|
|
|
if err := PrepareWebhooks(ctx, source, webhook_module.HookEventActionRunSuccess, payload); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
// send another event when this is a recover
|
|
|
|
if lastRun != nil && !lastRun.Status.IsSuccess() {
|
|
|
|
payload.Action = api.HookActionRecover
|
|
|
|
if err := PrepareWebhooks(ctx, source, webhook_module.HookEventActionRunRecover, payload); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
payload.Action = api.HookActionFailure
|
|
|
|
if err := PrepareWebhooks(ctx, source, webhook_module.HookEventActionRunFailure, payload); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 09:12:33 +01:00
|
|
|
func notifyPackage(ctx context.Context, sender *user_model.User, pd *packages_model.PackageDescriptor, action api.HookPackageAction) {
|
2023-01-01 16:23:15 +01:00
|
|
|
source := EventSource{
|
2022-10-21 18:21:56 +02:00
|
|
|
Repository: pd.Repository,
|
|
|
|
Owner: pd.Owner,
|
2022-03-30 10:42:47 +02:00
|
|
|
}
|
|
|
|
|
2022-05-07 18:21:15 +02:00
|
|
|
apiPackage, err := convert.ToPackage(ctx, pd, sender)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error converting package: %v", err)
|
|
|
|
return
|
2022-03-30 10:42:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := PrepareWebhooks(ctx, source, webhook_module.HookEventPackage, &api.PackagePayload{
|
2022-05-07 18:21:15 +02:00
|
|
|
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 21:37:34 +08:00
|
|
|
Sender: convert.ToUser(ctx, sender, nil),
|
2022-03-30 10:42:47 +02:00
|
|
|
}); err != nil {
|
|
|
|
log.Error("PrepareWebhooks: %v", err)
|
|
|
|
}
|
|
|
|
}
|