2026-02-21 16:03:46 +00:00
|
|
|
package forge
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
feat: modernise to Go 1.26 iterators and stdlib helpers
Add ListIter in pagination + generic Resource.Iter for streaming
paginated results as iter.Seq2[T, error]. Add Iter* methods across
all service files (actions, admin, branches, issues, labels, notifs,
orgs, packages, pulls, releases, repos, teams, users, webhooks).
Modernise cmd/forgegen with slices.Sort, maps.Keys, strings.FieldsFuncSeq.
Co-Authored-By: Gemini <noreply@google.com>
Co-Authored-By: Virgil <virgil@lethean.io>
2026-02-23 05:39:07 +00:00
|
|
|
"iter"
|
2026-02-21 16:03:46 +00:00
|
|
|
|
2026-03-22 01:51:29 +00:00
|
|
|
"dappco.re/go/core/forge/types"
|
2026-02-21 16:03:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// WebhookService handles webhook (hook) operations within a repository.
|
|
|
|
|
// Embeds Resource for standard CRUD on /api/v1/repos/{owner}/{repo}/hooks/{id}.
|
2026-03-26 18:00:20 +00:00
|
|
|
//
|
|
|
|
|
// Usage:
|
|
|
|
|
//
|
|
|
|
|
// f := forge.NewForge("https://forge.lthn.ai", "token")
|
|
|
|
|
// _, err := f.Webhooks.ListAll(ctx, forge.Params{"owner": "core", "repo": "go-forge"})
|
2026-02-21 16:03:46 +00:00
|
|
|
type WebhookService struct {
|
|
|
|
|
Resource[types.Hook, types.CreateHookOption, types.EditHookOption]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newWebhookService(c *Client) *WebhookService {
|
|
|
|
|
return &WebhookService{
|
|
|
|
|
Resource: *NewResource[types.Hook, types.CreateHookOption, types.EditHookOption](
|
|
|
|
|
c, "/api/v1/repos/{owner}/{repo}/hooks/{id}",
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestHook triggers a test delivery for a webhook.
|
|
|
|
|
func (s *WebhookService) TestHook(ctx context.Context, owner, repo string, id int64) error {
|
2026-03-26 18:00:20 +00:00
|
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/hooks/{id}/tests", pathParams("owner", owner, "repo", repo, "id", int64String(id)))
|
2026-02-21 16:03:46 +00:00
|
|
|
return s.client.Post(ctx, path, nil, nil)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 22:27:49 +00:00
|
|
|
// ListGitHooks returns all Git hooks for a repository.
|
|
|
|
|
func (s *WebhookService) ListGitHooks(ctx context.Context, owner, repo string) ([]types.GitHook, error) {
|
|
|
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/hooks/git", pathParams("owner", owner, "repo", repo))
|
|
|
|
|
return ListAll[types.GitHook](ctx, s.client, path, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetGitHook returns a single Git hook for a repository.
|
|
|
|
|
func (s *WebhookService) GetGitHook(ctx context.Context, owner, repo, id string) (*types.GitHook, error) {
|
|
|
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/hooks/git/{id}", pathParams("owner", owner, "repo", repo, "id", id))
|
|
|
|
|
var out types.GitHook
|
|
|
|
|
if err := s.client.Get(ctx, path, &out); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &out, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EditGitHook updates an existing Git hook in a repository.
|
|
|
|
|
func (s *WebhookService) EditGitHook(ctx context.Context, owner, repo, id string, opts *types.EditGitHookOption) (*types.GitHook, error) {
|
|
|
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/hooks/git/{id}", pathParams("owner", owner, "repo", repo, "id", id))
|
|
|
|
|
var out types.GitHook
|
|
|
|
|
if err := s.client.Patch(ctx, path, opts, &out); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &out, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DeleteGitHook deletes a Git hook from a repository.
|
|
|
|
|
func (s *WebhookService) DeleteGitHook(ctx context.Context, owner, repo, id string) error {
|
|
|
|
|
path := ResolvePath("/api/v1/repos/{owner}/{repo}/hooks/git/{id}", pathParams("owner", owner, "repo", repo, "id", id))
|
|
|
|
|
return s.client.Delete(ctx, path)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 22:25:41 +00:00
|
|
|
// ListUserHooks returns all webhooks for the authenticated user.
|
|
|
|
|
func (s *WebhookService) ListUserHooks(ctx context.Context) ([]types.Hook, error) {
|
|
|
|
|
return ListAll[types.Hook](ctx, s.client, "/api/v1/user/hooks", nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IterUserHooks returns an iterator over all webhooks for the authenticated user.
|
|
|
|
|
func (s *WebhookService) IterUserHooks(ctx context.Context) iter.Seq2[types.Hook, error] {
|
|
|
|
|
return ListIter[types.Hook](ctx, s.client, "/api/v1/user/hooks", nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetUserHook returns a single webhook for the authenticated user.
|
|
|
|
|
func (s *WebhookService) GetUserHook(ctx context.Context, id int64) (*types.Hook, error) {
|
|
|
|
|
path := ResolvePath("/api/v1/user/hooks/{id}", pathParams("id", int64String(id)))
|
|
|
|
|
var out types.Hook
|
|
|
|
|
if err := s.client.Get(ctx, path, &out); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &out, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CreateUserHook creates a webhook for the authenticated user.
|
|
|
|
|
func (s *WebhookService) CreateUserHook(ctx context.Context, opts *types.CreateHookOption) (*types.Hook, error) {
|
|
|
|
|
var out types.Hook
|
|
|
|
|
if err := s.client.Post(ctx, "/api/v1/user/hooks", opts, &out); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &out, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EditUserHook updates an existing authenticated-user webhook.
|
|
|
|
|
func (s *WebhookService) EditUserHook(ctx context.Context, id int64, opts *types.EditHookOption) (*types.Hook, error) {
|
|
|
|
|
path := ResolvePath("/api/v1/user/hooks/{id}", pathParams("id", int64String(id)))
|
|
|
|
|
var out types.Hook
|
|
|
|
|
if err := s.client.Patch(ctx, path, opts, &out); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &out, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DeleteUserHook deletes an authenticated-user webhook.
|
|
|
|
|
func (s *WebhookService) DeleteUserHook(ctx context.Context, id int64) error {
|
|
|
|
|
path := ResolvePath("/api/v1/user/hooks/{id}", pathParams("id", int64String(id)))
|
|
|
|
|
return s.client.Delete(ctx, path)
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-21 16:03:46 +00:00
|
|
|
// ListOrgHooks returns all webhooks for an organisation.
|
|
|
|
|
func (s *WebhookService) ListOrgHooks(ctx context.Context, org string) ([]types.Hook, error) {
|
2026-03-26 18:00:20 +00:00
|
|
|
path := ResolvePath("/api/v1/orgs/{org}/hooks", pathParams("org", org))
|
2026-02-21 16:03:46 +00:00
|
|
|
return ListAll[types.Hook](ctx, s.client, path, nil)
|
|
|
|
|
}
|
feat: modernise to Go 1.26 iterators and stdlib helpers
Add ListIter in pagination + generic Resource.Iter for streaming
paginated results as iter.Seq2[T, error]. Add Iter* methods across
all service files (actions, admin, branches, issues, labels, notifs,
orgs, packages, pulls, releases, repos, teams, users, webhooks).
Modernise cmd/forgegen with slices.Sort, maps.Keys, strings.FieldsFuncSeq.
Co-Authored-By: Gemini <noreply@google.com>
Co-Authored-By: Virgil <virgil@lethean.io>
2026-02-23 05:39:07 +00:00
|
|
|
|
|
|
|
|
// IterOrgHooks returns an iterator over all webhooks for an organisation.
|
|
|
|
|
func (s *WebhookService) IterOrgHooks(ctx context.Context, org string) iter.Seq2[types.Hook, error] {
|
2026-03-26 18:00:20 +00:00
|
|
|
path := ResolvePath("/api/v1/orgs/{org}/hooks", pathParams("org", org))
|
feat: modernise to Go 1.26 iterators and stdlib helpers
Add ListIter in pagination + generic Resource.Iter for streaming
paginated results as iter.Seq2[T, error]. Add Iter* methods across
all service files (actions, admin, branches, issues, labels, notifs,
orgs, packages, pulls, releases, repos, teams, users, webhooks).
Modernise cmd/forgegen with slices.Sort, maps.Keys, strings.FieldsFuncSeq.
Co-Authored-By: Gemini <noreply@google.com>
Co-Authored-By: Virgil <virgil@lethean.io>
2026-02-23 05:39:07 +00:00
|
|
|
return ListIter[types.Hook](ctx, s.client, path, nil)
|
|
|
|
|
}
|
2026-04-01 07:09:12 +00:00
|
|
|
|
|
|
|
|
// GetOrgHook returns a single webhook for an organisation.
|
|
|
|
|
func (s *WebhookService) GetOrgHook(ctx context.Context, org string, id int64) (*types.Hook, error) {
|
|
|
|
|
path := ResolvePath("/api/v1/orgs/{org}/hooks/{id}", pathParams("org", org, "id", int64String(id)))
|
|
|
|
|
var out types.Hook
|
|
|
|
|
if err := s.client.Get(ctx, path, &out); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &out, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CreateOrgHook creates a webhook for an organisation.
|
|
|
|
|
func (s *WebhookService) CreateOrgHook(ctx context.Context, org string, opts *types.CreateHookOption) (*types.Hook, error) {
|
|
|
|
|
path := ResolvePath("/api/v1/orgs/{org}/hooks", pathParams("org", org))
|
|
|
|
|
var out types.Hook
|
|
|
|
|
if err := s.client.Post(ctx, path, opts, &out); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &out, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EditOrgHook updates an existing organisation webhook.
|
|
|
|
|
func (s *WebhookService) EditOrgHook(ctx context.Context, org string, id int64, opts *types.EditHookOption) (*types.Hook, error) {
|
|
|
|
|
path := ResolvePath("/api/v1/orgs/{org}/hooks/{id}", pathParams("org", org, "id", int64String(id)))
|
|
|
|
|
var out types.Hook
|
|
|
|
|
if err := s.client.Patch(ctx, path, opts, &out); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &out, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DeleteOrgHook deletes an organisation webhook.
|
|
|
|
|
func (s *WebhookService) DeleteOrgHook(ctx context.Context, org string, id int64) error {
|
|
|
|
|
path := ResolvePath("/api/v1/orgs/{org}/hooks/{id}", pathParams("org", org, "id", int64String(id)))
|
|
|
|
|
return s.client.Delete(ctx, path)
|
|
|
|
|
}
|