1
0
Fork 0
forked from forgejo/forgejo

Add user webhooks (#21563)

Currently we can add webhooks for organizations but not for users. This
PR adds the latter. You can access it from the current users settings.


![grafik](https://user-images.githubusercontent.com/1666336/197391408-15dfdc23-b476-4d0c-82f7-9bc9b065988f.png)
This commit is contained in:
KN4CK3R 2023-03-10 15:28:32 +01:00 committed by GitHub
parent dad057b639
commit 2173f14708
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 737 additions and 234 deletions

View file

@ -33,6 +33,7 @@ const (
tplHooks base.TplName = "repo/settings/webhook/base"
tplHookNew base.TplName = "repo/settings/webhook/new"
tplOrgHookNew base.TplName = "org/settings/hook_new"
tplUserHookNew base.TplName = "user/settings/hook_new"
tplAdminHookNew base.TplName = "admin/hook_new"
)
@ -54,8 +55,8 @@ func Webhooks(ctx *context.Context) {
ctx.HTML(http.StatusOK, tplHooks)
}
type orgRepoCtx struct {
OrgID int64
type ownerRepoCtx struct {
OwnerID int64
RepoID int64
IsAdmin bool
IsSystemWebhook bool
@ -64,10 +65,10 @@ type orgRepoCtx struct {
NewTemplate base.TplName
}
// getOrgRepoCtx determines whether this is a repo, organization, or admin (both default and system) context.
func getOrgRepoCtx(ctx *context.Context) (*orgRepoCtx, error) {
if len(ctx.Repo.RepoLink) > 0 {
return &orgRepoCtx{
// getOwnerRepoCtx determines whether this is a repo, owner, or admin (both default and system) context.
func getOwnerRepoCtx(ctx *context.Context) (*ownerRepoCtx, error) {
if is, ok := ctx.Data["IsRepositoryWebhook"]; ok && is.(bool) {
return &ownerRepoCtx{
RepoID: ctx.Repo.Repository.ID,
Link: path.Join(ctx.Repo.RepoLink, "settings/hooks"),
LinkNew: path.Join(ctx.Repo.RepoLink, "settings/hooks"),
@ -75,37 +76,35 @@ func getOrgRepoCtx(ctx *context.Context) (*orgRepoCtx, error) {
}, nil
}
if len(ctx.Org.OrgLink) > 0 {
return &orgRepoCtx{
OrgID: ctx.Org.Organization.ID,
if is, ok := ctx.Data["IsOrganizationWebhook"]; ok && is.(bool) {
return &ownerRepoCtx{
OwnerID: ctx.ContextUser.ID,
Link: path.Join(ctx.Org.OrgLink, "settings/hooks"),
LinkNew: path.Join(ctx.Org.OrgLink, "settings/hooks"),
NewTemplate: tplOrgHookNew,
}, nil
}
if ctx.Doer.IsAdmin {
// Are we looking at default webhooks?
if ctx.Params(":configType") == "default-hooks" {
return &orgRepoCtx{
IsAdmin: true,
Link: path.Join(setting.AppSubURL, "/admin/hooks"),
LinkNew: path.Join(setting.AppSubURL, "/admin/default-hooks"),
NewTemplate: tplAdminHookNew,
}, nil
}
if is, ok := ctx.Data["IsUserWebhook"]; ok && is.(bool) {
return &ownerRepoCtx{
OwnerID: ctx.Doer.ID,
Link: path.Join(setting.AppSubURL, "/user/settings/hooks"),
LinkNew: path.Join(setting.AppSubURL, "/user/settings/hooks"),
NewTemplate: tplUserHookNew,
}, nil
}
// Must be system webhooks instead
return &orgRepoCtx{
if ctx.Doer.IsAdmin {
return &ownerRepoCtx{
IsAdmin: true,
IsSystemWebhook: true,
IsSystemWebhook: ctx.Params(":configType") == "system-hooks",
Link: path.Join(setting.AppSubURL, "/admin/hooks"),
LinkNew: path.Join(setting.AppSubURL, "/admin/system-hooks"),
NewTemplate: tplAdminHookNew,
}, nil
}
return nil, errors.New("unable to set OrgRepo context")
return nil, errors.New("unable to set OwnerRepo context")
}
func checkHookType(ctx *context.Context) string {
@ -122,9 +121,9 @@ func WebhooksNew(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook_module.HookEvent{}}
orCtx, err := getOrgRepoCtx(ctx)
orCtx, err := getOwnerRepoCtx(ctx)
if err != nil {
ctx.ServerError("getOrgRepoCtx", err)
ctx.ServerError("getOwnerRepoCtx", err)
return
}
@ -205,9 +204,9 @@ func createWebhook(ctx *context.Context, params webhookParams) {
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook_module.HookEvent{}}
ctx.Data["HookType"] = params.Type
orCtx, err := getOrgRepoCtx(ctx)
orCtx, err := getOwnerRepoCtx(ctx)
if err != nil {
ctx.ServerError("getOrgRepoCtx", err)
ctx.ServerError("getOwnerRepoCtx", err)
return
}
ctx.Data["BaseLink"] = orCtx.LinkNew
@ -236,7 +235,7 @@ func createWebhook(ctx *context.Context, params webhookParams) {
IsActive: params.WebhookForm.Active,
Type: params.Type,
Meta: string(meta),
OrgID: orCtx.OrgID,
OwnerID: orCtx.OwnerID,
IsSystemWebhook: orCtx.IsSystemWebhook,
}
err = w.SetHeaderAuthorization(params.WebhookForm.AuthorizationHeader)
@ -577,19 +576,19 @@ func packagistHookParams(ctx *context.Context) webhookParams {
}
}
func checkWebhook(ctx *context.Context) (*orgRepoCtx, *webhook.Webhook) {
orCtx, err := getOrgRepoCtx(ctx)
func checkWebhook(ctx *context.Context) (*ownerRepoCtx, *webhook.Webhook) {
orCtx, err := getOwnerRepoCtx(ctx)
if err != nil {
ctx.ServerError("getOrgRepoCtx", err)
ctx.ServerError("getOwnerRepoCtx", err)
return nil, nil
}
ctx.Data["BaseLink"] = orCtx.Link
var w *webhook.Webhook
if orCtx.RepoID > 0 {
w, err = webhook.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
} else if orCtx.OrgID > 0 {
w, err = webhook.GetWebhookByOrgID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id"))
w, err = webhook.GetWebhookByRepoID(orCtx.RepoID, ctx.ParamsInt64(":id"))
} else if orCtx.OwnerID > 0 {
w, err = webhook.GetWebhookByOwnerID(orCtx.OwnerID, ctx.ParamsInt64(":id"))
} else if orCtx.IsAdmin {
w, err = webhook.GetSystemOrDefaultWebhook(ctx, ctx.ParamsInt64(":id"))
}