section 8: Phase 8: Run and stop project actions with URL templates

This commit is contained in:
Chris Scott committed 2026-10-03 02:09:54 -04:00
1 parent 852797a897
commit d6aeafeae9
6 files changed
+470 -5

No files matched your search

+24 -1
View File
@@ -3,7 +3,7 @@ import type { Context } from 'hono'
import type { ContentfulStatusCode } from 'hono/utils/http-status'
import type { Database } from 'bun:sqlite'
import type { Repo } from '@opencode-manager/shared/types'
import { UpdateProjectActionsRequestSchema, UpdateWorktreeSetupRequestSchema, TrustRepoConfigRequestSchema, MoveProjectItemRequestSchema } from '@opencode-manager/shared/schemas'
import { UpdateProjectActionsRequestSchema, UpdateWorktreeSetupRequestSchema, TrustRepoConfigRequestSchema, MoveProjectItemRequestSchema, RunProjectActionRequestSchema } from '@opencode-manager/shared/schemas'
import * as repoService from '../services/repo'
import { resolveRepoWorkingDirectory } from '../services/repo'
import { getRepoById } from '../db/queries'
@@ -165,5 +165,28 @@ export function createRepoProjectConfigRoutes(
}
})
app.post('/:id/project-config/actions/:actionId/run', async (c) => {
const body = await c.req.json().catch(() => ({}))
const parsed = RunProjectActionRequestSchema.safeParse(body)
if (!parsed.success) {
return c.json({ error: 'Invalid request', details: parsed.error.flatten() }, 400)
}
const resolved = await resolveRepoDirectory(c, c.req.param('id'), parsed.data.directory)
if (resolved instanceof Response) return resolved
try {
const response = await deps.projectConfigService.runAction(
resolved.repo,
resolved.directory,
c.req.param('actionId'),
deps.terminalService,
)
return c.json(response)
} catch (error: unknown) {
return handleProjectConfigError(c, error)
}
})
return app
}
+96 -1
View File
@@ -12,11 +12,15 @@ import {
type ProjectAction,
type ProjectConfigResponse,
type RepoProjectFile,
type RunProjectActionResponse,
} from '@opencode-manager/shared/schemas'
import { getRepoByDirectory, getRepoSetting, setRepoSetting } from '../db/queries'
import { executeCommand } from '../utils/process'
import { getErrorMessage } from '../utils/error-utils'
import { canonicalPathSync } from '../utils/fs-safe'
import type { GitService } from './git/GitService'
import type { GitAuthService } from './git-auth'
import type { TerminalService } from './terminal'
const PROJECT_ACTIONS_KEY = 'projectActions'
const WORKTREE_SETUP_KEY = 'worktreeSetupCommands'
@@ -55,6 +59,8 @@ export class ProjectConfigError extends Error {
}
export class ProjectConfigService {
private readonly actionRunQueues = new Map<string, Promise<void>>()
constructor(
private readonly database: Database,
private readonly git: GitService,
@@ -124,8 +130,14 @@ export class ProjectConfigService {
repoFile.hash !== null &&
repoFile.hash === getRepoSetting(this.database, projectRepo.id, REPO_CONFIG_TRUST_KEY)
const resolvedActions = await Promise.all(
actions.map(async (action) =>
action.url ? { ...action, resolvedUrl: await this.resolveActionUrl(action.url, directory) } : action,
),
)
return {
actions,
actions: resolvedActions,
worktreeSetup,
repoFile: {
path: '.ocm/project.json',
@@ -138,6 +150,89 @@ export class ProjectConfigService {
}
}
async resolveActionUrl(template: string, directory: string): Promise<string> {
const withWorktree = template.replaceAll('{worktree}', path.basename(directory))
if (!withWorktree.includes('{branch}')) {
return withWorktree
}
try {
const branch = (
await executeCommand(['git', '-C', directory, 'rev-parse', '--abbrev-ref', 'HEAD'], {
env: this.gitAuthService.getGitEnvironment(),
silent: true,
})
).trim()
return withWorktree.replaceAll('{branch}', branch)
} catch {
return template
}
}
async runAction(
repo: Repo,
directory: string,
actionId: string,
terminalService: TerminalService,
): Promise<RunProjectActionResponse> {
const config = await this.getConfig(repo, directory)
const action = config.actions.find((item) => item.id === actionId)
if (!action) {
throw new ProjectConfigError(404, 'Action not found')
}
if (action.source === 'repo' && !config.repoFile.trusted) {
throw new ProjectConfigError(409, 'Repository commands are not trusted', 'REPO_CONFIG_UNTRUSTED', {
hash: config.repoFile.hash,
})
}
const key = this.actionRunKey(directory, actionId)
return this.enqueueActionRun(key, async () => {
const terminals = await terminalService.list(directory)
const running = terminals.find(
(terminal) => terminal.kind === 'action' && terminal.actionId === actionId && terminal.status === 'running',
)
const terminal =
running ??
(await terminalService.create(directory, {
kind: 'action',
actionId,
name: action.name,
command: '/bin/sh',
args: ['-c', action.command],
}))
return {
terminal,
alreadyRunning: running !== undefined,
...(action.resolvedUrl ? { resolvedUrl: action.resolvedUrl } : {}),
autoOpenUrl: action.autoOpenUrl,
}
})
}
private actionRunKey(directory: string, actionId: string): string {
const canonical = canonicalPathSync(path.resolve(directory))
return `${canonical}\u0000${actionId}`
}
private enqueueActionRun<T>(key: string, operation: () => Promise<T>): Promise<T> {
const previous = this.actionRunQueues.get(key) ?? Promise.resolve()
const result = previous.then(operation, operation)
const tail = result.then(
() => undefined,
() => undefined,
)
this.actionRunQueues.set(key, tail)
void tail.then(() => {
if (this.actionRunQueues.get(key) === tail) {
this.actionRunQueues.delete(key)
}
})
return result
}
readRepoFile(directory: string): RepoFileState {
const filePath = this.getRepoFilePath(directory)
if (!fs.existsSync(filePath)) {