diff --git a/ocm-cli/src/remote-context.ts b/ocm-cli/src/remote-context.ts index f483f33d..97c61fa6 100644 --- a/ocm-cli/src/remote-context.ts +++ b/ocm-cli/src/remote-context.ts @@ -1,15 +1,19 @@ export const REMOTE_MANAGER_URL_ENV = 'OCM_REMOTE_MANAGER_URL' export const REMOTE_REPO_NAME_ENV = 'OCM_REMOTE_REPO_NAME' +export const REMOTE_REPO_ID_ENV = 'OCM_REMOTE_REPO_ID' export type RemoteContext = { + managerUrl: string managerHost: string repoName?: string + repoId?: number } -export function buildRemoteAttachEnv(managerUrl: string, repoName: string): Record { +export function buildRemoteAttachEnv(managerUrl: string, repoName: string, repoId: number): Record { return { [REMOTE_MANAGER_URL_ENV]: managerUrl, [REMOTE_REPO_NAME_ENV]: repoName, + [REMOTE_REPO_ID_ENV]: String(repoId), } } @@ -17,14 +21,19 @@ export function readRemoteContext(env: NodeJS.ProcessEnv): RemoteContext | undef const urlValue = env[REMOTE_MANAGER_URL_ENV] if (!urlValue) return undefined + const managerUrl = urlValue.trim() + let managerHost: string try { - managerHost = new URL(urlValue).host + managerHost = new URL(managerUrl).host } catch { - managerHost = urlValue.trim() + managerHost = managerUrl } const repoName = env[REMOTE_REPO_NAME_ENV] || undefined - return { managerHost, repoName } -} \ No newline at end of file + const repoIdValue = Number(env[REMOTE_REPO_ID_ENV]) + const repoId = Number.isInteger(repoIdValue) && repoIdValue > 0 ? repoIdValue : undefined + + return { managerUrl, managerHost, repoName, repoId } +} diff --git a/ocm-cli/src/warp.ts b/ocm-cli/src/warp.ts index f1de7185..7c1ae749 100644 --- a/ocm-cli/src/warp.ts +++ b/ocm-cli/src/warp.ts @@ -42,7 +42,7 @@ export function buildAttachInvocation(target: AttachTarget): AttachInvocation { if (target.sessionID) args.push('--session', target.sessionID) return { args, - env: { ...process.env, OPENCODE_PASSWORD: target.token, ...buildRemoteAttachEnv(target.managerUrl, target.repoName) }, + env: { ...process.env, OPENCODE_PASSWORD: target.token, ...buildRemoteAttachEnv(target.managerUrl, target.repoName, target.repoId) }, } } diff --git a/ocm-cli/test/remote-context.test.ts b/ocm-cli/test/remote-context.test.ts index d7a5255b..8e259691 100644 --- a/ocm-cli/test/remote-context.test.ts +++ b/ocm-cli/test/remote-context.test.ts @@ -1,16 +1,22 @@ import { describe, it, expect } from 'vitest' import { REMOTE_MANAGER_URL_ENV, + REMOTE_REPO_ID_ENV, buildRemoteAttachEnv, readRemoteContext, } from '../src/remote-context.js' describe('buildRemoteAttachEnv', () => { it('round-trips through readRemoteContext', () => { - const env = buildRemoteAttachEnv('https://mgr.example.com', 'oc-manager') + const env = buildRemoteAttachEnv('https://mgr.example.com', 'oc-manager', 7) const result = readRemoteContext(env) - expect(result).toEqual({ managerHost: 'mgr.example.com', repoName: 'oc-manager' }) + expect(result).toEqual({ + managerUrl: 'https://mgr.example.com', + managerHost: 'mgr.example.com', + repoName: 'oc-manager', + repoId: 7, + }) }) }) @@ -43,4 +49,32 @@ describe('readRemoteContext', () => { expect(result?.repoName).toBeUndefined() }) + + it('returns the trimmed raw manager URL', () => { + const env = { [REMOTE_MANAGER_URL_ENV]: ' https://mgr.example.com ' } + const result = readRemoteContext(env) + + expect(result?.managerUrl).toBe('https://mgr.example.com') + }) + + it('returns undefined repoId when the repo id var is missing', () => { + const env = { [REMOTE_MANAGER_URL_ENV]: 'https://mgr.example.com' } + const result = readRemoteContext(env) + + expect(result?.repoId).toBeUndefined() + }) + + it.each(['', 'abc', '0', '-1', '3.5'])('returns undefined repoId for %j', (value) => { + const env = { [REMOTE_MANAGER_URL_ENV]: 'https://mgr.example.com', [REMOTE_REPO_ID_ENV]: value } + const result = readRemoteContext(env) + + expect(result?.repoId).toBeUndefined() + }) + + it('returns the numeric repoId when the repo id var is a positive integer', () => { + const env = { [REMOTE_MANAGER_URL_ENV]: 'https://mgr.example.com', [REMOTE_REPO_ID_ENV]: '42' } + const result = readRemoteContext(env) + + expect(result?.repoId).toBe(42) + }) }) \ No newline at end of file diff --git a/ocm-cli/test/warp.test.ts b/ocm-cli/test/warp.test.ts index deb8d771..2533f861 100644 --- a/ocm-cli/test/warp.test.ts +++ b/ocm-cli/test/warp.test.ts @@ -1,7 +1,7 @@ import { describe, it, expect, vi, beforeEach } from 'vitest' import type { WarpTarget } from '../src/warp.js' import { setPendingWarp, takePendingWarp, buildAttachInvocation, runPendingWarp } from '../src/warp.js' -import { REMOTE_MANAGER_URL_ENV, REMOTE_REPO_NAME_ENV } from '../src/remote-context.js' +import { REMOTE_MANAGER_URL_ENV, REMOTE_REPO_ID_ENV, REMOTE_REPO_NAME_ENV } from '../src/remote-context.js' const sampleTarget: WarpTarget = { managerUrl: 'https://manager.example.com', @@ -24,6 +24,7 @@ describe('buildAttachInvocation', () => { OPENCODE_PASSWORD: 'tok_abc123', [REMOTE_MANAGER_URL_ENV]: 'https://manager.example.com', [REMOTE_REPO_NAME_ENV]: 'my-repo', + [REMOTE_REPO_ID_ENV]: '42', }) })