Commit 8a7642b427
Verified · cmc
internal/store/labels.go +19 −6
| @@ -82,29 +82,42 @@ func orgHoldsLabel(q interface { | ||
| 82 | 82 | // SetLabel creates the repository's label or sets its colour. A name the |
| 83 | 83 | // org holds is refused with ErrOrgScoped. |
| 84 | 84 | func (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 | 91 | if err != nil { |
| 87 | 92 | return err |
| 88 | 93 | } |
| 89 | 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 | 97 | ON CONFLICT (repo_id, name) WHERE repo_id IS NOT NULL DO UPDATE SET color = excluded.color`, |
| 93 | 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 | 105 | // DeleteLabel removes the repository's label and takes it off every issue. |
| 98 | 106 | // An org's label is ErrOrgScoped; no label at all is ErrNotFound. |
| 99 | 107 | func (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 | 114 | if err != nil { |
| 102 | 115 | return err |
| 103 | 116 | } |
| 104 | 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 | 121 | if err != nil { |
| 109 | 122 | return err |
| 110 | 123 | } |