Files
kandev/apps/backend/internal/linear/poller.go
T
João Salavisa 7b5b3f329d feat: add Linear integration (#736)
* feat: add Linear integration

Mirrors the existing Jira integration so a workspace can connect to Linear
with a personal API key, browse and search issues, and start a Kandev task
from any issue. Backend uses Linear's GraphQL API with a workspace-scoped
config + secret store and a 90s auth-health poller; the frontend adds a
settings page, an issue dialog with state transitions, link/import buttons
in the task UI, and a paginated /linear browse page with a Start task flow.

* fix(linear): address PR review feedback

- Route httpListStates through Service.ListStates instead of clientFor
- Extract repeated workspace_id required string into a constant
- Log GetConfig errors during credential resolution instead of dropping them
- Clear popover error state on close in import + link buttons
- Pick the first workflow with steps in the quick task launcher (don't hard-pin to workflows[0])
- Reset useLinearEnabled state on workspace change via tag-based pattern
- Add cursor-pointer to anchor + Link elements per project convention
- Add tests: handlers_test.go, linear-api.test.ts, use-linear-enabled.test.ts, linear-issue-common.test.tsx
- Document internal/jira and internal/linear in CLAUDE.md/AGENTS.md

* fix(linear): address second-round PR review feedback

- Round Linear's fractional state position instead of truncating it via
  int(math.Round(s.Position)) so adjacent fractional positions don't collapse
- Force LinearSettings to remount on workspace change with key={id} so the
  previous workspace's config / form / testResult / teams cannot bleed into
  the next workspace before the new fetches settle
- Broaden the legacy-value useLinearEnabled test to cover "true", "1", "yes",
  and "legacy" so a regression to strict value === "true" would fail

* fix(linear): address third-round PR review feedback

- Propagate context.Canceled / context.DeadlineExceeded from ListStates in
  GetIssue instead of swallowing them — a caller cancellation must not return
  a partial-success response
- Drop the one-off contains() test helper in handlers_test.go and use
  strings.Contains directly so the assertion is self-documenting
- Add getLinearIssue URL-shape test that locks identifier-as-path-segment
  vs workspaceId-as-query-param so accidentally swapping them would fail

* test(linear): extract repeated literals in linear-api.test.ts

Pulls "ws-1", the API base URL, and the auth-method literal into module-
level constants so the assertions read as the route shape they care about
and the duplicate-strings ESLint rule (currently warn, threshold 4)
stays quiet as the file grows.

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
2026-05-01 10:50:14 +01:00

97 lines
2.0 KiB
Go

package linear
import (
"context"
"sync"
"time"
"go.uber.org/zap"
"github.com/kandev/kandev/internal/common/logger"
)
// defaultAuthPollInterval is how often the auth-health poller probes each
// configured workspace.
const defaultAuthPollInterval = 90 * time.Second
// Poller probes the stored Linear credentials of every configured workspace on
// a fixed cadence and persists the result on the LinearConfig row.
type Poller struct {
service *Service
logger *logger.Logger
interval time.Duration
mu sync.Mutex
cancel context.CancelFunc
wg sync.WaitGroup
started bool
}
// NewPoller returns a poller that uses the default 90s cadence.
func NewPoller(svc *Service, log *logger.Logger) *Poller {
return &Poller{service: svc, logger: log, interval: defaultAuthPollInterval}
}
// Start launches the background loop. Calling Start more than once without
// Stop is a no-op.
func (p *Poller) Start(ctx context.Context) {
p.mu.Lock()
defer p.mu.Unlock()
if p.started || p.service == nil {
return
}
p.started = true
ctx, p.cancel = context.WithCancel(ctx)
p.wg.Add(1)
go p.loop(ctx)
p.logger.Info("Linear auth poller started")
}
// Stop cancels the loop and waits for it to drain.
func (p *Poller) Stop() {
p.mu.Lock()
if !p.started {
p.mu.Unlock()
return
}
cancel := p.cancel
p.mu.Unlock()
if cancel != nil {
cancel()
}
p.wg.Wait()
p.mu.Lock()
p.started = false
p.mu.Unlock()
p.logger.Info("Linear auth poller stopped")
}
func (p *Poller) loop(ctx context.Context) {
defer p.wg.Done()
p.probeAll(ctx)
ticker := time.NewTicker(p.interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
p.probeAll(ctx)
}
}
}
func (p *Poller) probeAll(ctx context.Context) {
ids, err := p.service.Store().ListConfiguredWorkspaces(ctx)
if err != nil {
p.logger.Warn("linear poller: list workspaces failed", zap.Error(err))
return
}
for _, id := range ids {
if ctx.Err() != nil {
return
}
p.service.RecordAuthHealth(ctx, id)
}
}