diff --git a/package-lock.json b/package-lock.json index 83e081a..e37c0e1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@dev-mainsequence/devtools", - "version": "0.3.17", + "version": "0.3.18", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@dev-mainsequence/devtools", - "version": "0.3.17", + "version": "0.3.18", "license": "MIT", "bin": { "ms-devtools": "dist/runner.js" diff --git a/package.json b/package.json index 25d6585..391b73d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dev-mainsequence/devtools", - "version": "0.3.17", + "version": "0.3.18", "description": "Shared MainSequence developer actions, project operations, MCP client and command runner", "license": "MIT", "engines": { diff --git a/src/shared/repositoryModels.ts b/src/shared/repositoryModels.ts index 7f2db7e..1cb00d8 100644 --- a/src/shared/repositoryModels.ts +++ b/src/shared/repositoryModels.ts @@ -29,7 +29,8 @@ export type GitHubRepositoryBinding = { code_repository_uid: string; code_repository_name: string; repo_name: string; - github_full_name: string; + /** The backend leaves it empty for some bindings; setup clones from `git_ssh_url` and never reads it. */ + github_full_name: string | null; git_ssh_url?: string | null; git_repo_url?: string | null; }; @@ -55,6 +56,14 @@ function requiredObject(value: unknown, label: string): Record { return value as Record; } +/** A label nothing depends on: missing or empty is null, never a reason to refuse the record. */ +function optionalLabel(value: unknown, label: string): string | null { + const normalized = typeof value === 'string' ? value.trim() : ''; + if (!normalized) return null; + if (/[\r\n]/.test(normalized)) throw new ApiError(`${label} must be a single line.`); + return normalized; +} + function optionalSingleLine(value: unknown, label: string): string | null | undefined { if (value == null) return value as null | undefined; return requiredSingleLine(value, label); @@ -111,7 +120,7 @@ export function gitHubRepositoryBindingFromResponse(value: unknown, label: strin code_repository_uid: requiredSingleLine(payload.code_repository_uid, `${label} CodeRepository UID`), code_repository_name: requiredSingleLine(payload.code_repository_name, `${label} CodeRepository name`), repo_name: requiredSingleLine(payload.repo_name, `${label} repository name`), - github_full_name: requiredSingleLine(payload.github_full_name, `${label} GitHub full name`), + github_full_name: optionalLabel(payload.github_full_name, `${label} GitHub full name`), git_ssh_url: optionalSingleLine(payload.git_ssh_url, `${label} SSH URL`), git_repo_url: optionalSingleLine(payload.git_repo_url, `${label} Git URL`) }; diff --git a/tests/repository-models.test.cjs b/tests/repository-models.test.cjs new file mode 100644 index 0000000..670020d --- /dev/null +++ b/tests/repository-models.test.cjs @@ -0,0 +1,18 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { gitHubRepositoryBindingFromResponse } = require('../dist/shared/repositoryModels.js'); + +const binding = { uid: 'binding-1', code_repository_uid: 'repo-1', code_repository_name: 'binance-connectors', repo_name: 'binance-connectors', + git_ssh_url: 'git@github.com:mainsequence-projects/binance-connectors.git', git_repo_url: 'https://github.com/mainsequence-projects/binance-connectors.git' }; + +test('a GitHub binding without a GitHub full name is still a binding: setup clones from its SSH URL', () => { + for (const github_full_name of ['', ' ', null, undefined]) { + const parsed = gitHubRepositoryBindingFromResponse({ ...binding, github_full_name }, 'GitHub binding'); + assert.equal(parsed.github_full_name, null, JSON.stringify(github_full_name)); + assert.equal(parsed.git_ssh_url, binding.git_ssh_url); + } + assert.equal(gitHubRepositoryBindingFromResponse({ ...binding, github_full_name: ' mainsequence-projects/binance-connectors ' }, 'GitHub binding').github_full_name, + 'mainsequence-projects/binance-connectors'); + assert.throws(() => gitHubRepositoryBindingFromResponse({ ...binding, github_full_name: 'a\nb' }, 'GitHub binding'), /GitHub full name must be a single line/); + assert.throws(() => gitHubRepositoryBindingFromResponse({ ...binding, uid: '' }, 'GitHub binding'), /GitHub binding UID is required/); +}); diff --git a/tests/setup.test.cjs b/tests/setup.test.cjs index 57866b6..bbcb7d7 100644 --- a/tests/setup.test.cjs +++ b/tests/setup.test.cjs @@ -31,7 +31,7 @@ const fetchImpl = async (url, options) => { let result; if(endpoint === '/api/v1/code-repositories/repo-1/') result = {uid:'repo-1',code_repository_name:'Shared',code_repository_type:'python',primary_language:'python',framework:'none',github_repository_binding_uid:'binding-1',branches:[{uid:'branch-1',repository_branch:'main'}]}; else if(endpoint === '/api/v1/code-repository-branches/branch-1/') result = {uid:'branch-1',code_repository_uid:'repo-1',code_repository_name:'Shared',repository_branch:'main',github_repository_binding_uid:'binding-1',is_initialized:true}; - else if(endpoint === '/api/v1/github-repository-bindings/binding-1/') result = {uid:'binding-1',code_repository_uid:'repo-1',code_repository_name:'Shared',repo_name:'Shared',github_full_name:'Example/Shared',git_ssh_url:'git@github.com:Example/Shared.git'}; + else if(endpoint === '/api/v1/github-repository-bindings/binding-1/') result = {uid:'binding-1',code_repository_uid:'repo-1',code_repository_name:'Shared',repo_name:'Shared',github_full_name:'',git_ssh_url:'git@github.com:Example/Shared.git'}; // the backend leaves the name empty for some bindings else if(endpoint === '/api/v1/users/me/') result = {username:'fixture',organization:{name:'Fixture Org'}}; else if(endpoint === '/api/v1/code-repositories/repo-1/add-deploy-key/') { const key = JSON.parse(options.body); if(!key.public_key.startsWith('ssh-ed25519 ')) throw Error('invalid fixture key'); result={};