Files
kandev/apps/backend/internal/gitlab/service_issue_watches.go
T
João Salavisa dfaab11959 feat: full GitLab integration — parity with GitHub (#1120)
* feat(backend): full GitLab parity — store, service, poller, WS, presets

Add the watcher/poll/write-action surface on top of the existing GitLab
client + service skeleton:

* 5 new SQLite tables (mr_watches, review_watches, review_mr_tasks,
  issue_watches, issue_watch_tasks, action_presets) + CRUD
* Client + Service: MergeMR, GetProjectMergeMethods, GetProtectedBranch,
  ListUserProjects, SearchProjects, SetMRLabels, SetMRAssignees
* Service: review/issue/MR watch CRUD + Check/Trigger, GetStats,
  CleanupAllReview/IssueTasks (cleanup-policy aware), TriggerMRSync,
  action-presets CRUD with defaults
* Background poller (MR / review / issue loops) wired from main.go
* ~35 GitLab WS action constants + handlers.go registrations
* Mock controller for E2E seeding (MRs, issues, pipelines, discussions,
  approvals, branches)
* eventBus + taskDeleter + taskSessionChecker plumbed through Service
* Event types: GitLabMRFeedback / NewReviewMR / NewIssue / TaskMRUpdated

* feat(web): GitLab frontend parity — types, API, store slice, domain hooks

Extend the frontend GitLab integration on top of the new backend surface:

* HTTP API client expansion (gitlab-api.ts): full CRUD for review/issue/MR
  watches, MR write actions (merge/approve/labels/assignees), MR
  files/commits/feedback, action presets, projects autocomplete, stats
* Store slice expansion: review/issue/MR watches lists, action presets
  by workspace, stats + status with loading flags. Properties prefixed
  with gitlab to avoid collision with GitHub slice
* Domain hooks: use-gitlab-status, use-gitlab-stats,
  use-gitlab-review-watches, use-gitlab-issue-watches,
  use-gitlab-action-presets — mirror github hook shapes
* Type model: ReviewWatch, IssueWatch, MRWatch, ActionPresets, Stats,
  ProjectMergeMethods, MRApproval, Pipeline, MRFeedback, etc.
* Backend controller HTTP routes for watches/presets/write-actions/
  projects/stats so the frontend HTTP API client has endpoints to hit

All passes: backend (build + vet + lint + tests), web (typecheck + lint +
2400 tests).

* fix(gitlab): wire orchestrator event handlers, fix label-filter drop, add tests

Address Claude review blockers on the parity PR:

1. Wire orchestrator subscriptions for GitLab review/issue watch events.
   Add event_handlers_gitlab.go with handleGitLabNewReviewMR /
   handleGitLabNewIssue, dedup reservation handshake, and task-creator
   interfaces (mirrors event_handlers_github.go). main.go now calls
   orchestratorSvc.SetGitLabService so the dedup APIs are usable
2. Fix silent label-filter drop: when a custom_query is set on an issue
   watch, labels were being appended to the unused filter arg. Fold
   labels into customQuery (or default filter) so they actually reach
   GitLab; extracted appendLabelsToQuery helper with explicit handling
   of pre-existing labels= clauses
3. Switch event payloads to pointer types (mirrors GitHub) so
   handlers can type-assert pointer events
4. Backend tests: store_watches_test.go (CRUD + reserve/assign for all
   4 new tables + presets), service_watches_test.go (label-filter
   helper, cleanup policy, project normalisation), action_presets_test.go
5. Frontend slice tests: review/issue watch CRUD round-trips, action
   presets + stats reducer coverage

All green: backend (build+vet+lint+tests), web (typecheck+lint+2403 tests).

* fix(gitlab): address remaining blockers — cleanup wiring, safe defaults, nil guards

Address the second round of Claude review blockers:

1. main.go: wire SetTaskDeleter + SetTaskSessionChecker on services.GitLab
   (mirrors GitHub). Without these, manual cleanup sweeps would always
   error with "task deleter not configured"
2. service_cleanup.go: default cleanup policy on transient DB error is now
   CleanupPolicyNever (preserve tasks) instead of CleanupPolicyAuto
   (silently delete). Genuine "watch was deleted" path also falls under
   the safe-side default; user can manually delete the orphan tasks
3. service_watches.go: add nil guards on requireStore() so the 6 MR-watch
   list/get/delete methods return an errStoreUnavailable error instead
   of panicking when the SQLite store failed to construct at boot
4. use-gitlab-status.ts + use-gitlab-stats.ts: add per-mount attemptedRef
   so an unreachable GitLab doesn't trigger an infinite re-fetch loop —
   the previous useEffect re-ran every render because the failure path
   left status null, satisfying the !status guard

* fix(gitlab): propagate session-check errors, clamp poll interval on update

Address remaining Claude blockers:

* service_cleanup.go: HasUserAuthoredMessage transient errors are now
  preserved (return false → skip delete) instead of silently ignored;
  the alternative was occasionally deleting tasks a user had touched
* service_watches.go: extract clampPollInterval helper applying the same
  bounds (0 → default, <60 → 60) as the create path; both
  applyReviewWatchPatch and applyIssueWatchPatch use it so user-supplied
  zero or tiny values via UpdateXxxWatchRequest no longer hammer GitLab

* fix(gitlab): clear dupl lint, fix preset retry loop + per-workspace watch cache

* service_watches.go: applyReviewWatchPatch / applyIssueWatchPatch get
  nolint:dupl markers — they share shape but per-domain field validation
  lives in the create paths, so deduplicating via generics would obscure
  the contract. Restores backend lint to 0 issues
* use-gitlab-action-presets.ts: per-workspace attemptedRef set so a
  failing preset fetch doesn't retry on every render
* use-gitlab-review-watches.ts + use-gitlab-issue-watches.ts: track
  lastFetchedRef per consumer so a workspace switch triggers a refetch.
  The slice-level loaded flag is shared across instances and can't
  double as a per-workspace cache key

* fix(gitlab): add nil-store guards across review/issue watch + cleanup paths

Round-3 Claude finding: ReviewWatch / IssueWatch / preset / reservation
methods still called s.requireStore().X() directly. If NewStore fails at
boot (table migration error), the service struct's store is nil and every
list/get/update/create/reserve method panics on first request.

Apply the same store-nil → errStoreUnavailable pattern from f95a929 to
every remaining method:
* CreateReviewWatch / CreateIssueWatch
* GetReviewWatch / ListReviewWatches / ListAllReviewWatches /
  UpdateReviewWatch / TriggerReviewWatchAll
* GetIssueWatch / ListIssueWatches / ListAllIssueWatches /
  UpdateIssueWatch / TriggerIssueWatchAll
* ReserveReviewMRTask / AssignReviewMRTaskID / ReleaseReviewMRTask
* ReserveIssueWatchTask / AssignIssueWatchTaskID / ReleaseIssueWatchTask
* lookupReviewPolicy / lookupIssuePolicy (return CleanupPolicyNever)

* fix(gitlab): nil-store guards on CheckXxxWatch + DeleteXxxWatch + cleanup paths

Round-4 Claude finding: 4 remaining panic sites the previous nil-guard
pass missed — these run from the background poller (every 5 min) and the
manual delete/cleanup flows, so a boot-time NewStore failure would
crash the orchestrator process on first tick.

* service_watches.go: add store-nil guards to DeleteReviewWatch /
  DeleteIssueWatch / CheckReviewWatch / CheckIssueWatch
* service_cleanup.go: guard CleanupAllReviewTasks / CleanupAllIssueTasks
  entry points; wrap the trailing DeleteReviewMRTask / DeleteIssueWatchTask
  calls in `if store := s.requireStore(); store != nil` blocks so the
  cleanup succeeds even when the dedup-row delete is unreachable

* refactor(gitlab): split service_watches.go to stay under 800-line revive limit

CI's --new-from-rev lint caught service_watches.go at 808 lines (limit
800). Split into three focused files:

