gito/services/core/internal/provider/provider.go
toki f3bbb4e579 feat: webhook adapter wiring - router/runtime/provider refactor
- Move old task files to archive
- Update router.go with new adapter wiring
- Update runtime.go with provider integration
- Refactor provider.go with new abstraction
- Update provider tests
2026-06-19 19:45:53 +09:00

104 lines
2.6 KiB
Go

package provider
import (
"context"
"strings"
"time"
"git.toki-labs.com/toki/gito/services/core/internal/core"
)
type ProviderID string
const (
ProviderGitHub ProviderID = "github"
ProviderGitLab ProviderID = "gitlab"
ProviderForgejo ProviderID = "forgejo"
ProviderPlane ProviderID = "plane"
ProviderJira ProviderID = "jira"
)
type Capability uint64
const (
CapabilityChangeRequest Capability = 1 << iota
CapabilityWebhook
CapabilityChecks
CapabilityComment
)
func (c Capability) Has(flag Capability) bool {
return flag != 0 && c&flag == flag
}
type Config struct {
ID ProviderID
Endpoint string
CredentialRef string
Capabilities Capability
CreatedAt time.Time
UpdatedAt time.Time
}
func (c Config) Supports(flag Capability) bool {
return c.Capabilities.Has(flag)
}
func (c Config) Normalized() Config {
c.ID = ProviderID(strings.ToLower(strings.TrimSpace(string(c.ID))))
c.Endpoint = strings.TrimSpace(c.Endpoint)
c.CredentialRef = strings.TrimSpace(c.CredentialRef)
return c
}
type ChangeRequestAdapter interface {
CreateChangeRequest(ctx context.Context, input core.ChangeRequest) (core.ChangeRequest, error)
UpdateChangeRequest(ctx context.Context, input core.ChangeRequest) (core.ChangeRequest, error)
CommentChangeRequest(ctx context.Context, id string, body string) error
CloseChangeRequest(ctx context.Context, id string) error
}
type WebhookEvent struct {
Provider string
EventType string
ExternalID string
Payload []byte
}
// WebhookRequest represents a provider webhook with all metadata needed
// for signature verification and normalization.
type WebhookRequest struct {
Provider ProviderID
EventType string
ExternalID string
Payload []byte
Headers map[string][]string
Query map[string][]string
ReceivedAt time.Time
}
// WebhookCandidate represents a provider-neutral normalization candidate
// that contains minimal metadata without requiring provider secrets.
type WebhookCandidate struct {
Type string
Provider ProviderID
ExternalID string
Revision *core.RevisionEvent
}
// WebhookVerifier defines the interface for verifying webhook signatures.
type WebhookVerifier interface {
VerifyWebhook(ctx context.Context, req WebhookRequest) error
}
// WebhookNormalizer defines the interface for normalizing webhook events
// into provider-neutral candidates.
type WebhookNormalizer interface {
NormalizeWebhook(ctx context.Context, req WebhookRequest) ([]WebhookCandidate, error)
}
// WebhookAdapter combines verification and normalization capabilities.
type WebhookAdapter interface {
WebhookVerifier
WebhookNormalizer
}