Files
kandev/apps/backend/internal/linear/store.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

183 lines
5.5 KiB
Go

package linear
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
"github.com/jmoiron/sqlx"
)
// Store persists Linear workspace configurations. Secret values are delegated
// to the shared encrypted secret store and not stored here.
type Store struct {
db *sqlx.DB
ro *sqlx.DB
}
// NewStore creates a new Store and initializes the schema if needed.
func NewStore(writer, reader *sqlx.DB) (*Store, error) {
s := &Store{db: writer, ro: reader}
if err := s.initSchema(); err != nil {
return nil, fmt.Errorf("linear schema init: %w", err)
}
return s, nil
}
const createTablesSQL = `
CREATE TABLE IF NOT EXISTS linear_configs (
workspace_id TEXT PRIMARY KEY,
auth_method TEXT NOT NULL,
default_team_key TEXT NOT NULL DEFAULT '',
org_slug TEXT NOT NULL DEFAULT '',
last_checked_at DATETIME,
last_ok INTEGER NOT NULL DEFAULT 0,
last_error TEXT NOT NULL DEFAULT '',
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL
);
`
// addedColumns lists columns introduced after the initial schema. Kept for
// parity with the Jira store so future column additions follow the same
// migration pattern; empty for now since the table ships fully-formed.
var addedColumns = []struct {
name string
sql string
}{}
func (s *Store) initSchema() error {
if _, err := s.db.Exec(createTablesSQL); err != nil {
return err
}
return s.migrateAddedColumns()
}
// migrateAddedColumns applies ALTER TABLE statements for columns introduced
// after the initial schema. Existing databases need these columns backfilled;
// new databases already have them from createTablesSQL and the ALTERs are
// skipped.
func (s *Store) migrateAddedColumns() error {
if len(addedColumns) == 0 {
return nil
}
existing, err := s.tableColumns("linear_configs")
if err != nil {
return err
}
for _, col := range addedColumns {
if _, ok := existing[col.name]; ok {
continue
}
if _, err := s.db.Exec(col.sql); err != nil {
return fmt.Errorf("add column %s: %w", col.name, err)
}
}
return nil
}
func (s *Store) tableColumns(table string) (map[string]struct{}, error) {
rows, err := s.db.Query(fmt.Sprintf("PRAGMA table_info(%s)", table))
if err != nil {
return nil, err
}
defer func() { _ = rows.Close() }()
cols := make(map[string]struct{})
for rows.Next() {
var (
cid int
name string
ctype string
notnull int
dflt sql.NullString
pk int
)
if err := rows.Scan(&cid, &name, &ctype, &notnull, &dflt, &pk); err != nil {
return nil, err
}
cols[name] = struct{}{}
}
return cols, rows.Err()
}
const selectConfigColumns = `workspace_id, auth_method, default_team_key, org_slug,
last_checked_at, last_ok, last_error, created_at, updated_at`
// GetConfig returns the Linear config for a workspace, or nil when no row
// exists.
func (s *Store) GetConfig(ctx context.Context, workspaceID string) (*LinearConfig, error) {
var cfg LinearConfig
err := s.ro.GetContext(ctx, &cfg,
`SELECT `+selectConfigColumns+` FROM linear_configs WHERE workspace_id = ?`, workspaceID)
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
if err != nil {
return nil, err
}
return &cfg, nil
}
// UpsertConfig inserts or updates the config row for a workspace. It never
// touches the secret store — callers must persist the token separately. The
// last_* health columns and org_slug are deliberately not touched here; the
// poller owns those and writes them via UpdateAuthHealth.
func (s *Store) UpsertConfig(ctx context.Context, cfg *LinearConfig) error {
now := time.Now().UTC()
if cfg.CreatedAt.IsZero() {
cfg.CreatedAt = now
}
cfg.UpdatedAt = now
_, err := s.db.ExecContext(ctx, `
INSERT INTO linear_configs (workspace_id, auth_method, default_team_key, created_at, updated_at)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(workspace_id) DO UPDATE SET
auth_method = excluded.auth_method,
default_team_key = excluded.default_team_key,
updated_at = excluded.updated_at`,
cfg.WorkspaceID, cfg.AuthMethod, cfg.DefaultTeamKey, cfg.CreatedAt, cfg.UpdatedAt)
return err
}
// DeleteConfig removes the Linear config row for a workspace. Secrets must be
// cleared separately by the caller.
func (s *Store) DeleteConfig(ctx context.Context, workspaceID string) error {
_, err := s.db.ExecContext(ctx, `DELETE FROM linear_configs WHERE workspace_id = ?`, workspaceID)
return err
}
// ListConfiguredWorkspaces returns the IDs of all workspaces that have a
// Linear config row. Used by the auth-health poller.
func (s *Store) ListConfiguredWorkspaces(ctx context.Context) ([]string, error) {
var ids []string
err := s.ro.SelectContext(ctx, &ids,
`SELECT workspace_id FROM linear_configs ORDER BY workspace_id`)
if err != nil {
return nil, err
}
return ids, nil
}
// UpdateAuthHealth records the result of a credential probe. orgSlug is
// captured opportunistically from successful probes; pass "" to leave the
// existing slug unchanged. If the workspace row no longer exists, the update
// is a silent no-op.
func (s *Store) UpdateAuthHealth(ctx context.Context, workspaceID string, ok bool, errMsg, orgSlug string, checkedAt time.Time) error {
if orgSlug != "" {
_, err := s.db.ExecContext(ctx, `
UPDATE linear_configs
SET last_checked_at = ?, last_ok = ?, last_error = ?, org_slug = ?
WHERE workspace_id = ?`,
checkedAt, ok, errMsg, orgSlug, workspaceID)
return err
}
_, err := s.db.ExecContext(ctx, `
UPDATE linear_configs
SET last_checked_at = ?, last_ok = ?, last_error = ?
WHERE workspace_id = ?`,
checkedAt, ok, errMsg, workspaceID)
return err
}