* service_events.go (94 LOC) — publish helpers for MR feedback /
  new review MR / new issue / watch lifecycle events
* service_issue_watches.go (302 LOC) — Issue watch CRUD + Check +
  Trigger + fetch + helpers
* service_reservations.go (59 LOC) — Reserve/Assign/Release dedup
  handles used by the orchestrator event handlers

service_watches.go is now 505 lines and contains MR watch + Review
watch only. No behavioral change.

* fix(gitlab): address inline code-review feedback

Address actionable inline comments from Claude/CodeRabbit/cubic/Greptile:

* appendLabelsToQuery: use url.ParseQuery for exact key match instead
  of strings.Contains; previously false-matched keys like mylabels=
  and silently dropped the watch's labels (test added)
* fetchReviewMRs: drop dead `filter = watch.CustomQuery` assignment —
  SearchMRs's buildMRSearchQuery returns customQuery verbatim and
  ignores filter when customQuery is non-empty
* store.go: add workspace_id indexes on gitlab_review_watches and
  gitlab_issue_watches (the 5-min poller and HTTP list endpoints did
  full table scans)
* mock_client.SearchProjects: switch from == to case-insensitive
  Contains so partial-query autocomplete returns the seeded project,
  matching the doc comment
* Poller: add sync.Mutex to guard `started` field; Start/Stop are
  now safe to call concurrently (go test -race would have flagged
  the previous read/write race)
