-
-
Notifications
You must be signed in to change notification settings - Fork 6
feat: Linear status sync during loop execution #274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { createLinearSync } from '../linear-sync.js'; | ||
|
|
||
| const updateTask = vi.fn().mockResolvedValue({ | ||
| id: 'uuid-123', | ||
| identifier: 'ENG-42', | ||
| title: 'Test issue', | ||
| url: 'https://linear.app/team/ENG-42', | ||
| status: 'In Progress', | ||
| source: 'linear', | ||
| }); | ||
| const addComment = vi.fn().mockResolvedValue(undefined); | ||
|
|
||
| // Mock the LinearIntegration class | ||
| vi.mock('../../integrations/linear/source.js', () => ({ | ||
| LinearIntegration: class MockLinearIntegration { | ||
| updateTask = updateTask; | ||
| addComment = addComment; | ||
| }, | ||
| })); | ||
|
|
||
| describe('createLinearSync', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| // Reset default resolved value | ||
| updateTask.mockResolvedValue({ | ||
| id: 'uuid-123', | ||
| identifier: 'ENG-42', | ||
| title: 'Test issue', | ||
| url: 'https://linear.app/team/ENG-42', | ||
| status: 'In Progress', | ||
| source: 'linear', | ||
| }); | ||
| }); | ||
|
|
||
| it('should move issue to In Progress on creation', async () => { | ||
| const handler = await createLinearSync({ issueId: 'ENG-42', headless: true }); | ||
|
|
||
| expect(handler).not.toBeNull(); | ||
| expect(updateTask).toHaveBeenCalledWith('ENG-42', { status: 'In Progress' }); | ||
| }); | ||
|
|
||
| it('should return null if updateTask fails (no auth)', async () => { | ||
| updateTask.mockRejectedValueOnce(new Error('No API key')); | ||
|
|
||
| const handler = await createLinearSync({ issueId: 'ENG-42', headless: true }); | ||
| expect(handler).toBeNull(); | ||
| }); | ||
|
|
||
| it('should move issue to Done on complete event', async () => { | ||
| const handler = await createLinearSync({ issueId: 'ENG-42', headless: true }); | ||
|
|
||
| await handler!({ | ||
| type: 'complete', | ||
| summary: 'Implemented feature X', | ||
| commits: 3, | ||
| iterations: 5, | ||
| cost: '$0.42', | ||
| }); | ||
|
|
||
| expect(updateTask).toHaveBeenCalledWith('ENG-42', { status: 'Done' }); | ||
| expect(addComment).toHaveBeenCalledWith( | ||
| 'ENG-42', | ||
| expect.stringContaining('Loop completed successfully') | ||
| ); | ||
| expect(addComment).toHaveBeenCalledWith('ENG-42', expect.stringContaining('Commits: 3')); | ||
| expect(addComment).toHaveBeenCalledWith('ENG-42', expect.stringContaining('$0.42')); | ||
| }); | ||
|
|
||
| it('should move issue to In Review on failed event', async () => { | ||
| const handler = await createLinearSync({ issueId: 'ENG-42', headless: true }); | ||
|
|
||
| await handler!({ | ||
| type: 'failed', | ||
| error: 'circuit_breaker', | ||
| iterations: 3, | ||
| }); | ||
|
|
||
| expect(updateTask).toHaveBeenCalledWith('ENG-42', { status: 'In Review' }); | ||
| expect(addComment).toHaveBeenCalledWith('ENG-42', expect.stringContaining('Loop stopped')); | ||
| expect(addComment).toHaveBeenCalledWith('ENG-42', expect.stringContaining('circuit_breaker')); | ||
| }); | ||
|
|
||
| it('should not throw on event handler errors', async () => { | ||
| const handler = await createLinearSync({ issueId: 'ENG-42', headless: true }); | ||
|
|
||
| // Make the next updateTask call fail | ||
| updateTask.mockRejectedValueOnce(new Error('Network error')); | ||
|
|
||
| // Should not throw | ||
| await expect( | ||
| handler!({ type: 'complete', summary: 'done', commits: 1, iterations: 1 }) | ||
| ).resolves.not.toThrow(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /** | ||
| * Linear Status Sync | ||
| * | ||
| * Syncs loop execution status to a Linear issue in real-time. | ||
| * Updates issue state at key transitions: start → In Progress, complete → Done, failed → In Review. | ||
| */ | ||
|
|
||
| import chalk from 'chalk'; | ||
| import { LinearIntegration } from '../integrations/linear/source.js'; | ||
|
|
||
| export type LinearSyncConfig = { | ||
| /** Linear issue identifier (e.g., "ENG-42") or UUID */ | ||
| issueId: string; | ||
| /** Suppress console output */ | ||
| headless?: boolean; | ||
| }; | ||
|
|
||
| export type LinearSyncEvent = | ||
| | { type: 'start' } | ||
| | { type: 'iteration'; iteration: number; totalIterations: number; success: boolean } | ||
| | { type: 'complete'; summary: string; commits: number; iterations: number; cost?: string } | ||
| | { type: 'failed'; error: string; iterations: number }; | ||
|
rubenmarcus marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Creates a Linear sync handler that updates issue status at key loop transitions. | ||
| * | ||
| * Returns null if auth is missing or the issue can't be found (non-blocking). | ||
| */ | ||
| export async function createLinearSync( | ||
| config: LinearSyncConfig | ||
| ): Promise<((event: LinearSyncEvent) => Promise<void>) | null> { | ||
| const linear = new LinearIntegration(); | ||
|
rubenmarcus marked this conversation as resolved.
|
||
| const log = config.headless ? (..._args: unknown[]) => {} : console.log.bind(console); | ||
|
|
||
| // Verify auth + issue exist by moving to "In Progress" (non-blocking on failure) | ||
| try { | ||
| await linear.updateTask(config.issueId, { status: 'In Progress' }); | ||
| log(chalk.dim(` Linear sync: ${config.issueId} → In Progress`)); | ||
| } catch (err) { | ||
| log( | ||
| chalk.yellow(` Linear sync: could not update ${config.issueId} — ${(err as Error).message}`) | ||
| ); | ||
| return null; | ||
| } | ||
|
|
||
| return async (event: LinearSyncEvent) => { | ||
| try { | ||
| switch (event.type) { | ||
| case 'start': | ||
| // Already moved to "In Progress" during init | ||
| break; | ||
|
|
||
| case 'iteration': | ||
| // No status change per iteration | ||
| break; | ||
|
|
||
| case 'complete': { | ||
| const lines = ['**Loop completed successfully**', '']; | ||
| lines.push(`- Iterations: ${event.iterations}`); | ||
| if (event.commits > 0) lines.push(`- Commits: ${event.commits}`); | ||
| if (event.cost) lines.push(`- Cost: ${event.cost}`); | ||
| if (event.summary) { | ||
| lines.push('', `**Summary:** ${event.summary.slice(0, 500)}`); | ||
| } | ||
|
|
||
| await linear.updateTask(config.issueId, { status: 'Done' }); | ||
| await linear.addComment(config.issueId, lines.join('\n')); | ||
|
rubenmarcus marked this conversation as resolved.
|
||
| log(chalk.dim(` Linear sync: ${config.issueId} → Done`)); | ||
| break; | ||
| } | ||
|
|
||
| case 'failed': { | ||
| const lines = ['**Loop stopped**', '']; | ||
| lines.push(`- Iterations: ${event.iterations}`); | ||
| if (event.error) { | ||
| lines.push(`- Reason: ${event.error.slice(0, 300)}`); | ||
| } | ||
|
|
||
| await linear.updateTask(config.issueId, { status: 'In Review' }); | ||
| await linear.addComment(config.issueId, lines.join('\n')); | ||
| log(chalk.dim(` Linear sync: ${config.issueId} → In Review`)); | ||
| break; | ||
| } | ||
| } | ||
| } catch (err) { | ||
| // Non-blocking — log and continue | ||
| if (process.env.RALPH_DEBUG) { | ||
| console.error(`[DEBUG] Linear sync error: ${(err as Error).message}`); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.