Skip to content
Closed
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
30 changes: 16 additions & 14 deletions gitnexus/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion gitnexus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"mnemonist": "^0.39.0",
"onnxruntime-node": "^1.24.0",
"pandemonium": "^2.4.0",
"path-to-regexp": "^8.4.2",
"tree-sitter": "^0.21.1",
"tree-sitter-c": "0.23.2",
"tree-sitter-c-sharp": "^0.23.1",
Expand All @@ -89,14 +90,14 @@
"tree-sitter-swift": "^0.6.0"
},
"devDependencies": {
"gitnexus-shared": "file:../gitnexus-shared",
"@types/cli-progress": "^3.11.6",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/js-yaml": "^4.0.9",
"@types/node": "^20.0.0",
"@types/uuid": "^10.0.0",
"@vitest/coverage-v8": "^4.0.18",
"gitnexus-shared": "file:../gitnexus-shared",
"tsx": "^4.0.0",
"typescript": "^5.4.5",
"vitest": "^4.0.18"
Expand Down
16 changes: 12 additions & 4 deletions gitnexus/src/cli/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ export function registerGroupCommands(program: Command): void {
)
.action(async (groupName: string, groupPath: string, registryName: string) => {
const { getGroupDir, getDefaultGitnexusDir } = await import('../core/group/storage.js');
const { loadGroupConfig } = await import('../core/group/config-parser.js');
const { loadGroupConfig, serializeGroupConfig } = await import('../core/group/config-parser.js');
const path = await import('node:path');
const fs = await import('node:fs/promises');
const groupDir = getGroupDir(getDefaultGitnexusDir(), groupName);
const config = await loadGroupConfig(groupDir);
config.repos[groupPath] = registryName;

await fs.writeFile(path.join(groupDir, 'group.yaml'), yaml.dump(config), 'utf-8');
await fs.writeFile(
path.join(groupDir, 'group.yaml'),
yaml.dump(serializeGroupConfig(config)),
'utf-8',
);
console.log(`Added ${registryName} as "${groupPath}" to group "${groupName}"`);
console.log(`Run: gitnexus group sync ${groupName}`);
});
Expand All @@ -45,7 +49,7 @@ export function registerGroupCommands(program: Command): void {
.description('Remove a repo from a group')
.action(async (groupName: string, repoPath: string) => {
const { getGroupDir, getDefaultGitnexusDir } = await import('../core/group/storage.js');
const { loadGroupConfig } = await import('../core/group/config-parser.js');
const { loadGroupConfig, serializeGroupConfig } = await import('../core/group/config-parser.js');
const path = await import('node:path');
const fs = await import('node:fs/promises');
const groupDir = getGroupDir(getDefaultGitnexusDir(), groupName);
Expand All @@ -56,7 +60,11 @@ export function registerGroupCommands(program: Command): void {
return;
}
delete config.repos[repoPath];
await fs.writeFile(path.join(groupDir, 'group.yaml'), yaml.dump(config), 'utf-8');
await fs.writeFile(
path.join(groupDir, 'group.yaml'),
yaml.dump(serializeGroupConfig(config)),
'utf-8',
);
console.log(`Removed "${repoPath}" from group "${groupName}"`);
});

Expand Down
93 changes: 92 additions & 1 deletion gitnexus/src/core/group/config-parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { createRequire } from 'node:module';
import type { GroupConfig, GroupManifestLink, ContractType, ContractRole } from './types.js';
import type {
GroupConfig,
GroupManifestLink,
ContractType,
ContractRole,
HttpMappingRule,
} from './types.js';

const _require = createRequire(import.meta.url);
const yaml = _require('js-yaml') as typeof import('js-yaml');
Expand All @@ -21,6 +27,30 @@ const DEFAULT_MATCHING = {
max_candidates_per_step: 3,
};

export function serializeGroupConfig(config: GroupConfig): Record<string, unknown> {
return {
version: config.version,
name: config.name,
description: config.description,
repos: config.repos,
links: config.links,
http_mappings: config.httpMappings.map((mapping) => ({
from: mapping.from,
to: {
repo: mapping.to.repo,
...(mapping.to.service ? { service: mapping.to.service } : {}),
},
...(mapping.methods ? { methods: mapping.methods } : {}),
match: mapping.match,
rewrite: mapping.rewrite,
...(mapping.when ? { when: mapping.when } : {}),
})),
packages: config.packages,
detect: config.detect,
matching: config.matching,
};
}

export function parseGroupConfig(yamlContent: string): GroupConfig {
const raw = yaml.load(yamlContent, { schema: yaml.JSON_SCHEMA }) as Record<string, unknown>;

Expand Down Expand Up @@ -73,6 +103,66 @@ export function parseGroupConfig(yamlContent: string): GroupConfig {
};
});

const rawHttpMappings = (raw.http_mappings as unknown[]) || [];
const httpMappings: HttpMappingRule[] = rawHttpMappings.map((entry: unknown, i: number) => {
const mapping = entry as Record<string, unknown>;
if (!mapping.from || !repoPaths.has(mapping.from as string)) {
throw new Error(`http_mappings[${i}].from "${mapping.from}" does not match any repo path`);
}
if (!mapping.to || typeof mapping.to !== 'object' || Array.isArray(mapping.to)) {
throw new Error(`http_mappings[${i}].to is required and must be an object`);
}
const target = mapping.to as Record<string, unknown>;
if (!target.repo || !repoPaths.has(target.repo as string)) {
throw new Error(
`http_mappings[${i}].to.repo "${target.repo}" does not match any repo path`,
);
}
if (mapping.match === undefined || String(mapping.match).trim() === '') {
throw new Error(`http_mappings[${i}].match is required`);
}
if (mapping.rewrite === undefined || String(mapping.rewrite).trim() === '') {
throw new Error(`http_mappings[${i}].rewrite is required`);
}

let methods: string[] | undefined;
if (mapping.methods !== undefined) {
if (!Array.isArray(mapping.methods)) {
throw new Error(`http_mappings[${i}].methods must be an array when provided`);
}
methods = mapping.methods.map((method) => String(method).trim().toUpperCase()).filter(Boolean);
if (methods.length === 0) {
throw new Error(`http_mappings[${i}].methods must not be empty`);
}
}

let when: Record<string, string> | undefined;
if (mapping.when !== undefined) {
if (typeof mapping.when !== 'object' || Array.isArray(mapping.when) || mapping.when === null) {
throw new Error(`http_mappings[${i}].when must be an object when provided`);
}
when = Object.fromEntries(
Object.entries(mapping.when as Record<string, unknown>).map(([key, value]) => [
key,
String(value),
]),
);
}

return {
from: mapping.from as string,
to: {
repo: target.repo as string,
service:
target.service === undefined || target.service === null ? undefined : String(target.service),
},
methods,
match: String(mapping.match),
rewrite: String(mapping.rewrite),
when,
};
});

const detect = { ...DEFAULT_DETECT, ...((raw.detect as object) || {}) };
const matching = { ...DEFAULT_MATCHING, ...((raw.matching as object) || {}) };
const packages = (raw.packages as Record<string, Record<string, string>>) || {};
Expand All @@ -83,6 +173,7 @@ export function parseGroupConfig(yamlContent: string): GroupConfig {
description: (raw.description as string) || '',
repos,
links,
httpMappings,
packages,
detect,
matching,
Expand Down
36 changes: 36 additions & 0 deletions gitnexus/src/core/group/extractors/http-route-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as path from 'node:path';
import { glob } from 'glob';
import type { ContractExtractor, CypherExecutor } from '../contract-extractor.js';
import type { ExtractedContract, RepoHandle } from '../types.js';
import { collectRequestLikeImportBindings } from '../../ingestion/request-like-clients.js';

const HANDLES_ROUTE_QUERY = `
MATCH (handlerFile:File)-[r:CodeRelation {type: 'HANDLES_ROUTE'}]->(route:Route)
Expand Down Expand Up @@ -418,6 +419,7 @@ export class HttpRouteExtractor implements ContractExtractor {
if (!content) continue;
out.push(...this.scanFetchConsumers(content, rel));
out.push(...this.scanAxiosConsumers(content, rel));
out.push(...this.scanRequestLikeConsumers(content, rel));
}
return this.dedupeContracts(out);
}
Expand Down Expand Up @@ -451,6 +453,40 @@ export class HttpRouteExtractor implements ContractExtractor {
return out;
}

private scanRequestLikeConsumers(content: string, filePath: string): ExtractedContract[] {
const bindings = collectRequestLikeImportBindings(content);
if (bindings.size === 0) return [];

const names = [...bindings].map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
const bindingRe = `(?:${names.join('|')})`;
const out: ExtractedContract[] = [];

const directRe = new RegExp(
`${bindingRe}\\s*\\(\\s*[\\\`'"]([^\\\`'"]+)[\\\`'"](?:\\s*,\\s*\\{[^}]*method:\\s*['"](\\w+)['"][^}]*\\})?\\s*\\)`,
'gi',
);
let directMatch: RegExpExecArray | null;
while ((directMatch = directRe.exec(content)) !== null) {
const pathNorm = normalizeHttpPath(this.templateToPattern(directMatch[1]));
const method = (directMatch[2] || 'GET').toUpperCase();
out.push(this.makeConsumer(filePath, method, pathNorm, 0.75));
}

const memberRe = new RegExp(
`${bindingRe}\\.(get|post|put|delete|patch|head|options|request|ajax)\\s*\\(\\s*[\\\`'"]([^\\\`'"]+)[\\\`'"]`,
'gi',
);
let memberMatch: RegExpExecArray | null;
while ((memberMatch = memberRe.exec(content)) !== null) {
const rawMethod = memberMatch[1].toUpperCase();
const method = rawMethod === 'REQUEST' || rawMethod === 'AJAX' ? 'GET' : rawMethod;
const pathNorm = normalizeHttpPath(this.templateToPattern(memberMatch[2]));
out.push(this.makeConsumer(filePath, method, pathNorm, 0.75));
}

return out;
}

private makeConsumer(
filePath: string,
method: string,
Expand Down
Loading