Skip to content

Commit 44b34e7

Browse files
committed
fix(backend): Add warnings for local repo index failures
When a local git repository connection fails to find any valid repositories, the system now logs warnings instead of failing silently. This helps users diagnose issues with their local repository paths. - Added warning when glob pattern matches no paths - Added warning when paths match but no valid git repos are found - Added info logs to show progress during local repo compilation - Added tests to verify the new warning behavior
1 parent 0b436bb commit 44b34e7

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import { expect, test, vi, describe, beforeEach, afterEach } from 'vitest';
2+
import { compileGenericGitHostConfig_file } from './repoCompileUtils';
3+
4+
// Mock the git module
5+
vi.mock('./git.js', () => ({
6+
isPathAValidGitRepoRoot: vi.fn(),
7+
getOriginUrl: vi.fn(),
8+
}));
9+
10+
// Mock the glob module
11+
vi.mock('glob', () => ({
12+
glob: vi.fn(),
13+
}));
14+
15+
import { isPathAValidGitRepoRoot, getOriginUrl } from './git.js';
16+
import { glob } from 'glob';
17+
18+
const mockedGlob = vi.mocked(glob);
19+
const mockedIsPathAValidGitRepoRoot = vi.mocked(isPathAValidGitRepoRoot);
20+
const mockedGetOriginUrl = vi.mocked(getOriginUrl);
21+
22+
describe('compileGenericGitHostConfig_file', () => {
23+
beforeEach(() => {
24+
vi.clearAllMocks();
25+
});
26+
27+
afterEach(() => {
28+
vi.resetAllMocks();
29+
});
30+
31+
test('should return warning when glob pattern matches no paths', async () => {
32+
mockedGlob.mockResolvedValue([]);
33+
34+
const config = {
35+
type: 'git' as const,
36+
url: 'file:///path/to/nonexistent/repo',
37+
};
38+
39+
const result = await compileGenericGitHostConfig_file(config, 1);
40+
41+
expect(result.repoData).toHaveLength(0);
42+
expect(result.warnings).toHaveLength(1);
43+
expect(result.warnings[0]).toContain('No paths matched the pattern');
44+
expect(result.warnings[0]).toContain('/path/to/nonexistent/repo');
45+
});
46+
47+
test('should return warning when path is not a valid git repo', async () => {
48+
mockedGlob.mockResolvedValue(['/path/to/not-a-repo']);
49+
mockedIsPathAValidGitRepoRoot.mockResolvedValue(false);
50+
51+
const config = {
52+
type: 'git' as const,
53+
url: 'file:///path/to/not-a-repo',
54+
};
55+
56+
const result = await compileGenericGitHostConfig_file(config, 1);
57+
58+
expect(result.repoData).toHaveLength(0);
59+
expect(result.warnings.length).toBeGreaterThanOrEqual(1);
60+
expect(result.warnings.some(w => w.includes('not a git repository'))).toBe(true);
61+
expect(result.warnings.some(w => w.includes('No valid git repositories found'))).toBe(true);
62+
});
63+
64+
test('should return warning when git repo has no origin url', async () => {
65+
mockedGlob.mockResolvedValue(['/path/to/repo']);
66+
mockedIsPathAValidGitRepoRoot.mockResolvedValue(true);
67+
mockedGetOriginUrl.mockResolvedValue(null);
68+
69+
const config = {
70+
type: 'git' as const,
71+
url: 'file:///path/to/repo',
72+
};
73+
74+
const result = await compileGenericGitHostConfig_file(config, 1);
75+
76+
expect(result.repoData).toHaveLength(0);
77+
expect(result.warnings.length).toBeGreaterThanOrEqual(1);
78+
expect(result.warnings.some(w => w.includes('remote.origin.url not found'))).toBe(true);
79+
expect(result.warnings.some(w => w.includes('No valid git repositories found'))).toBe(true);
80+
});
81+
82+
test('should successfully compile when valid git repo is found', async () => {
83+
mockedGlob.mockResolvedValue(['/path/to/valid/repo']);
84+
mockedIsPathAValidGitRepoRoot.mockResolvedValue(true);
85+
mockedGetOriginUrl.mockResolvedValue('https://github.com/test/repo.git');
86+
87+
const config = {
88+
type: 'git' as const,
89+
url: 'file:///path/to/valid/repo',
90+
};
91+
92+
const result = await compileGenericGitHostConfig_file(config, 1);
93+
94+
expect(result.repoData).toHaveLength(1);
95+
expect(result.warnings).toHaveLength(0);
96+
expect(result.repoData[0].cloneUrl).toBe('file:///path/to/valid/repo');
97+
expect(result.repoData[0].name).toBe('github.com/test/repo');
98+
});
99+
100+
test('should return warnings for invalid repos and success for valid ones', async () => {
101+
mockedGlob.mockResolvedValue(['/path/to/valid/repo', '/path/to/invalid/repo']);
102+
mockedIsPathAValidGitRepoRoot.mockImplementation(async ({ path }) => {
103+
return path === '/path/to/valid/repo';
104+
});
105+
mockedGetOriginUrl.mockImplementation(async (path: string) => {
106+
if (path === '/path/to/valid/repo') {
107+
return 'https://github.com/test/repo.git';
108+
}
109+
return null;
110+
});
111+
112+
const config = {
113+
type: 'git' as const,
114+
url: 'file:///path/to/**/repo',
115+
};
116+
117+
const result = await compileGenericGitHostConfig_file(config, 1);
118+
119+
expect(result.repoData).toHaveLength(1);
120+
expect(result.warnings).toHaveLength(1);
121+
expect(result.warnings[0]).toContain('/path/to/invalid/repo');
122+
expect(result.warnings[0]).toContain('not a git repository');
123+
});
124+
});

packages/backend/src/repoCompileUtils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,19 @@ export const compileGenericGitHostConfig_file = async (
479479
const repos: RepoData[] = [];
480480
const warnings: string[] = [];
481481

482+
// Warn if the glob pattern matched no paths at all
483+
if (repoPaths.length === 0) {
484+
const warning = `No paths matched the pattern '${configUrl.pathname}'. Please verify the path exists and is accessible.`;
485+
logger.warn(warning);
486+
warnings.push(warning);
487+
return {
488+
repoData: repos,
489+
warnings,
490+
};
491+
}
492+
493+
logger.info(`Found ${repoPaths.length} path(s) matching pattern '${configUrl.pathname}'`);
494+
482495
await Promise.all(repoPaths.map((repoPath) => gitOperationLimit(async () => {
483496
const isGitRepo = await isPathAValidGitRepoRoot({
484497
path: repoPath,
@@ -535,6 +548,15 @@ export const compileGenericGitHostConfig_file = async (
535548
repos.push(repo);
536549
})));
537550

551+
// Log summary of results
552+
if (repos.length === 0) {
553+
const warning = `No valid git repositories found from ${repoPaths.length} matched path(s). Check the warnings for details on individual paths.`;
554+
logger.warn(warning);
555+
warnings.push(warning);
556+
} else {
557+
logger.info(`Successfully found ${repos.length} valid git repository(s) from ${repoPaths.length} matched path(s)`);
558+
}
559+
538560
return {
539561
repoData: repos,
540562
warnings,

0 commit comments

Comments
 (0)