oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package chat
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "fmt"
8
9 "amadan.net/oficina/equipo/internal/wire"
10)
11
12// projectAutonomy reads standing authority in the action's transaction.
13// The owner who granted it must still be a current owner.
14func (t *txn) projectAutonomy(project string) (wire.ProjectAutonomy, error) {
15 var p projectRecord
16 if err := t.db.First(&p, "id = ?", project).Error; err != nil {
17 return wire.ProjectAutonomy{}, err
18 }
19 return t.autonomyOf(p)
20}
21
22func (t *txn) autonomyOf(p projectRecord) (wire.ProjectAutonomy, error) {
23 out := wire.ProjectAutonomy{Revision: p.AutonomyRevision}
24 if p.AutonomyOwner == "" || p.State != wire.ProjectActive {
25 return out, nil
26 }
27 if err := t.isOwner(p.AutonomyOwner); err != nil {
28 if errors.Is(err, ErrForbidden) {
29 return out, nil
30 }
31 return out, err
32 }
33 if p.Autonomy != "" {
34 if err := json.Unmarshal([]byte(p.Autonomy), &out); err != nil {
35 return wire.ProjectAutonomy{}, err
36 }
37 }
38 out.Owner, out.Revision = p.AutonomyOwner, p.AutonomyRevision
39 return out, nil
40}
41
42// SetProjectAutonomy is called with the verified session's address, never a
43// submitted actor. JSON storage leaves room for later, separately checked actions.
44func (s *Store) SetProjectAutonomy(ctx context.Context, person, project string, autoMergeCI, publicPreviews bool, revision int64, sensitive ...bool) error {
45 return s.tx(ctx, func(t *txn) error {
46 if err := s.authority.CurrentPerson(ctx, t.db); err != nil {
47 return err
48 }
49 if err := t.isOwner(person); err != nil {
50 return err
51 }
52 var p projectRecord
53 if err := t.db.First(&p, "id = ? AND state = ?", project, wire.ProjectActive).Error; err != nil {
54 return err
55 }
56 if p.AutonomyRevision != revision {
57 return ErrCardConflict
58 }
59 flags := wire.ProjectAutonomy{AutoMergeSensitive: len(sensitive) > 0 && sensitive[0], AutoMergeCI: autoMergeCI, PublicPreviews: publicPreviews}
60 if publicPreviews {
61 old, err := t.autonomyOf(p)
62 if err != nil {
63 return err
64 }
65 flags.PublicPreviewsSince = old.PublicPreviewsSince
66 if !old.PublicPreviews || old.Owner != person {
67 flags.PublicPreviewsSince = t.now()
68 }
69 }
70 raw, err := json.Marshal(flags)
71 if err != nil {
72 return err
73 }
74 p.Autonomy, p.AutonomyOwner = string(raw), person
75 p.AutonomyRevision++
76 p.Revision++
77 p.UpdatedAt = t.now()
78 if err := t.db.Model(&projectRecord{}).Where("id = ?", p.ID).Select("autonomy", "autonomy_owner", "autonomy_revision", "revision", "updated_at").Updates(&p).Error; err != nil {
79 return err
80 }
81 if err := s.authority.Audit(t.db, person, "project.autonomy", fmt.Sprintf("%s/revision:%d/auto_merge_ci:%t/public_previews:%t/auto_merge_sensitive:%t", p.ID, p.AutonomyRevision, autoMergeCI, publicPreviews, flags.AutoMergeSensitive)); err != nil {
82 return err
83 }
84 if err := t.refreshMergeCards("project_id = ?", p.ID); err != nil {
85 return err
86 }
87 return t.projectEvent(p)
88 })
89}
90