mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-04-20 05:59:22 +00:00
wip: webhook
This commit is contained in:
parent
21fb2f8fe0
commit
c9c5e3fe9c
5 changed files with 58 additions and 26 deletions
|
@ -33,6 +33,8 @@ const (
|
|||
HookEventPackage HookEventType = "package"
|
||||
HookEventSchedule HookEventType = "schedule"
|
||||
HookEventWorkflowDispatch HookEventType = "workflow_dispatch"
|
||||
HookEventActionRunSuccessAfterFailure HookEventType = "action_run_success_after_failur"
|
||||
HookEventActionRunFailure HookEventType = "action_run_hook_event_failure"
|
||||
)
|
||||
|
||||
// Event returns the HookEventType as an event string
|
||||
|
@ -65,6 +67,10 @@ func (h HookEventType) Event() string {
|
|||
return "repository"
|
||||
case HookEventRelease:
|
||||
return "release"
|
||||
case HookEventActionRunSuccessAfterFailure:
|
||||
return "action_run_success_after_failur"
|
||||
case HookEventActionRunFailure:
|
||||
return "action_run_hook_event_failure"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ package webhook
|
|||
import (
|
||||
"context"
|
||||
|
||||
actions_model "forgejo.org/models/actions"
|
||||
issues_model "forgejo.org/models/issues"
|
||||
packages_model "forgejo.org/models/packages"
|
||||
"forgejo.org/models/perm"
|
||||
|
@ -887,6 +888,27 @@ func (m *webhookNotifier) PackageDelete(ctx context.Context, doer *user_model.Us
|
|||
notifyPackage(ctx, doer, pd, api.HookPackageDeleted)
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
if run.Status.IsSuccess() {
|
||||
if lastRun.Status.IsSuccess() {
|
||||
return
|
||||
}
|
||||
|
||||
if err := PrepareWebhooks(ctx, source, webhook_module.HookEventActionRunSuccessAfterFailure, &api.PackagePayload{}); err != nil {
|
||||
log.Error("PrepareWebhooks: %v", err)
|
||||
}
|
||||
} else {
|
||||
if err := PrepareWebhooks(ctx, source, webhook_module.HookEventActionRunFailure, &api.PackagePayload{}); err != nil {
|
||||
log.Error("PrepareWebhooks: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func notifyPackage(ctx context.Context, sender *user_model.User, pd *packages_model.PackageDescriptor, action api.HookPackageAction) {
|
||||
source := EventSource{
|
||||
Repository: pd.Repository,
|
||||
|
|
|
@ -36,6 +36,7 @@ type PayloadConvertor[T any] interface {
|
|||
Release(*api.ReleasePayload) (T, error)
|
||||
Wiki(*api.WikiPayload) (T, error)
|
||||
Package(*api.PackagePayload) (T, error)
|
||||
Action(*api.PackagePayload) (T, error)
|
||||
}
|
||||
|
||||
func convertUnmarshalledJSON[T, P any](convert func(P) (T, error), data []byte) (T, error) {
|
||||
|
@ -86,6 +87,8 @@ func NewPayload[T any](rc PayloadConvertor[T], data []byte, event webhook_module
|
|||
return convertUnmarshalledJSON(rc.Wiki, data)
|
||||
case webhook_module.HookEventPackage:
|
||||
return convertUnmarshalledJSON(rc.Package, data)
|
||||
case webhook_module.HookEventActionRunSuccessAfterFailure, webhook_module.HookEventActionRunFailure:
|
||||
return convertUnmarshalledJSON(rc.Action, data)
|
||||
}
|
||||
var t T
|
||||
return t, fmt.Errorf("newPayload unsupported event: %s", event)
|
||||
|
|
|
@ -142,6 +142,7 @@ func SlackLinkToRef(repoURL, ref string) string {
|
|||
return SlackLinkFormatter(url, refName)
|
||||
}
|
||||
|
||||
// TODO: fix spelling to Converter
|
||||
// Create implements payloadConvertor Create method
|
||||
func (s slackConvertor) Create(p *api.CreatePayload) (SlackPayload, error) {
|
||||
refLink := SlackLinkToRef(p.Repo.HTMLURL, p.Ref)
|
||||
|
|
|
@ -103,7 +103,7 @@ type EventSource struct {
|
|||
Owner *user_model.User
|
||||
}
|
||||
|
||||
// handle delivers hook tasks
|
||||
// handler delivers hook tasks
|
||||
func handler(items ...int64) []int64 {
|
||||
ctx := graceful.GetManager().HammerContext()
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue