Commit 8a7642b427

8a7642b42755b0a3ac36d283ded97f7c8f638796

parent: d722ba57f3

Verified · cmc

cmc <hello@cleberg.net> · 2026-09-11 15:08 UTC

store: SetLabel and DeleteLabel check the org inside their transaction

Ref #203

Layout: unified · split

internal/store/labels.go +19 −6
@@ -82,29 +82,42 @@ func orgHoldsLabel(q interface {
82// SetLabel creates the repository's label or sets its colour. A name the 82// SetLabel creates the repository's label or sets its colour. A name the
83// org holds is refused with ErrOrgScoped. 83// org holds is refused with ErrOrgScoped.
84func (s *Store) SetLabel(repo Repo, name, color string) error { 84func (s *Store) SetLabel(repo Repo, name, color string) error {
85 if held, err := orgHoldsLabel(s.DB, repo, name); err != nil || held { 85 tx, err := s.DB.Begin()
86 if err != nil {
87 return err
88 }
89 defer tx.Rollback()
90 if held, err := orgHoldsLabel(tx, repo, name); err != nil || held {
86 if err != nil { 91 if err != nil {
87 return err 92 return err
88 } 93 }
89 return ErrOrgScoped 94 return ErrOrgScoped
90 } 95 }
91 _, err := s.DB.Exec(`INSERT INTO labels (repo_id, name, color) VALUES (?, ?, ?) 96 _, err = tx.Exec(`INSERT INTO labels (repo_id, name, color) VALUES (?, ?, ?)
92 ON CONFLICT (repo_id, name) WHERE repo_id IS NOT NULL DO UPDATE SET color = excluded.color`, 97 ON CONFLICT (repo_id, name) WHERE repo_id IS NOT NULL DO UPDATE SET color = excluded.color`,
93 repo.ID, name, color) 98 repo.ID, name, color)
94 return err 99 if err != nil {
100 return err
101 }
102 return tx.Commit()
95} 103}
96 104
97// DeleteLabel removes the repository's label and takes it off every issue. 105// DeleteLabel removes the repository's label and takes it off every issue.
98// An org's label is ErrOrgScoped; no label at all is ErrNotFound. 106// An org's label is ErrOrgScoped; no label at all is ErrNotFound.
99func (s *Store) DeleteLabel(repo Repo, name string) error { 107func (s *Store) DeleteLabel(repo Repo, name string) error {
100 res, err := s.DB.Exec("DELETE FROM labels WHERE repo_id = ? AND name = ?", repo.ID, name) 108 tx, err := s.DB.Begin()
109 if err != nil {
110 return err
111 }
112 defer tx.Rollback()
113 res, err := tx.Exec("DELETE FROM labels WHERE repo_id = ? AND name = ?", repo.ID, name)
101 if err != nil { 114 if err != nil {
102 return err 115 return err
103 } 116 }
104 if n, _ := res.RowsAffected(); n > 0 { 117 if n, _ := res.RowsAffected(); n > 0 {
105 return nil 118 return tx.Commit()
106 } 119 }
107 if held, err := orgHoldsLabel(s.DB, repo, name); err != nil || held { 120 if held, err := orgHoldsLabel(tx, repo, name); err != nil || held {
108 if err != nil { 121 if err != nil {
109 return err 122 return err
110 } 123 }