* Add ErrWatchNotFound sentinel; controller_watches.go maps it to
  HTTP 404 via httpRespondError helper. Update/Trigger handlers
  previously returned 500 for missing watches, hiding the distinction
  from real server faults
* service_events.go: log Publish errors on watch lifecycle events
  instead of swallowing them silently
* handlers.go: reject malformed JSON payloads in list-style WS
  handlers rather than silently falling through to broader
  list/search behavior
* event_handlers_gitlab.go: remove dead gitlabRepoSlug helper +
  blank-identifier suppression

* ci: re-trigger to clear flaky E2E shard 3

* fix(gitlab): address final review round — nil guards, store merge, logs, body validation

- CheckMRWatch, UpdateReviewWatch, UpdateIssueWatch: nil guards for store/req
- handlers: wsNewDiscussionNote rejects empty body
- action_presets: Update reads raw stored row (no default freezing);
  Reset validates workspace_id
- service_search.GetStats: log Warn per sub-call failure before zero
- event_handlers_gitlab: log Release* dedup-row errors with context
- web/store.ts: post-slice merge restores every GitLab sub-state field;
  extract buildStateOverrides helper
- web/default-state.ts: add GitLab fields to defaultState +
  mergeInitialState via mergeGitLabFields helper
- web/gitlab-api.ts: UpdateXxxWatchRequest now Omit workspace_id

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-02 22:10:07 +01:00

306 lines
8.5 KiB
Go

