1package webhook23import (4 "context"5 "fmt"67 "github.com/charmbracelet/soft-serve/git"8 "github.com/charmbracelet/soft-serve/pkg/config"9 "github.com/charmbracelet/soft-serve/pkg/db"10 "github.com/charmbracelet/soft-serve/pkg/proto"11 "github.com/charmbracelet/soft-serve/pkg/store"12)1314// BranchTagEvent is a branch or tag event.15type BranchTagEvent struct {16 Common1718 // Ref is the branch or tag name.19 Ref string `json:"ref" url:"ref"`20 // Before is the previous commit SHA.21 Before string `json:"before" url:"before"`22 // After is the current commit SHA.23 After string `json:"after" url:"after"`24 // Created is whether the branch or tag was created.25 Created bool `json:"created" url:"created"`26 // Deleted is whether the branch or tag was deleted.27 Deleted bool `json:"deleted" url:"deleted"`28}2930// NewBranchTagEvent sends a branch or tag event.31func NewBranchTagEvent(ctx context.Context, user proto.User, repo proto.Repository, ref, before, after string) (BranchTagEvent, error) {32 var event Event33 if git.IsZeroHash(before) {34 event = EventBranchTagCreate35 } else if git.IsZeroHash(after) {36 event = EventBranchTagDelete37 } else {38 return BranchTagEvent{}, fmt.Errorf("invalid branch or tag event: before=%q after=%q", before, after)39 }4041 payload := BranchTagEvent{42 Ref: ref,43 Before: before,44 After: after,45 Created: git.IsZeroHash(before),46 Deleted: git.IsZeroHash(after),47 Common: Common{48 EventType: event,49 Repository: Repository{50 ID: repo.ID(),51 Name: repo.Name(),52 Description: repo.Description(),53 ProjectName: repo.ProjectName(),54 Private: repo.IsPrivate(),55 CreatedAt: repo.CreatedAt(),56 UpdatedAt: repo.UpdatedAt(),57 },58 Sender: User{59 ID: user.ID(),60 Username: user.Username(),61 },62 },63 }6465 cfg := config.FromContext(ctx)66 payload.Repository.HTTPURL = repoURL(cfg.HTTP.PublicURL, repo.Name())67 payload.Repository.SSHURL = repoURL(cfg.SSH.PublicURL, repo.Name())68 payload.Repository.GitURL = repoURL(cfg.Git.PublicURL, repo.Name())6970 // Find repo owner.71 dbx := db.FromContext(ctx)72 datastore := store.FromContext(ctx)73 owner, err := datastore.GetUserByID(ctx, dbx, repo.UserID())74 if err != nil {75 return BranchTagEvent{}, db.WrapError(err)76 }7778 payload.Repository.Owner.ID = owner.ID79 payload.Repository.Owner.Username = owner.Username80 payload.Repository.DefaultBranch, _ = getDefaultBranch(repo)8182 return payload, nil83}