mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git), so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form used by the handful of files that had ours. Shape is now: // Copyright 2026 Hanzo AI, Inc. All rights reserved. // Copyright 2015 The Gogs Authors. All rights reserved. // Copyright 2019 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT Ours on top, theirs retained verbatim. Retained because MIT requires it, and separately because they are true — those authors did write that code. Not renamed to "The Git Authors": Git is a different project under a different license, and putting its name here would attribute this code to people who have nothing to do with it. Adding our line is the fix; overwriting theirs would have been both a licence violation and a false statement. gofmt still reports import-order drift in some of these files. It predates this change (verified against a stash) and is left alone rather than burying a one-line-per-file legal change under 3000 files of unrelated reordering. Build clean. Co-authored-by: Hanzo Dev <[email protected]>
109 lines
3.2 KiB
Go
109 lines
3.2 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package webhook
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/hanzoai/git/models/db"
|
|
"github.com/hanzoai/git/modules/optional"
|
|
|
|
"github.com/hanzoai/builder"
|
|
)
|
|
|
|
// ListSystemWebhookOptions options for listing system or default webhooks
|
|
type ListSystemWebhookOptions struct {
|
|
db.ListOptions
|
|
IsActive optional.Option[bool]
|
|
IsSystem optional.Option[bool]
|
|
}
|
|
|
|
func (opts ListSystemWebhookOptions) ToConds() builder.Cond {
|
|
cond := builder.NewCond()
|
|
cond = cond.And(builder.Eq{"webhook.repo_id": 0}, builder.Eq{"webhook.owner_id": 0})
|
|
if opts.IsActive.Has() {
|
|
cond = cond.And(builder.Eq{"webhook.is_active": opts.IsActive.Value()})
|
|
}
|
|
if opts.IsSystem.Has() {
|
|
cond = cond.And(builder.Eq{"is_system_webhook": opts.IsSystem.Value()})
|
|
}
|
|
return cond
|
|
}
|
|
|
|
// GetGlobalWebhooks returns global (default and/or system) webhooks
|
|
func GetGlobalWebhooks(ctx context.Context, opts *ListSystemWebhookOptions) ([]*Webhook, int64, error) {
|
|
return db.FindAndCount[Webhook](ctx, opts)
|
|
}
|
|
|
|
// GetDefaultWebhooks returns all admin-default webhooks.
|
|
func GetDefaultWebhooks(ctx context.Context) ([]*Webhook, error) {
|
|
webhooks := make([]*Webhook, 0, 5)
|
|
return webhooks, db.GetEngine(ctx).
|
|
Where("repo_id=? AND owner_id=? AND is_system_webhook=?", 0, 0, false).
|
|
Find(&webhooks)
|
|
}
|
|
|
|
// GetSystemOrDefaultWebhook returns admin system or default webhook by given ID.
|
|
func GetSystemOrDefaultWebhook(ctx context.Context, id int64) (*Webhook, error) {
|
|
webhook := &Webhook{ID: id}
|
|
has, err := db.GetEngine(ctx).
|
|
Where("repo_id=? AND owner_id=?", 0, 0).
|
|
Get(webhook)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if !has {
|
|
return nil, ErrWebhookNotExist{ID: id}
|
|
}
|
|
return webhook, nil
|
|
}
|
|
|
|
// GetSystemWebhooks returns all admin system webhooks.
|
|
func GetSystemWebhooks(ctx context.Context, isActive optional.Option[bool]) ([]*Webhook, error) {
|
|
webhooks := make([]*Webhook, 0, 5)
|
|
if !isActive.Has() {
|
|
return webhooks, db.GetEngine(ctx).
|
|
Where("repo_id=? AND owner_id=? AND is_system_webhook=?", 0, 0, true).
|
|
Find(&webhooks)
|
|
}
|
|
return webhooks, db.GetEngine(ctx).
|
|
Where("repo_id=? AND owner_id=? AND is_system_webhook=? AND is_active = ?", 0, 0, true, isActive.Value()).
|
|
Find(&webhooks)
|
|
}
|
|
|
|
// DeleteDefaultSystemWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0)
|
|
func DeleteDefaultSystemWebhook(ctx context.Context, id int64) error {
|
|
return db.WithTx(ctx, func(ctx context.Context) error {
|
|
count, err := db.GetEngine(ctx).
|
|
Where("repo_id=? AND owner_id=?", 0, 0).
|
|
Delete(&Webhook{ID: id})
|
|
if err != nil {
|
|
return err
|
|
} else if count == 0 {
|
|
return ErrWebhookNotExist{ID: id}
|
|
}
|
|
|
|
_, err = db.DeleteByBean(ctx, &HookTask{HookID: id})
|
|
return err
|
|
})
|
|
}
|
|
|
|
// CopyDefaultWebhooksToRepo creates copies of the default webhooks in a new repo
|
|
func CopyDefaultWebhooksToRepo(ctx context.Context, repoID int64) error {
|
|
ws, err := GetDefaultWebhooks(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("GetDefaultWebhooks: %v", err)
|
|
}
|
|
|
|
for _, w := range ws {
|
|
w.ID = 0
|
|
w.RepoID = repoID
|
|
if err := CreateWebhook(ctx, w); err != nil {
|
|
return fmt.Errorf("CreateWebhook: %v", err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|