Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/triggers/shared/manual-runner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { runAgent } from '../../agents/registry.js';
import { getRunById } from '../../db/repositories/runsRepository.js';
import { withPMCredentials } from '../../pm/context.js';
import { createPMProvider, pmRegistry, withPMProvider } from '../../pm/index.js';
import type { AgentInput, CascadeConfig, ProjectConfig } from '../../types/index.js';
import { logger } from '../../utils/logging.js';

Expand Down Expand Up @@ -99,7 +101,13 @@ export async function triggerManualRun(
};

try {
const result = await runAgent(input.agentType, agentInput);
const pmProvider = createPMProvider(project);
const result = await withPMCredentials(
project.id,
project.pm?.type,
(t) => pmRegistry.getOrNull(t),
() => withPMProvider(pmProvider, () => runAgent(input.agentType, agentInput)),
);
logger.info('Manual agent run completed', {
projectId: input.projectId,
agentType: input.agentType,
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/triggers/manual-runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,27 @@ vi.mock('../../../src/utils/logging.js', () => ({
},
}));

vi.mock('../../../src/pm/index.js', () => ({
createPMProvider: vi.fn(() => ({ type: 'mock-pm' })),
pmRegistry: { getOrNull: vi.fn(() => null) },
withPMProvider: vi.fn((_provider: unknown, fn: () => unknown) => fn()),
}));

vi.mock('../../../src/pm/context.js', () => ({
withPMCredentials: vi.fn(
(
_projectId: string,
_pmType: string | undefined,
_getIntegration: unknown,
fn: () => unknown,
) => fn(),
),
}));

import { runAgent } from '../../../src/agents/registry.js';
import { getRunById } from '../../../src/db/repositories/runsRepository.js';
import { withPMCredentials } from '../../../src/pm/context.js';
import { createPMProvider, withPMProvider } from '../../../src/pm/index.js';
import {
clearTriggerTracking,
isTriggerRunning,
Expand Down Expand Up @@ -141,6 +160,34 @@ describe('triggerManualRun', () => {
);
});

it('wraps runAgent with PM credential and provider context', async () => {
vi.mocked(runAgent).mockResolvedValue({
success: true,
output: 'Done',
runId: 'run-pm',
});

await triggerManualRun(
{ projectId: 'test-project', agentType: 'review', prNumber: 42 },
mockProject,
mockConfig,
);

// createPMProvider called with the project
expect(createPMProvider).toHaveBeenCalledWith(mockProject);

// withPMCredentials called with project.id, pm type, registry lookup, and inner fn
expect(withPMCredentials).toHaveBeenCalledWith(
'test-project',
undefined, // mockProject has no pm.type
expect.any(Function),
expect.any(Function),
);

// withPMProvider called with the created provider and inner fn
expect(withPMProvider).toHaveBeenCalledWith({ type: 'mock-pm' }, expect.any(Function));
});

it('marks trigger as complete after runAgent finishes', async () => {
const projectId = 'test-project';
const agentType = 'implementation';
Expand Down