oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package setup
2
3import (
4 "amadan.net/oficina/equipo/internal/equipo/kernel"
5 "context"
6 "errors"
7 "gorm.io/gorm"
8 "strings"
9)
10
11// At keeps account creation, project validation, replay and audit in one transaction.
12func (s *ProjectsStore) At(tx *gorm.DB) *ProjectsStore {
13 at := *s
14 at.Write = func(ctx context.Context, fn func(*gorm.DB) error) error { return fn(tx.WithContext(ctx)) }
15 return &at
16}
17
18func (s *ProjectsStore) SaveNewProject(ctx context.Context, actor Actor, project Project, owners []DecisionOwner, request string) (Project, error) {
19 if project.ID != 0 {
20 return Project{}, s.Invalid
21 }
22 repo, err := CanonicalRepository(project.Repository, s.Invalid)
23 if err != nil {
24 return Project{}, err
25 }
26 project.Repository = repo
27 parts := strings.Split(repo, "/")
28 if project.Name == "" {
29 project.Name = parts[2]
30 }
31 if project.Kind == "" {
32 project.Kind = kernel.ProjectRepository
33 }
34 if project.DefaultBranch == "" {
35 project.DefaultBranch = "main"
36 }
37 for _, decision := range []string{"work", "merge"} {
38 found := false
39 for _, o := range owners {
40 if o.Decision == decision {
41 found = true
42 }
43 }
44 if !found {
45 owners = append(owners, DecisionOwner{Decision: decision, Address: actor.Address})
46 }
47 }
48 var out Project
49 err = s.Write(ctx, func(tx *gorm.DB) error {
50 if err := s.Owner(tx, actor); err != nil {
51 return err
52 }
53 if project.AccountID == 0 {
54 var account Account
55 err := tx.Where("label=?", parts[1]).First(&account).Error
56 if errors.Is(err, gorm.ErrRecordNotFound) {
57 account, err = s.At(tx).SaveAccount(ctx, actor, Account{Label: parts[1]})
58 }
59 if err != nil {
60 return err
61 }
62 project.AccountID = account.ID
63 }
64 var err error
65 out, err = s.At(tx).SaveProject(ctx, actor, project, owners, request)
66 return err
67 })
68 return out, err
69}
70