| 1 | package conversation |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/ownermode" |
| 5 | "amadan.net/oficina/equipo/internal/release" |
| 6 | "crypto/rand" |
| 7 | "errors" |
| 8 | "gorm.io/gorm" |
| 9 | "strconv" |
| 10 | ) |
| 11 | |
| 12 | func (s *Application) setupMessage(tx *gorm.DB, actor Actor, msg Message) error { |
| 13 | cs := s.Conversation(tx) |
| 14 | var done bool |
| 15 | mutation := Mutation{RequestID: msg.ID} |
| 16 | if ok, err := cs.CoordinatorReplay(actor, "coordinator.setup", msg.ID, mutation, msg.ID, &done); ok || err != nil { |
| 17 | return err |
| 18 | } |
| 19 | b, err := cs.Binding(actor) |
| 20 | note := "" |
| 21 | exists := err == nil |
| 22 | if exists { |
| 23 | b, err = s.FallbackCoordinator(tx, actor, b) |
| 24 | } |
| 25 | if errors.Is(err, s.NotFound) { |
| 26 | b, note, err = s.autoCoordinator(tx, actor) |
| 27 | } |
| 28 | if err != nil { |
| 29 | return err |
| 30 | } |
| 31 | if b.ID != "" && !exists { |
| 32 | if _, err = cs.QueueCoordinator(actor, msg.ID, 0, Mutation{RequestID: "message:" + msg.ID}); err != nil { |
| 33 | return err |
| 34 | } |
| 35 | } |
| 36 | if note != "" { |
| 37 | if err = s.humanNote(tx, actor, msg.ConversationID, note); err != nil { |
| 38 | return err |
| 39 | } |
| 40 | } |
| 41 | return cs.SaveCoordinatorResult(actor, "coordinator.setup", msg.ID, mutation, msg.ID, true) |
| 42 | } |
| 43 | |
| 44 | func (s *Application) autoCoordinator(tx *gorm.DB, actor Actor) (BindingRecord, string, error) { |
| 45 | var empty BindingRecord |
| 46 | teamID, err := s.Conversation(tx).deps.Authorize(tx, actor) |
| 47 | if err != nil { |
| 48 | return empty, "", err |
| 49 | } |
| 50 | connections, key, err := ownermode.OwnConnections(tx, actor.Address, teamID) |
| 51 | if err != nil { |
| 52 | return empty, "", err |
| 53 | } |
| 54 | if key == "coordinator.setup_runner" { |
| 55 | return empty, s.Copy("coordinator.runner_required"), nil |
| 56 | } |
| 57 | if key != "" { |
| 58 | return empty, s.Copy(key), nil |
| 59 | } |
| 60 | |
| 61 | manifest := release.BuildManifest() |
| 62 | if s.BindingRelease != nil { |
| 63 | manifest = *s.BindingRelease |
| 64 | } |
| 65 | if err = ownermode.RankConnections(tx, connections, s.Now(), s.DecodeConnection); err != nil { |
| 66 | return empty, "", err |
| 67 | } |
| 68 | for _, c := range connections { |
| 69 | if err = s.DecodeConnection(&c); err != nil { |
| 70 | return empty, "", err |
| 71 | } |
| 72 | for _, r := range c.Reports { |
| 73 | allowed, _, err := ownermode.AutoCandidate(tx, c, r, manifest, s.BindingEligible) |
| 74 | if err != nil { |
| 75 | return empty, "", err |
| 76 | } |
| 77 | if !allowed { |
| 78 | continue |
| 79 | } |
| 80 | |
| 81 | memberRevision, connectionRevision, err := s.BindingIdentity(tx, actor, c.RunnerID, c.ID) |
| 82 | if err != nil { |
| 83 | return empty, "", err |
| 84 | } |
| 85 | b := BindingRecord{CoordinatorBinding: CoordinatorBinding{ID: rand.Text(), Person: actor.Address, RunnerID: c.RunnerID, ConnectionID: c.ID, Generation: 1, Revision: 1}, TeamID: teamID, PersonKey: actor.Address, MemberRevision: memberRevision, ConnectionRevision: connectionRevision} |
| 86 | old, oldErr := s.Conversation(tx).Binding(actor) |
| 87 | if oldErr == nil && old.ConnectionID == c.ID && old.RunnerID == c.RunnerID && old.MemberRevision == memberRevision && old.ConnectionRevision == connectionRevision { |
| 88 | return old, "", nil |
| 89 | } |
| 90 | if oldErr == nil { |
| 91 | changed, changeErr := s.changeCoordinator(tx.Statement.Context, actor, c.RunnerID, c.ID, false, Mutation{Revision: old.Revision, RequestID: "fallback:" + old.ID + ":" + strconv.FormatInt(old.Revision, 10) + ":" + c.ID + ":" + strconv.FormatInt(c.Revision, 10)}) |
| 92 | if changeErr != nil { |
| 93 | return empty, "", changeErr |
| 94 | } |
| 95 | b = BindingRecord{} |
| 96 | if err = tx.First(&b, "id=?", changed.ID).Error; err != nil { |
| 97 | return empty, "", err |
| 98 | } |
| 99 | } else if errors.Is(oldErr, s.NotFound) { |
| 100 | if err = tx.Create(&b).Error; err != nil { |
| 101 | return empty, "", err |
| 102 | } |
| 103 | } else { |
| 104 | return empty, "", oldErr |
| 105 | } |
| 106 | if err = s.Audit(tx, actor, "coordinator.auto_bind", b.ID); err != nil { |
| 107 | return empty, "", err |
| 108 | } |
| 109 | return b, "", nil |
| 110 | } |
| 111 | } |
| 112 | if err = s.loginNotes(tx, actor, BindingRecord{TeamID: teamID}); err != nil { |
| 113 | return empty, "", err |
| 114 | } |
| 115 | return empty, "", nil |
| 116 | } |
| 117 | |