mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-06-22 09:18:30 -04:00
Add packagist webhook (#18224)
Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
parent
87141b908d
commit
3349fd8f79
20 changed files with 416 additions and 3 deletions
|
@ -682,6 +682,59 @@ func WechatworkHooksNewPost(ctx *context.Context) {
|
|||
ctx.Redirect(orCtx.Link)
|
||||
}
|
||||
|
||||
// PackagistHooksNewPost response for creating packagist hook
|
||||
func PackagistHooksNewPost(ctx *context.Context) {
|
||||
form := web.GetForm(ctx).(*forms.NewPackagistHookForm)
|
||||
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
||||
ctx.Data["PageIsSettingsHooks"] = true
|
||||
ctx.Data["PageIsSettingsHooksNew"] = true
|
||||
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
|
||||
ctx.Data["HookType"] = webhook.PACKAGIST
|
||||
|
||||
orCtx, err := getOrgRepoCtx(ctx)
|
||||
if err != nil {
|
||||
ctx.ServerError("getOrgRepoCtx", err)
|
||||
return
|
||||
}
|
||||
|
||||
if ctx.HasError() {
|
||||
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
||||
return
|
||||
}
|
||||
|
||||
meta, err := json.Marshal(&webhook_service.PackagistMeta{
|
||||
Username: form.Username,
|
||||
APIToken: form.APIToken,
|
||||
PackageURL: form.PackageURL,
|
||||
})
|
||||
if err != nil {
|
||||
ctx.ServerError("Marshal", err)
|
||||
return
|
||||
}
|
||||
|
||||
w := &webhook.Webhook{
|
||||
RepoID: orCtx.RepoID,
|
||||
URL: fmt.Sprintf("https://packagist.org/api/update-package?username=%s&apiToken=%s", url.QueryEscape(form.Username), url.QueryEscape(form.APIToken)),
|
||||
ContentType: webhook.ContentTypeJSON,
|
||||
HookEvent: ParseHookEvent(form.WebhookForm),
|
||||
IsActive: form.Active,
|
||||
Type: webhook.PACKAGIST,
|
||||
Meta: string(meta),
|
||||
OrgID: orCtx.OrgID,
|
||||
IsSystemWebhook: orCtx.IsSystemWebhook,
|
||||
}
|
||||
if err := w.UpdateEvent(); err != nil {
|
||||
ctx.ServerError("UpdateEvent", err)
|
||||
return
|
||||
} else if err := webhook.CreateWebhook(db.DefaultContext, w); err != nil {
|
||||
ctx.ServerError("CreateWebhook", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
||||
ctx.Redirect(orCtx.Link)
|
||||
}
|
||||
|
||||
func checkWebhook(ctx *context.Context) (*orgRepoCtx, *webhook.Webhook) {
|
||||
ctx.Data["RequireHighlightJS"] = true
|
||||
|
||||
|
@ -719,6 +772,8 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *webhook.Webhook) {
|
|||
ctx.Data["TelegramHook"] = webhook_service.GetTelegramHook(w)
|
||||
case webhook.MATRIX:
|
||||
ctx.Data["MatrixHook"] = webhook_service.GetMatrixHook(w)
|
||||
case webhook.PACKAGIST:
|
||||
ctx.Data["PackagistHook"] = webhook_service.GetPackagistHook(w)
|
||||
}
|
||||
|
||||
ctx.Data["History"], err = w.History(1)
|
||||
|
@ -1137,6 +1192,50 @@ func WechatworkHooksEditPost(ctx *context.Context) {
|
|||
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
||||
}
|
||||
|
||||
// PackagistHooksEditPost response for editing packagist hook
|
||||
func PackagistHooksEditPost(ctx *context.Context) {
|
||||
form := web.GetForm(ctx).(*forms.NewPackagistHookForm)
|
||||
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
||||
ctx.Data["PageIsSettingsHooks"] = true
|
||||
ctx.Data["PageIsSettingsHooksEdit"] = true
|
||||
|
||||
orCtx, w := checkWebhook(ctx)
|
||||
if ctx.Written() {
|
||||
return
|
||||
}
|
||||
ctx.Data["Webhook"] = w
|
||||
|
||||
if ctx.HasError() {
|
||||
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
||||
return
|
||||
}
|
||||
|
||||
meta, err := json.Marshal(&webhook_service.PackagistMeta{
|
||||
Username: form.Username,
|
||||
APIToken: form.APIToken,
|
||||
PackageURL: form.PackageURL,
|
||||
})
|
||||
if err != nil {
|
||||
ctx.ServerError("Marshal", err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Meta = string(meta)
|
||||
w.URL = fmt.Sprintf("https://packagist.org/api/update-package?username=%s&apiToken=%s", url.QueryEscape(form.Username), url.QueryEscape(form.APIToken))
|
||||
w.HookEvent = ParseHookEvent(form.WebhookForm)
|
||||
w.IsActive = form.Active
|
||||
if err := w.UpdateEvent(); err != nil {
|
||||
ctx.ServerError("UpdateEvent", err)
|
||||
return
|
||||
} else if err := webhook.UpdateWebhook(w); err != nil {
|
||||
ctx.ServerError("UpdateWebhook", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
|
||||
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
||||
}
|
||||
|
||||
// TestWebhook test if web hook is work fine
|
||||
func TestWebhook(ctx *context.Context) {
|
||||
hookID := ctx.ParamsInt64(":id")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue