Simplify codebase per audit of fd0529b

This commit is contained in:
Ian Gulliver
2026-10-23 09:56:40 -07:00
parent fd0529b949
commit 2bc462ada6
10 changed files with 101 additions and 219 deletions
+26 -48
View File
@@ -215,6 +215,15 @@ func (s *Store) Path() string {
return s.path
}
func updatedProject(ctx context.Context, tx *sql.Tx, query string, args ...any) (string, error) {
projectID := ""
err := tx.QueryRowContext(ctx, query, args...).Scan(&projectID)
if errors.Is(err, sql.ErrNoRows) {
return "", ErrNotFound
}
return projectID, err
}
func (s *Store) EnsureUser(ctx context.Context, login, displayName string) (api.User, error) {
_, err := s.db.ExecContext(ctx, `
INSERT INTO users (login, display_name, created_at) VALUES (?, ?, ?)
@@ -421,12 +430,8 @@ func (s *Store) GetTask(ctx context.Context, id string) (api.Task, error) {
func (s *Store) SetMergeTitle(ctx context.Context, id, title, actor string) error {
return s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
projectID := ""
err := tx.QueryRowContext(ctx, `UPDATE tasks SET merge_title = ? WHERE id = ? AND status = ? AND merging_by = '' AND finalizing = '' AND merge_queued_by = '' RETURNING project_id`,
title, id, string(api.TaskStatusActive)).Scan(&projectID)
if errors.Is(err, sql.ErrNoRows) {
return ErrNotFound
}
projectID, err := updatedProject(ctx, tx, `UPDATE tasks SET merge_title = ? WHERE id = ? AND status = ? AND merging_by = '' AND finalizing = '' AND merge_queued_by = '' RETURNING project_id`,
title, id, string(api.TaskStatusActive))
if err != nil {
return err
}
@@ -436,12 +441,8 @@ func (s *Store) SetMergeTitle(ctx context.Context, id, title, actor string) erro
func (s *Store) SetTitles(ctx context.Context, id, title, mergeTitle, actor string) error {
return s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
projectID := ""
err := tx.QueryRowContext(ctx, `UPDATE tasks SET title = ?, merge_title = ? WHERE id = ? AND status = ? AND merging_by = '' AND finalizing = '' AND merge_queued_by = '' RETURNING project_id`,
title, mergeTitle, id, string(api.TaskStatusActive)).Scan(&projectID)
if errors.Is(err, sql.ErrNoRows) {
return ErrNotFound
}
projectID, err := updatedProject(ctx, tx, `UPDATE tasks SET title = ?, merge_title = ? WHERE id = ? AND status = ? AND merging_by = '' AND finalizing = '' AND merge_queued_by = '' RETURNING project_id`,
title, mergeTitle, id, string(api.TaskStatusActive))
if err != nil {
return err
}
@@ -487,9 +488,8 @@ func (s *Store) TakeMergingBy(ctx context.Context, id string) (string, error) {
func (s *Store) ClearFinalizing(ctx context.Context, id string) error {
return s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
projectID := ""
err := tx.QueryRowContext(ctx, `UPDATE tasks SET finalizing = '', finalizing_commit = '', finalizing_notes = '', finalizing_unchanged = 0 WHERE id = ? AND finalizing != '' RETURNING project_id`, id).Scan(&projectID)
if errors.Is(err, sql.ErrNoRows) {
projectID, err := updatedProject(ctx, tx, `UPDATE tasks SET finalizing = '', finalizing_commit = '', finalizing_notes = '', finalizing_unchanged = 0 WHERE id = ? AND finalizing != '' RETURNING project_id`, id)
if errors.Is(err, ErrNotFound) {
return nil
}
if err != nil {
@@ -509,12 +509,8 @@ func (s *Store) SetFinalizingUnchanged(ctx context.Context, id string) error {
func (s *Store) setFinalizing(ctx context.Context, id, commit, notes, pushed string, unchanged bool) error {
return s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
projectID := ""
err := tx.QueryRowContext(ctx, `UPDATE tasks SET finalizing_commit = ?, finalizing_notes = ?, finalizing_unchanged = ? WHERE id = ? AND status = ? AND finalizing != '' RETURNING project_id`,
commit, notes, unchanged, id, string(api.TaskStatusActive)).Scan(&projectID)
if errors.Is(err, sql.ErrNoRows) {
return ErrNotFound
}
projectID, err := updatedProject(ctx, tx, `UPDATE tasks SET finalizing_commit = ?, finalizing_notes = ?, finalizing_unchanged = ? WHERE id = ? AND status = ? AND finalizing != '' RETURNING project_id`,
commit, notes, unchanged, id, string(api.TaskStatusActive))
if err != nil || commit == "" {
return err
}
@@ -592,9 +588,8 @@ func (s *Store) SetParentMerge(ctx context.Context, id, login string) error {
func (s *Store) ClearParentMerge(ctx context.Context, id string) error {
return s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
projectID := ""
err := tx.QueryRowContext(ctx, `UPDATE tasks SET parent_merge_by = '' WHERE id = ? AND parent_merge_by != '' RETURNING project_id`, id).Scan(&projectID)
if errors.Is(err, sql.ErrNoRows) {
projectID, err := updatedProject(ctx, tx, `UPDATE tasks SET parent_merge_by = '' WHERE id = ? AND parent_merge_by != '' RETURNING project_id`, id)
if errors.Is(err, ErrNotFound) {
return nil
}
if err != nil {
@@ -688,11 +683,7 @@ func (s *Store) SetMergeAfterReview(ctx context.Context, id string, mar MergeAft
func (s *Store) CancelMergeAfterReview(ctx context.Context, id, actor string) error {
return s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
projectID := ""
err := tx.QueryRowContext(ctx, `UPDATE tasks SET merge_after_review_by = '', merge_after_review_id = '', merge_after_review_target = '' WHERE id = ? AND merge_after_review_by != '' RETURNING project_id`, id).Scan(&projectID)
if errors.Is(err, sql.ErrNoRows) {
return ErrNotFound
}
projectID, err := updatedProject(ctx, tx, `UPDATE tasks SET merge_after_review_by = '', merge_after_review_id = '', merge_after_review_target = '' WHERE id = ? AND merge_after_review_by != '' RETURNING project_id`, id)
if err != nil {
return err
}
@@ -728,12 +719,8 @@ func (s *Store) TakeMergeAfterReview(ctx context.Context, id string, reviewIDs [
func (s *Store) SetTaskSummary(ctx context.Context, id, summary, actor string) error {
return s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
projectID := ""
err := tx.QueryRowContext(ctx, `UPDATE tasks SET summary = ? WHERE id = ? AND status = ? RETURNING project_id`,
summary, id, string(api.TaskStatusActive)).Scan(&projectID)
if errors.Is(err, sql.ErrNoRows) {
return ErrNotFound
}
projectID, err := updatedProject(ctx, tx, `UPDATE tasks SET summary = ? WHERE id = ? AND status = ? RETURNING project_id`,
summary, id, string(api.TaskStatusActive))
if err != nil {
return err
}
@@ -751,11 +738,7 @@ func (s *Store) SuggestDiscard(ctx context.Context, id, reason, actor string) er
func (s *Store) ClearReady(ctx context.Context, id, actor, reason string) error {
return s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
projectID := ""
err := tx.QueryRowContext(ctx, `UPDATE tasks SET ready = '' WHERE id = ? AND status = ? AND ready != '' RETURNING project_id`, id, string(api.TaskStatusActive)).Scan(&projectID)
if errors.Is(err, sql.ErrNoRows) {
return ErrNotFound
}
projectID, err := updatedProject(ctx, tx, `UPDATE tasks SET ready = '' WHERE id = ? AND status = ? AND ready != '' RETURNING project_id`, id, string(api.TaskStatusActive))
if err != nil {
return err
}
@@ -770,9 +753,8 @@ func (s *Store) ClearReady(ctx context.Context, id, actor, reason string) error
func (s *Store) TakeReady(ctx context.Context, id, ready string) (bool, error) {
taken := false
err := s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
projectID := ""
err := tx.QueryRowContext(ctx, `UPDATE tasks SET ready = '' WHERE id = ? AND status = ? AND ready = ? RETURNING project_id`, id, string(api.TaskStatusActive), ready).Scan(&projectID)
if errors.Is(err, sql.ErrNoRows) {
projectID, err := updatedProject(ctx, tx, `UPDATE tasks SET ready = '' WHERE id = ? AND status = ? AND ready = ? RETURNING project_id`, id, string(api.TaskStatusActive), ready)
if errors.Is(err, ErrNotFound) {
return nil
}
if err != nil {
@@ -790,11 +772,7 @@ func (s *Store) TakeReady(ctx context.Context, id, ready string) (bool, error) {
func (s *Store) setReady(ctx context.Context, id, actor, ready, kind string, payload map[string]any) error {
return s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
projectID := ""
err := tx.QueryRowContext(ctx, `UPDATE tasks SET ready = ? WHERE id = ? AND status = ? RETURNING project_id`, ready, id, string(api.TaskStatusActive)).Scan(&projectID)
if errors.Is(err, sql.ErrNoRows) {
return ErrNotFound
}
projectID, err := updatedProject(ctx, tx, `UPDATE tasks SET ready = ? WHERE id = ? AND status = ? RETURNING project_id`, ready, id, string(api.TaskStatusActive))
if err != nil {
return err
}