(
apiBase: string,
query: string,
token: string,
type: GitHubSearchType,
)
| 23 | const REPO_PATTERN = /^[\w.-]+\/[\w.-]+$/; |
| 24 | |
| 25 | async function searchGitHub( |
| 26 | apiBase: string, |
| 27 | query: string, |
| 28 | token: string, |
| 29 | type: GitHubSearchType, |
| 30 | ): Promise<Array<SearchOption | Separator>> { |
| 31 | const cacheKey = `${apiBase}|${type}|${query}`; |
| 32 | const cached = githubSearchCache.get(cacheKey); |
| 33 | if (cached) { |
| 34 | return cached; |
| 35 | } |
| 36 | |
| 37 | const headers: Record<string, string> = { |
| 38 | 'User-Agent': 'setup-sourcebot', |
| 39 | ...(token ? { Authorization: `Bearer ${token}` } : {}), |
| 40 | }; |
| 41 | const url = type === 'repo' |
| 42 | ? `${apiBase}/search/repositories?q=${encodeURIComponent(query)}&per_page=8` |
| 43 | : `${apiBase}/search/users?q=${encodeURIComponent(query)}+type:${type}&per_page=8`; |
| 44 | const res = await fetch(url, { headers }); |
| 45 | const data = await res.json() as { items?: Array<{ login?: string; full_name?: string }> }; |
| 46 | |
| 47 | const literalFallback = (): SearchOption | null => { |
| 48 | return { name: query, value: query }; |
| 49 | }; |
| 50 | |
| 51 | if (!res.ok) { |
| 52 | const warning = |
| 53 | (res.status === 403 && res.headers.get('x-ratelimit-remaining') === '0') |
| 54 | ? '⚠ Autocomplete disabled — GitHub rate limit exceeded.' |
| 55 | : '⚠ Autocomplete disabled — authentication failed, check your PAT.'; |
| 56 | const fallback = literalFallback(); |
| 57 | return fallback ? [fallback, new Separator(warning)] : [new Separator(warning)]; |
| 58 | } |
| 59 | |
| 60 | const results: SearchOption[] = (data.items ?? []).map((item) => { |
| 61 | const value = type === 'repo' ? item.full_name! : item.login!; |
| 62 | return { name: value, value }; |
| 63 | }); |
| 64 | if (results.length === 0) { |
| 65 | const fallback = literalFallback(); |
| 66 | return fallback ? [fallback] : []; |
| 67 | } |
| 68 | githubSearchCache.set(cacheKey, results); |
| 69 | return results; |
| 70 | } |
| 71 | |
| 72 | export async function collectGitHubConfig(connectionName: string): Promise<CollectResult> { |
| 73 | const env: EnvVars = {}; |
no test coverage detected