|
| 1 | +import type { BrandedId } from '@gitbutler/shared/utils/branding'; |
| 2 | + |
| 3 | +export type WorkspaceRuleId = BrandedId<'WorkspaceRule'>; |
| 4 | +/** |
| 5 | + * A workspace rule. |
| 6 | + * @remarks |
| 7 | + * A rule is evaluated in the app and determines what happens to files or changes based on triggers, filters, and actions. |
| 8 | + */ |
| 9 | +export interface WorkspaceRule { |
| 10 | + /** A UUID unique identifier for the rule. */ |
| 11 | + id: WorkspaceRuleId; |
| 12 | + /** The time when the rule was created, represented as a Unix timestamp in milliseconds. */ |
| 13 | + createdAt: string; // ISO string or Date, depending on backend serialization |
| 14 | + /** Whether the rule is currently enabled or not. */ |
| 15 | + enabled: boolean; |
| 16 | + /** The trigger of the rule is what causes it to be evaluated in the app. */ |
| 17 | + trigger: Trigger; |
| 18 | + /** These filters determine what files or changes the rule applies to. Multiple filters are combined with OR logic. */ |
| 19 | + filters: Filter[]; |
| 20 | + /** The action determines what happens to the files or changes that matched the filters. */ |
| 21 | + action: Action; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Represents the kinds of events in the app that can cause a rule to be evaluated. |
| 26 | + */ |
| 27 | +export type Trigger = |
| 28 | + /** When a file is added, removed or modified in the Git worktree. */ |
| 29 | + 'fileSytemChange'; |
| 30 | + |
| 31 | +/** |
| 32 | + * A filter is a condition that determines what files or changes the rule applies to. |
| 33 | + * Multiple conditions in a filter are combined with AND logic. |
| 34 | + */ |
| 35 | +export type Filter = |
| 36 | + | { type: 'pathMatchesRegex'; subject: string[] } // regex patterns as strings |
| 37 | + | { type: 'contentMatchesRegex'; subject: string[] } // regex patterns as strings |
| 38 | + | { type: 'fileChangeType'; subject: TreeStatus } |
| 39 | + | { type: 'semanticType'; subject: SemanticType }; |
| 40 | + |
| 41 | +/** |
| 42 | + * Represents the type of change that occurred in the Git worktree. |
| 43 | + * Matches the TreeStatus of the TreeChange. |
| 44 | + */ |
| 45 | +export type TreeStatus = 'addition' | 'deletion' | 'modification' | 'rename'; |
| 46 | + |
| 47 | +/** |
| 48 | + * Represents a semantic type of change that was inferred for the change. |
| 49 | + * Typically this means a heuristic or an LLM determined that a change represents a refactor, a new feature, a bug fix, or documentation update. |
| 50 | + */ |
| 51 | +export type SemanticType = |
| 52 | + | { type: 'refactor' } |
| 53 | + | { type: 'newFeature' } |
| 54 | + | { type: 'bugFix' } |
| 55 | + | { type: 'documentation' } |
| 56 | + | { type: 'userDefined'; subject: string }; |
| 57 | + |
| 58 | +/** |
| 59 | + * Represents an action that can be taken based on the rule evaluation. |
| 60 | + * An action can be either explicit (user defined) or implicit (determined by heuristics or AI). |
| 61 | + */ |
| 62 | +export type Action = |
| 63 | + | { type: 'explicit'; subject: Operation } |
| 64 | + | { type: 'implicit'; subject: ImplicitOperation }; |
| 65 | + |
| 66 | +/** |
| 67 | + * Represents the operation that a user can configure to be performed in an explicit action. |
| 68 | + */ |
| 69 | +export type Operation = |
| 70 | + | { type: 'assign'; stackId: string } |
| 71 | + | { type: 'amend'; commitId: string } |
| 72 | + | { type: 'newCommit'; branchName: string }; |
| 73 | + |
| 74 | +/** |
| 75 | + * Represents the implicit operation that is determined by heuristics or AI. |
| 76 | + */ |
| 77 | +export type ImplicitOperation = |
| 78 | + | { type: 'assignToAppropriateBranch' } |
| 79 | + | { type: 'absorbIntoDependentCommit' } |
| 80 | + | { type: 'llmPrompt'; subject: string }; |
| 81 | + |
| 82 | +/** |
| 83 | + * A request to create a new workspace rule. |
| 84 | + */ |
| 85 | +export interface CreateRuleRequest { |
| 86 | + /** The trigger that causes the rule to be evaluated. */ |
| 87 | + trigger: Trigger; |
| 88 | + /** The filters that determine what files or changes the rule applies to. Cannot be empty. */ |
| 89 | + filters: Filter[]; |
| 90 | + /** The action that determines what happens to the files or changes that matched the filters. */ |
| 91 | + action: Action; |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * A request to update an existing workspace rule. |
| 96 | + */ |
| 97 | +export interface UpdateRuleRequest { |
| 98 | + /** The ID of the rule to update. */ |
| 99 | + id: WorkspaceRuleId; |
| 100 | + /** The new enabled state of the rule. If not provided, the existing state is retained. */ |
| 101 | + enabled: boolean | null; |
| 102 | + /** The new trigger for the rule. If not provided, the existing trigger is retained. */ |
| 103 | + trigger: Trigger | null; |
| 104 | + /** The new filters for the rule. If not provided, the existing filters are retained. */ |
| 105 | + filters: Filter[] | null; |
| 106 | + /** The new action for the rule. If not provided, the existing action is retained. */ |
| 107 | + action: Action | null; |
| 108 | +} |
0 commit comments