Files
devclaw-gitea/lib/providers/provider.ts
Lauren ten Hoor dfeadf742a feat: make workflow states dynamic with XState-style statechart config (#147) (#160)
## Summary

Introduces a configurable workflow state machine that replaces all hardcoded
state labels. The default workflow matches current behavior exactly, ensuring
backward compatibility.

## Architecture

### lib/workflow.ts — Core workflow engine

XState-style statechart configuration:

```typescript
type StateConfig = {
  type: 'queue' | 'active' | 'hold' | 'terminal';
  role?: 'dev' | 'qa';
  label: string;
  color: string;
  priority?: number;
  on?: Record<string, TransitionTarget>;
};
```

All behavior is derived from the config:
- Queue states: `type: 'queue'`, grouped by role, ordered by priority
- Active states: `type: 'active'` — worker occupied
- Transitions: defined with optional actions (gitPull, detectPr, closeIssue, reopenIssue)
- Labels and colors: derived from state.label and state.color

### Derivation functions

- `getStateLabels()` — all labels for issue tracker sync
- `getLabelColors()` — label → color mapping
- `getQueueLabels(role)` — queue labels for a role, ordered by priority
- `getActiveLabel(role)` — the active/in-progress label for a role
- `getRevertLabel(role)` — queue label to revert to on failure
- `detectRoleFromLabel()` — detect role from a queue label
- `getCompletionRule(role, result)` — derive transition rule from config

## Files Changed

- **lib/workflow.ts** — NEW: workflow engine and default config
- **lib/providers/provider.ts** — deprecate STATE_LABELS, LABEL_COLORS; derive from workflow
- **lib/providers/github.ts** — use workflow config for label operations
- **lib/providers/gitlab.ts** — use workflow config for label operations
- **lib/services/pipeline.ts** — use getCompletionRule() from workflow
- **lib/services/tick.ts** — use workflow for queue/active labels
- **lib/services/health.ts** — use workflow for active/revert labels
- **lib/tools/work-start.ts** — use workflow for target label

## Backward Compatibility

- DEFAULT_WORKFLOW matches current hardcoded behavior exactly
- Deprecated exports kept for any external consumers
- No breaking changes to tool interfaces or project state

## Future Work

- Load per-project workflow overrides from projects.json
- User-facing config in projects/workflow.json
- Tool schema generation from workflow states
2026-02-13 18:50:09 +08:00

76 lines
2.7 KiB
TypeScript

/**
* IssueProvider — Abstract interface for issue tracker operations.
*
* Implementations: GitHub (gh CLI), GitLab (glab CLI).
*
* Note: STATE_LABELS and LABEL_COLORS are kept for backward compatibility
* but new code should use the workflow config via lib/workflow.ts.
*/
import { DEFAULT_WORKFLOW, getStateLabels, getLabelColors } from "../workflow.js";
// ---------------------------------------------------------------------------
// State labels — derived from default workflow for backward compatibility
// ---------------------------------------------------------------------------
/**
* @deprecated Use workflow.getStateLabels() instead.
* Kept for backward compatibility with existing code.
*/
export const STATE_LABELS = getStateLabels(DEFAULT_WORKFLOW) as readonly string[];
/**
* StateLabel type — union of all valid state labels.
* This remains a string type for flexibility with custom workflows.
*/
export type StateLabel = string;
/**
* @deprecated Use workflow.getLabelColors() instead.
* Kept for backward compatibility with existing code.
*/
export const LABEL_COLORS: Record<string, string> = getLabelColors(DEFAULT_WORKFLOW);
// ---------------------------------------------------------------------------
// Issue types
// ---------------------------------------------------------------------------
export type Issue = {
iid: number;
title: string;
description: string;
labels: string[];
state: string;
web_url: string;
};
export type IssueComment = {
author: string;
body: string;
created_at: string;
};
// ---------------------------------------------------------------------------
// Provider interface
// ---------------------------------------------------------------------------
export interface IssueProvider {
ensureLabel(name: string, color: string): Promise<void>;
ensureAllStateLabels(): Promise<void>;
createIssue(title: string, description: string, label: StateLabel, assignees?: string[]): Promise<Issue>;
listIssuesByLabel(label: StateLabel): Promise<Issue[]>;
getIssue(issueId: number): Promise<Issue>;
listComments(issueId: number): Promise<IssueComment[]>;
transitionLabel(issueId: number, from: StateLabel, to: StateLabel): Promise<void>;
closeIssue(issueId: number): Promise<void>;
reopenIssue(issueId: number): Promise<void>;
hasStateLabel(issue: Issue, expected: StateLabel): boolean;
getCurrentStateLabel(issue: Issue): StateLabel | null;
hasMergedMR(issueId: number): Promise<boolean>;
getMergedMRUrl(issueId: number): Promise<string | null>;
addComment(issueId: number, body: string): Promise<void>;
healthCheck(): Promise<boolean>;
}
/** @deprecated Use IssueProvider */
export type TaskManager = IssueProvider;