package gitlab
import (
"context"
"fmt"
"net/url"
"strings"
"time"
"go.uber.org/zap"
)
// CreateIssueWatch persists a new issue watch.
func (s *Service) CreateIssueWatch(ctx context.Context, req *CreateIssueWatchRequest) (*IssueWatch, error) {
if req == nil {
return nil, fmt.Errorf("nil request")
}
if !IsValidCleanupPolicy(req.CleanupPolicy) {
return nil, fmt.Errorf("invalid cleanup_policy: %q", req.CleanupPolicy)
}
iw := &IssueWatch{
WorkspaceID: req.WorkspaceID,
WorkflowID: req.WorkflowID,
WorkflowStepID: req.WorkflowStepID,
Projects: normalizeProjectFilters(req.Projects),
AgentProfileID: req.AgentProfileID,
ExecutorProfileID: req.ExecutorProfileID,
Prompt: req.Prompt,
Labels: req.Labels,
CustomQuery: req.CustomQuery,
Enabled: true,
PollIntervalSeconds: clampPollInterval(req.PollIntervalSeconds),
CleanupPolicy: NormalizeCleanupPolicy(req.CleanupPolicy),
}
store := s.requireStore()
if store == nil {
return nil, errStoreUnavailable
}
if err := store.CreateIssueWatch(ctx, iw); err != nil {
return nil, fmt.Errorf("create issue watch: %w", err)
}
go s.initialIssueCheck(context.Background(), iw)
return iw, nil
}
func (s *Service) initialIssueCheck(ctx context.Context, watch *IssueWatch) {
issues, err := s.CheckIssueWatch(ctx, watch)
if err != nil {
s.logger.Debug("initial gitlab issue check failed",
zap.String("watch_id", watch.ID), zap.Error(err))
return
}
for _, issue := range issues {
s.publishNewIssueEvent(ctx, watch, issue)
}
}
// GetIssueWatch returns an issue watch by id.
func (s *Service) GetIssueWatch(ctx context.Context, id string) (*IssueWatch, error) {
store := s.requireStore()
if store == nil {
return nil, errStoreUnavailable
}
return store.GetIssueWatch(ctx, id)
}
// ListIssueWatches lists issue watches in a workspace.
func (s *Service) ListIssueWatches(ctx context.Context, workspaceID string) ([]*IssueWatch, error) {
store := s.requireStore()
if store == nil {
return nil, errStoreUnavailable
}
return store.ListIssueWatches(ctx, workspaceID)
}
// ListAllIssueWatches returns every issue watch.
func (s *Service) ListAllIssueWatches(ctx context.Context) ([]*IssueWatch, error) {
store := s.requireStore()
if store == nil {
return nil, errStoreUnavailable
}
return store.ListAllIssueWatches(ctx)
}
// UpdateIssueWatch applies a partial update.
func (s *Service) UpdateIssueWatch(ctx context.Context, id string, req *UpdateIssueWatchRequest) error {
if req == nil {
return fmt.Errorf("nil request")
}
store := s.requireStore()
if store == nil {
return errStoreUnavailable
}
iw, err := store.GetIssueWatch(ctx, id)
if err != nil {
return err
}
if iw == nil {
return fmt.Errorf("%w: issue watch %s", ErrWatchNotFound, id)
}
applyIssueWatchPatch(iw, req)
if req.CleanupPolicy != nil && !IsValidCleanupPolicy(*req.CleanupPolicy) {
return fmt.Errorf("invalid cleanup_policy: %q", *req.CleanupPolicy)
}
return store.UpdateIssueWatch(ctx, iw)
}
//nolint:dupl // see applyReviewWatchPatch — same shape, different domain.
func applyIssueWatchPatch(iw *IssueWatch, req *UpdateIssueWatchRequest) {
if req.WorkflowID != nil {
iw.WorkflowID = *req.WorkflowID
}
if req.WorkflowStepID != nil {
iw.WorkflowStepID = *req.WorkflowStepID
}
if req.Projects != nil {
iw.Projects = normalizeProjectFilters(*req.Projects)
}
if req.AgentProfileID != nil {
iw.AgentProfileID = *req.AgentProfileID
}
if req.ExecutorProfileID != nil {
iw.ExecutorProfileID = *req.ExecutorProfileID
}
if req.Prompt != nil {
iw.Prompt = *req.Prompt
}
if req.Labels != nil {
iw.Labels = *req.Labels
}
if req.CustomQuery != nil {
iw.CustomQuery = *req.CustomQuery
}
if req.Enabled != nil {
iw.Enabled = *req.Enabled
}
if req.PollIntervalSeconds != nil {
iw.PollIntervalSeconds = clampPollInterval(*req.PollIntervalSeconds)
}
if req.CleanupPolicy != nil {
iw.CleanupPolicy = NormalizeCleanupPolicy(*req.CleanupPolicy)
}
}
// DeleteIssueWatch removes an issue watch and best-effort reaps tasks.
func (s *Service) DeleteIssueWatch(ctx context.Context, id string) error {
store := s.requireStore()
if store == nil {
return errStoreUnavailable
}
s.mu.RLock()
deleter := s.taskDeleter
s.mu.RUnlock()
if deleter != nil {
tasks, err := store.ListIssueWatchTasksByWatch(ctx, id)
if err != nil {
s.logger.Warn("failed to list issue tasks for pre-delete sweep",
zap.String("watch_id", id), zap.Error(err))
} else {
s.sweepIssueWatchTasksOnDelete(ctx, id, tasks, deleter)
}
}
return store.DeleteIssueWatch(ctx, id)
}
func (s *Service) sweepIssueWatchTasksOnDelete(ctx context.Context, watchID string, tasks []*IssueWatchTask, deleter TaskDeleter) {
for _, t := range tasks {
if t.TaskID == "" {
continue
}
if err := deleter.DeleteTask(ctx, t.TaskID); err != nil {
s.logger.Warn("failed to delete issue task during watch cleanup",
zap.String("watch_id", watchID),
zap.String("task_id", t.TaskID),
zap.Error(err))
}
}
}
// CheckIssueWatch polls a single watch and returns new issues.
//
//nolint:dupl // see CheckReviewWatch — same shape, different domain types.
func (s *Service) CheckIssueWatch(ctx context.Context, watch *IssueWatch) ([]*Issue, error) {
if watch == nil {
return nil, fmt.Errorf("watch is nil")
}
if !watch.Enabled {
return nil, nil
}
client := s.Client()
if client == nil {
return nil, ErrNoClient
}
store := s.requireStore()
if store == nil {
return nil, errStoreUnavailable
}
issues, err := s.fetchIssues(ctx, watch)
if err != nil {
return nil, err
}
out := make([]*Issue, 0, len(issues))
for _, issue := range issues {
exists, err := store.HasIssueWatchTask(ctx, watch.ID, issue.ProjectPath, issue.IID)
if err != nil {
s.logger.Error("check issue watch dedup", zap.Error(err))
continue
}
if !exists {
out = append(out, issue)
}
}
now := time.Now().UTC()
if err := store.RecordIssueWatchPoll(ctx, watch.ID, now); err != nil {
s.logger.Warn("record issue watch poll", zap.String("watch_id", watch.ID), zap.Error(err))
}
return out, nil
}
func (s *Service) fetchIssues(ctx context.Context, watch *IssueWatch) ([]*Issue, error) {
client := s.Client()
username, err := client.GetAuthenticatedUser(ctx)
if err != nil {
return nil, fmt.Errorf("resolve gitlab username: %w", err)
}
// When a custom_query is set, labels need to be folded into it (the
// client's buildIssueSearchQuery returns customQuery verbatim and
// ignores the auxiliary `filter` arg). Otherwise build a default
// "assigned to me" filter and append labels there.
filter := ""
customQuery := watch.CustomQuery
switch {
case customQuery != "":
if len(watch.Labels) > 0 {
customQuery = appendLabelsToQuery(customQuery, watch.Labels)
}
default:
filter = "assignee_username=" + url.QueryEscape(username)
if len(watch.Labels) > 0 {
filter += "&labels=" + url.QueryEscape(strings.Join(watch.Labels, ","))
}
}
issues, err := client.ListIssues(ctx, filter, customQuery)
if err != nil {
return nil, fmt.Errorf("list issues: %w", err)
}
if len(watch.Projects) == 0 {
return issues, nil
}
allowed := make(map[string]bool, len(watch.Projects))
for _, p := range watch.Projects {
allowed[strings.ToLower(strings.TrimSpace(p.Path))] = true
}
out := issues[:0]
for _, i := range issues {
if allowed[strings.ToLower(i.ProjectPath)] {
out = append(out, i)
}
}
return out, nil
}
// TriggerIssueWatch runs the watch once on demand.
func (s *Service) TriggerIssueWatch(ctx context.Context, id string) ([]*Issue, error) {
iw, err := s.GetIssueWatch(ctx, id)
if err != nil {
return nil, err
}
if iw == nil {
return nil, fmt.Errorf("%w: issue watch %s", ErrWatchNotFound, id)
}
found, err := s.CheckIssueWatch(ctx, iw)
if err != nil {
return nil, err
}
for _, issue := range found {
s.publishNewIssueEvent(ctx, iw, issue)
}
return found, nil
}
// TriggerIssueWatchAll runs every enabled watch.
func (s *Service) TriggerIssueWatchAll(ctx context.Context) (int, error) {
store := s.requireStore()
if store == nil {
return 0, errStoreUnavailable
}
watches, err := store.ListEnabledIssueWatches(ctx)
if err != nil {
return 0, err
}
total := 0
for _, iw := range watches {
found, err := s.CheckIssueWatch(ctx, iw)
if err != nil {
s.logger.Warn("trigger issue watch all", zap.String("watch_id", iw.ID), zap.Error(err))
continue
}
for _, issue := range found {
s.publishNewIssueEvent(ctx, iw, issue)
}
total += len(found)
}
return total, nil
}