MCPcopy Index your code
hub / github.com/sourcebot-dev/sourcebot / searchGitLab

Function searchGitLab

packages/setupWizard/src/gitlab.ts:22–84  ·  view source on GitHub ↗
(
    apiBase: string,
    query: string,
    token: string,
    type: GitLabSearchType,
)

Source from the content-addressed store, hash-verified

20const PROJECT_PATTERN = /^[\w.-]+(\/[\w.-]+)+$/;
21
22async function searchGitLab(
23 apiBase: string,
24 query: string,
25 token: string,
26 type: GitLabSearchType,
27): Promise<Array<SearchOption | Separator>> {
28 const cacheKey = `${apiBase}|${type}|${query}`;
29 const cached = gitlabSearchCache.get(cacheKey);
30 if (cached) {
31 return cached;
32 }
33
34 const headers: Record<string, string> = {
35 'User-Agent': 'setup-sourcebot',
36 ...(token ? { Authorization: `Bearer ${token}` } : {}),
37 };
38
39 const endpoint = type === 'group' ? 'groups' : type === 'project' ? 'projects' : 'users';
40 const extraParams = type === 'project' ? '&simple=true' : '';
41 const url = `${apiBase}/${endpoint}?search=${encodeURIComponent(query)}&per_page=8${extraParams}`;
42 const res = await fetch(url, { headers });
43
44 const literalFallback = (): SearchOption | null => {
45 if (type === 'project') {
46 return PROJECT_PATTERN.test(query) ? { name: query, value: query } : null;
47 }
48 return { name: query, value: query };
49 };
50
51 if (!res.ok) {
52 const warning = res.status === 401
53 ? '⚠ Autocomplete disabled — authentication failed, check your PAT.'
54 : `⚠ Autocomplete disabled — GitLab API error (${res.status}).`;
55 const fallback = literalFallback();
56 return fallback ? [fallback, new Separator(warning)] : [new Separator(warning)];
57 }
58
59 const data = await res.json() as Array<{
60 full_path?: string;
61 path_with_namespace?: string;
62 username?: string;
63 }>;
64
65 const results: SearchOption[] = data.map((item) => {
66 let value: string;
67 if (type === 'group') {
68 value = item.full_path!;
69 } else if (type === 'project') {
70 value = item.path_with_namespace!;
71 } else {
72 value = item.username!;
73 }
74 return { name: value, value };
75 });
76
77 if (results.length === 0) {
78 const fallback = literalFallback();
79 return fallback ? [fallback] : [];

Callers 1

collectGitLabConfigFunction · 0.85

Calls 1

literalFallbackFunction · 0.70

Tested by

no test coverage detected